Cleaned up various compiler warnings. Added packet parser framework for

handling overlay frames.
This commit is contained in:
gardners 2011-08-12 21:05:11 +02:00
parent 9108fd5c59
commit 7e30d62b79
8 changed files with 77 additions and 26 deletions

View File

@ -1,10 +1,13 @@
SRCS= dna.c server.c client.c peers.c ciphers.c responses.c packetformats.c dataformats.c \
hlrdata.c srandomdev.c simulate.c batman.c export.c gateway.c \
overlay.c overlay_buffer.c overlay_interface.c overlay_payload.c overlay_route.c
overlay.c overlay_buffer.c overlay_interface.c overlay_payload.c overlay_route.c \
overlay_packetformats.c
OBJS= dna.o server.o client.o peers.o ciphers.o responses.o packetformats.o dataformats.o \
hlrdata.o srandomdev.o simulate.o batman.o export.o gateway.o \
overlay.o overlay_buffer.o overlay_interface.o overlay_payload.o overlay_route.o
overlay.o overlay_buffer.o overlay_interface.o overlay_payload.o overlay_route.o \
overlay_packetformats.o
HDRS= Makefile mphlr.h
LDFLAGS= -L/Developer/SDKs/MacOSX10.6.sdk/usr/lib
CFLAGS= -I/Developer/SDKs/MacOSX10.6.sdk/usr/include

View File

@ -1,10 +1,13 @@
SRCS= dna.c server.c client.c peers.c ciphers.c responses.c packetformats.c dataformats.c \
hlrdata.c srandomdev.c simulate.c batman.c export.c gateway.c \
overlay.c overlay_buffer.c overlay_interface.c overlay_payload.c overlay_route.c
overlay.c overlay_buffer.c overlay_interface.c overlay_payload.c overlay_route.c \
overlay_packetformats.c
OBJS= dna.o server.o client.o peers.o ciphers.o responses.o packetformats.o dataformats.o \
hlrdata.o srandomdev.o simulate.o batman.o export.o gateway.o \
overlay.o overlay_buffer.o overlay_interface.o overlay_payload.o overlay_route.o
overlay.o overlay_buffer.o overlay_interface.o overlay_payload.o overlay_route.o \
overlay_packetformats.o
HDRS= Makefile mphlr.h
LDFLAGS= @LDFLAGS@
CFLAGS= @CFLAGS@

View File

@ -281,7 +281,7 @@ int fixResponses(struct response_set *responses)
bcopy(rr->response,new,rr->value_bytes);
bcopy(addr,&new[rr->value_bytes],alen+1);
free(rr->response); rr->response=NULL;
rr->response=new;
rr->response=(unsigned char *)new;
rr->value_len+=alen;
rr->value_bytes+=alen;
if (debug>1) fprintf(stderr,"Response string now '%s'\n",rr->response);
@ -607,7 +607,7 @@ int requestItem(char *did,char *sid,char *item,int instance,unsigned char *buffe
extractSid(r->sid,&slen,sid);
switch(r->code)
{
case ACTION_OKAY: printf("OK:%s\n",sid); if (buffer) {strcpy(buffer,sid); *len=strlen(sid); } successes++; break;
case ACTION_OKAY: printf("OK:%s\n",sid); if (buffer) {strcpy((char *)buffer,sid); *len=strlen(sid); } successes++; break;
case ACTION_DECLINED: printf("DECLINED:%s\n",sid); errors++; break;
case ACTION_DATA:
/* Display data.
@ -625,7 +625,7 @@ int requestItem(char *did,char *sid,char *item,int instance,unsigned char *buffe
did[0]=0;
extractDid(r->response,&dlen,did);
printf("DIDS:%s:%d:%s\n",sid,r->var_instance,did);
if (buffer) {strcpy(buffer,did); *len=strlen(did); }
if (buffer) {strcpy((char *)buffer,did); *len=strlen(did); }
successes++;
}
break;

View File

@ -315,6 +315,7 @@ int getReplyPackets(int method,int peer,int batchP,
unsigned char *transaction_id,int timeout);
int clearResponse(struct response **response);
int nextHlr(unsigned char *hlr,int *ofs);
int seedHlr();
int findHlr(unsigned char *hlr,int *ofs,char *sid,char *did);
int createHlr(char *did,char *sid);
struct hlrentry_handle *openhlrentry(unsigned char *hlr,int hofs);
@ -364,6 +365,10 @@ int exportHlr(unsigned char *hlr,char *text);
int openHlrFile(char *backing_file,int size);
int runCommand(char *cmd);
int asteriskObtainGateway(char *requestor_sid,char *did,char *uri_out);
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id);
int packetOkOverlay(unsigned char *packet,int len,unsigned char *transaction_id);
int prepareGateway(char *gatewayspec);
#define CRYPT_CIPHERED 1
#define CRYPT_SIGNED 2
@ -573,6 +578,8 @@ int overlay_rx_messages();
int overlay_check_ticks();
int overlay_add_selfannouncement();
int overlay_payload_package_fmt1(overlay_payload *p,overlay_buffer *b);
int overlay_interface_args(char *arg);
int overlay_get_nexthop(overlay_payload *p,unsigned char *nexthop,int *nexthoplen);
extern int overlay_interface_count;

View File

@ -152,6 +152,7 @@ int overlay_interface_init_socket(int interface,struct sockaddr_in src_addr,stru
perror("bind()");
return WHY("MP HLR server could not bind to requested UDP port (bind() failed)");
}
fprintf(stderr,"Bound to port 0x%04x\n",src_addr.sin_port);
int broadcastP=1;
if(setsockopt(I(socket), SOL_SOCKET, SO_BROADCAST, &broadcastP, sizeof(broadcastP)) < 0) {
@ -203,11 +204,36 @@ int overlay_rx_messages()
/* XXX Okay, so how are we managing out-of-process consumers?
They need some way to register their interest in listening to a port.
*/
for(i=0;i<overlay_interface_count;i++)
{
}
unsigned char packet[16384];
int plen=0;
int c[OVERLAY_MAX_INTERFACES];
int count=0;
/* Look at all interfaces */
for(i=0;i<overlay_interface_count;i++) { c[i]=(overlay_interfaces[i].observed>0); count+=c[i]; }
return WHY("Not implemented");
/* Grab packets from interfaces in round-robin fashion until all have been grabbed,
or until we have spent too long (maybe 10ms?) */
while(count>0)
for(i=0;i<overlay_interface_count;i++)
{
struct sockaddr src_addr;
unsigned int addrlen=sizeof(src_addr);
unsigned char transaction_id[8];
plen=recvfrom(overlay_interfaces[i].socket,packet,sizeof(packet),MSG_DONTWAIT,
&src_addr,&addrlen);
if (plen<0) { c[i]=0; count--; } else {
/* We have a frame from this interface */
fprintf(stderr,"Received %d bytes on interface #%d\n",plen,i);
bzero(&transaction_id[0],8);
if (!packetOk(packet,plen,transaction_id)) WHY("Malformed packet");
}
}
return 0;
}
int overlay_tx_messages()
@ -367,7 +393,7 @@ int overlay_tick_interface(int i, long long now)
/* Stale, so remove from queue */
*p=pp->next;
pp->next->prev=*p;
op_free(p);
op_free(*p);
}
else
{
@ -421,7 +447,8 @@ int overlay_tick_interface(int i, long long now)
}
return 0;
}
else WHY("overlay_broadcast_ensemble() failed");
else return WHY("overlay_broadcast_ensemble() failed");
}
int overlay_check_ticks()
@ -441,7 +468,6 @@ int overlay_check_ticks()
now=nowtv.tv_sec*1000;
now=now+nowtv.tv_usec/1000;
/* Now check if the next tick time for the interfaces is no later than that time.
If so, trigger a tick on the interface. */
for(i=0;i<overlay_interface_count;i++)
@ -455,7 +481,7 @@ int overlay_check_ticks()
overlay_interfaces[i].last_tick_ms=now;
}
}
return 0;
}

View File

@ -58,6 +58,19 @@ int process_packet(unsigned char *packet,int len,struct sockaddr *sender,int sen
}
int packetOk(unsigned char *packet,int len,unsigned char *transaction_id)
{
if (len<HEADERFIELDS_LEN) return setReason("Packet is too short");
if (packet[0]==0x41&&packet[1]==0x10)
return packetOkDNA(packet,len,transaction_id);
if (packet[0]==0x4F&&packet[1]==0x10)
return packetOkOverlay(packet,len,transaction_id);
return setReason("Packet type not recognised.");
}
int packetOkDNA(unsigned char *packet,int len,unsigned char *transaction_id)
{
/* Make sure that the packet is meant for us, and is not mal-formed */
int version;
@ -65,9 +78,6 @@ int packetOk(unsigned char *packet,int len,unsigned char *transaction_id)
int length;
int payloadRotation;
if (len<HEADERFIELDS_LEN) return setReason("Packet is too short");
if (packet[0]!=0x41||packet[1]!=0x10) return setReason("Packet has incorrect magic value");
version=(packet[2]<<8)|packet[3];
length=(packet[4]<<8)|packet[5];
cipher=(packet[6]<<8)|packet[7];

View File

@ -108,9 +108,8 @@ int getBroadcastAddresses(struct in_addr peers[],int *peer_count,int peer_max){
#else
#ifdef HAVE_IFADDRS_H
struct ifaddrs *ifaddr,*ifa;
int family, s;
char host[NI_MAXHOST];
int family;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddr()");
return WHY("getifaddrs() failed");
@ -126,7 +125,6 @@ int getBroadcastAddresses(struct in_addr peers[],int *peer_count,int peer_max){
*/
peers[(*peer_count)++].s_addr=((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
{
struct sockaddr_in broadcast;
unsigned int local=(((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr);
unsigned int netmask=(((struct sockaddr_in *)(ifa->ifa_netmask))->sin_addr.s_addr);
peers[(*peer_count)++].s_addr=local|~netmask;

View File

@ -85,7 +85,7 @@ int server(char *backing_file,int size,int foregroundMode)
waittime.tv_usec=(ms%1000)*1000;
waittime.tv_sec=ms/1000;
fprintf(stderr,"%d,%d\n",waittime.tv_sec,waittime.tv_usec);
fprintf(stderr,"%d,%d\n",(int)waittime.tv_sec,waittime.tv_usec);
int r=select(maxfd+1,&read_fds,NULL,NULL,&waittime);
if (r<0) {
/* select had a problem */
@ -93,9 +93,13 @@ int server(char *backing_file,int size,int foregroundMode)
return WHY("select() complained.");
} else if (r>0) {
/* We have data, so try to receive it */
fprintf(stderr,"select() reports packets waiting\n");
overlay_rx_messages();
} else {
/* No data before tick occurred, so do nothing. */
/* No data before tick occurred, so do nothing.
Well, for now let's just check anyway. */
fprintf(stderr,"select() timeout.\n");
overlay_rx_messages();
}
/* Check if we need to trigger any ticks on any interfaces */
overlay_check_ticks();
@ -234,11 +238,11 @@ int processRequest(unsigned char *packet,int len,
char messageLen = packet[pofs];
pofs++;
strncpy(emitterPhoneNumber, (const char*)packet+pofs, emitterPhoneNumberLen);
emitterPhoneNumber[emitterPhoneNumberLen]=0;
emitterPhoneNumber[(unsigned int)emitterPhoneNumberLen]=0;
pofs+=emitterPhoneNumberLen;
strncpy(message, (const char*)packet+pofs, messageLen);
message[messageLen]=0;
message[(unsigned int)messageLen]=0;
pofs+=messageLen;