Np Report

download Np Report

of 22

description

solutions for the np lab

Transcript of Np Report

Page 1NP LAB REPORTLAB 1EXERCISE 1.1QUESTION 1 The default directory is 120905642@networklab-24:~$

QUESTION 2 my working directory is 120905642@networklab-24:~/120905642$

EXERCISE 1.2QUESTION 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 3The 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 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 #include 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); else break; } close(fd1); close(fd2); return 1; } OUTPUT- copies the file

EXERCISE 1.7demonstrating the use of pipes

#include #include#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 2FTP CLIENT#include #include #include #include #include #include #include #include #include #include #include #include

#define DEST_IP "127.0.0.1"#define DEST_PORT 6893

void usage(const char *progname) { fprintf(stderr, "Usage: %s \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 ----------//

// 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#include #include #include #include #include #include #include #include #include #include #include #include

#define PORT 6893

void usage(const char *progname) { fprintf(stderr, "Usage: %s \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) { 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 } 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 #include #include #include #include #include #include #include #include #include

#define MYPORT 7000 // the port users will be connecting toint 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 #include #include #include #include #include #include #include #include

#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 #include #include #include #include #include #include #include #include #include

#define MYPORT 7000 #define MAXBUFLEN 100 // 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));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 #include #include #include #include #include #include #include #include

#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); } 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 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 #include #include #include #include #include #include #include #include #include #include #include

#define PORT 6893

void usage(const char *progname) { fprintf(stderr, "Usage: %s \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) { 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 } 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-5TIME CLIENT#include #include #include #include #include #include #include #include #include #include

#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[]){ 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