Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
-- prepare a device or file for IO close -- terminate use of opened device or file read -- obtain data from from an open file or device write -- store data into an open file or device seek -- move to a specific position in a file or device ioctl -- control or configure a device
Not sufficient! network addresses client vs. server different transport layers
Socket Descriptors
Treat as traditional file descriptors File descriptor table augmented A lot more complicated to fill in the data structure for socket descriptors
Specifying remote end and local end Is the socket listening or connecting Will we use TCP or UDP?
Struct sockaddr { /* struct to hold address */ u_char sa_len; /* total length */ u_short sa_family; /* address family */ char sa_data[14]; /* value of address */ }
TCP/IP protocol endpoint specification Address family: AF_INET All TCP/IP applications should use sockaddr_in instead of the generic one
Struct sockaddr_in { /* struct to hold address */ u_char sa_len; /* total length */ u_short sa_family; /* address family */ u_short sin_port; /* protocol port number */ struct in_addr sin_addr; /* IP address */ char sin_zero[8]; /* unused */ }
Server OS chooses based on routing
Well-Known or Ephemeral
More Details
Network byte order / Host byte order Why does it matter? Protocol port field of a sockaddr_in structure uses network byte order Utility functions: htons, ntohs, htonl, ntohl Symbolic constants (e.g. AF_INET)
Server
socket() bind() listen() accept()
Client
socket()
Handshake
Data (request)
connect() write()
read()
End-of-File close()
TCP Client
PF_INET PF_INET6 PF_UNIX PF_X25 STREAM DGRAM RAW 0, used by RAW socket
family port
ephemeral port ip addr (routing)
addr
CONNECT actions 1. socket is valid 2. fill remote endpoint addr/port 3. choose local endpoint add/port 4. initiate 3-way handshaking
disconnect sequence
TCP Server
family port addr
bind port # MASTER SOCKET
SLAVE SOCKET
disconnect sequence
close (ssd);
10
TCP Server
family port family
addr
msock
liste n
accep t
client port/addr
port
addr
bind
port
11
Create a socket
socket()
PF_INET (IPv4), PF_INET6 (IPv6), PF_LOCAL (local Unix), PF_ROUTE (access to routing tables), PF_KEY (encryption) type is one of SOCK_STREAM (TCP), SOCK_DGRAM (UDP) SOCK_RAW (for special IP packets, PING, etc. Must be root) setuid bit (-rws--x--x root 2002 /bin/ping*) protocol is 0 (used for some raw socket options) upon success returns socket descriptor Integer, like file descriptor Return -1 if failure
family is one of
12
Connect to server
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
sockfd is socket descriptor from socket() servaddr is a pointer to a structure with:
connect()
13
bind()
sockfd is socket descriptor from socket() myaddr is a pointer to address struct with:
host will pick ephemeral port (very rare for server) How do you know assigned port number?
bind()
address and port
process specifies
IP address port
result
kernel chooses IP addr and port
wildcard
wildcard
local IP addr local IP addr
nonzero
0 nonzero
15
listen()
16
accept()
Return next completed connection
client returns brand new descriptor, created by OS if used with fork(), can create concurrent server
17
18
close()
Close socket for use
int close(int sockfd);
closes socket for reading/writing returns (doesnt block) attempts to send any unsent data socket option SO_LINGER
Returns -1 if error
block until data sent or discard any remaining data
19
Client
socket()
well-known port
bind()
recvfrom()
close()
20
recvfrom fills in address of where packet came from sento requires address of where sending packet to
21
connect() call
22
MSG_DONTWAIT (this send non-blocking) MSG_OOB (out of band data) MSG_PEEK (look, but dont remove) MSG_WAITALL (dont give me less than max) MSG_DONTROUTE (bypass routing table)
23