Beispiel #1
0
/* 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");
}
Beispiel #2
0
int extractResponses(struct in_addr sender,unsigned char *buffer,int len,struct response_set *responses)
{
  int ofs=OFS_PAYLOAD;
  
  struct response *first_response=NULL;

  while(ofs<len)
    {
      /* XXX should allocate responses from a temporary and bounded slab of memory */
      struct response *r=calloc(sizeof(struct response),1);
      if (!r) exit(WHY("calloc() failed."));
      
      r->code=buffer[ofs];
      r->sender=sender;
      /* XXX doesn't make sure it is SID instead of DID */
      bcopy(&buffer[HEADERFIELDS_LEN+1],r->sid,SID_SIZE);

      switch(buffer[ofs])
	{
	case ACTION_EOT:
	  if (debug&DEBUG_DNARESPONSES) DEBUGF("Reached response packet EOT");
	case ACTION_DECLINED: case ACTION_OKAY:
	case ACTION_CREATEHLR:
	  r->response_len=0; break;
	case ACTION_GET: 
	  /* Followed by variable # to fetch.
	     XXX If variable number >=0x80 then get instance information */
	  r->response_len=1; break;
	case ACTION_ERROR:
	  r->response_len=buffer[++ofs];
	  break;
	case ACTION_DATA:
	  /* Extract variable value */
	  unpackageVariableSegment(&buffer[ofs+1],len-ofs,WITHDATA,r);
	  break;
	case ACTION_DONE:
	  r->value_offset=buffer[ofs+1];
	  r->response_len=1;
	  break;
	case ACTION_PAD:
	  /* Skip padding bytes */
	  r->response_len=1+buffer[ofs+1];	 
	  break;
	case ACTION_WROTE:
	  /* Extract info about the variable segment that was written.
	     This uses the same format as the request to write it, but without the data */
	  unpackageVariableSegment(&buffer[ofs+1],len-ofs,WITHOUTDATA,r);
	  r->response=NULL;
	  break;
	case ACTION_RECVTTL:
	  r->recvttl=buffer[ofs+1];
	  r->response_len=1;
	  /* Attach TTL to other responses from this packet */
	  {
	    struct response *rr=first_response;
	    while(rr) {
	      rr->recvttl=r->recvttl;
	      rr=rr->next;
	    }
	  }
	  break;
	case ACTION_SET:
	case ACTION_DEL:
	case ACTION_XFER:
	default:
	  free(r);
	  if (debug&(DEBUG_DNARESPONSES|DEBUG_PACKETFORMATS)) DEBUGF("Encountered unimplemented response code 0x%02x @ 0x%x",buffer[ofs],ofs);
	  fixResponses(responses);
	  return WHY("Encountered unimplemented response type");
	}
      ofs++;
      if (r->response_len) {
	/* extract bytes of response */
	unsigned char *rr;
	if (r->response) rr=r->response; else rr=&buffer[ofs];
	r->response=malloc(r->response_len+1);
	if (!r->response) exit(WHY("malloc() failed."));
	bcopy(&rr[0],r->response,r->response_len);
	r->response[r->response_len]=0;
	ofs+=r->response_len;
      }

      /* Work out peer ID */
      r->sender=sender;
      for(r->peer_id=0;r->peer_id<peer_count;r->peer_id++)
	{
	  if (sender.s_addr==peers[r->peer_id].s_addr) break;
	}
      if (r->peer_id>peer_count) r->peer_id=-1;

      /* Link new response into chain */
      if (debug&DEBUG_DNARESPONSES) DEBUGF("Linking response into response set");
      r->prev=responses->last_response;
      if (responses->last_response)
	responses->last_response->next=r;
      else
	responses->responses=r;
      responses->last_response=r;
      responses->response_count++;

      if (!first_response) first_response=r;

      responseFromPeer(responses,r->peer_id);

      if (debug&DEBUG_DNARESPONSES) dumpResponses(responses);
    }
  
  fixResponses(responses);
  return 0;
}
Beispiel #3
0
int packetSendRequest(int method,unsigned char *packet,int packet_len,int batchP,
		      unsigned char *transaction_id,struct response_set *responses)
{
  int i;
  int cumulative_timeout=0; /* ms */
  int this_timeout=125; /* ms */
  int peer_low,peer_high;

  struct timeval time_in,now;
 
  /* Prepare ephemeral UDP socket (hence no binding)
     If in server mode, then we already have a socket available to us and appropriately bound */
  if (!serverMode) {
    sock=socket(PF_INET,SOCK_DGRAM,0);
    if (sock<0) {
      fprintf(stderr,"Could not create UDP socket.\n");
      exit(-3);
    }
  }
  
  /* Deal with special case */
  if (method==REQ_REPLY)
    {
      int r=sendto(sock,packet,packet_len,0,(struct sockaddr *)&recvaddr,sizeof(recvaddr));
      if (r<packet_len)	{
	if (debug) fprintf(stderr,"Could not send to client %s\n",inet_ntoa(client_addr));
      } else {
	if (debug>1) fprintf(stderr,"Sent request to client %s\n",inet_ntoa(client_addr));
      }
      return 0;
    }

  getPeerList();

  gettimeofday(&time_in,NULL);

  /* REQ_SERIAL & REQ_PARALLEL work in fundamentally different ways, 
     but it turns out the retry/timeout code is the dominant part.
     So we do a bit of fiddling around to make one loop that can handle both */
  if (method==REQ_SERIAL) {
    peer_low=0; peer_high=peer_count-1;
    /* If there are too many peers to allow sending to each three times, then we should 
       adjust our incremental timeout accordingly, so far as is practicable */
    if (this_timeout*peer_count*3>timeout)
      {
	this_timeout=timeout/(3*peer_count);
	if (this_timeout<10) this_timeout=10; /* 10ms minimum sending interval */
      }
  } else 
    { peer_low=-1; peer_high=-1;}

  while(cumulative_timeout<=timeout)
    {
      /* If not in serial mode, then send request to everyone immediately.
         Make sure we only ask once in parallel mode, since it will always ask everyone */
      if (method==REQ_PARALLEL) sendToPeers(packet,packet_len,method,0,responses);
      else if (method!=REQ_SERIAL)
	for(i=0;i<peer_count;i++) sendToPeers(packet,packet_len,method,i,responses);

      /* If in serial mode, send request to peers in turn until one responds positively, 
	 otherwise just deal with the reply fetching loop to listen to as many or few reply. */
      for(i=peer_low;i<=peer_high;i++) {
	struct response *rr;
	if (i>-1) sendToPeers(packet,packet_len,REQ_SERIAL,i,responses);
	
	/* Placing the timeout calculation here means that the total timeout is shared among
	   all peers in a serial request, but round-robining after each time-step.
	   We adjust this_timeout if there are many peers to allow 3 sends to each peer where possible.
	*/
	cumulative_timeout+=this_timeout;
	int timeout_remaining=this_timeout;
	
	while(1)
	  {
	    /* Wait for response */
	    int r=getReplyPackets(method,i,batchP,responses,transaction_id,timeout_remaining);
	    if (r&&debug>1) fprintf(stderr,"getReplyPackets(): Returned on timeout\n");
	    
	    switch(method)
	      {
	      case REQ_PARALLEL:
		/* XXX We could stop once all peers have replied.
		   (need to update the test script if we do that, so that it tests with multiple
		    peers and so tests that we wait if not all peers have responded) */
		break;
	      case REQ_FIRSTREPLY:
		if (debug>1) fprintf(stderr,"Returning with first reply (REQ_FIRSTREPLY)\n");
		if (!r) return 0;
		break;
	      case REQ_SERIAL:
		if (!r) {
		  /* Stop if we have an affirmative response.
		     XXX - doesn't allow for out of order replies. */
		  if (debug>1) dumpResponses(responses);
		  rr=responses->last_response;
		  while (rr)
		    {
		      if (rr->checked) break;
		      if (debug>1) 
			fprintf(stderr,"Got a response code 0x%02x, checking if that is what we need.\n",rr->code);
		      switch (rr->code)
			{
			case ACTION_OKAY: case ACTION_DATA:
			  /* bingo */
			  if (!batchP) return 0;
			  break;
			}
		      rr->checked=1;
		      rr=rr->prev;
		    }
		  
		  /* not what we are after, so clear response and try with next peer */
		  clearResponses(responses);
		}
		break;
	      }
	    
	    /* Wait for the previous timeout to really expire,
	       (this is for the case where all peers have replied) */
	    {
	      int elapsed_usecs=0;
	      int cumulative_usecs=cumulative_timeout*1000;
	      int remaining_usecs;
	      
	      gettimeofday(&now,NULL);
	      elapsed_usecs=(now.tv_sec-time_in.tv_sec)*1000000;
	      elapsed_usecs+=(now.tv_usec-time_in.tv_usec);
	      
	      remaining_usecs=cumulative_usecs-elapsed_usecs;
	      
	      if (remaining_usecs<=0) break;
	      else timeout_remaining=remaining_usecs/1000;		    
	    }
	    
	  }
      }
      cumulative_timeout+=this_timeout;
    }
  if (debug>1) if (cumulative_timeout>=timeout) 
		 fprintf(stderr,"Request timed out after retries (timeout=%d, elapsed=%d).\n",
			 timeout,cumulative_timeout);

  return 0;
}