int packetSetDid(unsigned char *packet,int packet_maxlen,int *packet_len,char *did) { /* Set the subject field to the supplied DID. DIDs get encoded 4bits per digit (0-9,#,*,+,SPARE1,ESCAPE,END) */ int ofs=OFS_SIDDIDFIELD; /* where the DID/subscriber ID gets written */ /* Put DID (ie not SID) marker into packet */ packet[ofs++]=0x00; return stowDid(packet,&ofs,did); }
int createHlr(char *did,char *sid) { int i; int record_offset=0; /* Generate random SID */ for(i=0;i<64;i++) sid[i]=hexdigit[random()&0xf]; sid[64]=0; if (debug>1) fprintf(stderr,"Creating new HLR entry with sid %s\n",sid); /* Find first free byte of HLR */ findHlr(hlr,&record_offset,NULL,NULL); if (record_offset>=hlr_size) { /* No space */ return setReason("No space in HLR for a new record"); } else { /* We have found space, but is it enough? */ int bytes=hlr_size-record_offset; if (bytes<1024) return setReason("<1KB space in HLR"); /* Write shiny fresh new record. 32bit - record length 32 bytes - SID Total length = 4+32=36 bytes. */ if (stowSid(hlr,record_offset+4,sid)) return setReason("Could not store SID in new HLR entry"); /* Write length last of all to make entry valid */ hlr[record_offset]=0; hlr[record_offset+1]=0; hlr[record_offset+2]=0; hlr[record_offset+3]=36; /* Store the DID */ { unsigned char packeddid[DID_MAXSIZE]; int pdidlen=0; stowDid(packeddid,&pdidlen,did); /* Work out reduced length of DID */ for(pdidlen=1;pdidlen<DID_MAXSIZE;pdidlen++) if (packeddid[pdidlen-1]==0xff) break; hlrSetVariable(hlr,record_offset,VAR_DIDS,0x00,packeddid,pdidlen); } if (debug) fprintf(stderr,"Created new 36 byte HLR record for DID=[%s] @ 0x%x with SID=[%s]\n", did,record_offset,sid); if (debug>2) dump("after HLR create",&hlr[0],256); return 0; } return setReason("Unreachable code turned out not to be"); }
int findHlr(unsigned char *hlr,int *ofs,char *sid,char *did) { unsigned int record_length; int match=0; int records_searched=0; int pid_len=0; unsigned char packed_id[40]; if ((*ofs)>=hlr_size) return 0; if (debug>1) fprintf(stderr,"Searching for HLR record sid=[%s]/did=[%s]\n",sid?sid:"NULL",did?did:"NULL"); if (did&&did[0]) { /* Make packed version of DID so that we can compare faster with the DIDs in the HLR */ if (stowDid(packed_id,&pid_len,did)) return setReason("DID appears to be invalid"); /* Find significant length of packed DID */ for(pid_len=0;pid_len<DID_MAXSIZE;pid_len++) if ((packed_id[pid_len]&0x0f)==0x0f) { pid_len++; break; } if (debug>1) dump("Searching for DID records that match",packed_id,pid_len); } if (sid&&sid[0]) { /* Make packed version of SID for fast comparison */ if (stowSid(packed_id,pid_len,sid)) return setReason("SID appears to be invalid"); pid_len=SID_SIZE; } while(!match) { /* Get length of this record */ record_length =hlr[(*ofs)+3]<<0; record_length|=hlr[(*ofs)+2]<<8; record_length|=hlr[(*ofs)+1]<<16; record_length|=hlr[(*ofs)+0]<<24; if (!record_length) return 0; if (debug>1) fprintf(stderr,"Considering HLR entry @ 0x%x\n",*ofs); records_searched++; if (sid&&sid[0]) { /* Lookup by SID, so just see if it matches */ if (!bcompare(packed_id,&hlr[(*ofs)+4],SID_SIZE)) { if (debug>1) fprintf(stderr,"Found requested SID at address 0x%x.\n",*ofs); match=1; } } if (did&&did[0]) { /* Lookup by did, so see if there are any matching DID entries for this subscriber */ int rofs=(*ofs); struct hlrentry_handle *h=openhlrentry(hlr,rofs); while(h) { /* Search through variables for matching DIDs */ if (debug>2) { fprintf(stderr,"Considering variable 0x%02x, instance %d.\n", h->var_id,h->var_instance); dump("variable value",h->value,h->value_len); } if (h->var_id==VAR_DIDS) { /* DID entry */ if (debug>2) fprintf(stderr,"Checking DID against record DID\n"); if (!bcompare(packed_id,h->value,pid_len)) { if (debug>1) fprintf(stderr,"Found matching DID in HLR record #%d\n",records_searched); match=1; break; } } else { if (debug>2) fprintf(stderr,"Skipping non-DID variable while searching for DID.\n"); } h=hlrentrygetent(h); } } /* For each match ... */ if (match) { if (debug>1) fprintf(stderr,"Returning HLR entry @ 0x%x\n",*ofs); return 1; } /* Consider next record */ (*ofs)+=record_length; if ((*ofs)>=hlr_size) return 0; } return 0; }