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);
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
..........
#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
}
#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
}
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, โฆโฆโฆโฆโฆโฆโฆโฆ
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
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
..........
#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, ..);
.............
...............
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); /
}
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
}
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
}
serv.c
์ cli.c
์ port number๋ฅผ ์์๋ก 12520์ผ๋ก ์ค์ ํ๊ณ ์ปดํ์ผํ์ฌ server์ client๋ฅผ ๊ตฌ๋์์ผฐ๋ค.
์ผ์ชฝ ํฐ๋ฏธ๋์ด server์ด๊ณ , ์ค๋ฅธ์ชฝ ํฐ๋ฏธ๋์ด client์ด๋ค.
client๊ฐ server์๊ฒ โHI!โ๋ผ๊ณ ๋ณด๋๊ณ , server๋ client์๊ฒ โBYEโ๋ผ๊ณ ํ์๋ค.
tcpdump
for Unix/Linux/macOS.
windump
for Windows
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
qualifier = type | dir | proto |
type = host(default) | net | port | portrange |
dir = src | dst | src or dst (default) | src and dst |
proto = ether | ip | arp | rarp | wlan | tcp | udp |
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
)
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.
(For macOS, use sftp to download cli.c from
lab server and use it to connect to the server.)
tcpdump
to monitor packets for specified port.$ tcpdump -D
1, 2, 3, 4, โฆ ๋ฑ์ด Running ์ผ๋ก available ํ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
sudo tcpdump -eSXX -i 1 -s 80 port 12520
-i
์ต์
์ ์ด๋ ์ธํฐํ์ด์ค๋ฅผ ๊ฒฝ์ ํ๋ ํจํท๋ค์ ์ก์์ง ์ง์ ํ๋ค. ์ง์ ๋์ง ์์ผ๋ฉด ์์คํ
์ ์ธํฐํ์ด์ค ๋ฆฌ์คํธ๋ฅผ ๋ค์ ธ์ ๊ฐ์ฅ ๋ฎ์ ๋ฒํธ๋ฅผ ๊ฐ์ง ์ธํฐํ์ด์ค๋ฅผ ์ ํํ๋ค(์ด ๋ loopback์ ์ ์ธ๋๋ค).
์ถ์ฒ: https://sugerent.tistory.com/348 [MISTERY:ํฐ์คํ ๋ฆฌ]
tcpdump
๋ช
๋ น์ด๋ฅผ ์คํํ ํฐ๋ฏธ๋ ์ฐฝ(์คํฌ๋ฆฐ์ท์ ์์ชฝ)์ ์ ์งํ ํ, โservโ(์คํฌ๋ฆฐ์ท์ ์ข์ธก ํ๋จ)์ โcliโ(์คํฌ๋ฆฐ์ท์ ์ฐ์ธก ํ๋จ)์ 3 hand shake ๊ณผ์ ์ ์งํํด ์ฃผ์๋ค.
$ 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}
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 ..
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 '.
ping
๋ช
๋ น์ด ์คํ ๋์ค Ctrl+c
์
๋ ฅ์ฒซ๋ฒ์งธ 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 ..............
7819 f706 4001 08f8 bc6a a8db 0800
์ Ethernet header์ด๋ค.
7819 f706 4001
์ dest MAC addr๋ฅผ, 08f8 bc6a a8db
๋ src MAC addr๋ฅผ ์๋ฏธํ๋ค.0800
๋ IP packet์ type์ ์๋ฏธํ๋ค.4500 0040 0000 4000 4006 e9e5 a5f6 de4e a5f6 2697
์ IP header์ด๋ค.
4500
์์
4
๋ IP version์ ์๋ฏธํ๋ค.5
๋ IP Header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก IP header์ length๋ 5
*4 = 20bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.00
์ type of service๋ฅผ ์๋ฏธํ๋ค.0040
์ packet length๋ฅผ ์๋ฏธํ๋ค.e9e5
๋ ip header checksum์ ์๋ฏธํ๋ค.a5f6 de4e
๋ src ip๋ฅผ, a5f6 2697
๋ dest ip๋ฅผ ์๋ฏธํ๋ค.d995 30e8 83a1 a858 0000 0000 b002 ffff 381f 0000 0204 05b4 0103 0306 0101 080a 8bb6 ebdb 0000 0000 0402 0000
์ด TCP header ์ด๋ฉฐ, ํฌ๊ธฐ๋ 44bytes์ด๊ณ , ๋ด์ฉ์ ๋ฐ๋ผ ๊ธธ์ด๊ฐ ๊ฐ๋ณ์ ์ด๋ค. (20+@bytes)
d995
๋ src port๋ฅผ, 30e8
์ dst port๋ฅผ ์๋ฏธํ๋ค.83a1 a858
๋ seq number๋ฅผ ์๋ฏธํ๋ค.b002
์์
b
๋ TCP header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก TCP header์ length๋ b
*4 = 11*4 = 44bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.002
๋ฅผ 2์ง๋ฒ์ผ๋ก ๋ฐ๊พธ๋ฉด 000000 000010
์ด๋ค.
000000
์ reserved๋ฅผ,000010
์ ์๋ฆฟ์ ๋ณ๋ก ๊ฐ๊ฐ URG ACK PSH RST SYN FIN์ ์ฌ๋ถ๋ฅผ ๋ํ๋ธ๋ค.
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}........
08f8 bc6a a8db 7819 f706 4001 0800
์ Etherntet header์ด๋ค.
08f8 bc6a a8db
์ dest MAC addr๋ฅผ, 7819 f706 4001
๋ src MAC addr๋ฅผ ์๋ฏธํ๋ค.0800
์ IP packet์ type์ ์๋ฏธํ๋ค.4500 003c 0000 4000 3f06 eae9 a5f6 2697 a5f6 de4e
์ IP header์ด๋ค.
4500
์์
4
๋ IP version์ ์๋ฏธํ๋ค.5
๋ IP Header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก IP header์ length๋ 5
*4 = 20bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.00
์ type of service๋ฅผ ์๋ฏธํ๋ค.0040
์ packet length๋ฅผ ์๋ฏธํ๋ค.eae9
๋ ip header checksum์ ์๋ฏธํ๋ค.a5f6 2697
๋ src ip๋ฅผ, a5f6 de4e
๋ dest ip๋ฅผ ์๋ฏธํ๋ค.30e8 d995 8277 71b0 83a1 a859 a012 3890 1e49 0000 0204 05b4 0402 080a a893 557d
์ด TCP header ์ด๋ฉฐ, ํฌ๊ธฐ๋ 40bytes์ด๊ณ , ๋ด์ฉ์ ๋ฐ๋ผ ๊ธธ์ด๊ฐ ๊ฐ๋ณ์ ์ด๋ค. (20+@bytes)
30e8
๋ src port๋ฅผ, d995
์ dst port๋ฅผ ์๋ฏธํ๋ค.8277 71b0
๋ seq number๋ฅผ ์๋ฏธํ๋ค.83a1 a859 a012
์ค a012
์์
a
๋ TCP header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก TCP header์ length๋ a
*4 = 10*4 = 40bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.012
๋ฅผ 2์ง๋ฒ์ผ๋ก ๋ฐ๊พธ๋ฉด 000000 001100
์ด๋ค.
000000
์ reserved๋ฅผ,001100
์ ์๋ฆฟ์ ๋ณ๋ก ๊ฐ๊ฐ URG ACK PSH RST SYN FIN์ ์ฌ๋ถ๋ฅผ ๋ํ๋ธ๋ค.
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}
7819 f706 4001 08f8 bc6a a8db 0800
์ Etherntet header์ด๋ค.
7819 f706 4001
์ dest MAC addr๋ฅผ, 08f8 bc6a a8db
๋ src MAC addr๋ฅผ ์๋ฏธํ๋ค.0800
์ IP packet์ type์ ์๋ฏธํ๋ค.4500 0034 0000 4000 4006 e9f1 a5f6 de4e a5f6 2697
์ IP header์ด๋ค.
4500
์์
4
๋ IP version์ ์๋ฏธํ๋ค.5
๋ IP Header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก IP header์ length๋ 5
*4 = 20bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.00
์ type of service๋ฅผ ์๋ฏธํ๋ค.0040
์ packet length๋ฅผ ์๋ฏธํ๋ค.e9f1
๋ ip header checksum์ ์๋ฏธํ๋ค.a5f6 de4e
๋ src ip๋ฅผ, a5f6 2697
๋ dest ip๋ฅผ ์๋ฏธํ๋ค.d995 30e8 83a1 a859 8277 71b1 8010 080a 7d91 0000 0101 080a 8bb6 ebe5 a893 557d
์ด TCP header ์ด๋ฉฐ, ํฌ๊ธฐ๋ 32bytes์ด๊ณ , ๋ด์ฉ์ ๋ฐ๋ผ ๊ธธ์ด๊ฐ ๊ฐ๋ณ์ ์ด๋ค. (20+@bytes)
d995
๋ src port๋ฅผ, 30e8
์ dst port๋ฅผ ์๋ฏธํ๋ค.83a1 a859
๋ seq number๋ฅผ ์๋ฏธํ๋ค.8277 71b1 8010
์ค 8010
์์
8
๋ TCP header์ length๋ฅผ ํํํ ๊ฒ์ผ๋ก TCP header์ length๋ 8
*4 = 8*4 = 32bytes ๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.010
๋ฅผ 2์ง๋ฒ์ผ๋ก ๋ฐ๊พธ๋ฉด 000000 001010
์ด๋ค.
000000
์ reserved๋ฅผ,001010
์ ์๋ฆฟ์ ๋ณ๋ก ๊ฐ๊ฐ URG ACK PSH RST SYN FIN์ ์ฌ๋ถ๋ฅผ ๋ํ๋ธ๋ค.
-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์ ์ ์ํ ํ์ ๊ฒฐ๊ณผ ํ๋ฉด์ ์๋์ ๊ฐ๋ค.
-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
์ ์ผ๋ถ๋ถ๋ง์ ๋ณด์ด๋ฉด ์๋์ ๊ฐ๋ค.
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;
}
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.