This is a source code on a DNS server to map a Correspondent Node

with a Mobile Node

 

/#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <time.h>

char iptableslog[200];
char syslog[200];
char CN[10][40];
char MN[10][40];
int indexall = 0;
int shouldSendUpdate = 0;

long syslogIndex = 0;
long syslogIndexRead = 0;

long iptablesIndex = 0;
long iptablesIndexRead = 0;

//int indexalltemp = 0;
time_t currentTimiptablesIndexReade,checkTime;
int indexMNtemp = 0;
int indexCNtemp = 0;
int sizearray = 10;
char tempMN[10][40]; // temp table to store MN address from syslog
char tempCN[10][40]; // temp table to store CN address from iptables.log
char tempMNname[10][40];// temp table to contains MNname from syslog

int compareAddress(char *str1, char *str2);
int getandputCN(char *temp);
int fillMNarray(char *src, int index);
int fillCNarray(char *dest, int index);
void doubleCheck();

/* printerror(), #includes and #defines like in the server */
void printerror(char *str){
perror(str);
exit(1);
}

void arraycopy(char *dest, char *sourc, int length, int position){
int temp;
for(temp=0; temp<length; temp++)
dest[temp+position] = sourc[temp];
}

int main()
{
char buf[180]; //temporary buffer
int i=0;
FILE *fp;
char *length;
int lengthofstr = 0;

while(1)
{

indexMNtemp = 0;
indexCNtemp = 0;

/* Open the file for reading. */
if ( (fp = fopen("/home/pangcake/Desktop/iplog", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}

/* If end of file not reached, read a line and display it. */

iptablesIndex = 0;

while ( !feof(fp) )
{
fgets(buf, 180, fp);
iptablesIndex++;

if(iptablesIndex > iptablesIndexRead)
{
iptablesIndexRead = iptablesIndex;

//str[i]=buf;
lengthofstr = strlen(buf);
//sprintf(iptableslog,"%s",buf);
// getandputCN((char *)iptableslog);
// read all iptables.log
getandputCN((char *)buf);
// printf("iptableslog = %s",iptableslog);
memset(buf,0,200);
}
}

fclose(fp);
readsyslog(); //after this stage get tempCN and tempMN table
putCNintable();

doubleCheck();

if(shouldSendUpdate == 1)
checkMNname();

shouldSendUpdate = 0;
//printf("sleeping\n");

// printf("Number of Lines read = %d \n",syslogIndex);

int x;
printf("\tMN[] \t\t\t\t CN[] \n");
for(x = 0; x < 10; x++)
{
printf("%i. %s \t\t\t %s\n", x,MN[x], CN[x]);
}

printf("tempMN[] \t\t\t\t tempCN[] \n");
for(x = 0; x < 10; x++)
{
printf("%i. %s \t\t\t %s \n", x,tempMN[x], tempCN[x]);
}


sleep(10);
//(void) time(&currentTime);
//printf("CurrentTime: %s",currentTime);
}

return 0;
}

int getandputCN(char *temp) // get the CN address and put it in the tempCN
{
//printf("temp = %s \n",temp);
//get DST
//printf("%s\n",temp);

char *result = strstr(temp,(char *)"DNS:");

//printf("result = %s \n", result);

if(result != NULL)
{

result = strstr(temp,(char *)"SRC=");
//printf("result = %s \n",result);

int lengthdst1 = strlen(result);
//printf("length1 = %i \n", lengthdst1);
//printf("result = %s \n",result);

char *result2 = strstr(temp,(char *)" DST");
int lengthdst2 = strlen(result2);
//printf("length2 = %i \n", lengthdst2);
//printf("result2 = %s \n",result2);

char address[lengthdst1 - lengthdst2 - 4];
int ix;
strncpy(address,result+4,(lengthdst1 - lengthdst2 - 4)); // dest, source, size
// address[lengthdst1 - lengthdst2-1] = '\0';
// sprintf(address,"%s",address+4);

for(ix = 0; ix < (lengthdst1 - lengthdst2 - 4); ix++)
{
address[ix] = address[ix];
}

address[ix] = '\0';

char *result3 = strstr(temp,(char *)" L");

//int lengthdst3 = strlen(result3);
//printf("length3 = %i \n", lengthdst3);
//printf("result3 = %s \n",result3);

//char address2[lengthdst2 - lengthdst3 - 5];
//strncpy(address2,result2+5,(lengthdst2 - lengthdst3 - 5)); // dest, source, size

 

//for(ix = 0; ix < (lengthdst2 - lengthdst3 - 5); ix++)
//{
// address2[ix] = address2[ix];
// }

//address2[ix] = '\0';

//if(strcmp(address,address2) != 0)
{

// compare ipaddres that is the same or not if the same just break it and put it in the tempCN file
int k,v;
for(k=0; k < sizearray; k++)
{
v = strcmp(address,tempCN[k]);
if(v == 0)break;
}

 

if(v != 0)
{
filltempCN(address,indexCNtemp,(lengthdst1 - lengthdst2 - 4));
//fillMNarray("eins.mylinux.lan",indexall);
if (indexCNtemp < sizearray)
{
//printf("aaaa \n");
indexCNtemp++;
}

//printf("MN[%i] = %s \n",indexall-1,MN[indexall-1]);
printf("tempCN[%i] = %s \n",indexCNtemp-1,tempCN[indexCNtemp-1]);
}
}

}
else
{
return -1;
}
return 1;
}

int filltempMN(char *str, int index, int lenstr)
{
//int lengthsrc = strlen(str);
int lengthsrc = lenstr;
int i;
int done = -1;

for (i=0; i< lengthsrc; i++)
{
tempMN[index][i] = str[i];
done = 1;
}

if(i <= 38)
tempMN[index][i] = '\0';

//printf("XXX tempMN[%i] = %s and Length = %i and i = %i \n",index,tempMN[index], strlen(tempMN[index]),i);
return done;
}

int filltempCN(char *str, int index, int len)
{
//int lengthdest = strlen(str);
int lengthdest = len;
int i;
int done = -1;

//printf("dest = %s \n",dest);

for (i=0; i< lengthdest; i++)
{
tempCN[index][i] = str[i];
done = 1;
}
if(i <= 38)
tempCN[index][i] = '\0';
return done;
}

int filltempMNname(char *str, int index, int nameLen)
{
//int lengthsrc = strlen(str);
int lengthsrc = nameLen;
int i;
int done = -1;

//printf("src = %s \n",src);

for (i=0; i< lengthsrc; i++)
{
tempMNname[index][i] = str[i];
done = 1;
}

if(i <= 38)
tempMNname[index][i] = '\0';
return done;
}
//2004:db4:8000:0:21b:77ff:fe2a:1199

int fillMNarray(char *str, int index)
{
int lengthsrc = strlen(str);
int i;
int done = -1;

//printf("src = %s \n",src);

for (i=0; i< lengthsrc; i++)
{
MN[index][i] = str[i];
done = 1;
}
return done;
}

int fillCNarray(char *str, int index)
{
int lengthdest = strlen(str);
int i;
int done = -1;
//printf("dest = %s \n",dest);

for (i=0; i< lengthdest; i++)
{
CN[index][i] = str[i];
done = 1;
}
return done;
}
// read a syslog file and break it into 2 parts: MN's name, IP address
// put MN address in tempMN table
//
int readsyslog ()
{
char buf[200]; //temporary buffer
int i=0;
FILE *fp;
char *length;
int lengthofstr = 0;
char *result;
char *result2;
int lengthdst2;
int lengthdst1;
char *result3;
char *result4;
int lengthdst3;
int lengthdst4;


/* Open the file for reading. */
if ( (fp = fopen("/home/pangcake/Desktop/syslog", "r")) == NULL)
{
fprintf(stderr, "Error opening file.");
return -1;
//exit(1);
}

syslogIndex = 0;

/* If end of file not reached, read a line and display it. */

while ( !feof(fp) )
{
fgets(buf, 200, fp);
syslogIndex++;

if(syslogIndex > syslogIndexRead)
{
syslogIndexRead = syslogIndex;
lengthofstr = strlen(buf);
sprintf(syslog,"%s",buf);
//getandputCN((char *)iptableslog);
getandputCN((char *)buf);
//printf("syslog = %s",syslog);


result = strstr(buf,"updating zone");

//printf("result = %s \n", result);

if(result != NULL)
{

result = strstr(buf,"client");
//printf("result = %s \n",result);

lengthdst1 = strlen(result);
//printf("length1 = %i \n", lengthdst1);
//printf("result = %s \n",result);

result2 = strstr(buf,"#");
lengthdst2 = strlen(result2);
//printf("length2 = %i \n", lengthdst2);
//printf("result2 = %s \n",result2);

char address[lengthdst1 - lengthdst2 - 7];
// char address[lengthdst1 - lengthdst2 - 7];
int ix;
strncpy(address,result+7,(lengthdst1 - lengthdst2-7)); // dest, source, size

for(ix = 0; ix < (lengthdst1 - lengthdst2 - 7); ix++)
{
address[ix] = address[ix];
}

address[ix] = '\0';

// compare ipaddres that is the same or not if the same just break it
int k,v;
for(k=0; k < sizearray; k++)
{
//v = strcmp(addressDummy,tempMN[k]);
v = compareAddress(address,tempMN[k]);
if(v == 0)break;
}

if(v != 0)
{
// put MN address in the tempMN table
filltempMN(address,indexMNtemp,(lengthdst1 - lengthdst2 - 7));
//fillMNarray("eins.mylinux.lan",indexall);

//printf("MN[%i] = %s \n",indexall-1,MN[indexall-1]);
printf("tempMN[%i] = %s \n",indexMNtemp,tempMN[indexMNtemp]);

//printf("print anything\n");
//printf("print anything\n");

//get MN name in syslog
result3 = strstr(buf,"at ");
//printf("result3 = %s \n",result3);

lengthdst3 = strlen(result3);
//printf("length1 = %i \n", lengthdst1);
//printf\t\t\t\t\t("result = %s \n",result);

result4 = strstr(buf,"' AAAA");
lengthdst4 = strlen(result4);
//printf("length2 = %i \n", lengthdst2);
//printf("result2 = %s \n",result2);

char MNname[lengthdst3 - lengthdst4 - 4];
strncpy(MNname,result3+4,(lengthdst3 - lengthdst4 - 4)); // dest, source, size
//address[lengthdst1 - lengthdst2] = '\0';
//sprintf(MNname,"%s",MNname+4);
//printf("MNname = %s \n", MNname);

for(ix = 0; ix < (lengthdst3 - lengthdst4 - 4); ix++)
{
MNname[ix] = MNname[ix];
}

MNname[ix] = '\0';

filltempMNname(MNname,indexMNtemp,(lengthdst3 - lengthdst4 - 4));
shouldSendUpdate = 1;

//printf("MN[%i] = %s \n",indexall-1,MN[indexall-1]);
printf("tempMNname[%i] = %s \n",indexMNtemp,tempMNname[indexMNtemp]);

if (indexMNtemp < sizearray)
{
//printf("aaaa \n");
indexMNtemp++;
}


}

}
}
}
fclose(fp);
//printf("1q2w3e\n");
return 1;
}

// check tempCN and put it in the mapping table
void putCNintable ()
{
int i,j,k;
char tempstr[40];
for (i = 0; i < sizearray; i++ )
{

for (k = 0; k < sizearray; k++)// compare tempCN with tempMN 1:many
{
//j = strcmp(tempCN[i],tempMN[k]);
j = compareAddress(tempCN[i],tempMN[k]);
if (j == 0)
{
break;
}
}

if (j != 0)
{
int k,v;
for(k=0; k < sizearray; k++)
{
//v = strcmp(tempCN[i],CN[k]);
v = strcmp(tempCN[i],CN[k]);
if(v == 0)break;
}

if(v != 0)
{
fillCNarray(tempCN[i],indexall);
fillMNarray("eins.mylinux.lan",indexall);
printf("CN[%i] = %s \n",indexall,CN[indexall]);
printf("MN[%i] = %s \n",indexall,MN[indexall]);

if (indexall < sizearray)
{
indexall++;
}
}
}
}
}

int checkMNname()
{

int a,j,v;
int sizetempMNarray = 10;
int sizearray = 10;
int numberOfUpdates = 0;

for(a = 0; a < sizetempMNarray; a++)
{
for(j=0; j < sizearray; j++)
{
if (strcmp(tempMNname[a],"")!=0)
{
v = strcmp(tempMNname[a],MN[j]);

if (v == 0)
{
updatetoCN(a,CN[j]); // using index of tempMNname
numberOfUpdates++;
}
}
}
}
printf("Number Of Nodes Updated = %i \n",numberOfUpdates);
return numberOfUpdates;
}

int updatetoCN(int index, char *CNaddress)
{
printf("connecting to CN %s \n",CNaddress);

char dest[40];

arraycopy(dest, CNaddress, strlen(CNaddress), 0);

// send a new MN_IP to the CN
struct sockaddr_in6 si_me; // structure for holding an address
int sock; // s is a descriptor and it holds an integer that describes the socket
int slen=sizeof(si_me);// the size of the address
int value; //return value of connect
CNaddress = "::1\0";

// anytime to use the socket, just use the descriptor that is sock
// if cannt create the socket. it will return -1
if ((sock=socket(AF_INET6, SOCK_STREAM, 0))==-1){//create the socket, address family , TCP socket
printerror("socket");// tell socket error
return -1;
}

//initilizes siother to 0
memset((char *) &si_me, 0, sizeof(si_me)); // initilize the si_me to be 0
si_me.sin6_family = AF_INET6;
si_me.sin6_port = htons(5564);// port that we will connect to



if (inet_pton(AF_INET6,CNaddress, &si_me.sin6_addr)==0){ // addresss that we will connect to
printerror("inet_aton() failed\n");// if it's not connect, it will print out the error msg
return -1;
}
//if (inet_pton(AF_INET6,"2004:db4:6000:0000:0000:0000:0000:6000", &si_me.sin6_addr)==0) // addresss that we will connect to
// printerror("inet_aton() failed\n");// if it's not connect, it will print out the error msg


// connect to CN
value = connect(sock,(struct sockaddr *)&si_me, slen);// connecting to the socket

if(value == -1) return -1;

// form a packet
char wholepacket[100];
int MN_Len = strlen(tempMN[index]);

//printf("strlen(tempMN[index]) %i\n",MN_Len);
memset(wholepacket,0,100); // set the memory to be 0
wholepacket[0] = '1'; //version
wholepacket[1] = '1'; //type
wholepacket[2] = (char)MN_Len; // length of MN_IP
arraycopy(wholepacket,tempMN[index],strlen(tempMN[index]),3);// length of NewIP

//printf("wholepacket2 = %u\n",wholepacket[2]);

int temp_position = strlen(tempMN[index]) + 3;

arraycopy(wholepacket,tempMNname,strlen(tempMNname[index]),temp_position);
//printf("whole packet = %s \n",wholepacket);
// send MN_IP and CN_IP to the mapping table
if (send (sock, wholepacket, 100, 0)== -1)
printerror("send()");
// close socket
close (sock);
return 1;
}

//For comparing IP addresses
int compareAddress(char *str1, char *str2)
{
struct in6_addr si_1,si_2;
char straddr1[100];
char straddr2[100];

int x = inet_pton(AF_INET6,str1, &si_1);
int y = inet_pton(AF_INET6,str2, &si_2);

//printf("x = %i and y = %i\n",x,y);

x = inet_ntop(AF_INET6,&si_1,straddr1,sizeof(straddr1));
y = inet_ntop(AF_INET6,&si_2,straddr2,sizeof(straddr2));

//printf("x = %i and y = %i\n",x,y);

//printf("str1 = %s si_1 = %s \n",str1,straddr1);
//printf("str2 = %s si_2 = %s \n",str2,straddr2);

//printf("Compare results = %i \n",strcmp(straddr1,straddr2));

return strcmp(straddr1,straddr2);
}

void doubleCheck()
{
//lets compare tmepMN values to CN values and get rid of any MN values in CN
int k,j,v;

for(k = 0; k < 10; k++)//tempMN values
{

for(j = 0; j < 10; j++)//CN values
{
//v = strcmp(tempMN[k],CN[j]);
v = compareAddress(tempMN[k],CN[j]);
if(v == 0)
{
//remove this item from table
CN[j][0] = '\0';
MN[j][0] = '\0';
indexall--;
}
}

}

}

Domain Name System for Locator Identity Mapping

 


 

 

Home

Dissertation Report

Mobile Node Program

Mapping Table Program

Correspondent Node Program

Shell Script