Computer-Science


Review on socket programming

1. Communication using a file

p1:

x=open("/aa/bb", O_WRONLY, 00777);  // open a file
write(x, "korea", 5);                    // write some data
close(x);                              // close it

p2:

x=open("/aa/bb", O_RDONLY, 00777);  // open the same file
y=read(x, buf, 50);                      // read max 50 bytes from file x into buf
buf[y]=0;                              // make it a string
printf("the data was %s\n", buf);                    // check the contents
close(x);

2. Communication using a socket

p1(client):

x=socket(PF_INET, SOCK_STREAM, 0);
connect(x, &serv_addr, sizeof(serv_addr));      // open a socket
write(x, "hi", 2);                            // write some data
.........

p2(server):

x1=socket(PF_INET, SOCK_STREAM, 0);
bind(x1, &serv_addr, sizeof(serv_addr));
listen(x1, 5);
x2=accept(x1, &cli_addr, &xx);                 // open a socket
y= read(x2, buf, 2);                             // read
..........

3. Example

client:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERV_TCP_PORT 9924
#define SERV_ADDR "165.246.38.157"

main(){
   int x, y;
   struct sockaddr_in  serv_addr;
   char buf[100];

   printf("Hi, I am the client\n");

   bzero((char *) &serv_addr, sizeof(serv_addr));
   serv_addr.sin_family = PF_INET;
   serv_addr.sin_addr.s_addr = inet_addr(SERV_ADDR);
   serv_addr.sin_port = htons(SERV_TCP_PORT);

   /* open a tcp socket*/
   if ( (x =socket(PF_INET, SOCK_STREAM,0)) < 0){
      perror("socket creation error\n");
      exit(1);
   }
   printf(" socket opened successfully. socket num is %d\n", x);

   /* connect to  the server */
   if (connect(x, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0){
      perror("can't connect to the server\n");
      exit(1);
   }

    /* send msg to the server */
    printf("now i am connected to the erver. enter a string to send\n");
    scanf("%s", buf);
    write(x,buf,strlen(buf));

    // read from the server
    printf("now let's read from the server\n");
    y=read(x,buf,50);
    buf[y]=0;
    printf("what echoed from the server is %s\n",buf);
    close(x);   // disconnect the connection
}

server:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERV_TCP_PORT 9924
#define SERV_ADDR "165.246.38.157"

main(){
        int s1,s2,x, y;
        struct sockaddr_in serv_addr, cli_addr;
        char buf[100];
        size_t xx;

        printf("Hi, I am the server\n");

        bzero((char *)&serv_addr, sizeof(serv_addr));
        serv_addr.sin_family=PF_INET;
        serv_addr.sin_addr.s_addr=inet_addr(SERV_ADDR);
        serv_addr.sin_port=htons(SERV_TCP_PORT);

        // open a tcp socket
        if((s1=socket(PF_INET, SOCK_STREAM, 0))<0){
                perror("socket creation error\n");
                exit(1);
        }
        printf("socket created successfully. socket num is %d\n", s1);

        // bind ip
        x=bind(s1, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
        if (x < 0){
           perror("binding failed\n");
           exit(1);
        }
        printf("binding passed\n");

        listen(s1, 5);
        xx = sizeof(cli_addr);
        s2 = accept(s1,(struct sockaddr *)&cli_addr,&xx);
        printf("we passed accept. new socket num is %d\n", s2);

        // read msg from client
        printf(โ€œnow reading from client\nโ€);
        y=read(s2,buf,50);
        buf[y]=0;
        printf("we got %s from cli\n",buf);

        // send  msg to the client
        printf("what do you want to send  to cli? enter your string\n");
        scanf("%s", buf);
        write(s2,buf,strlen(buf));
        close(s2);  // disconnect the connection
        close(s1);  // close the original socket
}

4. protocol

1) well-known port

Internet service programs are waiting on well-known ports for service. The client can talk to these service programs by opening a socket on a well-known port and following the corresponding protocol. 21: ftp, 23: telnet, 25: smtp, 80: http, โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ

2) SMTP (Simple Mail Transfer Protocol) (rfc 2821) : for sending mail (port 25)

kchang(mail.inha.ac.kr) ==> kchang(mail.inha.ac.kr)
S: EHLO 165.246.38.219
R: 250-portal.inha.ac.kr Hello
   250-TURN
   .............
   250 OK
S: MAIL FROM:<kchang@inha.ac.kr>
R: 250 ............... OK
S: RCPT TO:<kchang@mail.inha.ac.kr>
R: 250 ................
S: DATA
R: 354 Start mail input; end with <CRLF>.<CRLF>
S: Blah blah blah...
S: <CRLF>.<CRLF>
R: 250 ..........
S: QUIT
R: Connection closed

3) POP3 (rfc 1939) : for retrieving mail (port 110)

S: USER ****
R: +OK
S: PASS ****
R: +OK
S: stat
R: +OK 1 539
S: list
R: +OK 1 539
S: retr 1
R: +OK
   mail here .........
S: quit

4) http client/server

http client(web browser):

      ..........
      #define SERV_TCP_PORT 80
      ..........
      connect(s, ........);
      write(s, "GET / HTTP/1.0\r\n\r\n", 18);  // 1st msg in http protocol
      read(s, buf, ..);
      .............

http server(web server):

     ...............
     s1=socket(...);
     bind(s1,...);  // bind on port 80
     listen(s1,...);
     for(;;){
       s2=accept(s1, .....);
       x=fork();
       if (x==0){
            close(s1);
            read(s2, in_buf, n); // read http request
            build_out_buf(in_buf, out_buf);
                                //find the request html file and return with
                                // a proper header.
            write(s2, out_buf, ....);
            close(s2);
            exit(0);
        } else cose(s2); /
     }

5. Exercise

1) Copy serv.c.

serv.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

#define SERV_TCP_PORT 13131
#define SERV_ADDR "165.246.38.151"

void main(){
   int s1,s2, x, y;
   struct sockaddr_in serv_addr, cli_addr;
   char buf[50];
   socklen_t  xx;

   printf("Hi, I am the server\n");

   bzero((char *)&serv_addr, sizeof(serv_addr));
   serv_addr.sin_family=PF_INET;
   serv_addr.sin_addr.s_addr=inet_addr(SERV_ADDR);
   serv_addr.sin_port=htons(SERV_TCP_PORT);

   //open a tcp socket
   if ((s1=socket(PF_INET, SOCK_STREAM, 0))<0){
      printf("socket creation error\n");
      exit(1);
   }
   printf("socket opened successfully. socket num is %d\n", s1);

   // bind ip
   x =bind(s1, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
   if (x < 0){
      printf("binding failed\n");
      exit(1);
   }
   printf("binding passed\n");
   listen(s1, 5);
   xx = sizeof(cli_addr);
   s2 = accept(s1, (struct sockaddr *)&cli_addr, &xx);
   printf("we passed accept. new socket num is %d\n", s2);

   // read msg from client
   printf("now reading from client\n");
   y=read(s2, buf, 50);
   buf[y]=0;
   printf("we got %s from cli\n", buf);
   // send msg to the client
   printf("enter a string to send to client\n");
   scanf("%s", buf);
   write(s2, buf, strlen(buf));
   close(s2);   // disconnect the connection
   close(s1);   // close the original socket
}

2) Copy cli.c

cli.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

#define SERV_TCP_PORT 9948
#define SERV_ADDR "165.246.38.151"

void main(){
   int x,y;
   struct sockaddr_in serv_addr;
   char buf[50];
   printf("Hi, I am the client\n");

   bzero((char *)&serv_addr, sizeof(serv_addr));
   serv_addr.sin_family=PF_INET;
   serv_addr.sin_addr.s_addr=inet_addr(SERV_ADDR);
   serv_addr.sin_port=htons(SERV_TCP_PORT);

   //open a tcp socket
   if ((x=socket(PF_INET, SOCK_STREAM, 0))<0){
      printf("socket creation error\n");
      exit(1);
   }
   printf("socket opened successfully. socket num is %d\n", x);

   //connect to the server
   if (connect(x, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
      printf("can't connect to the server\n");
      exit(1);
   }
   // send msg to the server
   printf("now i am connected to the server. enter a string to send\n");
   scanf("%s",buf);
   write(x, buf, strlen(buf));
   // read from server
   printf("now reading from server\n");
   y=read(x, buf, 50);
   buf[y]=0;
   printf("from server: %s\n", buf);
   close(x);  // disconect the communication
}

3) Adjust port number both server and client. Recompile both and run the server first and run the client next. The client should talk first and then the server.

serv.c์™€ cli.c์˜ port number๋ฅผ ์ž„์˜๋กœ 12520์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์ปดํŒŒ์ผํ•˜์—ฌ server์™€ client๋ฅผ ๊ตฌ๋™์‹œ์ผฐ๋‹ค.

์™ผ์ชฝ ํ„ฐ๋ฏธ๋„์ด server์ด๊ณ , ์˜ค๋ฅธ์ชฝ ํ„ฐ๋ฏธ๋„์ด client์ด๋‹ค.

client๊ฐ€ server์—๊ฒŒ โ€œHI!โ€๋ผ๊ณ  ๋ณด๋ƒˆ๊ณ , server๋Š” client์—๊ฒŒ โ€œBYEโ€๋ผ๊ณ  ํ•˜์˜€๋‹ค.


Packet Sniffing

1. Tool

tcpdump for Unix/Linux/macOS. windump for Windows

2. windump

3. usage (use tcpdump for macOS)

windump -AdDeflLnNOpqRStuUvxX -c count -C file_size -F file -i interface
         -s snaplen -T type -w file -W filecount expression

-A  print each packet in ascii
-c  exit after receiving count packets
-D  print available network interfaces
-e  print the link-level header on each dump line
-i  Listen on "interface"
-n  Don not t convert addresses to names
-q  short output format
-S  print absolute, rather than relative, TCP sequence numbers
-s  snarf "snaplen" bytes from each packet rather than the default of 68
-t  Do not print a timestamp on each dump line
-w  write the raw packets to "file" rather than parsing and printing them out. standard output is used if the file is "-"
-x  print in hex
-xx  same as `-x` but print link level header, too.
-X  print in hex and ascii
-XX  same as `-X` but print link level header, too

examples)

windump host sundown : print all in/out packets for sundown windump host ace and not helios : print all packets between ace and any host except helios windump -D : shows all available network interface windump -eSXX -i 2 -s 80 port 9924 : show all in/out packets in interface 2 for port 9924 in hex including the link header. Capture max 80 bytes. (in macOS: sudo tcpdump -eSXX -i 2 -s 80 port 9924)

4. Exercise:

Follow the steps below to display the contents of the packets between the server and the client. Submit explanation of each field in those packets.

1) Make a client in your PC as follows.

(For macOS, use sftp to download cli.c from lab server and use it to connect to the server.)

2) Run tcpdump to monitor packets for specified port.

(1) Check available network interfaces

$ tcpdump -D

1, 2, 3, 4, โ€ฆ ๋“ฑ์ด Running ์œผ๋กœ available ํ•œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

(2) Monitor packets at device 2 whose src or dest ports are 12520

sudo tcpdump -eSXX -i 1 -s 80 port 12520

-i ์˜ต์…˜์€ ์–ด๋Š ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฒฝ์œ ํ•˜๋Š” ํŒจํ‚ท๋“ค์„ ์žก์„์ง€ ์ง€์ •ํ•œ๋‹ค. ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ์‹œ์Šคํ…œ์˜ ์ธํ„ฐํŽ˜์ด์Šค ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ ธ์„œ ๊ฐ€์žฅ ๋‚ฎ์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ ํƒํ•œ๋‹ค(์ด ๋•Œ loopback์€ ์ œ์™ธ๋œ๋‹ค).
์ถœ์ฒ˜: https://sugerent.tistory.com/348 [MISTERY:ํ‹ฐ์Šคํ† ๋ฆฌ]

3) Run your server again. Run the client in your PC.

tcpdump ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ ํ„ฐ๋ฏธ๋„ ์ฐฝ(์Šคํฌ๋ฆฐ์ƒท์˜ ์œ„์ชฝ)์„ ์œ ์ง€ํ•œ ํ›„, โ€˜servโ€™(์Šคํฌ๋ฆฐ์ƒท์˜ ์ขŒ์ธก ํ•˜๋‹จ)์™€ โ€˜cliโ€™(์Šคํฌ๋ฆฐ์ƒท์˜ ์šฐ์ธก ํ•˜๋‹จ)์˜ 3 hand shake ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ์ฃผ์—ˆ๋‹ค.

(1) โ€˜servโ€™์™€ โ€˜cliโ€™๊ฐ€ ์—ฐ๊ฒฐ๋œ ์งํ›„

$ sudo tcpdump -eSXX -i 1 -s 80 port 12520
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on en0, link-type EN10MB (Ethernet), capture size 80 bytes
10:56:55.595785 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 78: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [S], seq 2208409688, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 2344020955 ecr 0,sackOK,eol], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0040 0000 4000 4006 e9e5 a5f6 de4e a5f6  .@..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a858 0000 0000 b002  &...0....X......
	0x0030:  ffff 381f 0000 0204 05b4 0103 0306 0101  ..8.............
	0x0040:  080a 8bb6 ebdb 0000 0000 0402 0000       ..............
10:56:55.605611 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 74: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [S.], seq 2188865968, ack 2208409689, win 14480, options [mss 1460,sackOK,TS val 2828227965 ecr 2344020955,nop,wscale 7], length 0
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  003c 0000 4000 3f06 eae9 a5f6 2697 a5f6  .<..@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b0 83a1 a859 a012  .N0....wq....Y..
	0x0030:  3890 1e49 0000 0204 05b4 0402 080a a893  8..I............
	0x0040:  557d 8bb6 ebdb 0103 0307                 U}........
10:56:55.605678 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [.], ack 2188865969, win 2058, options [nop,nop,TS val 2344020965 ecr 2828227965], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0034 0000 4000 4006 e9f1 a5f6 de4e a5f6  .4..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a859 8277 71b1 8010  &...0....Y.wq...
	0x0030:  080a 7d91 0000 0101 080a 8bb6 ebe5 a893  ..}.............
	0x0040:  557d                                     U}

(2) โ€˜cliโ€™๊ฐ€ โ€˜servโ€™์—๊ฒŒ โ€œHIโ€๋ผ๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ธ ์งํ›„

10:57:46.037858 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 68: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [P.], seq 2208409689:2208409691, ack 2188865969, win 2058, options [nop,nop,TS val 2344071396 ecr 2828227965], length 2
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0036 0000 4000 4006 e9ef a5f6 de4e a5f6  .6..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a859 8277 71b1 8018  &...0....Y.wq...
	0x0030:  080a 703e 0000 0101 080a 8bb7 b0e4 a893  ..p>............
	0x0040:  557d 4849                                U}HI
10:57:46.043774 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [.], ack 2208409691, win 114, options [nop,nop,TS val 2828278407 ecr 2344071396], length 0
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  0034 2438 4000 3f06 c6b9 a5f6 2697 a5f6  .4$8@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b1 83a1 a85b 8010  .N0....wq....[..
	0x0030:  0072 fb1c 0000 0101 080a a894 1a87 8bb7  .r..............
	0x0040:  b0e4                                     ..

(3) โ€˜servโ€™๊ฐ€ โ€˜cliโ€™์—๊ฒŒ โ€œBYEโ€๋ผ๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด๋‚ธ ์งํ›„

10:58:16.292552 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 69: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [P.], seq 2188865969:2188865972, ack 2208409691, win 114, options [nop,nop,TS val 2828308656 ecr 2344071396], length 3
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  0037 2439 4000 3f06 c6b5 a5f6 2697 a5f6  .7$9@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b1 83a1 a85b 8018  .N0....wq....[..
	0x0030:  0072 fd8e 0000 0101 080a a894 90b0 8bb7  .r..............
	0x0040:  b0e4 4259 45                             ..BYE
10:58:16.292559 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [F.], seq 2188865972, ack 2208409691, win 114, options [nop,nop,TS val 2828308656 ecr 2344071396], length 0
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  0034 243a 4000 3f06 c6b7 a5f6 2697 a5f6  .4$:@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b4 83a1 a85b 8011  .N0....wq....[..
	0x0030:  0072 84ef 0000 0101 080a a894 90b0 8bb7  .r..............
	0x0040:  b0e4                                     ..
10:58:16.292639 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [.], ack 2188865972, win 2058, options [nop,nop,TS val 2344101650 ecr 2828308656], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0034 0000 4000 4006 e9f1 a5f6 de4e a5f6  .4..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a85b 8277 71b4 8010  &...0....[.wq...
	0x0030:  080a 072a 0000 0101 080a 8bb8 2712 a894  ...*........'...
	0x0040:  90b0                                     ..
10:58:16.292695 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [.], ack 2188865973, win 2058, options [nop,nop,TS val 2344101650 ecr 2828308656], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0034 0000 4000 4006 e9f1 a5f6 de4e a5f6  .4..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a85b 8277 71b5 8010  &...0....[.wq...
	0x0030:  080a 0729 0000 0101 080a 8bb8 2712 a894  ...)........'...
	0x0040:  90b0                                     ..
10:58:16.292827 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [F.], seq 2208409691, ack 2188865973, win 2058, options [nop,nop,TS val 2344101650 ecr 2828308656], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0034 0000 4000 4006 e9f1 a5f6 de4e a5f6  .4..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a85b 8277 71b5 8011  &...0....[.wq...
	0x0030:  080a 0728 0000 0101 080a 8bb8 2712 a894  ...(........'...
	0x0040:  90b0                                     ..
10:58:16.296351 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [.], ack 2208409692, win 114, options [nop,nop,TS val 2828308662 ecr 2344101650], length 0
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  0034 0000 4000 3f06 eaf1 a5f6 2697 a5f6  .4..@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b5 83a1 a85c 8010  .N0....wq....\..
	0x0030:  0072 0eba 0000 0101 080a a894 90b6 8bb8  .r..............
	0x0040:  2712                                     '.

4) Find the first packet which is a SYN packet sent by the client to the server in the windump window. Extract all packet header information. Refer TCP packet structure in Section 6 below.

์ฒซ๋ฒˆ์งธ packet์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

10:56:55.595785 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 78: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [S], seq 2208409688, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 2344020955 ecr 0,sackOK,eol], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0040 0000 4000 4006 e9e5 a5f6 de4e a5f6  .@..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a858 0000 0000 b002  &...0....X......
	0x0030:  ffff 381f 0000 0204 05b4 0103 0306 0101  ..8.............
	0x0040:  080a 8bb6 ebdb 0000 0000 0402 0000       ..............

5) Analyze rest of the packets similarly.

๋‘๋ฒˆ์งธ packet์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

10:56:55.605611 78:19:f7:06:40:01 (oui Unknown) > 08:f8:bc:6a:a8:db (oui Unknown), ethertype IPv4 (0x0800), length 74: 165.246.38.151.12520 > 165.246.222.78.55701: Flags [S.], seq 2188865968, ack 2208409689, win 14480, options [mss 1460,sackOK,TS val 2828227965 ecr 2344020955,nop,wscale 7], length 0
	0x0000:  08f8 bc6a a8db 7819 f706 4001 0800 4500  ...j..x...@...E.
	0x0010:  003c 0000 4000 3f06 eae9 a5f6 2697 a5f6  .<..@.?.....&...
	0x0020:  de4e 30e8 d995 8277 71b0 83a1 a859 a012  .N0....wq....Y..
	0x0030:  3890 1e49 0000 0204 05b4 0402 080a a893  8..I............
	0x0040:  557d 8bb6 ebdb 0103 0307                 U}........

์„ธ๋ฒˆ์งธ packet์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

10:56:55.605678 08:f8:bc:6a:a8:db (oui Unknown) > 78:19:f7:06:40:01 (oui Unknown), ethertype IPv4 (0x0800), length 66: 165.246.222.78.55701 > 165.246.38.151.12520: Flags [.], ack 2188865969, win 2058, options [nop,nop,TS val 2344020965 ecr 2828227965], length 0
	0x0000:  7819 f706 4001 08f8 bc6a a8db 0800 4500  x...@....j....E.
	0x0010:  0034 0000 4000 4006 e9f1 a5f6 de4e a5f6  .4..@.@......N..
	0x0020:  2697 d995 30e8 83a1 a859 8277 71b1 8010  &...0....Y.wq...
	0x0030:  080a 7d91 0000 0101 080a 8bb6 ebe5 a893  ..}.............
	0x0040:  557d                                     U}

6) Connect to www.inha.ac.kr with your web browser and analyze SYN, S/ACK, ACK packets between the web browser and www.inha.ac.kr. You may need -c num option to capture the first num packets as below.

$ sudo tcpdump -eSXX -c 20 -i 1 -s 80 host www.inha.ac.kr

๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅ ํ›„, ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ www.inha.ac.kr์— ์ ‘์†ํ•œ ํ›„์˜ ๊ฒฐ๊ณผ ํ™”๋ฉด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.




7) Click login menu(๋กœ๊ทธ์ธ) and enter id and password. Find the packet that contains your login ID and password.
To capture login ID and password, make the capture size larger, e.g. 3000.
Use -w option to save the result in a file (e.g. pktout) and use -r option to read packets from a file.

$ sudo tcpdump -eSXX -w pktout -i 1 -s 3000 host www.inha.ac.kr
$ sudo tcpdump -eSXX -r pktout > x
$ vi x

ID๋กœ๋Š” โ€˜12181879โ€™๋ฅผ, PASSWORD๋กœ๋Š” โ€˜QWERTY123!@#โ€™์„ ์ž…๋ ฅํ•˜์˜€๋‹ค.

x์˜ ์ผ๋ถ€๋ถ„๋งŒ์„ ๋ณด์ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

8) Connect to portal.inha.ac.kr and do the same thing as in Problem 6) and 7).

5. wincli.cpp

#include "winsock2.h"
#include "ws2tcpip.h"
#include "stdio.h"

#define SERVER_PORT 9924  // server port number
#define BUF_SIZE 4096 // block transfer size
#define QUEUE_SIZE 10
#define IPAddress "165.246.38.152" // server IP address

int main()
{
	WORD		        wVersionRequested;
	WSADATA		wsaData;
	SOCKADDR_IN          target; //Socket address information
	SOCKET		        s;
	int			err;
	int			bytesSent;
	char		        buf[100];

	   //--- INITIALIZATION -----------------------------------
	   wVersionRequested = MAKEWORD( 1, 1 );
	   err = WSAStartup( wVersionRequested, &wsaData );

	   if ( err != 0 ) {
		printf("WSAStartup error %ld", WSAGetLastError() );
		WSACleanup();
		return false;
	   }
	   //------------------------------------------------------

	   //---- Build address structure to bind to socket.--------
	   target.sin_family = AF_INET; // address family Internet
	   target.sin_port = htons (SERVER_PORT); //Port to connect on
	   inet_pton(AF_INET, IPAddress, &(target.sin_addr.s_addr)); // target IP
           //--------------------------------------------------------


	   // ---- create SOCKET--------------------------------------
	   s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
	   if (s == INVALID_SOCKET)
	   {
		printf("socket error %ld" , WSAGetLastError() );
		WSACleanup();
		return false; //Couldn't create the socket
	   }
	   //---------------------------------------------------------


	   //---- try CONNECT -----------------------------------------
	   if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
	   {
		printf("connect error %ld", WSAGetLastError() );
		WSACleanup();
		return false; //Couldn't connect
	   }
	   //-------------------------------------------------------

	   //---- SEND bytes -------------------------------------------
	   printf("enter a string to send to server\n");
	   gets_s(buf, 99);
	   bytesSent = send( s, buf, strlen(buf), 0 ); // use "send" in windows
	   printf( "Bytes Sent: %ld \n", bytesSent );

           // now receive
	   int n;
	   n=recv(s, buf, 50, 0); // read max 50 bytes
	   buf[n]=0; // make a string
	   printf("received: %s\n", buf);

	   //--------------------------------------------------------
	   closesocket( s );
	   WSACleanup();

	   return 0;
}

6. TCP packet structure

7. TCP protocol

server: waits in accept()
client: calls connect()

client -> server: SYN packet
server-> client: SYN/ACK packet
client-> server:ACK packet

Now connection is established, and the client and server can send/receive data.