mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	Clean up the flow a bit (partly because I am allergic to huge compounded if statements)
This commit is contained in:
		
							parent
							
								
									68a482ed92
								
							
						
					
					
						commit
						ebb4ec7c33
					
				
					 1 changed files with 23 additions and 26 deletions
				
			
		| 
						 | 
					@ -423,40 +423,37 @@ func (sinfo *sessionInfo) doWorker() {
 | 
				
			||||||
func (sinfo *sessionInfo) doSend(bs []byte) {
 | 
					func (sinfo *sessionInfo) doSend(bs []byte) {
 | 
				
			||||||
	defer util_putBytes(bs)
 | 
						defer util_putBytes(bs)
 | 
				
			||||||
	if !sinfo.init {
 | 
						if !sinfo.init {
 | 
				
			||||||
 | 
							// To prevent using empty session keys
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	} // To prevent using empty session keys
 | 
						}
 | 
				
			||||||
 | 
						coords := sinfo.coords
 | 
				
			||||||
	// Read IPv6 flowlabel field (20 bits).
 | 
						// Read IPv6 flowlabel field (20 bits).
 | 
				
			||||||
	// Assumes packet at least contains IPv6 header.
 | 
						// Assumes packet at least contains IPv6 header.
 | 
				
			||||||
	flowkey := uint64(bs[1]&0x0f)<<16 | uint64(bs[2])<<8 | uint64(bs[3])
 | 
						flowkey := uint64(bs[1]&0x0f)<<16 | uint64(bs[2])<<8 | uint64(bs[3])
 | 
				
			||||||
	if flowkey == 0 /* not specified */ &&
 | 
						// Check if the flowlabel was specified
 | 
				
			||||||
		len(bs) >= 48 /* min UDP len, others are bigger */ &&
 | 
						if flowkey == 0 {
 | 
				
			||||||
		(bs[6] == 0x06 || bs[6] == 0x11 || bs[6] == 0x84) /* TCP UDP SCTP */ {
 | 
							// Does the packet meet the minimum UDP packet size? (others are bigger)
 | 
				
			||||||
 | 
							if len(bs) >= 48 {
 | 
				
			||||||
 | 
								// Is the protocol TCP, UDP, SCTP?
 | 
				
			||||||
 | 
								if bs[6] == 0x06 || bs[6] == 0x11 || bs[6] == 0x84 {
 | 
				
			||||||
				// if flowlabel was unspecified (0), try to use known protocols' ports
 | 
									// if flowlabel was unspecified (0), try to use known protocols' ports
 | 
				
			||||||
				// protokey: proto | sport | dport
 | 
									// protokey: proto | sport | dport
 | 
				
			||||||
				flowkey = uint64(bs[6])<<32 /* proto */ |
 | 
									flowkey = uint64(bs[6])<<32 /* proto */ |
 | 
				
			||||||
					uint64(bs[40])<<24 | uint64(bs[41])<<16 /* sport */ |
 | 
										uint64(bs[40])<<24 | uint64(bs[41])<<16 /* sport */ |
 | 
				
			||||||
					uint64(bs[42])<<8 | uint64(bs[43]) /* dport */
 | 
										uint64(bs[42])<<8 | uint64(bs[43]) /* dport */
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
	var coords []byte
 | 
					 | 
				
			||||||
	if flowkey != 0 {
 | 
					 | 
				
			||||||
		// Now we append something to the coords
 | 
					 | 
				
			||||||
		// Specifically, we append a 0, and then arbitrary data
 | 
					 | 
				
			||||||
		// The 0 ensures that the destination node switch forwards to the self peer (router)
 | 
					 | 
				
			||||||
		// The rest is ignored, but it's still part as the coords, so it affects switch queues
 | 
					 | 
				
			||||||
		// This helps separate traffic streams (coords, flowlabel) to be queued independently
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// TODO could we avoid allocations there and put this work into wire_trafficPacket.encode()?
 | 
					 | 
				
			||||||
		coords = append(coords, sinfo.coords...)  // Start with the real coords
 | 
					 | 
				
			||||||
		coords = append(coords, 0)                // Then target the local switchport
 | 
					 | 
				
			||||||
		coords = wire_put_uint64(flowkey, coords) // Then variable-length encoded flowkey
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		// flowlabel was unspecified (0) and protocol unrecognised.
 | 
					 | 
				
			||||||
		// To save bytes, we're not including it, therefore we won't need self-port override either.
 | 
					 | 
				
			||||||
		// So just use sinfo.coords directly to avoid golang GC allocations.
 | 
					 | 
				
			||||||
		// Recent enough Linux and BSDs support flowlabels (auto_flowlabel) out of the box so this will be rare.
 | 
					 | 
				
			||||||
		coords = sinfo.coords
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// If we have a flowkey, either through the IPv6 flowlabel field or through
 | 
				
			||||||
 | 
						// known TCP/UDP/SCTP proto-sport-dport triplet, then append it to the coords.
 | 
				
			||||||
 | 
						// Appending extra coords after a 0 ensures that we still target the local router
 | 
				
			||||||
 | 
						// but lets us send extra data (which is otherwise ignored) to help separate
 | 
				
			||||||
 | 
						// traffic streams into independent queues
 | 
				
			||||||
 | 
						if flowkey != 0 {
 | 
				
			||||||
 | 
							coords = append(coords, 0)                // First target the local switchport
 | 
				
			||||||
 | 
							coords = wire_put_uint64(flowkey, coords) // Then variable-length encoded flowkey
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Prepare the payload
 | 
				
			||||||
	payload, nonce := boxSeal(&sinfo.sharedSesKey, bs, &sinfo.myNonce)
 | 
						payload, nonce := boxSeal(&sinfo.sharedSesKey, bs, &sinfo.myNonce)
 | 
				
			||||||
	defer util_putBytes(payload)
 | 
						defer util_putBytes(payload)
 | 
				
			||||||
	p := wire_trafficPacket{
 | 
						p := wire_trafficPacket{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue