0% found this document useful (0 votes)
155 views10 pages

COE 4DN4 Lab1: DNS-Domain Name Server

The document describes creating a domain name server program that reads a table mapping URLs to IP addresses from a text file, looks up the IP address corresponding to a URL received from a client, and returns the IP address to the client if it is found in the table or an error message if not found. Code snippets are provided for the server program that handles client connections and looks up the URL in the table, as well as for a client program that sends a URL to the server and receives the response.

Uploaded by

Rohan Thivy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views10 pages

COE 4DN4 Lab1: DNS-Domain Name Server

The document describes creating a domain name server program that reads a table mapping URLs to IP addresses from a text file, looks up the IP address corresponding to a URL received from a client, and returns the IP address to the client if it is found in the table or an error message if not found. Code snippets are provided for the server program that handles client connections and looks up the URL in the table, as well as for a client program that sends a URL to the server and receives the response.

Uploaded by

Rohan Thivy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

MCMASTER UNIVERSITY

COE 4DN4 Lab1


DNS- Domain Name Server

Rohan Thivy -0461139

1/31/2010
Objective:
The objective of this lab experiment is to create a Domain-Name- Server by editing the given Server Code.
Instead of echoing back the received string. This program reads in a Table from a text file with the URL and
its appropriate IP address. The program checks the URL received from the client and compares it with the
Table from the text file. If it finds a match it sends back the IP address of the URL. If it doesn’t it sends an
error message.

Introduction:
The DNS server stores the IP address for each and every URL . When a URL is typed into the address bar of
a browser. The browser sends a message to the DNS server requesting its IP address. The DNS server looks
up the URL using a look up and table and sends the corresponding IP address back to the browser.

Successful Screen Capture:


Server Side

Client Side
Server Flow Chart
TCPEchoServer.c
HandleTCPClient.c
Server Code
TCPEchoServer.c

#include <stdio.h> /* for printf() and fprintf() */


#include <winsock.h>
#include <stdlib.h> /* for atoi() and exit() */

#define MAXPENDING 5 /* Maximum outstanding connection requests */

void DieWithError(char *errorMessage); /* Error handling function */


void HandleTCPClient(int clntSocket); /* TCP client handling function */

int main(int argc, char *argv[])


{
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
int r;
WSADATA wsaData;
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}

echoServPort = atoi(argv[1]); /* First arg: local port */

if (WSAStartup(MAKEWORD(2, 0),&wsaData) != 0)
{
fprintf(stderr,"WSAStartup()failed");
exit(1);
}
/* Create socket for incoming connections */
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");

/* Construct local address structure */


memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */


if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");

/* Mark the socket so it will listen for incoming connections */


if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");

for (;;) /* Run forever */


{
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);

/* Wait for a client to connect */


if((clntSock = accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen))< 0)
DieWithError("accept() failed");

/* clntSock is connected to a client! */

printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

HandleTCPClient(clntSock);
}

/* NOT REACHED */
}

TCPEchoServer.h

#include <stdio.h> /* for printf() and fprintf() */


#include <winsock.h>
#include <stdlib.h>

void DieWithError(char *errorMessage); /* Error handling function */


void HandleTCPClient(int clntSocket); /* TCP client handling function */
int CreateTCPServerSocket(unsigned short port); /* Create TCP server socket */
int AcceptTCPConnection(int servSock); /* Accept TCP connection request */

HandleTCPClient.c
#include <stdio.h> /* for printf() and fprintf() */
#include <winsock.h>
#include <stdlib.h>
#define RCVBUFSIZE 32 /* Size of receive buffer */

void DieWithError(char *errorMessage); /* Error handling function */

void HandleTCPClient(int clntSocket, int servSocket)


{
char echoBuffer[RCVBUFSIZE]={NULL}; /* Buffer for echo string */
int recvMsgSize; /* Size of received message */
int numbofEntrees=16;
int i=0;
int j= 0;

struct Table//table with IPV4 addresses


{
char IP[32];//IP address
char name[128];//URL

} Table[16];
char testing1[128];
char testing2[32];
char junk[100];
int status=0;

FILE *fptr= fopen("c:\\temp\\data.txt","r");//file opened for reading


if (!fptr) printf(" The file cannot be found ");

while(j<numbofEntrees){ //reads data into Table


fscanf(fptr, "%s %s", &testing1,&testing2);

strcpy(Table[j].name,testing1);
strcpy(Table[j].IP,testing2);

j++;
}
fclose(fptr);//file pointer closed

/* Receive message from client */

if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)


DieWithError("recv() failed");

for(i=0;i<numbofEntrees;i++)//looks for recieved URL in Table


{
if (strcmp(echoBuffer,Table[i].name)==0)
break;
}

/* Send received string and receive again until end of transmission */


while (recvMsgSize > 0) /* zero indicates end of transmission */
{

if(i!=16){//i!=16 when URL is found in Table


/* Echo message back to client */
if (send(clntSocket,Table[i].IP, 15, 0) != 15)//sends IP address of URL
recieved
DieWithError("send() failed");

/* See if there is more data to receive */


if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");

if(i==16){//i=16 when the URL is not found in the Table


{if (send(clntSocket,"URL not found\n", 13, 0) != 13)
DieWithError("send() failed");
}
/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
}
}
closesocket(clntSocket); /* Close client socket */
}

DieWithError
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
#include <winsock.h>

void DieWithError(char *errorMessage)


{
WSAGetLastError(errorMessage);
exit(1);
}

Client Code
TCPEchoClient.c

#include <stdio.h> /* for printf() and fprintf() */


#include <winsock.h>
#include <stdlib.h> /* for atoi() and exit() */

#define RCVBUFSIZE 32 /* Size of receive buffer */

void DieWithError(char *errorMessage); /* Error handling function */

int main(int argc, char *argv[])


{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()*/
WSADATA wsaData; /* and total bytes read */

if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n",
argv[0]);
exit(1);
}

servIP = argv[1]; /* First arg: server IP address (dotted quad) */


echoString = argv[2]; /* Second arg: string to echo */
if (argc == 4)
echoServPort = atoi(argv[3]); /* Use given port, if any */
else
echoServPort = 7; /* 7 is the well-known port for the echo service */

if (WSAStartup(MAKEWORD(2, 0),&wsaData) != 0){


fprintf(stderr,"WSAStartup()failed");
exit(1);
}

/* Create a reliable, stream socket using TCP */


if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");

/* Construct the server address structure */


memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */

/* Establish the connection to the echo server */


if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");

echoStringLen = strlen(echoString); /* Determine input length */

/* Send the string to the server */


if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");

/* Receive the same string back from the server */


totalBytesRcvd = 0;
printf("Received: "); /* Setup to print the echoed string */
while (totalBytesRcvd < echoStringLen)
{
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf("%s", echoBuffer); /* Print the echo buffer */
}

printf("\n"); /* Print a final linefeed */

closesocket(sock);
WSACleanup();
exit(0);
}
DieWithError
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
#include <winsock.h>

void DieWithError(char *errorMessage)


{
WSAGetLastError(errorMessage);
exit(1);
}

Conclusion:
Getting the code to run on a windows platform was the only difficulty initially incurred. The code was initially built to
run using a table initialized within the program. But then we made the program read the table in from a text file and
store it in a structure. While doing this we had some trouble compiling the code and getting it to run with the text file.
But that was quickly sorted out.

You might also like