/* Serval Mesh Copyright (C) 2010-2012 Paul Gardner-Stephen Copyright (C) 2010-2012 Serval Project Pty Limited Copyright (C) 2012 Serval Project Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include "xprintf.h" #define MAX_SPACES 120 const char *spaces=" "" "" "" " " "" "" "" " " "" "" "" "; const char *indent(int n) { return &spaces[MAX_SPACES-n]; } int senderSet=0; unsigned char senderAddress[32]; static void _dump(XPRINTF xpf, const unsigned char *data, size_t len, size_t ofs, const char *fmt, ...); #ifdef STANDALONE int main(int argc,char **argv) { int i,n; int len; unsigned char buff[8192]; for(n=0;n<1024;n++) { int i; len=random()%8192; for(i=0;i"); } else { xprintf(xpf," "); } (*ofs)++; break; case 0x01: /* by index */ xprintf(xpf,"
", packet[(*ofs)+1]); (*ofs)+=2; break; case 0x03: /* previously used address */ xprintf(xpf,""); (*ofs)++; break; case 0x09: /* prefix 3 bytes and assign index */ case 0x05: /* prefix 3 bytes */ { int skip=0; if (packet[*ofs]&8) skip=1; (*ofs)++; xprintf(xpf,"%02X%02X%02X* <24 bit prefix", packet[(*ofs)],packet[(*ofs)+1],packet[(*ofs)+2]); if (senderP) bcopy(&packet[*ofs],senderAddress,3); senderSet=3; if (skip) xprintf(xpf," assigned index 0x%02x",packet[(*ofs)+3]); xprintf(xpf,">"); (*ofs)+=3+skip; } break; case 0x0a: /* prefix 7 bytes and assign index */ case 0x06: /* prefix 7 bytes */ { int skip=0; if (packet[*ofs]&8) skip=1; (*ofs)++; xprintf(xpf,"%02X%02X%02X%02X%02X%02X%02X* <56 bit prefix", packet[(*ofs)],packet[(*ofs)+1],packet[(*ofs)+2],packet[(*ofs)+3], packet[(*ofs)+4],packet[(*ofs)+5],packet[(*ofs)+6]); if (senderP) bcopy(&packet[*ofs],senderAddress,7); senderSet=7; if (skip) xprintf(xpf," assigned index 0x%02x",packet[(*ofs)+7]); xprintf(xpf,">"); (*ofs)+=7+skip; } break; case 0x07: /* prefix 11 bytes */ (*ofs)++; xprintf(xpf,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X* <88 bit prefix>", packet[(*ofs)],packet[(*ofs)+1],packet[(*ofs)+2],packet[(*ofs)+3], packet[(*ofs)+4],packet[(*ofs)+5],packet[(*ofs)+6],packet[(*ofs)+7], packet[(*ofs)+8],packet[(*ofs)+9],packet[(*ofs)+10]); if (senderP) bcopy(&packet[*ofs],senderAddress,11); senderSet=11; (*ofs)+=11; break; case 0x0f: /* broadcast */ { int i; (*ofs)++; xprintf(xpf,""); break; } case 0x0b: /* prefix 11 bytes and assign index */ case 0x0d: /* prefix 11 bytes and assign 2-byte index */ case 0x02: /* reserved */ case 0x04: /* reserved */ case 0x0c: /* reserved */ xprintf(xpf,"",packet[(*ofs)]); return -1; break; default: case 0x0e: /* full address and assign 2-byte index */ case 0x08: /* full address and assign index */ { int skip=0; if (packet[*ofs]==0x08) { (*ofs)++; skip=1; } else if (packet[*ofs]==0x0e) { (*ofs)++; skip=2; } /* naturally presented 32 byte address */ { int i; for(i=0;i<32;i++) xprintf(xpf,"%02x",packet[(*ofs)+i]); if (senderP) bcopy(&packet[*ofs],senderAddress,32); senderSet=32; } if (skip) { xprintf(xpf," "); } else xprintf(xpf," "); (*ofs)+=32+skip; } } return 0; } int isOverlayPacket(XPRINTF xpf, const unsigned char *packet, size_t *ofs, size_t len) { if (packet[(*ofs)]!=0x4f) return 0; if (packet[(*ofs)+1]!=0x10) return 0; int version = (packet[(*ofs)+2]<<8)+packet[(*ofs)+3]; xprintf(xpf,"%sServal Overlay Mesh Packet version %d (0x%04x)\n", indent(4),version,version); if (version>0x001) { xprintf(xpf,"%s WARNING: Packet version is newer than I know about.\n",indent(4)); } (*ofs)+=4; senderSet=0; while((*ofs) frame_len) { xprintf(xpf,"%sERROR: Unexpected end of Frame -- skipping rest of frame.\n",indent(10)); break; } if (manifest_len>(frame_len-i)) { xprintf(xpf,"%sERROR: Manifest extends for 0x%x bytes, but frame contains only 0x%x more bytes -- skipping rest of frame.\n",indent(10),manifest_len,frame_len-i); _dump(xpf, frame, frame_len, 0, "%s", indent(12)); i=frame_len; break; } /* find manifest self-signature block */ for(j=0;j= ofs && i + j < len) xprintf(xpf," %02x", data[i+j]); else xprintf(xpf, " "); xprintf(xpf, " "); for (j = 0; j < 16 && i + j < len; ++j) xputc(i + j < ofs ? ' ' : data[i+j] >= ' ' && data[i+j] < 0x7c ? data[i+j] : '.', xpf); xputc('\n', xpf); } va_end(ap); }