This is a source code on a Correspondent Node to receive an update
from a DNS server
#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>
char buf[100];
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];
}
char* getData(char *dest, char *sourc, int length, int position){
int temp;
dest[0] = '\0';
for(temp=0; temp<length; temp++)
{
dest[temp] = sourc[temp+position];
//printf("%c", dest[temp]);
}
//printf("%c OK\n", dest[0]);
return dest;
}
void main()
{
// send a new MN_IP to the CN
struct sockaddr_in6 si_me, si_other; // structure for holding an address
char straddr[100];
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
//char *mnip = "2004:db4:3000::3000"; // DNS server IP ()
//printf("print1\n");
// 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
//printf("print2\n");
//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
si_me.sin6_addr = in6addr_any;
// bind and listen to the socket
if (bind(sock, &si_me, sizeof(si_me))==-1)
printerror("bind error");
//printf("print3\n");
int newfd; // int to describe the socket
//printf("print4\n");
while(1)
{
printf("listening\n");
// listen for connections
listen(sock,5); // 5 sockets at the same times
// accept connection if any
newfd = accept(sock, (struct sockaddr *)&si_other, &slen);
printf("accepting\n");
//receive data in buffer
if (recv(newfd, buf, 100, 0)==-1)
printerror("recvf()");
printf("Received packet from IP %s and port %d\nData %s\n",
inet_ntop(AF_INET6, &si_other.sin6_addr, straddr, sizeof(straddr)),
ntohs(si_other.sin6_port), buf+3); // convert an IP structure to be a string
close(newfd);
int MN_len = buf[2]; // convert the string to integer
char MN_IP[MN_len+1];
char MN_name[100-(3+MN_len)+1];
getData(MN_IP,buf,MN_len,3);
MN_IP[MN_len] = '\0';
getData(MN_name,buf,100-(3+MN_len),MN_len+3);
MN_name[100-(3+MN_len)] = '\0';
//printf("Length = %d \n",MN_len);
printf("A new MN_IP = %s \n",MN_IP);
printf("Mobile Node's name = %s \n",MN_name);
}
close (sock);
}
Domain Name System for Locator Identity Mapping