mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-12 13:27:51 +00:00
Hide some buffer implementations details
This commit is contained in:
parent
1cd6b6955a
commit
0e0137e968
@ -490,7 +490,7 @@ int process_explain(struct overlay_frame *frame){
|
||||
struct decode_context context;
|
||||
bzero(&context, sizeof context);
|
||||
|
||||
while(b->position < b->sizeLimit){
|
||||
while(ob_remaining(b)>0){
|
||||
int len = ob_get(b);
|
||||
if (len<=0 || len>SID_SIZE)
|
||||
return WHY("Badly formatted explain message");
|
||||
|
@ -143,17 +143,17 @@ int overlay_route_add_advertisements(struct decode_context *context, overlay_int
|
||||
struct subscriber *start = next_advertisement;
|
||||
next_advertisement=NULL;
|
||||
|
||||
int start_pos = e->position;
|
||||
int start_pos = ob_position(e);
|
||||
|
||||
// append announcements starting from the last node we couldn't advertise last time
|
||||
enum_subscribers(start, add_advertisement, e);
|
||||
|
||||
// if we didn't start at the beginning and still have space, start again from the beginning
|
||||
if (start && !next_advertisement && e->sizeLimit - e->position > 0){
|
||||
if (start && !next_advertisement && ob_limit(e) - ob_position(e) > 0){
|
||||
enum_subscribers(NULL, add_advertisement, e);
|
||||
}
|
||||
|
||||
if (e->position == start_pos){
|
||||
if (ob_position(e) == start_pos){
|
||||
// no advertisements? don't bother to send the payload at all.
|
||||
ob_rewind(e);
|
||||
}else
|
||||
@ -177,7 +177,7 @@ int overlay_route_saw_advertisements(int i, struct overlay_frame *f, struct deco
|
||||
IN();
|
||||
struct subscriber *previous=context->previous;
|
||||
// minimum record length is (address code, 3 byte sid, score, gateways)
|
||||
while(f->payload->position < f->payload->sizeLimit)
|
||||
while(ob_remaining(f->payload)>0)
|
||||
{
|
||||
struct subscriber *subscriber;
|
||||
context->invalid_addresses=0;
|
||||
|
@ -47,7 +47,7 @@ struct overlay_buffer *ob_static(unsigned char *bytes, int size){
|
||||
if (!ret) return NULL;
|
||||
ret->bytes = bytes;
|
||||
ret->allocSize = size;
|
||||
ret->allocated = 0;
|
||||
ret->allocated = NULL;
|
||||
ob_unlimitsize(ret);
|
||||
|
||||
return ret;
|
||||
@ -67,7 +67,7 @@ struct overlay_buffer *ob_slice(struct overlay_buffer *b, int offset, int length
|
||||
return NULL;
|
||||
ret->bytes = b->bytes+offset;
|
||||
ret->allocSize = length;
|
||||
ret->allocated = 0;
|
||||
ret->allocated = NULL;
|
||||
ob_unlimitsize(ret);
|
||||
|
||||
return ret;
|
||||
@ -95,8 +95,10 @@ struct overlay_buffer *ob_dup(struct overlay_buffer *b){
|
||||
int ob_free(struct overlay_buffer *b)
|
||||
{
|
||||
if (!b) return WHY("Asked to free NULL");
|
||||
if (b->bytes && b->allocated) free(b->bytes);
|
||||
if (b->bytes && b->allocated) free(b->allocated);
|
||||
// we're about to free this anyway, why are we clearing it?
|
||||
b->bytes=NULL;
|
||||
b->allocated=NULL;
|
||||
b->allocSize=0;
|
||||
b->sizeLimit=0;
|
||||
free(b);
|
||||
@ -164,7 +166,7 @@ int ob_makespace(struct overlay_buffer *b,int bytes)
|
||||
}
|
||||
if (0) DEBUGF("realloc(b->bytes=%p,newSize=%d)", b->bytes,newSize);
|
||||
/* XXX OSX realloc() seems to be able to corrupt things if the heap is not happy when calling realloc(), making debugging memory corruption much harder.
|
||||
So will do a three-stage malloc,bcopy,free to see if we can tease the bug out that way. */
|
||||
So will do a three-stage malloc,bcopy,free to see if we can tease bugs out that way. */
|
||||
/*
|
||||
unsigned char *r=realloc(b->bytes,newSize);
|
||||
if (!r) return WHY("realloc() failed");
|
||||
@ -192,9 +194,9 @@ int ob_makespace(struct overlay_buffer *b,int bytes)
|
||||
unsigned char *new=malloc(newSize);
|
||||
#endif
|
||||
bcopy(b->bytes,new,b->position);
|
||||
if (b->bytes) free(b->bytes);
|
||||
if (b->allocated) free(b->allocated);
|
||||
b->bytes=new;
|
||||
b->allocated=1;
|
||||
b->allocated=new;
|
||||
b->allocSize=newSize;
|
||||
return 0;
|
||||
}
|
||||
@ -348,6 +350,20 @@ int ob_patch_rfs(struct overlay_buffer *b){
|
||||
return ob_set_ui16(b,b->var_length_offset,b->position - (b->var_length_offset + 2));
|
||||
}
|
||||
|
||||
|
||||
int ob_position(struct overlay_buffer *b){
|
||||
return b->position;
|
||||
}
|
||||
int ob_limit(struct overlay_buffer *b){
|
||||
return b->sizeLimit;
|
||||
}
|
||||
int ob_remaining(struct overlay_buffer *b){
|
||||
return b->sizeLimit - b->position;
|
||||
}
|
||||
unsigned char *ob_ptr(struct overlay_buffer *b){
|
||||
return b->bytes;
|
||||
}
|
||||
|
||||
int asprintable(int c)
|
||||
{
|
||||
if (c<' ') return '.';
|
||||
|
@ -36,7 +36,7 @@ struct overlay_buffer {
|
||||
int allocSize;
|
||||
|
||||
// is this an allocated buffer? can it be resized? Should it be freed?
|
||||
int allocated;
|
||||
unsigned char * allocated;
|
||||
|
||||
// length position for later patching
|
||||
int var_length_offset;
|
||||
@ -70,5 +70,9 @@ uint16_t ob_get_ui16(struct overlay_buffer *b);
|
||||
int ob_dump(struct overlay_buffer *b,char *desc);
|
||||
int ob_set_ui16(struct overlay_buffer *b, int offset, uint16_t v);
|
||||
|
||||
|
||||
// information routines
|
||||
int ob_position(struct overlay_buffer *b);
|
||||
int ob_limit(struct overlay_buffer *b);
|
||||
int ob_remaining(struct overlay_buffer *b);
|
||||
unsigned char* ob_ptr(struct overlay_buffer *b);
|
||||
#endif
|
||||
|
@ -177,10 +177,10 @@ int overlay_payload_enqueue(struct overlay_frame *p)
|
||||
p->destination?alloca_tohex(p->destination->sid, 7): alloca_tohex(p->broadcast_id.id,BROADCAST_LEN),
|
||||
p->queue, queue->length);
|
||||
|
||||
if (p->payload && p->payload->position > p->payload->sizeLimit){
|
||||
if (p->payload && ob_remaining(p->payload)<0){
|
||||
// HACK, maybe should be done in each caller
|
||||
// set the size of the payload based on the position written
|
||||
p->payload->sizeLimit=p->payload->position;
|
||||
ob_limitsize(p->payload,ob_position(p->payload));
|
||||
}
|
||||
|
||||
if (queue->length>=queue->maxLength)
|
||||
@ -390,7 +390,7 @@ overlay_stuff_packet(struct outgoing_packet *packet, overlay_txqueue *queue, tim
|
||||
}
|
||||
|
||||
if (debug&DEBUG_OVERLAYFRAMES){
|
||||
DEBUGF("Sending payload type %x len %d for %s via %s", frame->type, frame->payload->position,
|
||||
DEBUGF("Sending payload type %x len %d for %s via %s", frame->type, ob_position(frame->payload),
|
||||
frame->destination?alloca_tohex_sid(frame->destination->sid):"All",
|
||||
frame->sendBroadcast?alloca_tohex(frame->broadcast_id.id, BROADCAST_LEN):alloca_tohex_sid(next_hop->sid));
|
||||
}
|
||||
@ -465,9 +465,9 @@ overlay_fill_send_packet(struct outgoing_packet *packet, time_ms_t now) {
|
||||
overlay_rhizome_add_advertisements(&packet->context, packet->i,packet->buffer);
|
||||
|
||||
if (debug&DEBUG_PACKETCONSTRUCTION)
|
||||
dump("assembled packet",&packet->buffer->bytes[0],packet->buffer->position);
|
||||
ob_dump(packet->buffer,"assembled packet");
|
||||
|
||||
if (overlay_broadcast_ensemble(packet->i, &packet->dest, packet->buffer->bytes, packet->buffer->position)){
|
||||
if (overlay_broadcast_ensemble(packet->i, &packet->dest, ob_ptr(packet->buffer), ob_position(packet->buffer))){
|
||||
// sendto failed. We probably don't have a valid route
|
||||
if (packet->unicast_subscriber){
|
||||
set_reachable(packet->unicast_subscriber, REACHABLE_NONE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user