Title: Socket API
1Socket API
2Socket API History
- Starting in 1980, ARPA funded research for TCP/IP
development - Adopted in 1983 in BSD v4.2 UNIX
- 1989 Berkeley released networking library freely
to other operating systems - deFacto standard for TCP/IP network sockets
- Many other types of APIs for networks, but the
Berkeley API has most popularity
3Internet Socket
- Communication endpoint
- either listening or talking
- Identified by the tuple (IP address, port,
protocol)? - Addressing
- IPv4 4 octets
- Octet is 0-255 (2 to the 8th power)?
- IPv6 32 octets
- Not all octets are used for endpoint
identification - 2 very special addresses
- 0.0.0.0 listening for anyone
- 255.255.255.255 broadcasting to everyone
4IP Addressing
- IPv4 addressing
- 4 octets
- Classes of addresses
5IP Addressing CIDR (classless inter-domain
routing)?
6Original Internet Map
7Port Numbers
- TCP 0 65535
- UDP 0 65535
- http//www.iana.org/assignments/port-numbers
- Well Known are 0-1023
- Usually have to run as 'root' for servers on
Well Known - Many applications listen on tcp and udp for a
given port number.... - whether they actually use both or not!
8Networking Layers
9Networking - Routing
10TCP state model
11TCP vs UDP Header
TCP Header
UDP Header
12Socket Library (API)?
- Original intention of Berkeley Sockets was not to
define network, only a communication method - Supports many different types of networks
- Main difference between is not in socket library
calls, only in address definition (rendezvous) - Abstraction was/is the key
- first build most atomic functions that do not
discriminate against any networks/protocols - second start abstracting out more functions that
accept arguments to define function in more
specifics
13Socket and File Descriptors
- File descriptors are integer values gt 0 that
identify an open file to a program - These values are held in a file descriptor table
- Table holds integer values as keys, and the value
of the key is a pointer to the data structure for
the open file - Holds things like v-node, offset/etc..
- Simply, socket descriptors also fall in that
table, but they point at a different kind of
structure - Point to a socket data structure
14File and Socket descriptor data structure
differences
- File Descriptor Data Structure
- current file offset
- v-node pointer
- status
- flags
- ....etc...
- Socket Descriptor Data Structure
- protocol family
- service
- local and remote IP address
- local and remote port numbers
- etc.....
15Active or Passive Socket Descriptors
- Active Socket Descriptor
- Client
- Initiating the connection
- Passive Socket Descriptor
- Server
- Listening for incoming active sockets
- Both created in the same way, just how they are
used!
16Endpoint Addressing
- Must understand that the socket library is an
ABSTRACTION, so when we talk about addressing, we
are describing TCP/IP addressing - 2 types based on families
- AF_INET Address Family
- PF_INET Protocol Family
- NOTE in the C language, if a VALUE is in all
caps, it usually is a defined constant with an
integer value!! - e.g. AF_INET is really the integer 2, and PF_INET
is also the same integer (usually quite
confusing)?
17Address Structure
- Again, since Berkeley Sockets are an ABSTRACTION,
there are generic and specific structures to hold
address types - Generic struct sockaddr
- Specific struct sockaddr_in (in for Internet)?
- NOTE USE THE SPECIFIC STRUCTURE!
- struct sockaddr_in
- u_char sin_len
- u_short sin_family
- u_short sin_port
- struct in_addr sin_addr
- char sin_zero8
18Socket Function Calls - socket()?
- include ltsys/types.hgt
- include ltsys/socket.hgt
- int socket(int domain, int type, int
protocol) - Returns socket descriptor if OK, -1 on error
- DOMAIN PARAMETER TYPE
- PF_UNIX, PF_LOCAL Local communication
unix(7) SOCK_STREAM TCP - PF_INET IPv4 Internet
protocols ip(7) SOCK_DGRAM UDP - PF_INET6 IPv6 Internet
protocols - PF_IPX IPX - Novell
protocols - PF_NETLINK Kernel user interface
device netlink(7)? - PF_X25 ITU-T X.25 /
ISO-8208 protocol x25(7)? - PF_AX25 Amateur radio AX.25
protocol - PF_ATMPVC Access to raw ATM
PVCs - PF_APPLETALK Appletalk
ddp(7)? - PF_PACKET Low level packet
interface packet(7)?
19Socket Function Calls - connect()?
- include ltsys/types.hgt
- include ltsys/socket.hgt
- int connect(int sockfd, const struct
sockaddr serv_addr, socklen_t addrlen) - Returns 0 if ok, -1 on error
20Socket Function Calls - send()?
- include ltsys/types.hgt
- include ltsys/socket.hgt
- ssize_t send(int s, const void buf, size_t
len, int flags) - ssize_t sendto(int s, const void buf,
size_t len, int flags, const - struct sockaddr to, socklen_t tolen)
- ssize_t sendmsg(int s, const struct msghdr
msg, int flags) - Returns number of characters sent, or -1 on
error
21Socket Function Calls Recv()
- include ltsys/types.hgt
- include ltsys/socket.hgt
- ssize_t recv(int s, void buf, size_t len, int
flags) - ssize_t recvfrom(int s, void buf, size_t len,
int flags, struct sock- addr from,
socklen_t fromlen) - ssize_t recvmsg(int s, struct msghdr msg, int
flags) - Returns number of bytes read if ok, 0 on
shutdown, -1 on error
22Socket Function Calls - close()?
- include ltunistd.hgt
- int close(int fd)
- Returns 0 if ok, -1 on error
23Socket Function Calls - bind()?
- include ltsys/types.hgt
- include ltsys/socket.hgt
- int bind(int sockfd, struct sockaddr my_addr,
socklen_t addrlen) - Returns 0 if ok, -1 on error
24Socket Function Calls - listen()?
- include ltsys/socket.hgt
- int listen(int s, int backlog)
- Returns 0 if ok, -1 on error
25Socket Function Calls - accept()?
- include ltsys/types.hgt
- include ltsys/socket.hgt
- int accept(int s, struct sockaddr addr,
socklen_t addrlen) - Returns socket descriptor if ok, -1 on error
26Socket Functions Client Sequence
- socket
- connect
- send
- recv
- close
27Socket Functions Server Sequence
- socket
- bind
- listen
- accept
-
- recv
-
- send
- close