mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
Fix payload length fields
This commit is contained in:
parent
27c24f377e
commit
e9566de0af
@ -94,9 +94,8 @@ int overlay_route_add_advertisements(overlay_buffer *e)
|
||||
if (ob_append_byte(e,OF_TYPE_NODEANNOUNCE))
|
||||
return WHY("could not add node advertisement header");
|
||||
ob_append_byte(e,1); /* TTL */
|
||||
int rfs_offset=e->length; /* remember where the RFS byte gets stored
|
||||
so that we can patch it later */
|
||||
ob_append_byte(e,1+8+1+1+8*slots_used/* RFS */);
|
||||
|
||||
ob_append_rfs(e,1+8+1+1+8*slots_used);
|
||||
|
||||
/* Stuff in dummy address fields */
|
||||
ob_append_byte(e,OA_CODE_BROADCAST);
|
||||
@ -159,7 +158,7 @@ int overlay_route_add_advertisements(overlay_buffer *e)
|
||||
if (oad_bin==bin&&oad_slot==slot) break;
|
||||
}
|
||||
|
||||
ob_setbyte(e,rfs_offset,1+8+1+1+8*slots_used);
|
||||
ob_patch_rfs(e,COMPUTE_RFS_LENGTH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -215,7 +215,7 @@ int ob_append_rfs(overlay_buffer *b,int l)
|
||||
if (l<0||l>0xffff) return -1;
|
||||
|
||||
/* First work out how long the field needs to be, then write dummy bytes
|
||||
and use ob_patch_length to set the value. That way we have only one
|
||||
and use ob_patch_rfs to set the value. That way we have only one
|
||||
lot of code that does the encoding. */
|
||||
|
||||
b->var_length_offset=b->length;
|
||||
@ -287,6 +287,10 @@ int ob_indel_space(overlay_buffer *b,int offset,int shift)
|
||||
|
||||
int ob_patch_rfs(overlay_buffer *b,int l)
|
||||
{
|
||||
if (l==COMPUTE_RFS_LENGTH){
|
||||
// assume the payload has been written, we can now calculate the actual length
|
||||
l = b->length - (b->var_length_offset + b->var_length_bytes);
|
||||
}
|
||||
if (l<0||l>0xffff) return -1;
|
||||
|
||||
/* Adjust size of field */
|
||||
|
@ -174,6 +174,9 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
break;
|
||||
}
|
||||
|
||||
if (f.rfs > len - ofs)
|
||||
return WHYF("Payload length %d is too long for the remaining packet buffer %d", f.rfs, len - ofs);
|
||||
|
||||
/* Now extract the next hop address */
|
||||
int alen=0;
|
||||
int offset=ofs;
|
||||
@ -196,11 +199,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
|
||||
}
|
||||
|
||||
/* Finally process the frame */
|
||||
long long now=overlay_gettime_ms();
|
||||
overlay_frame_process(interface,&f);
|
||||
long long elapsed=overlay_gettime_ms()-now;
|
||||
if (0) INFOF("overlay_frame_process (type=%d, len=%d) took %lldms",
|
||||
f.type,f.bytecount,elapsed);
|
||||
|
||||
/* Skip the rest of the bytes in this frame so that we can examine the next one in this
|
||||
ensemble */
|
||||
@ -284,7 +283,7 @@ int overlay_add_selfannouncement(int interface,overlay_buffer *b)
|
||||
|
||||
/* Add space for Remaining Frame Size field. This will always be a single byte
|
||||
for self-announcments as they are always <256 bytes. */
|
||||
if (ob_append_byte(b,1+8+1+(send_prefix?(1+7):SID_SIZE)+4+4+1))
|
||||
if (ob_append_rfs(b,1+8+1+(send_prefix?(1+7):SID_SIZE)+4+4+1))
|
||||
return WHY("Could not add RFS for self-announcement frame");
|
||||
|
||||
/* Add next-hop address. Always link-local broadcast for self-announcements */
|
||||
@ -335,6 +334,8 @@ int overlay_add_selfannouncement(int interface,overlay_buffer *b)
|
||||
if (ob_append_byte(b,interface))
|
||||
return WHY("Could not add interface number to self-announcement");
|
||||
|
||||
ob_patch_rfs(b, COMPUTE_RFS_LENGTH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -109,9 +109,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
|
||||
RETURN(WHY("could not add rhizome bundle advertisement header"));
|
||||
ob_append_byte(e, 1); /* TTL (1 byte) */
|
||||
|
||||
int rfs_offset=e->length; /* remember where the RFS byte gets stored
|
||||
so that we can patch it later */
|
||||
ob_append_byte(e,1+11+1+2+RHIZOME_BAR_BYTES*slots_used/* RFS */);
|
||||
ob_append_rfs(e,1+11+1+2+RHIZOME_BAR_BYTES*slots_used/* RFS */);
|
||||
|
||||
/* Stuff in dummy address fields (11 bytes) */
|
||||
ob_append_byte(e,OA_CODE_BROADCAST);
|
||||
@ -311,18 +309,7 @@ int overlay_rhizome_add_advertisements(int interface_number,overlay_buffer *e)
|
||||
if (statement) sqlite3_finalize(statement); statement=NULL;
|
||||
|
||||
if (0&&debug&DEBUG_RHIZOME) DEBUGF("Appended %d rhizome advertisements to packet using %d bytes.",bundles_advertised,bytes_used);
|
||||
int rfs_value=1+11+1+2+bytes_used;
|
||||
if (rfs_value<0xfa)
|
||||
ob_setbyte(e,rfs_offset,rfs_value);
|
||||
else
|
||||
{
|
||||
ob_makespace(e,1);
|
||||
ob_bcopy(e,rfs_offset,rfs_offset+1,
|
||||
e->length-rfs_offset);
|
||||
ob_setbyte(e,rfs_offset,0xfa+(rfs_value-250)/256);
|
||||
ob_setbyte(e,rfs_offset+1,(rfs_value-250)&0xff);
|
||||
e->length++;
|
||||
}
|
||||
ob_patch_rfs(e, COMPUTE_RFS_LENGTH);
|
||||
|
||||
RETURN(0);
|
||||
}
|
||||
|
3
serval.h
3
serval.h
@ -967,6 +967,9 @@ int overlay_abbreviate_clear_most_recent_address();
|
||||
#define RFS_PLUS1018 0xfd
|
||||
#define RFS_PLUS1274 0xfe
|
||||
#define RFS_3BYTE 0xff
|
||||
|
||||
#define COMPUTE_RFS_LENGTH -1
|
||||
|
||||
int rfs_length(int l);
|
||||
int rfs_encode(int l,unsigned char *b);
|
||||
int rfs_decode(unsigned char *b,int *offset);
|
||||
|
Loading…
Reference in New Issue
Block a user