302
리눅스 네트웍 프로그래밍 (Linux Network Programming) 경북대학교 전자전기컴퓨터학부

리눅스 네트웍 프로그래밍 (CPL-TR-09-03)

  • Upload
    ngocong

  • View
    235

  • Download
    13

Embed Size (px)

Citation preview

  • (Linux Network Programming)

  • i

    . ,

    .

    ,

    , .

    Unix Network Programming: The Sockets Networking API (R. Stevens )

    API .

    .

    .

    TCP/IP A

    .

    ,

    lnp.h B .

    http://protocol.knu.ac.kr/pub/lnp.h

    ,

    , , , , .

    2009 3

  • ii

    1. .................................................................................................................................... 1

    1.1 DAYTIME CLIENT ....................................................................................................... 2

    1.2 WRAPPING FUNCTIONS ............................................................................. 4

    1.3 DAYTIME SERVER ....................................................................................................... 5

    1.4 TCP/IP ............................................................................... 7

    2. : TCP, UDP, SCTP ..................................................................................... 9

    2.1 USER DATAGRAM PROTOCOL (UDP).................................................................................... 11

    2.2 TRANSMISSION CONTROL PROTOCOL (TCP) .......................................................................... 11

    2.3 STREAM CONTROL TRANSMISSION PROTOCOL (SCTP).............................................................. 13

    2.4 TCP ................................................................................................ 13

    2.5 TIME_WAIT .......................................................................................................... 18

    2.6 SCTP ASSOCIATION .................................................................................... 20

    2.7 .................................................................................................................... 24

    2.8 CONCURRENT ......................................................................................................... 25

    2.9 .......................................................................................................... 28

    3. SOCKETS ............................................................................................................ 33

    3.1 SOCKET ADDRESS ................................................................................................. 33

    3.2 VALUE-RESULT ARGUMENTS............................................................................................... 41

    3.3 BYTE ORDERING ...................................................................................................... 43

    3.4 BYTE MANIPULATION ................................................................................................ 47

    3.5 INET_ATON, INET_ADDR, INET_NTOA ............................................................................. 48

    3.6 INET_PTON, INET_NTOP ............................................................................................. 50

    4. TCP SOCKETS.................................................................................................................... 53

    4.1 SOCKET ................................................................................................................. 54

    4.2 CONNECT .............................................................................................................. 56

    4.3 BIND .................................................................................................................... 58

  • iii

    4.4 LISTEN .................................................................................................................. 61

    4.5 ACCEPT ................................................................................................................. 66

    4.6 FORK EXEC ....................................................................................................... 69

    4.7 CONCURRENT ......................................................................................................... 72

    4.8 CLOSE .................................................................................................................. 75

    4.9 GETSOCKNAME GETPEERNAME ............................................................................... 76

    5. TCP CLIENT/SERVER .................................................................................................. 77

    5.1 TCP ECHO SERVER .......................................................................................................... 78

    5.2 TCP ECHO CLIENT........................................................................................................... 80

    5.3 .................................................................................................. 82

    5.4 .......................................................................................................... 87

    5.5 ................................................................................................ 100

    5.6 SIGPIPE .......................................................................................................... 103

    5.7 ABNORMAL SERVER TERMINATION ..................................................................................... 105

    5.8 TCP ........................................................................................................... 107

    5.9 USER DATA FORMAT ...................................................................................................... 109

    6. I/O MULTIPLEXING: SELECT()............................................................................................ 113

    6.1 I/O .................................................................................................................... 114

    6.2 SELECT ................................................................................................................ 121

    6.3 STR_CLI : REVISITED ................................................................................................. 127

    6.4 SHUTDOWN ......................................................................................................... 129

    6.5 STR_CLI : REVISITED AGAIN........................................................................................ 131

    6.6 TCP ECHO : REVISITED ............................................................................................ 132

    6.7 PSELECT .............................................................................................................. 135

    7. SOCKET OPTIONS ........................................................................................................... 137

    7.1 GETSOCKOPT SETSOCKOPT .................................................................................. 137

    7.2 SOCKET OPTION ..................................................................................... 139

    7.3 GENERIC SOCKET OPTIONS .............................................................................................. 144

  • iv

    7.4 IPV4 SOCKET OPTIONS ................................................................................................... 157

    7.5 IPV6 SOCKET OPTIONS ................................................................................................... 159

    7.6 TCP SOCKET OPTIONS ................................................................................................... 162

    7.7 SCTP SOCKET OPTIONS.................................................................................................. 166

    7.8 FCNTL ................................................................................................................ 179

    8. UDP SOCKETS ................................................................................................................. 182

    8.1 RECVFROM SENDTO ........................................................................................... 183

    8.2 UDP ECHO ......................................................................................................... 184 8.3 UDP ECHO CLIENT ........................................................................................................ 187

    8.4 LOST DATAGRAM .......................................................................................................... 189

    8.5 SERVER NOT RUNNING ................................................................................................... 193

    8.6 UDP .......................................................................................................... 195

    8.7 UDP CONNECT ................................................................................... 197

    8.8 FLOW CONTROL UDP......................................................................................... 203

    8.9 UDP OUTGOING INTERFACE .......................................................................... 206

    9. SCTP SOCKETS ................................................................................................................ 207

    9.1 INTERFACE ........................................................................................................... 208

    9.2 SCTP_BINDX SCTP_BINDX .................................................................................... 213

    9.3 SCTP ................................................................................................... 216

    9.4 SCTP_SENDMSG ..................................................................................................... 218

    9.5 SCTP_RECVMSG ..................................................................................................... 219

    9.6 SCTP_OPT_INFO ..................................................................................................... 219

    9.7 SCTP_PEELOFF ....................................................................................................... 220

    9.8 SHUTDOWN ......................................................................................................... 221

    9.9 NOTIFICATIONS............................................................................................................. 222

    10. SCTP CLIENT/SERVER ............................................................................................ 228

    10.1 SCTP ONE-TO-MANY STYLE STREAMING ECHO SERVER ......................................................... 229

    10.2 SCTP ONE-TO-MANY STYLE STREAMING ECHO CLIENT .......................................................... 231

    10.3 SCTP STREAMING ECHO CLIENT ....................................................................................... 234

  • v

    10.4 HOL(HEAD-OF-LINE) BLOCKING ....................................................................................... 236

    10.5 STREAM ...................................................................................................... 244

    10.6 ....................................................................................................... 245

    A. TCP/IP ............................................................................................. 247

    A.1 ............................................................................................................... 248

    A.2 ............................................................................................ 251

    A.3 ............................................................................................................... 255

    A.4 .................................................................................................... 258

    A.5 ............................................................................................................... 262

    A.6 INTERNET PROTOCOL (IP).................................................................................................... 263

    A.7 USER DATAGRAM PROTOCOL (UDP)...................................................................................... 269

    A.8 TRANSMISSION CONTROL PROTOCOL (TCP) ............................................................................ 271

    A.9 STREAM CONTROL TRANSMISSION PROTOCOL (SCTP) ............................................................... 276

    A.10 ..................................................................................................... 278

    B. LNP.H .......................................................................................................... 282

  • 1

    1.

    Client-Server .

    (daemon) Client . Client

    . 1.1 Client

    .

    Client-Server

    Client ,

    (layer) . TCP/IP . , Client

    TCP . TCP IP(Internet Protocol) , IP

    . 1.2 TCP/IP .

  • 2

    1.1 DAYTIME CLIENT

    . Client TCP

    , .

    -------------------------------------------------------------------------- 1 #include lnp.h 2 int 3 main (int argc, char **argv) 4 { 5 int sockfd, n; 6 char revline[MAXLINE + 1]; 7 struct sockaddr_in servaddr; 8 if (argc !=2) 9 err_quit (usage: a.out ); 10 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))

  • 3

    Header

    lnp.h ,

    . header header

    .

    Command-line

    command-line main .

    TCP socket

    socket Internet(AF_INET), stream(SOCK_STREAM) socket TCP

    socket . descriptor , (connect,

    read) socket . if socket , sockfd

    , 0 . socket

    , socket() sockets API .

    socket err_sys . , error message

    . err_

    .

    Server IP port

    Internet socket address (serrvaddr) server IP port .

    , bzero() , address family AF_INET

    , port 13 (daytime server well-known port ) , IP command-

    line (argv[1]) . , IP port

    , htons("host to network short") inet_pton("presentation to numeric")

    .

    Server

    TCP , connect() socket address

    server TCP . Connect() socket address

    , sizeof operator .

  • 4

    Server

    Server fputs() . Server

    26-byte string .

    Mon May 26 20:58:40 2003

    TCP segment 26 byte data . data

    read() data . , TCP socket

    , read() loop read 0 ( )

    0 (error) loop .

    exit() . descriptor

    TCP socket .

    1.2 WRAPPING FUNCTIONS

    . socket(), inet_pton(), connect(), read(), fputs()

    , err_quit(), err_sys()

    .

    , wrapper .

    socket() (wrapping) Socket() .

    sockfd = Socket (AF_INET, SOCK_STREAM, 0);

    Socket() wrapper function .

    -------------------------------------------------------------------------- int Socket (int family, int type, int protocol) { int n; if ( ( n = socket(family, type, protocol)) < 0) err_sys(socket error); return (n); } --------------------------------------------------------------------------

  • 5

    wrapper .

    Unix/Linux API error , errno error type

    , -1 . err_sys() errno

    ( , errno ETIMEOUT Connection timed out"

    ). errno . .

    'E'

    .

    1.3 DAYTIME SERVER

    1.4 Daytime Client TCP Daytime Server .

    Server well-known port socket bind

    bind() socket address server well-known port(daytime service

    13) . Server host interface IP

    INADDR_ANY interface server client .

    Socket listening socket

    listen() socket listening socket . TCP server socket,

    bind, listen 3 listening descriptor( listenfd) . LISTENNQ

    lnp.h . client kernel listening descriptor

    queue .

    -------------------------------------------------------------------------- 1 #include lnp.h 2 #include 3 int 4 main (int argc, char **argv) 5 { 6 int listenfd, connfd; 7 struct sockaddr_in servaddr; 8 char buff [MAXLINE]; 9 time_t ticks; 10 listenfd = Socket (AF_INET, SOCK_STREAM, 0); 11 bzero (&servaddr, sizeof(servaddr)); 12 servaddr.sin_family = AF_INET;

  • 6

    13 servaddr.sin_addr.s_addr = htonl (INADDR_ANY); 14 servaddr.sin_port = htons (13); /* daytime server */ 15 bind (listendfd, (SA *) &servaddr, sizeof (servaddr)); 16 Listen (listendfd, LISTENQ); 17 for ( ; ; ) { 18 connfd = Accept (listenfd, (SA *) NULL, NULL); 19 ticks = time(NULL); 20 snprintf (buff, sizeof (buff), %.24s\r\n, ctime(&ticks)); 21 Write (connfd, buff, strlen (buff)); 22 close (connfd); 23 } 24 } --------------------------------------------------------------------------

    TCP Daytime Server

    Client

    server process accept() client .

    TCP three-way handshake , handshake accept()

    , connected descriptor descriptor . ( listening descriptor

    ). connected descriptor client ,

    listening socket client . client

    17 line loop .

    library time() , 1970 1 1 0 0

    0 Coordinated Universal Time(UTC)

    . library ctime() .

    Mon May 26 20:58:40 2003

    Server close() client . TCP

    . TCP FIN , FIN (ACK) .

    1.4 server client

    iterative server . client concurrent server

    . fork() (4) client

    child process . fork() thread()

    pre-fork server children process .

  • 7

    Daytime Client Server .

    daytimetcpcli.c daytimetcpsrv.c

    1.4 TCP/IP

    OSI(Open System Interconnection) model . model 7

    (layer) , 1.6 Internet protocol suite layer

    .

    OSI , TCP/IP

  • 8

    OSI layer 2 layer system device driver networking hardware.

    1500 (byte) MTU(maximum transfer unit) (Ethernet) data link

    layer .

    Network layer IPv4 IPv6 . Transport layer 2 TCP UDP

    ( 9 10 SCTP ). 1.6 TCP UDP

    application transport layer IPv4, IPv6

    . raw socket (ICMP raw socket ).

    OSI model 3 layer application layer . Web client (browser), Telnet client,

    Web server, FTP server application . Internet protocol OSI model

    3 layer .

    Socket programming interface application transport layer interface

    . (TCP UDP socket application

    ). Raw socket .

    1.6 . transport layer

    (interface) socket API . application (: FTP, Telnet, HTTP)

    4

    (: data , , ). ,

    TCP/IP

    ( (abstraction) ). API

    user process 4 OS(operating system) (kernel)

    . Unix/Linux OS user process kernel . socket

    API .

  • 9

    2. : TCP, UDP, SCTP

    TCP/IP TCP, UDP, SCTP

    . client/server application TCP UDP . SCTP

    VoIP(Voice over IP) (signaling) mission-critical

    . TCP, UDP

    SCTP .

    UDP (reliability) , TCP

    byte stream protocol. SCTP TCP

    (message) transport level

    multi-homing, multi-streaming .

    , transport

    protocol .

    .

    , TCP/IP TCP, IP .

    2.1 TCP/IP .

    TCP/IP

  • 10

    IPv4 IPv6 . IPv4 IPv6

    AF_INET AF_INET6 . application tcpdump BSD

    packet filter(BPF) datalink provider interface(DLPI) datalink

    . 9 API

    . traceroute program IP ICMP socket .

    2.1 .

    IPv4 Internet Protocol version 4. 32-bit . IPv4 TCP, UDP, SCTP, ICMP,

    IGMP packet service .

    IPv6

    Internet Protocol version 6. IPv6 1990 IPv4

    . 1990 Internet 128-bit .

    IPv6 TCP, UDP, SCTP, ICMPv6 packet service .

    IP" IP IP , IPv4

    IPv6 .

    TCP

    Transmission Control Protocol. TCP

    (full-duplex) byte stream . TCP socket stream socket

    . TCP , .

    TCP . TCP IPv4 IPv6 .

    UDP

    User Datagram Protocol. UDP connectionless protocol datagram socket

    . UDP . TCP

    UDP IPv4 IPv6 .

    SCTP Stream Control Transmission Protocol. TCP , multi-homing multi-

    streaming . TCP, UDP, SCTP IPv4 IPv6 .

    ICMP

    Internet Control Message Protocol. ICMP IP

    . ping traceroute ICMP . message

    user process TCP/IP networking software

    . ICMPv6 ICMPv4 .

    IGMP Internet Group Management Protocol. IPv4 multicasting .

    ARP

    Address Resolution Protocol. ARP IPv4 hardware ( Ethernet

    ) . ARP Ethernet, token ring, FDDI broadcast

    network point-to-point network .

  • 11

    ICMPv6 Internet Control Message Protocol version 6 . ICMPv4, IGMP, ARP

    .

    BPF BSD Packet Filter . datalink layer

    .

    2.1 USER DATAGRAM PROTOCOL (UDP)

    UDP transport-layer protocol. (application) UDP socket message

    UDP datagram IP datagram .

    UDP datagram network

    . , UDP datagram

    , checksum, error , .

    UDP service , UDP client server

    . , UDP client socket datagram

    datagram . UDP server

    UDP client datagram UDP socket .

    2.2 TRANSMISSION CONTROL PROTOCOL (TCP)

    TCP application service UDP service . TCP client

    server (connection) . TCP client server data

    .

    TCP . TCP data

    ACK(acknowledgment) . ACK TCP data

    .

    4~10 (network ). TCP

    100% . , endpoint data

    (: ) .

    TCP .

  • 12

    TCP client server RTT(Round Trip Time) . RTT

    network traffic TCP TCP RTT

    .

    TCP (error recovery) . byte data

    . application 2048 byte TCP socket TCP

    segment segment 1~1024 data

    segment 1025~2048 data (segment TCP IP data .).

    data (sequence

    number) ( reordering ). data

    ( )

    (discard) . , UDP . UDP ACK,

    , RTT, timeout, . , UDP datagram

    network host data .

    TCP flow control . TCP data byte

    . advertised window . window buffer

    sender overflow . window

    . Sender data window

    application buffer data window . window 0

    . TCP buffer data

    application buffer . , UDP flow

    control . UDP sender UDP receiver

    datagram .

    TCP (full-duplex) . application

    . TCP sender, receiver

    window . UDP .

  • 13

    2.3 STREAM CONTROL TRANSMISSION PROTOCOL (SCTP)

    SCTP UDP, TCP service . SCTP RFC 2960 , RFC 3309

    update . SCTP client server association ( TCP connection

    ). SCTP TCP , , flow control, data

    application . SCTP (connection) association'

    SCTP multi-homing . SCTP system

    2 IP .

    TCP SCTP message-oriented. , TCP UDP

    . SCTP

    multiple stream multi-streaming . stream

    message stream . stream

    data

    TCP .

    SCTP SCTP endpoint IP multi-homing .

    network network . SCTP ()

    SCTP association .

    2.4 TCP

    TCP connect(), accept(), close() TCP

    TCP (state transition diagram) .

    Three-Way Handshake

    TCP scenario .

    1) Server . socket, bind,

    listen passive open .

    2) Client connect() active open . TCP client

    data server synchronize"(SYN) segment

    . SYN IP

    header, TCP header .

  • 14

    3) Server client SYN ACK data

    . Server SYN segment client SYN ACK .

    4) Client sever SYN ACK .

    3 packet TCP three-way

    handshake . 2.2 .

    TCP

    Client J server K . ACK

    data . SYN 1byte ACK acknowledgment

    SYN 1 .

    TCP Connection Termination

    2.3 3 segment , 4

    segment .

    1) application close active close . TCP

    FIN segment .

    2) FIN passive close . TCP FIN ACK

    . FIN data .

    3) application socket close. TCP FIN .

    4) TCP FIN (active close ) FIN ACK .

  • 15

    TCP

    FIN ACK 4 segment .

    FIN data . 2, 3

    segment passive close segment .

    SYN FIN 1-byte (sequence number) . FIN ACK

    FIN 1 .

    2 3 passive close active close data

    . half-close 6 shutdown .

    2.3 client active close server active close

    . client active close HTTP protocol server .

    TCP State Transition Diagram

    TCP 2.4 (state transition diagram)

    . 11 (state)

    segment . , application CLOSED

    active open TCP SYN SYN_SENT . TCP

    SYN ACK ACK ESTABLISHED .

    data .

  • 16

    TCP

    ESTABLISHED . Application

    (EOF: end of file) close (active close), FIN_WAIT_1 .

    application ESTABLISHED FIN (passive close) CLOSE_WAIT

    . client sever .

    (simultaneous open) (simultaneous close, FIN

    ) .

  • 17

    TCP netstat 5 .

    Watching the Packet

    2.5 , data , TCP packet . ,

    endpoint TCP .

    Client MSS 536 server MSS 1460 (Ethernet MTU

    ) . MSS .

    client server . segment

    (, server 1460 ) . Server

    segment ( 536 ) .

    Data segment . Client ACK .

    piggybacking , server

    200ms ACK piggybacking . ACK

    .

    4 segment , active close ( client)

    TIME_WAIT . .

    2.5 TCP segment

    segment , TCP 10 segment .

    UDP 2 segment (

    ). UDP TCP UDP

    application . TCP (congestion control)

    UDP application . , application data

    UDP .

  • 18

    TCP

    2.5 TIME_WAIT

    TCP network programming TIME_WAIT .

    2.4 active close .

    maximum segment lifetime(MSL) , 2MSL .

    TCP MSL . RFC1122 2 , Berkeley

    30 . TIME_WAIT 1 4

  • 19

    . MSL IP datagram network .

    packet Internet MSL .

    Packet routing . Router

    router routing protocol

    . routing loop loop packet .

    TCP segment ,

    . ( packet MSL) routing loop packet

    . packet lost duplicate wandering

    duplicate . TCP .

    TIME_WAIT .

    1. (full-duplex)

    2. Network segment

    2.5 client ACK

    . Server FIN client ACK

    FIN client . client TIME_WAIT

    , client server FIN , RST(reset)

    . TCP 4 segment

    . TCP active close

    TIME_WAIT ACK .

    TIME_WAIT , 1.1.1.1 port 1500 2.2.2.2

    21 TCP . IP

    port . IP port

    (incarnation) . TCP

    . TCP TIME_WAIT

    ( port ) .

    TCP .

  • 20

    2.6 SCTP ASSOCIATION

    SCTP TCP connection-oriented association

    . SCTP handshake TCP .

    Four-Way Handshake

    TCP scenario SCTP association . , Server

    client . socket, bind, listen

    passive open .

    1) Client connect active open . client SCTP INIT

    message server IP , sequence number association

    .

    2) Server IP , , initiation tag , state cookie

    INIT-ACK message client . cookie

    COOKIE-ECHO .

    3) Client server cookie COOKIE-ECHO message .

    message packet user data .

    4) Server cookie client COOKIE-ACK message

    association . message packet user data .

    4 packet SCTP four-way handshake .

    2.6 4 segment .

    SCTP

  • 21

    SCTP four-way handshake cookie TCP three-way

    handshake . INIT ( chunk) initiation tag(Ta) initial sequence

    number(J) . Ta SCTP association

    packet (SCTP verification tag

    ). J TCP data chunk . initiation tag(Tz),

    (K) cookie C INIT-ACK chunk .

    Cookie , Cookie 3 COOKIE-ECHO ,

    client . TCP SYN-Flooding DoS(denial of service)

    attack . SCTP four-way handshake DoS

    Cookies . COOKIE-ACK SCTP association

    .

    Association Termination

    TCP SCTP half-closed" . association

    data . data queue

    . 2.6 .

    SCTP

    SCTP initiation tag association TCP TIME_WAIT

    . chunk INIT chunk INIT-ACK chunk initiation tag

    . tag

    . SCTP TCP .

  • 22

    SCTP State Transition Diagram

    2.8 SCTP (State transition diagram) .

    SCTP

    chunk . ,

    application CLOSED active open SCTP INIT

    COOKIE-WAIT . SCTP INIT-ACK COOKIE-ECHO

    COOKIE-ECHOED . SCTP COOKIE-ACK ESTABLISHED

  • 23

    . , COOKIE-ECHO COOKIE-ACK chunk chunk piggybacking

    .

    ESTABLISHED . application active

    close (outstanding) DATA chunk SHUTDOWN-PENDING

    , SHUTDOWN chunk SHUTDOWN-SENT .

    ESTABLISHED SHUTDOWN (passive close), SHUTDOWN-RECEIVED

    SHUTDOWN-ACK chunk . SHUTDOWN-ACK-SENT

    (, release),

    SHUTDOWN-COMPLETE chunk .

    Watching the Packets

    2.9 SCTP .

    SCTP

  • 24

    client COOKIE-ECHO data chunk piggyback COOKIE-ACK server

    data . COOKIE-ECHO one-to-many socket interface

    application data chunk . one-to-one

    socket data chunk . 9 .

    SCTP chunk'. 'chunk' chunk type, chunk flags, chunk length

    , DATA chunk control chunk . SCTP packet

    data/control chunk .

    2.7

    process , transport protocol(TCP, UDP, SCTP) 16-

    bit port process . TCP, UDP, SCTP

    () well-known port . , FTP TCP well-

    known port number 21 , TFTP(Trivial FTP) server UDP port 69 .

    client ephemeral (dynamic or private) port .

    client port client host .

    Internet Assigned Number Authority (IANA) port . RFC

    1700 . http://www.iana.org/ On-line database . Port 3

    .

    1) Well-known port: 0 ~ 1023. port IANA

    . TCP, USP, SCTP port . ,

    TCP , TCP UDP Web server port 80 .

    2) Registered port: 1024 ~ 49151. IANA , IANA

    list port. TCP UDP port .

    , port 6000 6063 protocol X window server

    , TCP .

    3) Dynamic or Private port: 49152 ~ 65535. IANA port .

    client port . (49152 65536 3/4 )

  • 25

    2.10 port .

    Socket pair

    TCP Socket pair endpoint local IP , local port, IP

    , port. socket pair network TCP . SCTP

    local IP , local port, IP , port .

    TCP multi-homed .

    endpoint IP port socket .

    UDP connectionless protocol socket UDP

    . Socket (bind, connect, getpeername ) ,

    () . , TCP, UDP, SCTP bind() local IP

    local port .

    2.8 CONCURRENT

    iterative server concurrent . UDP TCP

    SCTP concurrent . Concurrent server server client

    child process child

    port ? .

    server freebsd host 12.106.32.254 192.168.42.1 IP

    port ( 21) passive open . 2.11

    client .

  • 26

    Concurrent Server

    {8:21, *:*} socket pair . Server port 21 local interface( *) bind

    client . IP port *.*

    . listening socket . IP port :

    (HTTP ).

    local IP (wildcard) . Server host

    multi-homed (), server .

    server . Server . local

    (*) . 1 bind() socket

    IP INADDR_ANY .

    , client IP 206.168.112.219 host 12.106.32.254 server

    active open . dynamic port 1500 . 2.12

    . Client socket pair .

    client

  • 27

    2.13 , server client fork()

    (child process) child process client . (fork

    4 .)

    child process

    server host listening socket connected socket .

    Connected socket listening socket 21 port . Multi-homed

    connected socket local IP (12.106.32.254) .

    , client host client process server

    . client socket dynamic port (1501) .

    2.14 . Server . socket pair

    socket pair , client dynamic port(1501)

    .

  • 28

    client socket

    TCP segment endpoint socket pair 4

    . 2.14 local port(21) 3 socket

    (2 connected socket 1 listening socket). {206.168.112.219, 1500}

    {12.106.32.254, 21} segment child . ,

    {206.168.112.219, 1501} {12.106.32.254, 21} segment child

    . Port 21 segment listening socket .

    2.9

    network MTU . Ethernet MTU

    1,500 byte. host MTU (path) MTU .

    1,500byte Ethernet MTU MTU . Internet

    MTU . A B B A

    .

  • 29

    IP datagram , link MTU IPv4 IPv6 (fragmentation)

    . . IPv4 host datagram

    , IPv4 router datagram . IPv6

    datagram IPv6 router datagram .

    IPv4 header don't fragment"(DF) host router

    . DF link MTU datagram

    ICMPv4 destination unreachable, fragmentation needed but DF bit set"

    error message . IPv6 router datagram DF

    . IPv6 link MTU datagram ICMPv6 packet too big"

    error message .

    TCP data MSS(Maximum Segment Size) . MSS

    . MSS link MTU IP header TCP header . 1500 byte MTU

    Ethernet, IPv4 MSS 1,460 byte IPv6 MSS 1,440 byte (TCP header 20

    byte, IPv4 header 20 byte, IPv6 header 40 byte ).

    TCP Output

    2.15 application TCP socket data .

    TCP socket buffer SO_SNDBUF socket option

    (7). Application write() , kernel application buffer data socket

    buffer . socket buffer application buffer data

    (application buffer socket buffer , socket buffer data

    ) process (sleep) . socket .

    Application buffer byte socket buffer write()

    . , TCP socket write() application buffer

    . TCP data application

    data . TCP

    , .

  • 30

    TCP

    TCP data (transmission) socket buffer data

    TCP . TCP data (acknowledgment)

    TCP ACK TCP socket buffer . TCP

    data .

    TCP data MSS segment , MSS

    default 536 byte . IP header

    routing table IP (routing table interface

    ) link . IP datagram datalink

    . datalink queue queue packet

    protocol stack (datalink IP, IP TCP) . TCP error

    segment . application .

  • 31

    UDP Output

    2.16 application UDP socket data .

    UDP

    UDP socket buffer . UDP socket

    buffer socket UDP datagram

    . application socket buffer datagram EMSGSIZE

    . UDP application data

    buffer .

    UDP datagram 8 byte header IP . IPv4 IPv6

    header routing interface datagram datalink

    queue (MTU ) datagram datalink queue

    . TCP application data MSS UDP .

    UDP socket write datagram datagram datalink

    queue . queue datagram

    ENOBUFS application .

  • 32

    SCTP Output

    2.17 application SCTP socket data .

    SCTP

    SCTP TCP protocol buffer . TCP application

    SO_SNDBUF buffer (7). Application write()

    , kernel application buffer data socket buffer .

    Application buffer byte socket buffer write .

    SCTP SCTP data (transmission) socket buffer data SCTP

    . SCTP socket buffer data SCTP SACK

    .

  • 33

    3. SOCKETS

    socket API . socket

    address . : process kernel

    kernel process . kernel process value-

    result argument . argument .

    Protocol socket address sock_

    . code protocol .

    3.1 SOCKET ADDRESS

    socket argument socket address . protocol

    suite socket address . sockaddr_

    protocol suite .

    IPv4 Socket Address

    Internet socket address IPv4 socket address

    header socketaddr_in .

    struct in_addr {

    in_addr_t s_addr; /* 32-bit IPv4 address */

    /* network byte ordered */

    };

    struct sockaddr_in {

    uint8_t sin_len; /* length of structure */

    sa_family_t sin_family; /* AF_INET */

    in_port_t sin_port; /* 16-bit TCP or UDP port number */

    /* network byte ordered */

    struct in_addr sin_addr; /* 32-bit IPv4 address */

    /* network byte ordered */

    char sin_zero[8]; /* unused */

  • 34

    socket address .

    sin_len, length member OSI protocol , 4.3BSD-Reno .

    sin_len , member unsigned short sin_family. vender

    socket address length field , POSIX

    member . uint8_t datatype. POSIX

    datatype .

    socket address length field .

    length field , routing socket

    . protocol( routing table code) socket address

    routine kernel .

    processt kernel socket address 4 (bind, connect, sendto,

    sendmsg) Berkeley sockargs . process

    socket address ,

    sin_len member . Kernel process socket address

    5 socket (accept, recvfrom, recvmsg, getpeername, getsockname)

    process return sin_len member .

    socket address length field

    compile-time test . Code HAVE_SOCKADDR)SA)LEN

    .

    member

    . socket address length field IPv6 SIN6_LEN

    . IPv4 compile-time option(

    _SOCKADDR_LEN) socket address length field .

    .

    POSIX 3 member : sin_family, sin_addr, sin_port.

    POSIX member , Internet

    socket address . sin_zero member

    . socket address 16byte.

    s_addr, sin_family, sin_port member POSIX datatype . in_addr_t datatype

  • 35

    32bit unsigned integer type , sa_family_t unsigned integer type

    . length field , 8 bit unsigned integer,

    16 bit unsigned tnteger.

    POSIX datatype POSIX datatype .

    Datatype Description Header

    int8_t

    uint8_t

    int16_t

    uint16_t

    int32_t

    uint32_t

    Signed 8-bit integer

    Unsigned 8-bit integer

    Signed 16-bit integer

    Unsigned 16-bit integer

    Signed 32-bit integer

    Unsigned 32-bit integer

    sa_family_t

    socklen_t

    Address family of socket address structure

    Length of socket address structure, normally uint32_t

    in_addr_t

    in_port_t

    IPv4 address, normally uint32_t

    TCP or UDP port, normally uint16_t

    unsigned u_char, u_short, u_int, u_long datatype . POSIX

    . datatype .

    IPv4 TCP UDP port network byte .

    member .

    32-bit IPv4 address access . , serv

    Internet socket address , serv.sin_addr in_addr 32-bit

    IPv4 address , serv.sin_addr.s_addr in_addr_t( unsigned 32-bit

    integer) 32-bit IPv4 address . , IPv4 address

    . integer

    .

    in_addr_t sin_addr member . union

    in_addr (4.2 BSD) 32-bit IPv4 address 16-bit

  • 36

    4byte access . address byte class A, B,

    C . subnetting classless addressing address class

    union . union

    in_addr_t member in_addr .

    sin_zero member . 0

    . sin_zero member 0

    .

    non-wildcard IPv4 address bind , member 0

    , member 0.

    Socket address host : host

    , field( , IP address port) .

    (generic) Socket Address

    Socket address socket reference

    . socket protocol

    socket address .

    type . ANSI C,

    : void* pointer type. socket ANSI C 1982

    socket address

    .

    struct sockaddr {

    uint8_t sa_len; sa_family sa_family; /* address family: AF_xxx value */ char sa_date[14]; /* protocol-specific address */

    };

  • 37

    socket ANSI C bind prototype , socket

    address :

    int bind(int, struct sockaddr *, socklen_t);

    socket address

    protocol socket address . ,

    struct sockaddr_in serv; /* IPv4 socket address structure */

    /* fill in serv{} */

    bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));

    (struct sockaddr *) , C bind

    ANSI C prototype , warning: passing arg2 of bind from incompatible

    pointer type, warning .

    application programmer , socket address

    protocol .

    1 lnp.h ,

    struct sockaddr SA .

    kernel , socket address

    kernel , struct sockaddr * .

    type sa_family . application programmer

    . type void* , .

  • 38

    IPv6 Socket Address

    IPv6 socket address .

    struct in6_addr { uint8_t s6_addr[16]; /* 128-bit IPv6 address */ /* network byte ordered */

    }; #define SIN6_LEN /* required for compile-time tests */ struct sockaddr_in6 { uint8_t sin6_len; /* length of this struct (28) */ sa_family_t sin6_family; /* AF_INET6 */ in_port_t sin6_port; /* transport layer port# */ /* network byte ordered */ uint32_t sin6_flowinfo; /* flow information, undefined */ struct in6_addr sin6_addr; /* IPv6 address */ /* network byte ordered */ uint32_t sin6_scope)id; /* set of interfaces for a scope */

    };

    socket address length member , SIN6_LEN

    .

    IPv6 family AF_INET6, IPv4 family AF_INET.

    member . sockaddr_in6 64-bit

    , 128-bit sin6_addr member. 64-bit 64-bit data 64-bit

    .

    sin6_flowinfo member .

    - 20bit flow label.

    - 12bit .

    sin6_scope_id . link-local

    interface index.

  • 39

    Socket Address

    socket address IPv6 socket API , struct sockaddr

    . struct sockaddr , struct sockaddr_storage

    socket address . sockaddr_storage

    , .

    struct sockaddr_storage {

    uint8_t ss_len; /* length of this struct */

    sa_family_t ss_family; /* address family: AF_xxx value */

    /* implementation-dependent elements to provide:

    * a) alignment sufficient to fulfill the alignment requirements of

    * all socket address types that the system supports.

    * b) enough storage to hold any type of socket address that the

    * system supports.

    */

    sockaddr_storage type struct sockaddr socket address

    .

    a) socket address ,

    sockaddr_storage .

    b) sockaddr_storage socket address

    .

    sockaddr_storage field ss_family ss_len( ) ,

    . sockaddr_storage field access ss_family

    socket address casting .

  • 40

    Socket Address

    socket address : IPv4, IPv6,

    Unix domain, datalink, storage. , socket address byte length field

    . family field 1 byte . bit field

    bit .

    socket address

    socket address , Unix domain

    datalink . , socket

    socket address , .

    byte (4.4BSD ) .

    Sockaddr_un . ( )

    . , socket address (

    ) kernel .

    sockaddr_in{} .

  • 41

    3.2 VALUE-RESULT ARGUMENTS

    Socket address socket , reference

    . , . .

    : process kernel, .

    1. , bind, connect, sendto, process kernel socket address

    . socket address .

    .

    sturct sockaddr_in serv;

    /* fill in serv{} */

    connect (sockfd, (SA *) &serv, sizeof(serv));

    kernel , process kernel

    data . .

    socket address

    socket address datatype int socklen_t

    chapter . POSIX socklen_t uint32_t .

  • 42

    2. 4 , accept, recvfrom, getsockname, getpeername kernel

    process socket address .

    socket address .

    struct sockaddr_un cli; /* Unix domain */

    socklen_t len;

    len = sizeof(cli); /* len is a value */

    getpeername(unixfd, (SA *) &cli, &len);

    /* len may have changed */

    , (kernel

    kernel .)

    ( kernel process ).

    value-result . .

    socket address

  • 43

    process kernel socket address . 4.4

    BSD , socket kernel .

    , System V, socket process

    . kernel protocol stack

    . , , bind

    connect process kernel

    .

    Socket address length field value-result , socket

    address , kernel :

    IPv4 sockaddr_in 16 IPv6 sockaddr_in 28. socket address

    (, Unix sockaddr_un),

    .

    , value-result socket address

    . value-result .

    select (6)

    getsockopt (7)

    3.3 BYTE ORDERING

    2bytes 16 bit . 2bytes :

    little-endian byte , address byte, big-endian byte

    , address byte. .

  • 44

    Little-endian Big-endian

    ,

    . 16bit bit bit(MSB)

    bit bit(LSB) . , byte

    . . host byte order

    byte .

    host byte order .

    --------------------------------------------------------------------------

    1 #include lnp.h 2 int 3 main(int argc, char **argv) 4 { 5 union { 6 short s; 7 char c[sizeof(short)]; 8 }un; 9 un.s = 0x0102; 10 printf(%s: , CPU_VENDOR_OS); 11 if(sizeof(short) == 2) { 12 if(un.c[0] == 1 && un.c[1] == 2) 13 printf(big-endian\n); 14 else if (un.c[0] == 2 && un.c[1] == 1) 15 printf(little-endian\n); 16 else 17 printf(unknown\n); 18 } else 19 printf(sizeof(short) = %d\n, sizeof(short)); 20 exit(0); 21 }

    -------------------------------------------------------------------------

  • 45

    byteorder.c

    short integer 2byte 0x0102 c[0]( A) c[1]( A+1) byte

    order . CPU_VENDOR_OS

    , CPU , OS , GNU autoconf .

    .

    freebsd4 % byteorder

    i386-unknown-freebsd4.8: little-endian

    macosx % byteorder

    powerpc-apple-darwin6.6: big-endian

    freebsd5 % byteorder

    sparc64-unknown-freebsd5.1: big-endian

    aix % byteorder

    powerpc-ibm-aix5.1.0.0: big-endian

    hpux % byteorder

    hppa1.1-hp-hpux11.11: big-endian

    linux % byteorder

    i586-pc-linux-gnu: little-endian

    solaris % byteorder

    sparc-sun-solaris2.9: big-endian

  • 46

    16-bit byte ordering ; , 32-bit .

    byte ordering .

    network byte order . , TCP , 16-bit 32-

    bit IP . multi-byte byte

    . Internet multibyte big-endian byte

    ordering .

    , host byte order socket address .

    byte order host byte order .

    . POSIX

    socket address byte field .

    host byte order byte order . byte order

    .

    #include

    uint16_t htons (uint16_t host16bitvalue);

    uint32_t htonl (uint32_t host32bitvalue);

    Both return : value in network byte order

    uint16_t ntoh(uint16_t net16bitvalue);

    uint32_t ntoh(uint32_t net32bitvalue);

    Both return : value in host byte order

    , h host , n network , s short , l

    long . short long 4.2BSD Digital VAX . s

    16-bit (TCP UDP port ) , l 32-bit (IPv4 ) .

    , 64-bit Digital Alpha long 64bit . htonl ntohl 32-bit

    .

    , host byte order byte order (big-endian

    little-endian) . host

    byte order . Internet

    byte ordering , null .

  • 47

    3.4 BYTE MANIPULATION

    data interpreting, data C null-terminated , multibyte

    . socket address ,

    . IP 0 byte , C

    character . (string

    ) str null-terminated C character .

    b(byte ) 4.2BSD

    socket . mem(memory )

    ANSI C ANSI C

    .

    bzero, Berkeley-derived .

    , bcopy bcmp .

    #include

    void bzero(void *dest, size_t nbytes);

    void bcopy(const void *src, void *dest, size_t n_bytes);

    void bcmp(const void *ptr1, const void *ptr2, size_t nbytes);

    Returns: 0 if equal, nonzero if unequal

    bzero destination byte 0 .

    socket address 0 . bcopy source destination

    byte . bcmp byte string .

    byte string 0; 0 .

  • 48

    ANSI C .

    #include

    void *memset(void *dest, int c, size_t len);

    void *memcpy(void *dest, const void *src, size_t nbytes);

    int memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

    Returns: 0 if equal, 0 if unequal

    memset destination byte c . memcpy bcopy

    . . bcopy ,

    source destination memcpy . ANSI C

    memmove .

    memcpy C

    .

    dest = src;

    memcmp byte string 0 . ,

    0 . ptr1 byte 0

    , ptr2 byte 0 . byte unsigned chars

    .

    3.5 INET_ATON, INET_ADDR, INET_NTOA

    .

    Internet address ASCII ( ) byte ordered

    binary (socket address ) .

    1. inet_aton, inet_ntoa, inet_addr IPv4 dotted-decimal ( ,

    206.168.112.96) 32-bit byte ordered binary .

    .

  • 49

    2. inet_pton inet_ntop IPv4 IPv6 .

    . .

    #include

    int inet_aton(const char *strptr, struct in_addr *addrptr);

    Returns: 1 if string was valid, 0 on error

    in_addr_t inet_addr(const char *strptr);

    Returns: 32-bit binary network byte ordered IPv4 address;

    INADDR_NONE if error

    char *inet_ntoa(struct in_addr inaddr);

    Returns: pointer to dotted-decimal string

    , inet_aton strptr C character 32-bit

    binary byte ordered . addrptr .

    1 0 .

    inet_addr , 32-bit binary byte ordered

    . 232 IP addresses . (0.0.0.0

    255.255.255.255), INADDR_NONE ( 32

    one-bits) . dotted-decimal 255.255.255.255 (broadcast IPv4

    ) binary

    .

    , inet_addr . inet_aton .

    IPv4 IPv6 .

    inet_ntoa 32-bit binary byte ordered IPv4 dotted-decimal

    . .

    . ,

    . .

    .

  • 50

    3.6 INET_PTON, INET_NTOP

    IPv6 . IPv4 IPv6 .

    . p n presentation numeric .

    ASCII socket address

    .

    #include

    int inet_pton(int family, const char *strptr, void *addrptr);

    Returns: 1 if OK, 0 if input not a valid presentation format, -1 on error

    const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);

    Returns: pointer to result if OK, NULL on error

    family AF_INET AF_INET6. family ,

    errno EAFNOSUPPORT .

    strptr .

    addrptr . 1 .

    family , 0 .

    inet_ntop , (addrptr) (strptr). len

    , .

    , :

    #define INET_ADDRSTRLEN 16 /* for IPv4 dotted-decimal */

    #define INET6_ADDRSTRLEN 46 /* for IPv6 hex string */

    len , null , null

    errno ENOSPC .

    inet_ntop strptr null .

    . , .

  • 51

    .

    IPv6 ,

    .

    foo.sin_addr.s_addr = inet_addr(cp);

    inet_pton(AF_INET, cp, &foo.sin_addr);

    ptr = inet_ntoa(foo.sin_addr);

    char str[INET_ADDRSTRLEN];

    ptr = inet_ntop(AF_INET, &foo.sin_addr, str, sizeof(str));

  • 52

    IPv4 inet_pton . ,

    IPv4 inet_ntop .

    10 int 11 inet_pton(int family, const char *strptr, void *addrptr) 12 { 13 if (family == AF_INET) { 14 struct in_addr in_val; 15 if ( inet_aton(strptr, &in_val)) { 16 memcpy(addrptr, &in_val, sizeof(struct in_addr)); 17 return (1); 18 } 19 return (0); 20 } 21 errno = EAFNOSUPPORT; 22 return (-1); 23 }

    8 const char * 9 inet_ntop (int family, const void *addrptr, char *strptr, size_t

    len) 10 { 11 const u_char *p = (const u_char *) addrptr; 12 if (family == AF_INET) { 13 char temp[INET_ADDRSTRLEN]; 14 snprintf(temp, sizeof(temp), %d.%d.%d.%d, p[0], p[1], p[2],

    p[3]); 15 if (strlen(temp) >= len) { 16 errno = ENOSPC; 17 return (NULL); 18 } 19 strcpy(strptr, temp); 20 return (strptr); 21 } 22 errno = EAFNOSUPPORT; 23 return (NULL); 24 }

  • 53

    4. TCP SOCKETS

    TCP client server socket functions

    . ,

    . .

    client concurrency

    (concurrent servers) . client connection server

    fork. , fork one-process-per-client model .

    4.1 TCP client server . , server

    , server client . Client server request

    , server request , client request .

    client server end-of-file

    . server client

    .

    TCP

  • 54

    4.1 SOCKET

    #include

    int socket (int family, int type, int protocol);

    Returns: non-negative descriptor if OK, -1 on error

    Network I/O , (IPv4, IPv6,

    ) socket . Family ,

    4.1 . 4.2 .

    argument 4.4 type

    family type default 0 .

    Protocol family

    Family Description

    AF_INET IPv4 protocol

    AF_INET6 IPv6 protocol

    AF_LOCAL Unix domain protocols

    Type Description

    SOCK_STREAM Stream socket

    SOCK_DGRAM Datagram socket

    SOCK_SEQPACKET Sequenced packet socket

    SOCK_RAW Raw socket

  • 55

    family type . 4.3

    . Yes .

    socket family type

    AF_INET AF_INET6 AF_LOCAL

    SOCK_STREAM TCP|SCTP TCP|SCTP Yes

    SOCK_DGRAM UDP UDP Yes

    SOCK_SEQPACKET SCTP SCTP Yes

    SOCK_RAW IPv4 IPv6

    AF_xxx PF_xxx . AF_LOCAL AF_UNIX

    . SOCK_SEQPACKET SCTP , TCP SOCK_STREAM .

    Socket , file descriptor . socket

    descriptor sockfd . socket descriptor , protocol family(IPv4, IPv6,

    Unix) socket type(stream, datagram, raw) .

    AF_xxx vs PF_xxx

    AF_ prefix address family , PF_ prefix protocol_family .

    , single protocol family multiple protocol family ,

    PF_ AF_ .

    multiple address family ,

    PF_ AF_ .

    , ,

    . socket PF_ ,

    . POSIX socket PF_ AF_ socket address structure

    .

  • 56

    4.2 CONNECT

    Connect TCP client TCP server client .

    #include

    int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

    Returns: 0 if OK, -1 on error

    sockfd socket socket descriptor. parameter

    section3.3 socket address structure . Socket

    address structure server IP port .

    Client connect() bind() .

    ephemeral port source IP .

    TCP socket , connect TCP three-way handshake .

    . .

    1) client TCP SYN ETIMEDOUT .

    , connect SYN , 6 , 24

    . SYN 75

    . .

    2) Client SYN server reset(RST), server

    . (, server

    .) (hard error) RST ECONNREFUSED

    client .

    RST TCP TCP . RST

    SYN , TCP

    , TCP

    .

  • 57

    3) client SYN destination unreachable ICMP ,

    (soft error) . Client

    SYN . (: 75),

    ICMP EHOSTUNREACH ENETUNREACH .

    destination unreachable ICMP

    . ICMP . ,

    15 .

    1 client . daytime server

    local host(127.0.0.1) .

    solaris % daytimetcpcli 127.0.0.1

    Sun Jul 27 22:01:51 2003

    , IP .

    solaris % daytimetcpcli 192.6.38.100

    Sun Jul 27 22:04:59 PDT 2003

    , local subnet IP (192.168.1/24) host ID(100) .

    , subnet host ID 100 host client host ARP ( host

    ), ARP .

    solaris % daytimetcpcli 192.168.1.100

    connect error: Connection timed out

    Connect . err_sys ETIMEDOUT

    .

    daytime server host(local router) .

    solaris % daytimetcpcli 192.168.1.5

    connect error: Connection refused

    RST .

  • 58

    IP . Tcpdump 6 hop

    host ICMP .

    solaris % daytimetcpcli 192.3.4.5

    connect error: No route to host

    ETIMEDOUT , connect EHOSTUNREACH

    .

    TCP , connect CLOSED (socket )

    SYN_SENT , ESTABLISHED . Connect ,

    . connect

    . connect IP ,

    connect socket descriptor .

    4.3 BIND

    Bind local protocol address socket . 32bit IPv4

    128bit IPv6 16bit TCP UDP .

    #include

    int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);

    Returns: 0 if OK,-1 on error

    ,

    . TCP, bind IP

    .

    well-known port bind . TCP client server

    , connect listen socket ephemeral port

    . TCP client ephemeral port

  • 59

    . , TCP server ephemeral port

    , server well-known .

    RPC(Remote Procedure Call) server. server listening socket

    ephemeral port , RPC

    . client server connect ephemeral port

    . UDP RPC server .

    socket IP bind . IP server

    . TCP client , socket IP IP

    . TCP server , IP socket

    .

    TCP client socket IP bind . socket

    interface IP source,

    . TCP server socket IP bind ,

    client SYN destination IP source IP .

    , bind IP . 4.4

    sin_addr sin_port sin6_addr sin6_port .

    bind() IP

    IP port

    Wildcard 0 IP port

    Wildcard nonzero IP , port

    Local IP address 0 IP , port

    Local IP address nonzero IP port

  • 60

    0 , bind ephemeral port .

    wildcard IP ( IP ) socket (TCP) datagram

    socket local IP . IPv4 INADDR_ANY

    wildcard IP ( ) . 0. IP

    .

    struct sockaddr_in servaddr;

    servaddr.sin_addr.s_addr = htonl (INADDR_ANY); /* wildcard */

    IP 32 ( 0) IPv4 , IPv6

    . 128bit IPv6 . (C

    .)

    .

    struct sockaddr_in6 serv;

    serv.sin6_addr = in6addr_any; /* wildcard */

    in6addr_any IN6ADDR_ANY_INIT .

    in6addr_any extern .

    INADDR_ANY(0) htonl

    . INADDR_

    htonl() .

    Bind ADDRINUSE(Address already in use). 7

    SO_REUSEADDR SO_REUSEPORT socket option .

  • 61

    4.4 LISTEN

    TCP server listen . socket socket active socket

    . , connect client socket . Listen socket

    passive socket , socket .

    TCP listen socket CLOSE LISTEN .

    socket .

    #include

    #int listen (int sockfd, int backlog);

    Returns: 0 if OK, -1 on error

    bind accept . Backlog

    , listening socket queue .

    connection queue server three-way handshake

    client SYN entry . socket SYN_RCVD

    .

    connection queue TCP three-way handshake client

    entry . socket ESTABLISHED .

    4.2 listening socket queue .

  • 62

    TCP listening socket Queue

    Entry queue , listen socket

    . . server .

    4.3 queue .

    TCP listening socket

    Client SYN TCP queue entry three-way

    handshake client SYN ACK server SYN .

    entry three-way handshake (server SYN client

    ACK) entry queue .

  • 63

    three-way handshake queue entry queue entry

    queue queue entry

    sleep .

    queue .

    listen backlog queue .

    backlog 0 .

    . .

    listening socket client listening socket .

    , sample code backlog 5. Busy server

    1980 . Busy server

    . busy HTTP server

    backlog .

    backlog .

    , 5 backlog .

    . HTTP server

    server .

    command-line option .

    .

    listen wrapper function

    . LISTENQ .

  • 64

    ------------------------------------------------------------------------- 137 void 138 Listen (int fd, int backlog) 139 { 140 char *ptr; 141 /* can override 2nd argument with environment variable */ 142 if ( (ptr = getenv("LISTENQ")) != NULL) 143 backlog = atoi (ptr); 144 if (listen (fd, backlog) < 0) 145 err_sys ("listen error"); 146 } -------------------------------------------------------------------------

    Manual accept

    server . queue

    entry . busy Web server

    . backlog client SYN three-way handshake

    queue .

    client SYN queue , SYN RST .

    client TCP SYN queue

    . server TCP RST , client connect

    TCP .

    client SYN RST "there is a server at this port but its queues are full."

    .

    three-way handshake server accept

    server TCP socket .

    4.5 backlog

    . 7 backlog 5 .

  • 65

    OS backlog

    backlog MacOS Linux HP-UX FreeBSD Solaris

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    1

    2

    3

    4

    5

    7

    8

    10

    11

    13

    14

    16

    17

    19

    20

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    1

    1

    3

    4

    6

    7

    9

    10

    12

    13

    15

    16

    18

    19

    21

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    1

    2

    4

    5

    6

    8

    10

    11

    13

    14

    16

    17

    19

    20

    22

  • 66

    4.5 ACCEPT

    accept TCP server queue .

    queue sleep .

    #include

    int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

    Returns: non-negative descriptor if OK, -1 on error

    Cliaddr addrlen (client) .

    Addrlen value-result argument. *addrlen cliaddr

    socket address structure , , socket address

    structure .

    Accept , brand-new descriptor.

    descriptor client TCP . Accept , accept listen

    socket , accept socket . socket

    . Server listen socket .

    accept client socket . Server client

    socket .

    . socket descriptor

    . client

    , cliaddr addrlen null pointer . socket

    listen socket server . client

    identity accept null pointer .

    TCP Daytime Server .

  • 67

    -------------------------------------------------------------------------- 1 #include "unp.h" 2 2 #include 3 int 4 main(int argc, char **argv) 5 { 6 int listenfd, connfd; 7 socklen_t len; 8 struct sockaddr_in servaddr, cliaddr; 9 char buff[MAXLINE]; 10 time_t ticks; 11 listenfd = Socket(AF_INET, SOCK_STREAM, 0); 12 bzero(&servaddr, sizeof(servaddr)); 13 servaddr.sin_family = AF_INET; 14 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 15 servaddr.sin_port = htons(13); /* daytime server */ 16 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 17 Listen(listenfd, LISTENQ); 18 for ( ; ; ) { 19 len = sizeof(cliaddr); 20 connfd = Accept(listenfd, (SA *) &cliaddr, &len); 21 printf("connection from %s, port %d\n", 22 Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), 23 ntohs(cliaddr.sin_port)); 24 ticks = time(NULL); 25 snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); 26 Write(connfd, buff, strlen(buff)); 27 Close(connfd); 28 } 29 } --------------------------------------------------------------------------

    New declarations

    7-8

    value-result len client

    cliaddr.

    Accept connection and print client's address

    19-23

    len socket address structure cliaddr pointer len

    pointer accept . Socket address structure 32 IP

    ASCII inet_ntop ,

    16 ntohs .

  • 68

    .

    solaris % daytimetcpcli 127.0.0.1

    Thu Sep 11 12:44:00 2003

    solaris % daytimetcpcli 192.168.1.20

    Thu Sep 11 12:44:09 2003

    IP (127.0.0.1) IP (192.168.1.20)

    . .

    solaris # daytimetcpsrv1

    connection from 127.0.0.1, port 43388

    connection from 192.168.1.20, port 43389

    IP . Daytime client bind

    outgoing interface source IP

    section 4.4 . source IP

    IP . Solaris ephemeral port

    43388 43389 .

    shell prompt # . 13 reserved port

    . bind .

  • 69

    4.6 FORK EXEC

    (concurrent server) fork

    . .

    #include

    pid_t fork(void);

    Returns: 0 in child, process ID of child in parent, -1 on error

    fork (process) child process .

    (parent) .

    (child) ID . return value parent child

    .

    Fork child parent ID 0 child

    parent getppid parent ID .

    parent ID

    ID return value . ID fork return

    value .

    Fork descriptor fork

    . , accept fork

    . connected socket . connected socket

    . fork .

    .

    . .

    . Fork

    fork (

    ) exec( ) . shell

    .

  • 70

    exec . (

    exec .) exec

    main . ID . exec

    .

    exec

    (a) () () ,

    (b) pointer ,

    (c) , .

    #include

    int execl (const char *pathname, const char *arg0, ... /* (char *) 0 */ );

    int execv (const char *pathname, char *const argv[]);

    int execle (const char *pathname, const char *arg0, ...

    /* (char *) 0, char *const envp[] */ );

    int execve (const char *pathname, char *const argv[], char *const envp[]);

    int execlp (const char *filename, const char *arg0, ... /* (char *) 0 */ );

    int execvp (const char *filename, char *const argv[]);

    All six return: -1 on error, no return on success

    caller . main

    . 4.4 . execve

    execve .

  • 71

    EXEC

    .

    1. exec ,

    pointer .

    pointer argv . argv null pointer

    . .

    2. filename argument . PATH

    . Execlp execvp filename argument (/)

    PATH . pathname

    argument .

    3. environment pointer .

    environ environment list

    . environment list . Pointer envp

    null pointer .

  • 72

    4.7 CONCURRENT

    iterative server. daytime server .

    ,

    . (concurrent)

    fork .

    (concurrent) .

    pid_t pid; int listenfd, connfd; listenfd = Socket( ... ); /* fill in sockaddr_in{} with server's well-known port */ Bind(listenfd, ... ); Listen(listenfd, LISTENQ); for ( ; ; ) { connfd = Accept (listenfd, ... ); /* probably blocks */ if( (pid = Fork()) == 0) { Close(listenfd); /* child closes listening socket */ doit(connfd); /* process the request */ Close(connfd); /* done with this client */ exit(0); /* child terminates */ } Close(connfd); /* parent closes connected socket */ }

    accept , fork .

    (connfd connected socket) .

    (listenfd listening socket) connected socket

    .

    doit .

    , connected socket close. exit

    close . open descriptor

    . close .

    2 TCP close FIN

    . connfd close

  • 73

    ? reference count .

    Reference count file table entry , open descriptor

    . socket , listenfd file table entry reference

    count 1 . accept , connfd file table entry reference

    count 1 . fork descriptor

    file table entry reference count 2 . connfd

    reference count 2 1 . Reference count 0

    descriptor . connfd .

    . 4.5 accept

    (block)

    .

    Accept()

    accept , 4.6 . connfd

    . connected socket .

    Accept()

  • 74

    (concurrent) fork . 4.7 fork

    .

    fork()

    descriptor listenfd connfd .

    connected socket listening socket . 4.8 .

    parent child

    .

    listening socket accept .

  • 75

    4.8 CLOSE

    close TCP .

    #include

    int close (int sockfd);

    Returns: 0 if OK, -1 on error

    TCP close

    . socket descriptor . , read write

    . TCP queue .

    TCP .

    Descriptor Reference Counts

    (concurrent) connected socket close descriptor

    reference count . reference count 0 close

    TCP . connected

    socket (concurrent) .

    TCP FIN , close shutdown (6 ).

    accept connected socket close

    . descriptor .

    descriptor .

    . connected socket

    , reference count 2 1 connected socket

    1 . TCP .

  • 76

    4.9 GETSOCKNAME GETPEERNAME

    local protocol address (getsockname), getpeername

    foreign protocol address .

    #include

    int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);

    int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);

    Both return: 0 if OK, -1 on error

    value-result . localaddr

    peeraddr socket address structure .

    IP

    . domain name .

    .

    bind TCP connect getsockname

    local IP local .

    0( local ) bind getsockname

    local .

    address family getsockname .

    IP bind TCP (accept )

    getsockname local IP .

    socket descriptor argument connected socket .

  • 77

    5. TCP CLIENT/SERVER

    TCP / .

    .

    1. .

    2. .

    3. .

    5.1 / .

    Echo Client/Server

    , full-

    duplex TCP . fgets fputs .

    , TCP/IP TCP UDP

    . .

    / .

    / .

    .

    (

    .) .

  • 78

    5.1 TCP ECHO SERVER

    (concurrent) .

    -------------------------------------------------------------------------- 1 #include "lnp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int listenfd, connfd; 6 pid_t childpid; 7 socklen_t clilen; 8 struct sockaddr_in cliaddr, servaddr; 9 listenfd = Socket (AF_INET, SOCK_STREAM, 0); 10 bzero(&servaddr, sizeof(servaddr)); 11 servaddr.sin_family = AF_INET; 12 servaddr.sin_addr.s_addr = htonl (INADDR_ANY); 13 servaddr.sin_port = htons (SERV_PORT); 14 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 15 Listen(listenfd, LISTENQ); 16 for ( ; ; ) { 17 clilen = sizeof(cliaddr); 18 connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); 19 if ( (childpid = Fork()) == 0) { /* child process */ 20 Close(listenfd); /* close listening socket */ 21 str_echo(connfd); /* process the request */ 22 exit (0); 23 } 24 Close(connfd); /* parent closes connected socket */ 25 } 26 } --------------------------------------------------------------------------

    Create socket, bind server's well-known port

    9-15

    TCP . (INADDR_ANY) well

    known port . Wildcard address

    local interface . listen listening

    socket .

    Wait for client connection to complete

    17-18 accept .

  • 79

    Concurrent server

    19-24

    fork

    . Section 4.8 listening socket connected

    socket . str_echo (

    ) .

    str_echo . ,

    .

    -------------------------------------------------------------------------- 1 #include "unp.h" 2 void 3 str_echo(int sockfd) 4 { 5 ssize_t n; 6 char buf[MAXLINE]; 7 again: 8 while ( (n = read(sockfd, buf, MAXLINE) ) > 0) 9 Writen(sockfd, buf, n); 10 if (n < 0 && errno == EINTR) 11 goto again; 12 else if (n < 0) 13 err_sys("str_echo: read error"); 14 } --------------------------------------------------------------------------

    Read a buffer and echo the buffer

    8-9

    writen .

    (the normal scenario), FIN readline 0

    . , str_echo , .

  • 80

    5.2 TCP ECHO CLIENT

    TCP Echo main .

    -------------------------------------------------------------------------- 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int sockfd; 6 struct sockaddr_in servaddr; 7 if (argc != 2) 8 err_quit("usage: tcpcli "); 9 sockfd = Socket(AF_INET, SOCK_STREAM, 0); 10 bzero(&servaddr, sizeof(servaddr)); 11 servaddr.sin_family = AF_INET; 12 servaddr.sin_port = htons(SERV_PORT); 13 Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 14 Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); 15 str_cli(stdin, sockfd); /* do it all */ 16 exit(0); 17 } --------------------------------------------------------------------------

    .

    Create socket, fill in Internet socket address structure

    9-13

    TCP , internet socket address structure IP

    . IP command-line argument , well known

    port(SERV_PORT) unp.h .

    Connect to server

    14-15

    connect . str_cli() .

  • 81

    str_cli() . ,

    , , .

    ------------------------------------------------------------------------- 1 #include "unp.h" 2 void 3 str_cli(FILE *fp, int sockfd) 4 { 5 char sendline[MAXLINE], recvline[MAXLINE]; 6 while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen(sockfd, sendline, strlen (sendline)); 8 if (Readline(sockfd, recvline, MAXLINE) == 0) 9 err_quit("str_cli: server terminated prematurely"); 10 Fputs(recvline, stdout); 11 } 12 } -------------------------------------------------------------------------

    Read a line, write to server

    6-7

    fgets , writen .

    Read echoed line from server, write to standard output

    8-10

    readline fput .

    Return to main

    11-12

    fget null pointer . end-of-file(EOF)

    . Fgets wrapper

    Fgets end-of-file null pointer .

  • 82

    5.3

    Normal Startup

    TCP ( main str_echo, str_cli, readline, witen

    150), .

    ( ,

    , ) .

    (boundary conditions) TCP/IP ,

    .

    linux background .

    linux % tcpserv01 &

    [1] 17870

    , socket, bind, listen, accept accept . (

    .) , listening socket

    netstat .

    linux % netstat -a

    Active Internet connections (servers and established)

    Proto Recv-Q Send-Q Local Address Foreign Address State

    tcp 0 0 *:9877 *:* LISTEN

    (the heading) .

    . Listening socket

    a flag .

    . (wildcard) local IP 9877 local

    LISTEN . Netstat IP 0(INADDR_ANY, the wildcard) 0

    (asterisk) .

  • 83

    IP 127.0.0.1(loopback)

    . normal(non-loopback) IP .

    linux % tcpcli01 127.0.0.1

    socket connect , TCP three-way handshake

    . Three-way handshake , connect

    accept . .

    1. str_cli . fgets .

    2. accept , fork str_echo .

    readline , readline read , read

    .

    3. , accept

    .

    .

    Three_way handshake , ,

    . Three-way handshake

    connect , accept .

    accept connect RTT .

    , -

    .

    netstat TCP .

    linux % netstat -a

    Active Internet connections (servers and established)

    Proto Recv-Q Send-Q Local Address Foreign Address State

    tcp 0 0 local host:9877 localhost:42758 ESTABLISHED

    tcp 0 0 local host:42758 localhost:9877 ESTABLISHED

    tcp 0 0 *:9877 *:* LISTEN

  • 84

    ESTABLISHED local 9877 .

    ESTABLISHED local 42758 .

    .

    ps .

    linux % ps -t pts/6 -o pid,ppid,tty,stat,args,wchan

    PID PPID TT STAT COMMAND WCHAN

    22038 22036 pts/6 S -bash wait4

    17870 22038 pts/6 S ./tcpserv01 wait_for_connect

    19315 17870 pts/6 S ./tcpserv01 tcp_data_wait

    19314 22038 pts/6 S ./tcpcli01 127.0 read_chan

    . PID PPID

    . PPID PID tcpserv01

    tcpserv01 . PPID shell.

    STAT S, .

    , WCHAN . Linux accept

    connect wait_for_connect,

    tcp_data_wait, terminal I/O read_chain .

    WCHAN .

  • 85

    Normal Termination

    .

    linux % tcpcli01 127.0.0.1

    hello, world

    hello, world

    good bye

    good bye

    ^D

    echo , EOF (Control-D)

    . netstat .

    linux % netstat -a | grep 9877

    tcp 0 0 *:9877 *:* LISTEN

    tcp 0 0 localhost:42758 localhost:9877 TIME_WAIT

    (local 42758) TIME_WAIT listening socket

    .( netstat grep

    well-known port , the heading line .)

    .

    1. EOF , fgets null pointer str_cli() .

    2. str_cli main () , exit .

    3. open descriptors

    . FIN , TCP ACK

    . TCP .

    CLOSE_WAIT , FIN_WAIT_2 .

    4. TCP FIN , readline() readline

    0 . str_echo main .

    5. exit .

  • 86

    6. descriptors . connected socket

    TCP .

    FIN ACK. .

    TIME_WAIT .

    7. SIGCHLD

    .

    . . ps

    .

    linux % ps -t pts/6 -o pid,ppid,tty,stat,args,wchan

    PID PPID TT STAT COMMAND WCHAN

    22038 22036 pts/6 S -bash read_chan

    17870 22038 pts/6 S ./tcpserv01 wait_for_connect

    19315 17870 pts/6 Z [tcpserv01

  • 87

    5.4

    A.

    . software

    interruption . .

    .

    ( )

    SIGCHLD

    .

    action disposition . Sigaction()

    disposition disposition .

    . signal handler

    , . SIGKILL SIGSTOP

    . .

    . ,

    void handler(int signo);

    . , sigaction

    . SIGIO, SIGPOLL

    .

    disposition SIG_IGN . SIGKILL

    SIGSTOP .

    disposition SIG_DFL disposition .

    ,

    working directory . disposition

    . SIGCHLD .

  • 88

    Signal() Function

    POSIX disposition sigaction .

    . disposition

    signal .

    pointer SIG_IGN SIG_DFL .

    signal POSIX . POSIX

    sigaction . , POSIX sigaction

    signal . .

    -------------------------------------------------------------------------- 1 #include "lnp.h" 2 Sigfunc * 3 signal (int signo, Sigfunc *func) 4 { 5 struct sigaction act, oact; 6 act.sa_handler = func; 7 sigemptyset (&act.sa_mask); 8 act.sa_flags = 0; 9 if (signo == SIGALRM) { 10 #ifdef SA_INTERRUPT 11 act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ 12 #endif 13 } else { 14 #ifdef SA_RESTART 15 act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */ 16 #endif 17 } 18 if (sigaction (signo, &act, &oact) < 0) 19 return (SIG_ERR); 20 return (oact.sa_handler); 21 } --------------------------------------------------------------------------

    Simplify function prototype using typedef

    2-3

    signal .

    void (*signal (int signo, void (*func) (int))) (int);

  • 89

    lnp.h Sigfunc .

    typedef void Sigfunc(int);

    signal handler , (void).

    .

    Sigfunc *signal (int signo, Sigfunc *func);

    pointer ,

    .

    Set handler

    6 sigaction sa_handler func .

    Set signal mask for handler

    7

    POSIX signal handler .

    . Sa_mask signal handler

    . POSIX handler

    .

    Set SA_RESTART flag

    8-17

    SA_RESTART optional flag. , interrupted system

    .( interrupted system

    .) SIGALRM SA_RESTART flag(

    ) . (SIGALRM Section 14.2

    ,

    .) SunOS 4.x

    interrupted system . flag complement

    SA_INTERRUPT . flag SIGALRM flag

    .

  • 90

    Call sigaction

    18-20

    sigaction signal action .

    signal .

    POSIX Signal Semantics

    POSIX .

    signal handler .

    signal handler . signal handler

    sigaction sa_mask .

    sa_mask ,

    .

    . queue .

    .

  • 91

    B. SIGCHLD

    .

    ID (CPU , ) .

    ,

    ID 1(init ) , .

    (, init wait) COMMAND

    .

    Handling Zombies

    . ,

    . fork

    wait . SIGCHLD signal handler

    signal handler wait .

    listen signal handler .

    Signal (SIGCHLD, sig_chld);

    fork . signal

    handler .

    -------------------------------------------------------------------------- 1 #include "unp.h" 2 void 3 sig_chld(int signo) 4 { 5 pid_t pid; 6 int stat; 7 pid = wait(&stat); 8 printf("child %d terminated\", pid); 9 return; 10 } -------------------------------------------------------------------------

  • 92

    Solaris 9 signal

    .

    solaris % tcpserv02 & start server in background

    [2] 16939

    solaris % tcpcli01 127.0.0.1 then start client in foreground

    hi there we type this

    hi there and this is echoed

    ^D we type our EOF character

    child 16942 terminated output by printf in signal handler

    accept error: Interrupted system call

    main function aborts

    .

    1. EOF . TCP FIN

    ACK .

    2. FIN readline EOF . .

    3. SIGCHLD accept . sig_chld(signal

    handler) , wait PID printf .

    4. disposition(accept ) ,

    accept EINTR(interrupted system ) .

    .

    signal ,

    . Solaris 9

    C signal interrupted system

    . SA_RESTART flag signal

    . interrupted system .

    4.4BSD signal

    interrupted system accept .

  • 93

    signal handler return void

    . return statement

    .

    Handling Interrupted System Calls

    accept disposition

    . , return .

    . , accept

    . echo

    read(readline) . disposition

    . caller .

    disposition

    signal handler EINTR .

    interrupted system .

    ( SIGCHLD ), disposition EINTR

    . POSIX SA_RESTART flag

    can some . SA_RESTART flag

    interrupted system .

    , Berkeley-derived implementations select

    accept recvfrom .

    accept accept , for

    .

    for ( ; ; ) { clilen = sizeof (cliaddr); if ( (connfd = accept (listenfd, (SA *) &cliaddr, &clilen)) < 0) { if (errno == EINTR) continue; /* back to for () */ else err_sys ("accept error"); }

  • 94

    wrapper Accept accept .

    . interrupted system

    . read, write, select, open accept .

    connect. EINTR

    .

    . connect

    select .

    C. wait waitpid

    wait .

    #include