Вы находитесь на странице: 1из 22

Page 1

NP LAB REPORT
LAB 1
EXERCISE 1.1
QUESTION 1
The default directory is
120905642@networklab-24:~$
QUESTION 2
my working directory is
120905642@networklab-24:~/120905642$
EXERCISE 1.2
QUESTION 1
After executing ps -e
120905642@networklab-24:~/120905642$ ps -e
PID TTY
TIME CMD
1?
00:00:01 init
2?
00:00:00 kthreadd
3?
00:00:00 ksoftirqd/0
5?
00:00:00 kworker/0:0H
7?
00:00:00 rcu_sched
8?
00:00:00 rcuos/0
9?
00:00:00 rcuos/1
10 ?
00:00:00 rcuos/2
11 ?
00:00:00 rcuos/3
12 ?
00:00:00 rcu_bh
13 ?
00:00:00 rcuob/0
14 ?
00:00:00 rcuob/1
15 ?
00:00:00 rcuob/2
16 ?
00:00:00 migration/0
17 ?
00:00:00 watchdog/0
18 ?
00:00:00 watchdog/1
19 ?
00:00:00 migration/1
QUESTION 2
After executing the telnet command in the window when we give ps -e in the third window we get one more process running
8591 pts/6 00:00:00 telnet
QUESTION 3
The process id of the telnet process is
8591 pts/6 00:00:00 telnet
QUESTION 4
120905642@networklab-24:~$ kill 8591
EXERCISE 1.3
QUESTION 1
The xinetd is not started in my computer
120905642@networklab-24:~$ ps -e|grep xinetd
120905642@networklab-24:~$
QUESTION 2
The inetd is not started in my computer
120905642@networklab-24:~$ ps -e|grep inetd
120905642@networklab-24:~$
EXERCISE 1.4
QUESTION 1
The two files ser_more and ser_cp are identical. On executing the cmp command it shows they are identical

Page 2
120905642@networklab-24:~$ cmp ser_more ser_cp
120905642@networklab-24:~$
QUESTION 2
the file sizes are
ser_more =19002 ser_cp =19002 ser_cat= 37145
EXERCISE 1.5
QUESTION 1
arp- used with the IPv4 network. ARP is used to resolve the ip address to the mac address. the mac address is specific to the hardware
QUESTION 2
arping- arping requires CAP_NET_RAW capability to be executed. It is not recommended to be used as set-uid root, because it allows user to modify ARP
caches of neighbour hosts.

QUESTION 3
ifconfig- Ifconfig is used to configure the kernel-resident network interfaces.It is used at boot time to set up interfaces as necessary. After that, it is usually
only needed when debugging or when system tuning is needed.

QUESTION 4
tcpdump- Tcpdump prints out a description of the contents of packets on a network interface that match the boolean expression. It can also be run with the
-w flag, which causes it to save the packet data to a file for later analysis, and/or with the -r flag, which causes it to read from a saved packet file rather than to
read packets from a network interface
QUESTION 5
ping- ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.
ECHO_REQUEST datagrams (``pings'') have an IP and ICMP header, followed by a struct timeval and then an arbitrary number of ``pad'' bytes used to fill
out the packet.

QUESTION 6
netstat-Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

QUESTION 7
route-Route manipulates the kernel's IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has
been configured with the ifconfig(8) program. When the add or del options are used, route modifies the routing
tables. Without these options, route displays the current contents of
nethe routing tables.

QUESTION 8
wireshark- Wireshark is a GUI network protocol analyzer. It lets you interactively browse packet data from a live network or from a previously saved capture
file. Wireshark's native capture file format
is pcap format, which is also the format used by tcpdump and various
other tools.
EXERCISE 1.6
The C program to copy the content of 1 file to another is
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd1,fd2;
fd1=open("file1",O_RDONLY,0777);
fd2=open("file2",O_WRONLY,0777);
char c[1024];
int n,w;
while(1)
{
n=read(fd1,c,1024);
if(n!=0)
w=write(fd2,c,n);

Page 3
else
break;
}
close(fd1);
close(fd2);
return 1;
}
OUTPUT- copies the file
EXERCISE 1.7
demonstrating the use of pipes
#include<stdio.h>
#include<string.h>
#define READ 0;
#define WRITE 1;
char *msg= i am a message;
int main()
{
int fd[2];
int bytesread;
char message[100];
pipe(fd);
if(fork()==0)
{
close(fd[READ]);
write(fd[WRITE],msg,strlen(msg)+1);
}
else
{
close(fd[WRITE]);
bytesread=read(fd[READ],message,100);
printf(READ %d bytes %s,message,bytesread);
close (fd[READ]);
}
}
The program creates a parent and a child process where one writes from the pipe and one reads from the pipe.

LAB 2
FTP CLIENT
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <sys/time.h>
#define DEST_IP "127.0.0.1"
#define DEST_PORT 6893
void usage(const char *progname) {
fprintf(stderr, "Usage: %s <server-IP> <server-port>\n", progname);
} // End usage()
int sock_and_connect(struct in_addr server_ip, int port) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0); // Make appropriate socket(...) call
if(sock_fd == -1) { // Check error condition
perror("socket failed"); return -1;
}

Page 4
struct sockaddr_in serv_addr;
//---------- Checkpoint-1a: comment out the rest of this function to test until here ----------//
/* blocking connect */
serv_addr.sin_addr.s_addr = inet_addr(DEST_IP);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memset(&(serv_addr.sin_zero), '\0', 8);
int res = connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if(res == 0) { return sock_fd; } // connection formed
else { perror("connect failed"); return -1; }
} // End sock_and_connect()
int main(int argc, char *argv[]) {
if(argc != 3) {
usage(argv[0]);
exit(1);
}
// Get the IP address as an in_addr structure, from the string
const char *server_str = argv[1];
struct in_addr server_addr;
if (inet_aton(server_str, &server_addr) == 0) {
fprintf(stderr, "Invalid address: %s\n", server_str);
exit(2);
}
// Get the server port from the command-line
int server_port = atoi(argv[2]);
//---------- Checkpoint-0: comment out the code below to test until here ----------//
int loop = 0;
do {
char filename[256];
// Get filename from STDIN
printf("Enter filename: ");
scanf("%s", filename);
filename[sizeof(filename)-1] = '\0'; // For additional safety
int filename_len = strlen(filename);
// Make connection to server
int sock_fd = sock_and_connect(server_addr, server_port);
if(sock_fd == -1) {
fprintf(stderr, "Unable to connect to server\n");
continue;
}
loop=1;
//---------- Checkpoint-2: comment out the code below to test until here ----------//
// Send filename via TCP connection to server
send(sock_fd, filename, 256, 0);
//---------- Checkpoint-4: comment out the code below to test until here ----------//
// Read file contents via TCP connection and store to local file
char new_filename[266];
sprintf(new_filename, "%s-dl", filename);
FILE *local_file = fopen(new_filename, "w");
if(local_file == NULL) {
perror("Can't open file to write");
close(sock_fd);
continue;

Page 5
}
char recv_buf[2000];
int num_recd;
while((num_recd = recv( sock_fd, recv_buf, 2000, 0)) > 0) {
printf("%d bytes recvd", num_recd);
fwrite(recv_buf, 1, num_recd, local_file);
}
fclose(local_file);
close(sock_fd);
printf("Written file %s successfully\n", new_filename);
/*//---------- Checkpoint-5: If you've completed until here then you can test the whole functionality once ----------//
//---------- Checkpoint-6: UNCOMMENT the line below to test forever loop ----------//
//loop = 1;*/
} while(loop);
} // End main()
FTP SERVER
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <sys/time.h>
#define PORT 6893
void usage(const char *progname) {
fprintf(stderr, "Usage: %s <server-port>\n", progname);
} // End usage()
int sock_and_listen(int port) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0); // Make appropriate socket(...) call
if(sock_fd == -1) { // Check error condition
perror("socket failed"); return -1;
}
struct sockaddr_in serv_addr;
//---------- Checkpoint-1a: comment out the rest of this function to test until here ----------//
/* bind, listen */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(port);
if(bind(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0) {
perror("bind"); close(sock_fd); return -1;
}
//---------- Checkpoint-1b: comment out the rest of this function to test until here ----------//
if(listen(sock_fd, SOMAXCONN) < 0) {
perror("listen"); close(sock_fd); return -1;
}
return sock_fd;
} // End sock_and_listen()
int main(int argc, char *argv[]) {
if(argc != 2) {

Page 6
}

usage(argv[0]);
exit(1);

// Get the server port from the command-line


int server_port = atoi(argv[1]);
// socket, bind and listen
int listen_sock_fd = -1;
listen_sock_fd = sock_and_listen(server_port);
if(listen_sock_fd == -1) { perror("sock_and_listen"); exit(100); }
fprintf(stderr, "Listening on port %d\n", server_port);
//---------- Checkpoint-1: comment out the code below to test until here ----------//
int loop = 0;
do {
printf("Waiting for connection from client...\n");
// Accept connection from client
struct sockaddr_in cl_addr;
int cl_addr_size = sizeof(cl_addr);
char cl_str[1024];
int conn_sock_fd = accept(listen_sock_fd, &cl_addr, &cl_addr_size); // Make appropriate accept(...) call
if(conn_sock_fd < 0) {
perror("accept");
continue;
}
//---------- Checkpoint-2: comment out the code below to test until here ----------//
strcpy(cl_str, inet_ntoa(cl_addr.sin_addr)); // Convert IP address to string for printing
printf("Connection from %s:%d\n", cl_str, ntohs(cl_addr.sin_port));
loop = 1;
//---------- Checkpoint-3: comment out the code below to test until here ----------//
// Read filename from client
char filename[256];
int recd_len = recv( conn_sock_fd, filename, 256, 0); // Make appropriate recv(...) call
if(recd_len < 0) {
perror("recv");
close(conn_sock_fd);
continue;
}
if(recd_len == 0) {
fprintf(stderr, "Recd empty filename from client\n");
close(conn_sock_fd);
continue;
}
filename[sizeof(filename)-1] = '\0'; // for additional safety
printf("Got filename %s from client\n", filename);
//---------- Checkpoint-4: comment out the code below to test until here ----------//
// Send file to client
FILE *file2read = fopen(filename, "r");
if(file2read == NULL) {
perror("file open");
close(conn_sock_fd);
continue;
}
char buf[2000];
int num_read = 0;
while((num_read = fread(buf, 1, sizeof(buf), file2read)) > 0) {
send(conn_sock_fd, buf, num_read, 0); // Make appropriate send(...) call
}

Page 7
close(conn_sock_fd);
fclose(file2read);
printf("Done sending file %s\n", filename);
/*//---------- Checkpoint-5: If you've completed until here then you can test the whole functionality once ----------//
//---------- Checkpoint-6: UNCOMMENT the line below to test forever loop ----------//
//loop = 1;*/
} while(loop);
}
UDP ECHO CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MYPORT 7000 // the port users will be connecting to
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in their_addr; // connectors address information
struct hostent *he;
int numbytes;
if (argc != 3) {
fprintf(stderr,"usage: talker hostname message\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}
// get the host info
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("recvfrom");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
UDP ECHO SERVER
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

Page 8
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 7000
#define MAXBUFLEN 100

int main(void)
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; // connectors address information
int addr_len, numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // automatically fill with my IP
bzero(&(my_addr.sin_zero), 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
while(1==1){
if ((numbytes=recvfrom(sockfd,buf, MAXBUFLEN-1, 0,(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
printf("packet contains \"%s\"\n",buf);

}
close(sockfd);
return 0;

UDP CONCURRENT CLIENT


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MYPORT 7000
#define MAXBUFLEN 100 // the port users will be connecting to
int main(int argc, char *argv[])
{

Page 9
int sockfd;
struct sockaddr_in their_addr; // connectors address information
struct hostent *he;
int numbytes;
if (argc != 3) {
fprintf(stderr,"usage: talker hostname message\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}
// get the host info
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);

if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {


perror("recvfrom");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
char buf[MAXBUFLEN];
addr_len = sizeof(struct sockaddr);
while(1==1){
if ((numbytes=recvfrom(sockfd,buf, MAXBUFLEN-1, 0,(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("REPLY: %s\n", buf));
close(sockfd);
return 0;
}
close(sockfd);
return 0;

UDP CONCURRENT SERVER


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 7000
#define MAXBUFLEN 100

int main(void)
{
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr; // connectors address information
int addr_len, numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {

Page 10
perror("socket");
exit(1);

}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // automatically fill with my IP
bzero(&(my_addr.sin_zero), 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
while(1==1){
if ((numbytes=recvfrom(sockfd,buf, MAXBUFLEN-1, 0,(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
else{
if(fork()==0){
int child_id = getpid();
//fix this part of the code to convert to sqaure
int i,temp = 0;
for(i=0;i<numbytes;i++){
temp = (temp*10) + (buf[i]-'0';
temp = temp * temp;
char c[20];
//converts int to char
sprintf(c, "%d", temp);
int temp_sock;
if ((temp_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((numbytes = sendto(temp_sock, c, strlen(c), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("recvfrom");
exit(1);
}
printf("%d: reply sent %d\n",child_id);
}

}
close(sockfd);
return 0;

LAB 3
FTP CLIENT (TCP)
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <sys/time.h>
#define DEST_IP "127.0.0.1"
#define DEST_PORT 6893

Page 11
void usage(const char *progname) {
fprintf(stderr, "Usage: %s <server-IP> <server-port>\n", progname);
} // End usage()
int sock_and_connect(struct in_addr server_ip, int port) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0); // Make appropriate socket(...) call
if(sock_fd == -1) { // Check error condition
perror("socket failed"); return -1;
}
struct sockaddr_in serv_addr;
//---------- Checkpoint-1a: comment out the rest of this function to test until here ----------//
/* blocking connect */
serv_addr.sin_addr.s_addr = inet_addr(DEST_IP);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memset(&(serv_addr.sin_zero), '\0', 8);
int res = connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if(res == 0) { return sock_fd; } // connection formed
else { perror("connect failed"); return -1; }
} // End sock_and_connect()
int main(int argc, char *argv[]) {
if(argc != 3) {
usage(argv[0]);
exit(1);
}
// Get the IP address as an in_addr structure, from the string
const char *server_str = argv[1];
struct in_addr server_addr;
if (inet_aton(server_str, &server_addr) == 0) {
fprintf(stderr, "Invalid address: %s\n", server_str);
exit(2);
}
// Get the server port from the command-line
int server_port = atoi(argv[2]);
//---------- Checkpoint-0: comment out the code below to test until here ----------//
int loop = 0;
do {
char filename[256];
// Get filename from STDIN
printf("Enter filename: ");
scanf("%s", filename);
filename[sizeof(filename)-1] = '\0'; // For additional safety
int filename_len = strlen(filename);
// Make connection to server
int sock_fd = sock_and_connect(server_addr, server_port);
if(sock_fd == -1) {
fprintf(stderr, "Unable to connect to server\n");
continue;
}
loop=1;
//---------- Checkpoint-2: comment out the code below to test until here ----------//
// Send filename via TCP connection to server
send(sock_fd, filename, 256, 0);
//---------- Checkpoint-4: comment out the code below to test until here ----------//

Page 12
// Read file contents via TCP connection and store to local file
char new_filename[266];
sprintf(new_filename, "%s-dl", filename);
FILE *local_file = fopen(new_filename, "w");
if(local_file == NULL) {
perror("Can't open file to write");
close(sock_fd);
continue;
}
char recv_buf[2000];
int num_recd;
while((num_recd = recv( sock_fd, recv_buf, 2000, 0)) > 0) {
printf("%d bytes recvd", num_recd);
fwrite(recv_buf, 1, num_recd, local_file);
}
fclose(local_file);
close(sock_fd);
printf("Written file %s successfully\n", new_filename);
/*//---------- Checkpoint-5: If you've completed until here then you can test the whole functionality once ----------//
//---------- Checkpoint-6: UNCOMMENT the line below to test forever loop ----------//
//loop = 1;*/
} while(loop);
} // End main()

FTP SERVER (TCP)


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <sys/time.h>
#define PORT 6893
void usage(const char *progname) {
fprintf(stderr, "Usage: %s <server-port>\n", progname);
} // End usage()
int sock_and_listen(int port) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0); // Make appropriate socket(...) call
if(sock_fd == -1) { // Check error condition
perror("socket failed"); return -1;
}
struct sockaddr_in serv_addr;
//---------- Checkpoint-1a: comment out the rest of this function to test until here ----------//
/* bind, listen */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(port);
if(bind(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0) {
perror("bind"); close(sock_fd); return -1;
}

Page 13
//---------- Checkpoint-1b: comment out the rest of this function to test until here ----------//
if(listen(sock_fd, SOMAXCONN) < 0) {
perror("listen"); close(sock_fd); return -1;
}
return sock_fd;
} // End sock_and_listen()
int main(int argc, char *argv[]) {
if(argc != 2) {
usage(argv[0]);
exit(1);
}
// Get the server port from the command-line
int server_port = atoi(argv[1]);
// socket, bind and listen
int listen_sock_fd = -1;
listen_sock_fd = sock_and_listen(server_port);
if(listen_sock_fd == -1) { perror("sock_and_listen"); exit(100); }
fprintf(stderr, "Listening on port %d\n", server_port);
//---------- Checkpoint-1: comment out the code below to test until here ----------//
int loop = 0;
do {
printf("Waiting for connection from client...\n");
// Accept connection from client
struct sockaddr_in cl_addr;
int cl_addr_size = sizeof(cl_addr);
char cl_str[1024];
int conn_sock_fd = accept(listen_sock_fd, &cl_addr, &cl_addr_size); // Make appropriate accept(...) call
if(conn_sock_fd < 0) {
perror("accept");
continue;
}
//---------- Checkpoint-2: comment out the code below to test until here ----------//
strcpy(cl_str, inet_ntoa(cl_addr.sin_addr)); // Convert IP address to string for printing
printf("Connection from %s:%d\n", cl_str, ntohs(cl_addr.sin_port));
loop = 1;
//---------- Checkpoint-3: comment out the code below to test until here ----------//
// Read filename from client
char filename[256];
int recd_len = recv( conn_sock_fd, filename, 256, 0); // Make appropriate recv(...) call
if(recd_len < 0) {
perror("recv");
close(conn_sock_fd);
continue;
}
if(recd_len == 0) {
fprintf(stderr, "Recd empty filename from client\n");
close(conn_sock_fd);
continue;
}
filename[sizeof(filename)-1] = '\0'; // for additional safety
printf("Got filename %s from client\n", filename);
//---------- Checkpoint-4: comment out the code below to test until here ----------//
// Send file to client
FILE *file2read = fopen(filename, "r");

Page 14
if(file2read == NULL) {
perror("file open");
close(conn_sock_fd);
continue;
}
char buf[2000];
int num_read = 0;
while((num_read = fread(buf, 1, sizeof(buf), file2read)) > 0) {
send(conn_sock_fd, buf, num_read, 0); // Make appropriate send(...) call
}
close(conn_sock_fd);
fclose(file2read);
printf("Done sending file %s\n", filename);
/*//---------- Checkpoint-5: If you've completed until here then you can test the whole functionality once ----------//
//---------- Checkpoint-6: UNCOMMENT the line below to test forever loop ----------//
//loop = 1;*/
} while(loop);
}

LAB 4-5
TIME CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 9999
#define BUFLEN 100
#define MAX 5
#define p(s) perror("s")
typedef struct sockaddr_in sin;
int sock_and_connect(struct in_addr s_ip, int s_port)
{
int sock_fd=socket(AF_INET,SOCK_STREAM,0);
if(sock_fd==-1)
{
p(socket);
return -1;
}
struct sockaddr_in serv_addr;
serv_addr.sin_addr.s_addr = inet_addr("172.16.59.33");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(s_port);
memset(&(serv_addr.sin_zero), '\0', 8);

if(connect(sock_fd,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr))==0)


return sock_fd;
return -1;

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


{

Page 15
if(argc!=4)
{
printf("Too few arguments");
exit(1);
}
int s_port;
char *server_p=argv[1];
struct in_addr s_addr;
if(inet_pton(AF_INET,server_p,&s_addr)==-1)
{
p(pton);
exit(1);
}
s_port=atoi(argv[2]);
int loop=0;
/*printf("%s",argv[3]);*/
time_t t= time(NULL);
char *st;
st=ctime(&t);
//printf("%s",argv[3]);
/*printf("Enter string:\t");
scanf("%s",st);
printf("%s",st);*/
int sock_fd=sock_and_connect(s_addr,PORT);
if(sock_fd==-1)
{
printf("Unable to connect\n");
exit(1);
}
char buf[100];
int recb=recv(sock_fd,buf,200,0);
if(recb<0)
{
p(recv);
exit(1);
}
if(recb==0)
{
printf("\nRECIEVED NULL");
exit(1);
}
buf[recb-1]='\0';

printf("SERVER TIME:\t ,%s\n",buf);


printf("Client TIME:\t ,%s\n",st);
close(sock_fd);
printf("Send Sucessfull");
return 0;

TIME SERVER
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 9999
#define BUFLEN 100
#define MAX 5

Page 16
#define p(s) perror("s")
typedef struct sockaddr_in sin;
int sock_and_listen()
{
sin my_addr;
int sock_fd;
if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
p(socket);
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr=inet_addr("172.16.59.67 ");
my_addr.sin_port=htons(PORT);
bzero(&(my_addr.sin_zero),8);
if((bind(sock_fd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)))==-1)
{
p(bind);
exit(1);
}
if(listen(sock_fd,MAX)<0)
{
p(listen);
close(sock_fd);
return -1;
}
return sock_fd;
}
int main(int argc, char *argv[])
{
int sock_fd=sock_and_listen();
if(sock_fd==-1)
{
p(sock_and_listen);
exit(1);
}
fprintf(stderr, "Listening on port %d\n", PORT);
int loop=0;

printf("Waiting for connection...\n");


sin their_addr;
int their_addr_size=sizeof(their_addr);
int cl_fd=accept(sock_fd,(struct sockaddr*)&their_addr, &their_addr_size);
if(cl_fd<0)
{
p(accept);
exit(1);
}
char prinable[50];
if(inet_ntop(AF_INET,(&their_addr.sin_addr),prinable,sizeof(struct in_addr))==-1)
{
p(ntop);
}
char buf[200];
time_t t= time(NULL);
char *st;
st=ctime(&t);
send(cl_fd,st,strlen(st),0);
close(cl_fd);

Page 17
return 0;
}

LAB 6
6.1
eth0

Link encap:Ethernet HWaddr 00:1f:d0:40:78:db


inet addr:172.16.59.106 Bcast:172.16.59.255 Mask:255.255.255.0
inet6 addr: fe80::21f:d0ff:fe40:78db/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23341 errors:0 dropped:0 overruns:0 frame:0
TX packets:5880 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12205191 (12.2 MB) TX bytes:525385 (525.3 KB)
Interrupt:42 Base address:0x2000

lo

Link encap:Local Loopback


inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:7727 errors:0 dropped:0 overruns:0 frame:0
TX packets:7727 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:9799725 (9.7 MB) TX bytes:9799725 (9.7 MB)

There are 2 interfaces as listed above


eth0 is ethernet interface for connection with remote hosts
lo is loopback interface and all connections for the loopback IP address are routed back to the same system from which they originate.
MTU's are displayed above
network is subnetted because the mask refers to the mask of a Class C address but the IP address corresponds to that of a class B address.Reasons for
subnetting is that the allocated block may be too large and hence to prevent wastage of IP addresses we subdivide the block and allocate separately.

6.2
Yes the interface is on
One ICMP message is --15:49:39.561428 IP networklab-HP-dx2480-MT-KL969AV-16.local > mpl-ab-adc01.mahe.manipal.net: ICMP networklab-HP-dx2480-MT-KL969AV-16.local udp
port 23220 unreachable, length 70
This message is displayed because the packet wasnt able to reach the specified port number.
6.3
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0
1500 0 22207
0
00
5831
0
0
0 BMRU
lo
16436 0
7719
0
00
7719
0
0
0 LRU
6.6
15:57:17.349885 00:1f:d0:40:78:db > 00:1f:d0:40:fe:1c, ethertype IPv4 (0x0800), length 98: 172.16.59.26 > 172.16.59.20: ICMP echo reply, id 5316, seq 1,
length 64
0x0000: 4500 0054 527c 0000 4001 59de ac10 3b1a
0x0010: ac10 3b14 0000 4324 14c4 0001 ddd9 f554
0x0020: 0000 0000 0815 0e00 0000 0000 1011 1213
0x0030: 1415 1617 1819 1a1b 1c1d 1e1f 2021 2223
0x0040: 2425 2627 2829 2a2b 2c2d 2e2f 3031 3233
0x0050: 3435 3637
6.4
ARP TABLE
$ arp -a
? (172.16.59.115) at 28:d2:44:e7:6c:b4 [ether] on eth0
? (172.16.59.10) at 34:40:b5:d5:0e:ec [ether] on eth0
? (172.16.59.60) at 00:1f:d0:40:74:53 [ether] on eth0

Page 18
? (172.16.59.1) at 00:00:0c:07:ac:3b [ether] on eth0
? (172.16.59.16) at 6c:f0:49:90:4d:5c [ether] on eth0
arp table after pinging 172.16.58.23
? (172.16.59.115) at 28:d2:44:e7:6c:b4 [ether] on eth0
? (172.16.59.10) at 34:40:b5:d5:0e:ec [ether] on eth0
? (172.16.59.60) at 00:1f:d0:40:74:53 [ether] on eth0
? (172.16.59.1) at 00:00:0c:07:ac:3b [ether] on eth0
? (172.16.59.23) at 00:1c:c1:31:fa:6f [ether] on eth0
? (172.16.59.16) at 6c:f0:49:90:4d:5c [ether] on eth0
6.5
There are 4 retransmission requests according to the Wireshark Output displayed.

LAB 7
7.1
$ cat hostname
networklab-HP-dx2480-MT-KL969AV
$ cat hosts
127.0.0.1 localhost
127.0.1.1 networklab-HP-dx2480-MT-KL969AV
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
$ cat interfaces
auto lo
iface lo inet loopback
$ cat resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.0.1
search mahe.manipal.net
$ cat protocols
# Internet (IP) protocols
#
# Updated from http://www.iana.org/assignments/protocol-numbers and other
# sources.
# New protocols will be added on request if they have been officially
# assigned by IANA and are not historical.
# If you need a huge list of used numbers please install the nmap package.
.
.
.
.
# Local services
7.2
7.2.1
hostname:networklab-HP-dx2480-MT-KL969AV
IP address:172.16.59.26
Got this information from "infconfig" command and "hostname" file
7.2.2
(Next Hop Router info)
IP Add:172.16.59.3 ( using trace route )
MAC:00:1f:d0:40:78:db
using WireShark Application

Page 19
7.2.3
IP Add : 127.0.0.1
hostname:networklab-HP-dx2480-MT-KL969AV
DNS server hostname and IP address found by reading /etc/resolv.config file
IP address : 172.16.150.181
Hostname: dns.mahe.manipal.net
Local DNS server hostname and IP found by using the nslookup command
7.2.4
The numbers in the /etc/protocol file represent what number has to filed in the protocol field of the IP packet. It represents what kind of packet IP
encapsulates.
7.2.5
Service Port
Number
ftp
21
ssh
22
nfs
2049
smtp
25
Found by reading the /etc/services file and using the grep command to find the specific
service
7.3
$ wget --no-proxy http://mycse
--2015-03-03 16:44:28-- http://mycse/
Resolving mycse (mycse)... 172.16.59.10
Connecting to mycse (mycse)|172.16.59.10|:80... ^C
$ wget http://www.google.com
--2015-03-03 16:44:54-- http://www.google.com/
Connecting to 127.0.0.1:3128... connected.
Proxy request sent, awaiting response...
7.3.1
Command
Explanation
sudo tcpdump -n host 172.16.59.10 -w lab71.pcap
Stores packets only from mycse to lab71.pcap
wget --no-proxy http://mycse
Attempts to download the portal
7.3.2
a)

b)

c)

Next hop IP address : 172.16.59.10


Determined by reading the destination field of the IP header.
Next hop MAC address : (34:40:b5:d5:0e:ec)
Determined by reading the destination field of the ethernet header
Next hop IP address : 172.16.59.10
Determined by reading the destination field of the IP header.
Next hop MAC address : (34:40:b5:d5:0e:ec)
Determined by reading the destination field of the ethernet header
Ethernet Layer
Source: HewlettP_01:65:ad (24:be:05:01:65:ad)
Decimal Value: 36:190:05:01:101:173
The packet is sent to the IP layer (0x0800)
Decimal: 2048
IP Layer
Destination IP: 172.16.59.10
This packet is sent to TCP
Protocol: TCP (6)
Flags: 0x02 (Don't Fragment)
TCP layer
Destination port: http (80)

Page 20

LAB 8
We first use SSH command and check with whether remote login works with each others' PCs.
SSH
ssh (SSH client) is a program for logging into a remote machine and for
executing commands on a remote machine. It is intended to replace rlogin
and rsh, and provide secure encrypted communications between two
untrusted hosts over an insecure network. X11 connections and arbitrary
TCP ports can also be forwarded over the secure channel.
ssh connects and logs into the specified hostname (with optional user
name). The user must prove his/her identity to the remote machine using
one of several methods depending on the protocol version used (see
below).
The we run TCPdump on other terminal to capture the traffic.
We observe the various SSH and TCP packets sent to 172.16.59.10
The first packet is SYN
Rest are ACK

8.2
Address Resolution Protocol (ARP)
When Col. Raaja read up on how packet forwarding is done at a host, he came
to know that in cases where the destination IP address belongs to the host's
own subnet, the packet goes directly, otherwise it goes via a router. He wants
to check this out. Can you design an experiment that illustrates this? Also, he
wants to know what happens if you try to send a packet to a non existent host
within the same subnet. Help him with that as well.
I.
For the first case when you're sending to a system inside the network the msg goes directly. No intermediate router is used.
II
In the second case whilst sending to an outside subnet (10.107.1.1) the message is first sent to an intermediate destination (172.16.59.1), which is the router.
This address then sends it forward to the required address.
III
In the third case whilst sending to an invalid address (10.105.12.1) the message is first sent to an intermediate destination (172.16.59.1), which is the router.
This address then sends it forward to the required address(which in this case is not valid so it recieves destination unreachable message).

LAB 9
9.1
Header Format(In Decimal values)
Version: 4
Header length: 20 bytes
Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport))
Total Length: 52
Identification: 0x4c2d (19501)
Flags: 0x02 (Don't Fragment)
Fragment offset: 0
Time to live: 64
Protocol: TCP (6)
Header checksum: 0x203a [correct]
Source: 172.16.59.50 (172.16.59.50)
Destination: 172.16.59.10 (172.16.59.10)
The value of protocol is 6 because it is TCP.
The protocol field is used to specify which protocol is the packet using.
9.2
ARP Request
47
6.012296 Giga-Byt_40:78:db All-HSRP-routers_3b
Hardware type: Ethernet (1)
Protocol type: IP (0x0800)
Hardware size: 6
Protocol size: 4
Sender MAC address: Giga-Byt_40:78:db (00:1f:d0:40:78:db)

ARP

42

Who has 172.16.59.1? Tell 172.16.59.50

Page 21
Sender IP address: 172.16.59.50 (172.16.59.50)
Target MAC address: 00:00:00_00:00:00 (00:00:00:00:00:00)
Target IP address: 172.16.59.1 (172.16.59.1)

ARP Reply
48
6.018843 All-HSRP-routers_3b
Giga-Byt_40:78:db ARP
Hardware type: Ethernet (1)
Protocol type: IP (0x0800)
Hardware size: 6
Protocol size: 4
Sender MAC address: All-HSRP-routers_3b (00:00:0c:07:ac:3b)
Sender IP address: 172.16.59.1 (172.16.59.1)
Target MAC address: Giga-Byt_40:78:db (00:1f:d0:40:78:db)
Target IP address: 172.16.59.50 (172.16.59.50)

60

172.16.59.1 is at 00:00:0c:07:ac:3b

9.3
tcpdump udp port 520
listens and captures packets from the port 520 of a connected machine in the network.
tcpdump -x -s 120 ip proto 89
It captures 120 bytes at protocol 89.
tcpdump -x -s 70 host ip addr1 and (ip addr2 or ip addr3)
This command captures packets between (IPAdd #1 and IPAdd#2) and between (IPAdd #1 and IPAdd#3)
-x When parsing and printing, in addition to printing the headers of each packet, print the data of each packet (minus its link level header) in hex. The
smaller of the entire packet or snaplen bytes will be printed. Note that this is the entire link-layer packet, so for link layers that pad (e.g. Ethernet), the padding
bytes will also be printed
when the higher layer packet is shorter than the required padding.
-s Snarf snaplen bytes of data from each packet rather than the default of 65535 bytes. Packets truncated because of a limited snapshot are indicated in
the output with ``[|proto]'', where proto is the name of the protocol level at which the truncation has occurred. Note that taking larger snapshots both increases
the amount of time it takes to process packets and, effectively, decreases the amount of packet buffering. This may cause packets to be lost. You should
limit snaplen to the smallest number that will capture the protocol information you're interested in. Setting snaplen to 0 sets it to the default of 65535, for
backwards compatibility with recent older versions of tcpdump.
tcpdump -x -s 70 host ip addr1 and not ip addr2
This command captures all packets apart from those between (IPAdd #1 and IPAdd#2).
9.4
What are the port numbers used by the remote and the local computer?
Which machines port number matches the port number listed for telnet in
the /etc/services file?
Port used by remote computer is PORT 22
Port used by local computer is PORT 34834
The telnet server is not online. Hence any telnet command doesn't work.

LAB 10
10.1
First Send a UDP socket from one host and run tcpdump on another host
The TCP dump captures if the packet is fragmented or not and this same is displayed in the screeenshot below.
The grey area describes the the packets being discussed (Both way UDP communication).
10.2
1.
2.
3.
4.
5.

IP Address of DHCP Server is 10.129.1.53 and the port number is 67.


No relay because the Relay Address is 0.0.0.0.
The DHCP Server replied to 10.129.26.130 because in BootStrap Protocol, required IP is 10.129.26.130.
Offered IP is 10.129.26.130 and Offered Time is 10 minutes.
The additional information that the client has recived is subnet mask, router, domain name and domain name server.

10.3
The type 0, code 0 ICMP packet is of a ECHO Reply
The type 8, code 0 ICMP packet is of a ECHO Request
These are generated when a remote host is pinged using the ping command.

Page 22
The type 3,code 3 ICMP packet is of Port Unreachable and this is because i tried to connect to a particular remote host on ssh port but the same port is off in
the remote host.
10.4
1. sudo tracepath www.manipal.edu
1: networklab-HP-dx2480-MT-KL969AV-10.local
1: 172.16.59.3
8.806ms
1: 172.16.59.3
0.977ms
2: no reply
3: no reply
.
.
.
30: no reply
31: no reply
Too many hops: pmtu 1500
Resume: pmtu 1500

0.100ms pmtu 1500

sudo tracepath -b www.yahoo.com


[sudo] password for 120905642:
1: networklab-HP-dx2480-MT-KL969AV-10.local (172.16.59.57) 0.075ms pmtu 1500
1: 172.16.59.3 (172.16.59.3)
1.570ms
1: 172.16.59.3 (172.16.59.3)
1.112ms
2: no reply
3: no reply
.
.
.
31: no reply
Too many hops: pmtu 1500
Resume: pmtu 1500
2. Type 11: code:0 The ICMP is of Time-to-Live type
3. Similar to traceroute
4. 172.16.59.3

LAB 11
11.1
1. ns-tcp.tcl
a. TCP packet size = 1040 bytes
ACK packet size = 40 bytes
b. FTP feeds the TCP flow.
c. The queue size is 10.
d. For link 0 1 rate is 1 Mbps and propagation delay is 50 ms.
For link 1 2 rate is 100 Kbps and porpagation delay is 5 ms.
e. 16 TCP packets are dr opped and 0 ACK packets are dropped.
2. ns-simple.tcl
a. There are two types of flows, one for UDP and one for TCP
b. FTP feeds the TCP flow and CBR feeds the UDP flow.
c. Both Flows are injecting packets at 2 Mbps
d. FTP ends at time = 4s and CBR ends at time=4.5s

Вам также может понравиться