Download presentation
Presentation is loading. Please wait.
Published byFerdinand Dennis Modified over 8 years ago
1
Socket Program Training 10/27/2010
2
What is a Socket ? An interface between an application process and transport layer (TCP or UDP). 2
3
3 TCP Client Socket ( ) Connect ( ) send ( ) Close ( ) send ( ) Read ( ) Accept ( ) recv ( ) Listen ( ) Bind ( ) Socket ( ) recv ( ) Close ( ) Waiting for the requests from client Build a connection Data (request) Data (reply) Deal with the request TCP Server Notify the end of the file
4
4 FTP Server source code #include …… #define MAXDATA 1024 int main(void){ int sockfd; // sockfd is the socket file descriptor returned by socket() int recfd; int length; int filename, ndata, fdesc, fk; char buf[BUFSIZ]; char data[MAXDATA]; struct sockaddr_in myaddr; struct sockaddr_in client_addr;
5
5 FTP Server source code //Get a socket into TCP/IP if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){ // int socket (int family, int type, int protocol) perror("socket error"); return 0;} // AF_INET IPv4, AF_INET6 IPv6 //SOCK_STREAM TCP, SOCK_DGRAM UDP //Set up address memset((char*)&myaddr,0,sizeof(myaddr)); myaddr.sin_family=AF_INET; myaddr.sin_addr.s_addr=htonl(INADDR_ANY); myaddr.sin_port=htons(1234);
6
6 FTP Server source code if(bind(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr))<0){ perror("bind failed"); return 0;} if(listen(sockfd,1)<0){ // int listen (int sockfd, int backlog) ; perror("listen error"); return 0;} //Loop continuously, waiting for connection requests and performing the service length=sizeof(client_addr); printf("Server start !\n");
7
7 FTP Server source code while(1){ if((recfd=accept(sockfd,(struct sockaddr*)&client_addr,&length))<0){ perror("accept error"); return 0;} if((fk=fork())<0){ perror("fork error"); exit(1);} if(fk==0){ if((filename=read(recfd,buf,BUFSIZ))<0){ perror("server read error"); return 0;}
8
8 FTP Server source code printf("Create socket #%d from %s : %d\n",recfd,inet_ntoa(client_addr.sin_addr),htons(client_addr.sin_p ort)); printf("receive: %s\n",&buf); //Reserve three parts for you if((fdesc……)<0){ perror("open file error"); exit(1); } for(;;){ ndata……; if(ndata==0){ printf("file %s transmit complete ! \n",&buf); break; }else{
9
9 FTP Server source code if((write……)<0){ perror("server write error"); return 0;} memset(buf,0,sizeof(buf)); close(recfd);} close(sockfd); return 0;}
10
10 FTP Client source code int main (int argc, char *argv[]) //ex: file option1 option2 option3 //argc = 4, argv[0]="file“,argv[1]="option1“, argv[2]="option2“,argv[3]="option3" { …… int ndata, fdesc, k, fk, index; int nbytes, count; …… //Check for proper usage if(argc < 4){ printf("use : ftpc IP file_count file1 file2...\n"); return 0;}
11
11 FTP Client source code count=atoi(argv[2]); printf("%d files request!\n",count); index=3; for(k=0;k<count;k++){ if((fk=fork())<0){ perror("fork error!");} if(fk==0){ if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){ printf("socket error\n"); return 0;}
12
FTP Client source code memset((char*)&myaddr,0,sizeof(myaddr)); myaddr.sin_family=AF_INET; myaddr.sin_addr.s_addr=htonl(INADDR_ANY); myaddr.sin_port=htons(0); if(bind(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr))<0){ printf("bind error\n"); return 0;} memset((char*)&servaddr,0,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(1234); servaddr.sin_addr.s_addr=inet_addr(argv[1]); 12
13
13 FTP Client source code if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0){ printf("connect failed!"); return 0;} //Reserve four parts for you if(write……)<0){ // use argv function printf("write error!\n"); exit(1);} fdesc…… index++; if(fdesc<0){ perror("open error!"); exit(1);}
14
14 FTP Client source code for(;;){ if((nbytes=read(……))<0){ perror("first read"); exit(1);} if(nbytes==0){ printf("file %s transmit complete ! \n",argv[ ]); close(fdesc); close(sockfd); exit(1); break;} else{ if(write(……)<0){ perror("write error!"); exit(1); ……
15
15 Compile gcc -o filename filename.c –# gcc -o server server.c –# gcc -o client client.c Execute the filename –#./filename –#./filename server_IP number_of_file file1 file2 file3
16
Tcpdump and Tcpstat
17
17 Tcpdump and Tcpstat Listen the interfaces (network cards) of the router and write into a dump file. –Tcpdump options –-i, -w Listen the interfaces of the server and client.
18
18 Tcpdump and Tcpstat Gather statistics from the dump files. –Tcpstat options –-r, -f ‘ ’, -o “ ” –Ex: -f ‘port …’, -o “……” Filter out the three different files. –Tcpdump primitives –port, src net,
19
19 Tcpdump and Tcpstat Output formatting –%a: the average packet size –%b: the number of bits per second –% I : the number of IPv4 packets –%n the number of packets –%p: the number of packets per second
20
Backdoor program 20
21
Defined as a function in: net/ipv4/ip_input.c, line 379 net/ipv4/ip_input.c, line 379 Defined as a function prototype in: include/net/ip.h, line 93 include/net/ip.h, line 93 Referenced (in 3 files total) in: include/net/ip.h, line 93 include/net/ip.h, line 93 net/ipv4/af_inet.c, line 1560 net/ipv4/ip_input.c, line 379 Defined as a function in: net/ipv4/ip_input.c, line 379 net/ipv4/ip_input.c, line 379 Defined as a function prototype in: include/net/ip.h, line 93 include/net/ip.h, line 93 Referenced (in 3 files total) in: include/net/ip.h, line 93 include/net/ip.h, line 93 net/ipv4/af_inet.c, line 1560 net/ipv4/ip_input.c, line 379
22
IP Layer int count(struct sk_buff* skb){ struct iphdr *iph; struct udphdr *udph; struct timeval tv; static int total_packet = 0; static int last_timestamp = 0; 22
23
IP Layer iph = skb->nh.iph; printk("=================IP=================\n"); //IP Header printk("Version = %d\n",iph-> version); printk("IHL = %d\n",iph-> ihl*4); printk("Type of Service = %d\n",iph-> tos); printk("Total Length = %d\n",ntohs(iph-> tot_len)); printk("Identification = %d\n",iph-> id); printk("Fragmentation Offset = %d\n",iph-> frag_off); printk("Time to live = %d\n",iph-> ttl); printk("Protocol = %d\n",iph-> protocol); 23
24
IP Layer printk("Header Checksum = 0x%x\n",iph-> check); printk("Source Address = %d.%d.%d.%d\n",*(skb->nh.raw+12),*(skb- >nh.raw+13),*(skb->nh.raw+14),*(skb->nh.raw+15)); printk("Distination Address = %d.%d.%d.%d\n",*(skb- >nh.raw+16),*(skb->nh.raw+17),*(skb->nh.raw+18),*(skb- >nh.raw+19)); 24
25
TCP Layer if(iph-> protocol == IPPROTO_UDP) {//UDP Header printk("================UDP=================\n"); udph = (struct udphdr *)(skb->nh.raw + iph->ihl*4); printk("Source Port = %d\n",ntohs(udph->source)); printk("Distination Address = %d\n",ntohs(udph-> dest)); printk("Segment Length = %d\n",ntohs(udph-> len)); printk("Checksum = 0x%x\n",udph-> check); 25
26
Q&A 26
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.