/* Serval Distributed Numbering Architecture (DNA) Copyright (C) 2010 Paul Gardner-Stephen 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 "serval.h" int extractDid(unsigned char *packet,int *ofs,char *did) { int d=0; int highP=1; int nybl; nybl=0; while(nybl!=0xf&&(*ofs<(OFS_SIDDIDFIELD+SIDDIDFIELD_LEN))&&(d<64)) { if (highP) nybl=packet[*ofs]>>4; else nybl=packet[*ofs]&0xf; if (nybl<0xa) did[d++]='0'+nybl; else switch(nybl) { case 0xa: did[d++]='*'; break; case 0xb: did[d++]='#'; break; case 0xc: did[d++]='+'; break; } if (highP) highP=0; else { (*ofs)++; highP=1; } } if (d>63) return setReason("DID too long"); did[d]=0; return 0; } int stowDid(unsigned char *packet,int *ofs,char *did) { int highP=1; int nybl; int d=0; int len=0; if (debug&DEBUG_PACKETFORMATS) printf("Packing DID \"%s\"\n",did); while(did[d]&&(d=DID_MAXSIZE) { setReason("DID number too long"); return -1; } /* Append end of number code, filling the whole byte for fast and easy comparison */ if (highP) packet[(*ofs)++]=0xff; else packet[(*ofs)++]|=0x0f; len++; /* Fill remainder of field with randomness to protect any encryption */ for(;len>4]; sid[d++]=hexdigit[packet[*ofs]&0xf]; (*ofs)++; } sid[d]=0; return 0; } int stowSid(unsigned char *packet,int ofs,char *sid) { int i; if (debug&DEBUG_PACKETFORMATS) printf("Stowing SID \"%s\"\n",sid); size_t n = strlen(sid); if (n != SID_STRLEN) return setReason("Asked to stow invalid SID (strlen is %u but should be %u hex digits)", n, SID_STRLEN); for(i=0;i