#include "common.h"

void source_addr(struct sockaddr_in sin, struct sockaddr_in *son) {
int fd;
socklen_t length;

	/* Allocate a socket */
	if((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))<0)
		ERROR("socket");

	length = sizeof(struct sockaddr_in);
	if(connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)))
		ERROR("connect");

	if(getsockname(fd, (struct sockaddr *)son, &length))
		ERROR("getsockname");

	close(fd);
}

unsigned short cksum(unsigned short *ptr,int nbytes)
{
 register long sum;
 unsigned short oddbyte;
 register unsigned short anwser;

 sum = 0;
 while(nbytes>1)
 {
  sum += *ptr++;
  nbytes -= 2;
 }
 if(nbytes==1)
 {
  oddbyte = 0;
  *((unsigned char *) & oddbyte) = *(unsigned char *)ptr;
  sum += oddbyte;
 }
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 anwser = ~sum;
 return(anwser);
}

char *token( char *string, const int c ) {
 
        while( *string ) {
                if( *string == c ) { return( string ); }
                string++;
        }
 
return( NULL );
}
