/* Create a new HLR entry on a peer. Should always try localhost as first peer, which is why sendToPeers() does it that way. We just have to remember to ask for serialised attempts, rather than all at once. */ int requestNewHLR(char *did,char *pin,char *sid) { unsigned char packet[8000]; int packet_len=0; struct response_set responses; unsigned char transaction_id[8]; bzero(&responses,sizeof(responses)); /* Prepare the request packet */ if (packetMakeHeader(packet,8000,&packet_len,NULL)) return -1; bcopy(&packet[8],transaction_id,8); if (packetSetDid(packet,8000,&packet_len,did)) return -1; if (packetAddHLRCreateRequest(packet,8000,&packet_len)) return -1; if (packetFinalise(packet,8000,&packet_len)) return -1; /* Send it to peers, starting with ourselves, one at a time until one succeeds. XXX - This could take a while if we have long timeouts for each. */ if (packetSendRequest(REQ_SERIAL,packet,packet_len,NONBATCH,transaction_id,&responses)) return -1; /* Extract response */ if (debug>2) dumpResponses(&responses); if (!responses.response_count) { printf("NOREPLY\n"); return -1; } switch(responses.responses->code) { case ACTION_DECLINED: printf("DECLINED\n"); return -1; break; case ACTION_OKAY: { char sid[128]; int ofs=0; extractSid(&responses.responses->sid[0],&ofs,&sid[0]); printf("OK:%s\n",sid); } return 0; break; default: printf("ERROR:Unknown response 0x%02x\n",responses.responses->code); return -1; } return setReason("Request creation of new HLR not implemented"); }
int respondSimple(char *sid,int action,unsigned char *action_text,int action_len, unsigned char *transaction_id) { unsigned char packet[8000]; int pl=0; int *packet_len=&pl; int packet_maxlen=8000; int i; /* ACTION_ERROR is associated with an error message. For syntactic simplicity, we do not require the respondSimple() call to provide the length of the error message. */ if (action==ACTION_ERROR) { action_len=strlen((char *)action_text); /* Make sure the error text isn't too long. IF it is, trim it, as we still need to communicate the error */ if (action_len>255) action_len=255; } /* Prepare the request packet */ if (packetMakeHeader(packet,8000,packet_len,transaction_id)) return -1; if (sid&&sid[0]) { if (packetSetSid(packet,8000,packet_len,sid)) return setReason("invalid SID in reply"); } else { if (packetSetDid(packet,8000,packet_len,"")) return setReason("Could not set empty DID in reply"); } CHECK_PACKET_LEN(1+1+action_len); packet[(*packet_len)++]=action; if (action==ACTION_ERROR) packet[(*packet_len)++]=action_len; for(i=0;i<action_len;i++) packet[(*packet_len)++]=action_text[i]; if (debug>2) dump("Simple response octets",action_text,action_len); if (packetFinalise(packet,8000,packet_len)) return -1; if (debug) fprintf(stderr,"Sending response of %d bytes.\n",*packet_len); if (packetSendRequest(REQ_REPLY,packet,*packet_len,NONBATCH,transaction_id,NULL)) return -1; return 0; }
int requestItem(char *did,char *sid,char *item,int instance,unsigned char *buffer,int buffer_length,int *len, unsigned char *transaction_id) { unsigned char packet[8000]; int packet_len=0; struct response *r; struct response_set responses; bzero(&responses,sizeof(responses)); /* Prepare the request packet */ if (packetMakeHeader(packet,8000,&packet_len,transaction_id)) return -1; if (did&&(!sid)) { if (packetSetDid(packet,8000,&packet_len,did)) return -1; } else if (sid&&(!did)) { if (packetSetSid(packet,8000,&packet_len,sid)) return -1; } else return setReason("You must request items by DID or SID, not neither, nor both"); if (packetAddVariableRequest(packet,8000,&packet_len, item,instance,0,buffer_length)) return -1; if (packetFinalise(packet,8000,&packet_len)) return -1; int method=REQ_PARALLEL; if (sid) method=REQ_FIRSTREPLY; if (packetSendRequest(method,packet,packet_len,(instance==-1)?BATCH:NONBATCH,transaction_id,&responses)) return -1; r=responses.responses; while(r) { char sid[SID_SIZE*2+1]; int slen=0; extractSid(r->sid,&slen,sid); switch(r->code) { case ACTION_OKAY: printf("OK:%s\n",sid); break; case ACTION_DECLINED: printf("DECLINED:%s\n",sid); break; case ACTION_DATA: /* Display data. The trick is knowing the format of the data. Fortunately we get the variable id etc. XXX Need to deal with fragmented values. (perhaps the fragments should get auto-assembled when accepting the responses) */ switch(r->var_id) { case VAR_DIDS: { char did[DID_MAXSIZE+1]; int dlen=0; did[0]=0; extractDid(r->response,&dlen,did); printf("DIDS:%s:%d:%s\n",sid,r->var_instance,did); } break; case VAR_NOTE: default: /* ASCII formatted variable types */ { int v=0; int i=0; FILE *outputfile=stdout; while(vars[v].name&&vars[v].id!=r->var_id) v++; if (!vars[v].id) printf("0x%02x",r->var_id); while(vars[v].name[i]) fputc(toupper(vars[v].name[i++]),stdout); printf(":%s:%d:",sid,r->var_instance); if (outputtemplate) { char outputname[8192]; snprintf(outputname,8192,outputtemplate,sid,r->var_id,r->var_instance); outputfile=fopen(outputname,"w"); if (!outputfile) printf("ERROR:Could not open output file '%s'",outputname); if (debug) fprintf(stderr,"Writing output to '%s'\n",outputname); } if (outputfile) fwrite(r->response,r->value_bytes,1,outputfile); if (r->value_bytes<r->value_len) { /* Partial response, so ask for the rest of it */ unsigned char packet[8000]; int packet_len=0; struct response *rr; struct response_set responses; int offset,max_bytes; int recv_map[1+(r->value_len/MAX_DATA_BYTES)]; int recv_map_size=1+(r->value_len/MAX_DATA_BYTES); int needMoreData; int tries=0; /* work out EXACTLY how many installments we need */ while (((recv_map_size-1)*MAX_DATA_BYTES)>=r->value_len) recv_map_size--; recv_map[0]=0; /* we received the first installment, so mark it off ... */ /* ... but we haven't received the rest */ for(i=1;i<recv_map_size;i++) recv_map[i]=0; /* Ask for all remaining pieces in parallel, then keep track of what has arrived XXX - Not yet implemented. Currently uses a slow serial method, worse than TFTP */ needMoreData=recv_map_size-1; while(needMoreData&&(tries++<15)) { if (debug>1) fprintf(stderr,"Multi-packet request: try %d, %d fragments remaining.\n",tries,needMoreData); needMoreData=0; for(i=0;i<recv_map_size;i++) if (!recv_map[i]) { needMoreData++; offset=i*MAX_DATA_BYTES; if (debug>1) fprintf(stderr,"Asking for variable segment @ offset %d\n",offset); /* Send accumulated request direct to the responder */ if (packet_len>=MAX_DATA_BYTES) { if (packetFinalise(packet,8000,&packet_len)) return -1; packetSendFollowup(r->sender,packet,packet_len); packet_len=0; } /* Prepare a new request packet if one is not currently being built */ if (!packet_len) { if (packetMakeHeader(packet,8000,&packet_len,transaction_id)) return -1; if (packetSetSid(packet,8000,&packet_len,sid)) return setReason("SID went mouldy during multi-packet get"); } max_bytes=65535-offset; if (max_bytes>buffer_length) max_bytes=buffer_length; if (packetAddVariableRequest(packet,8000,&packet_len, item,r->var_instance,offset,max_bytes)) return -1; } /* Send accumulated request direct to the responder */ if (packet_len) { if (packetFinalise(packet,8000,&packet_len)) return -1; packetSendFollowup(r->sender,packet,packet_len); packet_len=0; } /* Collect responses to our multiple requests */ bzero(&responses,sizeof(responses)); /* XXX should target specific peer that sent first piece */ getReplyPackets(REQ_PARALLEL,i,0,&responses,transaction_id,250); rr=responses.responses; while(rr) { if (rr->code==ACTION_DATA&&rr->var_id==r->var_id&&rr->var_instance==r->var_instance) { int piece=rr->value_offset/MAX_DATA_BYTES; if (!recv_map[piece]) { if (debug>1) fprintf(stderr,"Extracted value fragment @ offset %d, with %d bytes\n",rr->value_offset,rr->value_bytes); if (debug>2) dump("Fragment",rr->response,rr->value_bytes); fseek(outputfile,rr->value_offset,SEEK_SET); fwrite(rr->response,rr->value_bytes,1,outputfile); recv_map[piece]=1; } else { if (debug>1) fprintf(stderr,"DUPLICATE value fragment @ offset %d, with %d bytes\n",rr->value_offset,rr->value_bytes); } } rr=rr->next; } clearResponses(&responses); } } if (outputtemplate) fclose(outputfile); else fflush(outputfile); printf("\n"); break; } } break; case ACTION_DONE: printf("DONE:%s:%d\n",sid,r->response[0]); break; case ACTION_GET: printf("ERROR:You cant respond with GET\n"); break; case ACTION_SET: printf("ERROR:You cant respond with SET\n"); break; case ACTION_WROTE: printf("ERROR:You cant respond with WROTE\n"); break; case ACTION_DEL: printf("ERROR:You cant respond with DEL\n"); break; case ACTION_CREATEHLR: printf("ERROR:You cant respond with CREATEHLR\n"); break; case ACTION_PAD: /* ignore it */ break; case ACTION_EOT: /* ignore it */ break; default: printf("ERROR:Unexpected response code 0x%02x\n",r->code); } fflush(stdout); r=r->next; } return -1; }
int writeItem(char *sid,int var_id,int instance,unsigned char *value, int value_start,int value_length,int flags) { unsigned char packet[8000]; int packet_len=0; struct response_set responses; struct response *r; unsigned char transaction_id[8]; bzero(&responses,sizeof(responses)); if (debug>1) fprintf(stderr,"Writing %d bytes of var %02x/%02x @ 0x%d flags=%d\n", value_length,var_id,instance,value_start,flags); if (!sid) { printf("ERROR:Must use SID when writing values.\n"); return -1; } /* Split long writes into many short writes. (since each write is acknowledged, we don't have to worry about batch mode) */ if (value_length-value_start>MAX_DATA_BYTES) { int o; if (debug) fprintf(stderr,"Writing large value (%d bytes)\n",value_length-value_start); for(o=value_start;o<value_length;o+=MAX_DATA_BYTES) { int bytes=MAX_DATA_BYTES; if (o+bytes>value_length) bytes=value_length-o; if (debug>1) fprintf(stderr," writing [%d,%d)\n",o,o+bytes-1); if (writeItem(sid,var_id,instance,&value[o-value_start],o,bytes, flags|((o>value_start)?SET_FRAGMENT:0))) { if (debug) fprintf(stderr," - writing installment failed\n"); return setReason("Failure during multi-packet write of long-value"); } } printf("OK:%s\n",sid); return 0; } /* Prepare the request packet */ if (packetMakeHeader(packet,8000,&packet_len,NULL)) return -1; bcopy(&packet[8],transaction_id,8); if (packetSetSid(packet,8000,&packet_len,sid)) return -1; if (packetAddVariableWrite(packet,8000,&packet_len,var_id,instance, value,value_start,value_length,flags)) return -1; if (packetFinalise(packet,8000,&packet_len)) return -1; /* XXX should be able to target to the peer holding the SID, if we have it. In any case, we */ if (packetSendRequest(REQ_FIRSTREPLY,packet,packet_len,NONBATCH,transaction_id,&responses)) return -1; r=responses.responses; while(r) { int slen; char sid[SID_SIZE*2+1]; extractSid(r->sid,&slen,sid); switch(r->code) { case ACTION_ERROR: /* We allocate an extra byte to allow us to do this */ r->response[r->response_len]=0; printf("ERROR:%s\n",(char *)r->response); break; case ACTION_OKAY: printf("ERROR:Unexpected OK response\n"); break; case ACTION_DECLINED: printf("DECLINED:%s\n",sid); break; case ACTION_WROTE: /* Supress success messages when writing fragments */ if (!(flags&SET_FRAGMENT)) printf("WROTE:%s\n",sid); break; case ACTION_DATA: printf("ERROR:DATA reponse not implemented\n"); break; case ACTION_GET: printf("ERROR:You cant respond with GET\n"); break; case ACTION_SET: printf("ERROR:You cant respond with SET\n"); break; case ACTION_DEL: printf("ERROR:You cant respond with DEL\n"); break; case ACTION_CREATEHLR: printf("ERROR:You cant respond with CREATEHLR\n"); break; case ACTION_PAD: /* ignore it */ break; case ACTION_EOT: /* ignore it */ break; default: printf("ERROR:Unexpected response code 0x%02x\n",r->code); } fflush(stdout); r=r->next; } return 0; }