Title: Berkeley Sockets Details for everyday use
1Berkeley SocketsDetails for everyday use
2Data types
- Basic data types
- Have a certain number of bits
- Not very important in platform-dependent
non-networked applications - Not very important in platform-dependent
networked applications - Very important in platform-independent networked
applications
3Data types
- Basic data types
- C data types
- char unsigned char - 8 bits
- short unsigned short - 16 bits
- int unsigned int - 32 (or 64) bits
- long unsigned long - 32 or 64 bits
- long long unsigned long long - 64 (or 128) bits
- Socket API portable types
- size_t 32 bits unsigned, 0 232-1
- ssize_t 32 bits signed, -231 231-1
4IP addresses and hostnames
- IPv4 host address
- When written on paper, it looks like
129.240.71.213 - dotted decimal notation
- Represents a 32 bit address
- Binary in bits
- 10000001 11110000 01000111 11010101
- Hexadecimal in bytes
- 0x81 0xf0 0x47 0xd5
- One 4 byte int on x86, StrongARM, XScale,
- 0xd547f081
- One 4 byte int on PowerPC, POWER, Sparc,
- 0x81f047d5
- In network byte order
- 0x81f047d5
5IP addresses and hostnames
- On x86 etc.
- ntohl(0x81f047d5) 0xd547f081
- On PowerPC etc.
- ntohl(0x81f047d5) 0x81f047d5
6IP addresses and hostnames
- IPv4 host address
- Corresponds to one network interface card (NIC)
- IPv4 network address
- Looks like 129.240.69.0/24
- Refers to add addresses that that the same first
24 bits - 10000001 11110000 01000111 11010101
- 129.240.69.213 is in that network
7IP addresses and hostnames
- IPv4 networks
- Institutes and companies own network address
ranges - e.g. 129.240.0.0/16 - UiO
- e.g. 9.0.0.0/8 IBM
- Institutes and companies assign addresses to
their computers - Fixed addresses
- Temporary addresses
Class A addresses
1.0.0.0/8 - 127.0.0.0/8
Class B addresses
128.0.0.0/16 - 191.255.0.0/16
192.0.0.0/24 - 223.255.255.0/24
Class C addresses
Class D or multicast addresses
224.0.0.0 - 239.255.255.255
8IP addresses and hostnames
- IPv4 networks
- Institutes and companies own network address
ranges - e.g. 129.240.0.0/16 - UiO
- e.g. 9.0.0.0/8 IBM
- Institutes and companies assign addresses to
their computers - Fixed addresses
- Temporary addresses
- They can also create subnets
- IFI has subnets of UiOs address space
- E.g. 129.250.69.0/24
- 129.250.69.0 cant be used for a computer, its
the networks address - 129.250.69.255 cant be used for a computer, its
an address for all computers in the network, the
broadcast address
9IP addresses and hostnames
- These are many addresses
- Why do we need IPv6?
- Most IPv4 addresses have owners
- No matter whether the addresses are needed
- Most in the US, using 1
- Several in Europe
- Really tight in Asia, only assigned when need is
proven - IPv6 addresses
- 128 bits
- In text
- 2FFF800000941 8 times 16 bits
- Hard to remember own address
10IP addresses and hostnames
- Hostnames
- More exactly fully qualified host names
- Look like niu.ifi.uio.no
- Host niu
- In subdomain ifi, Institutt for Informatik
- In domain uio, Universitet i Oslo
- In top level domain no, Norway
- Who decided this?
- .no - IANA gave it to Uninett
- .uio - Uninett gave it to UiO
- .ifi - USIT, UiOs drift, gave it to IFI
- niu - IFI drift gave it to the machine
11Name resolution
struct hostent hostp struct sockaddr_in
serveraddr int sock / Look in DNS for
the IP address of the name / if ((hostp
gethostbyname(machine)) 0)
fprintf(stderr,Unknown machine s\n",machine)
exit(1) bzero((void )
serveraddr, sizeof(serveraddr))
serveraddr.sin_family AF_INET
memcpy(serveraddr.sin_addr,
hostp-gth_addr, hostp-gth_length)
serveraddr.sin_port htons(port_number)
- Gethostbyname
- Takes a hostname
- Returns information about that hostname
- Including its IP address
- How?
12Name resolution
list of statically known hosts 127.0.0.1 loc
alhost 129.27.2.1 argul.ifi.uio.no
argul 129.27.2.21 fileserver.ifi.uio.no
fileserver
- Gethostbyname
- Takes a hostname
- Returns information about that hostname
- Including its IP address
- How?
- First
- Look into /etc/hosts
- But only for few, special, well-known hosts
13Name resolution
Root name server
- Gethostbyname
- Takes a hostname
- Returns information about that hostname
- Including its IP address
- How?
- Then
- Using DNS
- the Domain Name System
4
2
3
5
ifi.uio.no
dns.umass.edu
1
6
gaia.cs.umass.edu
a121.ifi.uio.no
14Name resolution
define h_addr h_addr_list0 struct hostent
/ official hostname / char h_name
/ alias names of the host entry NULL
indicates end of the list / char
h_aliases / host address type, e.g.
AF_INET / int h_addrtype / length of
each address / int h_length / list of
addresses, primary is 0th entry entry 0
indicates end of the list / char
h_addr_list
- Gethostbyname
- Takes a hostname
- Returns information about that hostname
- Including its IP address
- Return value
- Pointer tostruct hostent
- Contains more than just an IP address
- Other names
- All addresses
15Name resolution
eth0 Link encapEthernet HWaddr
00C04FA30CD3 inet
addr129.240.70.96 Bcast129.240.71.255
Mask255.255.248.0 UP BROADCAST
NOTRAILERS RUNNING MULTICAST MTU1500 Metric1
RX packets40543011 errors65 dropped0
overruns203 frame65 TX
packets9222810 errors0 dropped0 overruns0
carrier0 collisions0 txqueuelen100
RX bytes3360933562 (3205.2 Mb) TX
bytes2321704496 (2214.1 Mb)
Interrupt19 Base address0xfcc0 lo Link
encapLocal Loopback inet
addr127.0.0.1 Mask255.0.0.0 UP
LOOPBACK RUNNING MTU16436 Metric1
RX packets26429 errors0 dropped0 overruns0
frame0 TX packets26429 errors0
dropped0 overruns0 carrier0
collisions0 txqueuelen0 RX
bytes5877172 (5.6 Mb) TX bytes5877172 (5.6
Mb)
- Finding the own hostname
- Command line
- ifconfig a (Unix)
- ipconfig /a (Windows)
- Gives you all IP addresses
- Typically 2
- 127.0.0.1 localhost
- and the actual one
- Inside a program
- Difficult without a connected socket
- With a connected socket
- struct hostent getsockname(int sock)
- Similar to gethostbyname
16Netmasks
- Help your computer find the way
- All computers with addresses in the same network
can be reached directly - If you know your own computers address, e.g.
129.240.70.96 - and the relevant bits of your network, e.g. /24
- you know that all computers with addresses
129.240.70.? are in the same network - For each computer with another address, a member
of the own network (computer or router) must be
found that can send the data into the right
direction - This is sometimes wrong
- Understanding of network addresses becomes
important - Tell your computer its network address
- For computers with a fixed IP address set up
once - For computers with a dynamic address set up
together with address
17Netmasks
- The computer
- Is not told directly that its on the network
129.240.70.0/24 - Instead it is told that its address is
129.240.70.97 - And that its netmask is 255.255.255.0 these are
24 bits - It figures the network address out from this
- In ifconfig or ipconfig
- Lists hostname
- E.g. 129.240.70.97
- Lists netmask
- e.g. 255.255.255.0
- Or 0xffffff00
- Or the highest 24 bits set
- Implies that
- 129.240.70.97
- is part of the subnet 129.240.70.97/24
- which is the same as 129.240.70.0/24
- This answer from ifconfig implies also that
- All computers in the net 129.240.70.0/24 are
reachable directly - Without router
18Binding TCP client sockets to ports
int boundconnect( char servername, int port, int
ownport ) if ((sock socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP)) lt 0) bzero(ownaddr,
sizeof(ownaddr)) ownaddr.sin_family
AF_INET ownaddr.sin_addr.s_addr
INADDR_ANY ownaddr.sin_port
htons(ownport) if (bind(sock, (struct
sockaddr )ownaddr, sizeof(ownaddr)) lt
0) if ((hostp gethostbyname(machine))
0) bzero(serveraddr,
sizeof(serveraddr)) serveraddr.sin_family
AF_INET memcpy(serveraddr.sin_addr,
hostp-gth_addr, hostp-gth_length)
serveraddr.sin_port htons(port_number) if
(connect(sock, (struct sockaddr )serveraddr,
sizeof serveraddr)
lt 0) return sock
- Main reasons to do this
- Use a specific network card
- Other potential reasons
- Firewalls may restrict open ports
- Server demands connection from a priviledged port
- 0 1023
- Associate virtual circuit tags with source ports
19Connecting TCP socket
Client
Server
socket bind
socket
CLOSED
CLOSED
connect
Wait 30 sec
listen
Recv ACK
Send SYN
LISTEN
LAST_ACK
SYN_SENT
TIME_WAIT
Recv SYN, Send ACK and SYN
Rcvd SYN, ACK Send ACK
Recv FIN, Send ACK
Send FIN
read write
SYN_REVD
CLOSE_WAIT
ESTABLISHED
FIN_WAIT_2
close
read write
Recv ACK
Send FIN
Recv FIN, Send ACK
Recv ACK
ESTABLISHED
FIN_WAIT_1
20TCP Handshake
Client
Server
CLOSED
connect
Send SYN
LISTEN
SYN_SENT
Recv SYN, Send ACK and SYN
Rcvd SYN, ACK Send ACK
SYN_REVD
ESTABLISHED
Recv ACK
ESTABLISHED
21Connecting UDP sockets
- Datagram service UDP
- Usually unconnected
- connect can be used
- Has only local meaning
- Local UDP remembers a target socket
- Advantage
- Reading and writing is simpler
- Possible to figure best packet size
- Disadvantage
- Can use unconnected sockets to send to several
receivers - Connected sockets only to one
22Reading and writing function calls
- int read( int sock, void buffer, int n)
- sock must be connected
- int recv( int sock, void buffer, int n,
int options ) - sock must be connected
- Sometimes useful option MSG_PEEK read packet but
leave it in the queue - int recvfrom( int sock, void buffer, int n,
int options, struct
sockaddr src, int srclen ) - Meant for unconnected sockets
- If sock is connected, src is identical to
sockaddr from accept
23Reading and writing function calls
- int write( int sock, void buffer, int n)
- sock must be connected
- int send( int sock, void buffer, int n,
int options ) - sock must be connected
- int sendto( int sock, void buffer, int n,
int options, struct sockaddr
dest, int destlen ) - Meant for unconnected sockets
- If sock is connected, dest address must refer to
the other side
24Select
int select( int max_fd,
fd_set read_set, fd_set
write_set, fd_set except_set,
struct timeval timeout )
- Complicated at first
- But very useful
- Can wait for activity on many sockets
- New connections on request sockets
- New data on connected sockets
- Closing of a connected socket
- Ready-to-send on a socket
- Can wait for user input
- Can wait for timeouts
25Select
int select( int max_fd,
fd_set read_set, fd_set
write_set, fd_set except_set,
struct timeval timeout )
- For servers
- Serve many clients at once
- Handle clients that close connections, clients
that crash, - For the chat example
- Wait for data from chat partner
- Wait for typing of the user
26Select
int select( int max_fd,
fd_set read_set, fd_set
write_set, fd_set except_set,
struct timeval timeout )
- read_set
- Arriving connect requests
- Arriving data
- Closing sockets
- write_set
- Non often used
- Non-blocking send is finished
- except_set
- Hardly ever used
- sendto(.,.,.,MSG_OOB)
27Select
void wait_for_all(int clientsock, int
clients) fd_set read_set int
i,act,top0 FD_ZERO(read_set) for(
i0 iltclients i )
FD_SET(clientsocki,read_set) top
MAX(top,clientsocki) act select(
top1, read_set, NULL,
NULL, NULL)
- Using only the read_set is typical
- Clear the read_set
- Must be done every time
- Put all sockets into the read set
- Find the highest socket number, add 1
- NULL timeout
- Means forever
- Call select
- waits for arriving data
28Select
void wait_some_time(int sec, int usec)
struct timeval timeout timeout.tv_sec
sec timeout.tv_usec usec act
select( 0, NULL, NULL,
NULL, timeout )
- select can also wait only for a timeout
- Without sockets
- Timeout parameter
- NULL means wait forever
- Timeval 5,0 means wait 5 seconds
- Timeval 0,0 means dont wait at all
29Safe writing
int safewrite(int so, char buf, int l)
int i if (iwrite(so, buf, l)0)
printf("Can't write to socket, connection is
closed" ) exit(1) return i
- You never know when something strange happens on
your TCP connection - Safe writing is necessary
- Code from last lecture
- Considers closing of the socket
30Safer writing
- Not all data may be sent at once
- TCP takes as much as it wants to
- Try to send again
- If there was no error
31Safer writing
int safewrite( int sock, char buffer, int len
) int ij0 do i write(
sock, bufferj, len ) if( i -1 )
perror( Error writing to
socket ) exit( 1 )
else if( i 0 ) printf(
The socket has been closed, quitting\n )
exit( 1 ) len len
i j j i while( len gt 0
) return 1
32Safer writing
- Not all data may be sent at once
- TCP takes as much as it wants to
- Try to send again
- If there was no error
- If there was no important error
33Safer writing
else if( i 0 )
printf( The socket has been closed, quitting\n
) exit( 1 ) else
if( i gt 0 ) len len i
j j i while(
len gt 0 ) return 1
int safewrite( int sock, char buffer, int len
) int I,j0 do i
write( sock, bufferj, len ) if( i
-1 ) switch( errno )
case EINTR / do nothing, no
problem / break
default perror( Error writing
to socket ) exit( 1 )
34Safer writing
- Not all data may be sent at once
- TCP takes as much as it wants to
- Try to send again
- If there was no error
- If there was no important error
- You may receive a signal
- These are alarm signals in case of emergencies
- Pressing CTRL-C raises a signal
- SIGKILL
- An interrupt that ends your program if you dont
handle it - Server that use TCP
- Get SIGPIPE often
- When the receiver crashes in the middle of a
write
35Safer writing
default perror(
Error writing to socket )
exit( 1 ) else
if( i 0 ) printf( The
socket has been closed, quitting\n )
exit( 1 ) else if( i gt 0 )
len len i j
j i while( len gt 0 )
return 1
int main( ) signal( SIGPIPE, SIG_IGN
) / I dont want to do much / int
safewrite( int sock, char buffer, int len )
int I, j 0 do i write(
sock, bufferj, len ) if( i -1 )
switch( errno )
case EINTR / do nothing, no problem
/ break case EPIPE
/ do nothing, will write 0 next round /
break
36Safer writing
- Not all data may be sent at once
- TCP takes as much as it wants to
- Try to send again
- If there was no error
- If there was no important error
- You may receive a signal
- These are alarm signals in case of emergencies
- Pressing CTRL-C raises a signal
- SIGKILL
- An interrupt that ends your program if you dont
handle it - Server that use TCP
- Get SIGPIPE often
- When the receiver crashes in the middle of a
write
- Slow clients block the server
- Important if your server has other things to do
- For example sending data to many clients at once
- Send only the data that you can send immediately
- Send more data when the connection is ready to
take more - Set the socket in non-blocking mode
- and use select
37Safer writing
default perror( Error
writing to socket ) return -1
else if( i 0 )
printf( The socket has been closed, quitting\n
) return -1 else if( i gt 0 )
len len i j j i
return i
int main( ) signal( SIGPIPE, SIG_IGN
) / I dont want to do much / int
safewrite( int sock, char buffer, int len )
int I, j 0 i write( sock, bufferj,
len ) if( i -1 ) switch(
errno ) case EINTR / do
nothing, no problem / break
case EPIPE / do nothing, will write 0 next
round / break
38Safer writing
FD_ZERO( writable ) for( i0
iltnsock i ) FD_SET( socki,
writable ) maxMAX(socki,max)
retval select( max1, NULL, writable, NULL,
NULL ) if( retval gt 0 )
for( i0 iltnsock i ) if(
FD_ISSET( socki, writable ) )
retval safewrite( socki, bufwi,
li )
li li retval / if no error but not
finished / wi wi
retval if( li gt 0 ) again
1
free( w )
int createASocket( char machine, int
port_number ) socki
TCPClientSocket(machine, port_number) int
retval fcntl( socki, F_SETFL, O_NONBLOCK)
if( retval -1 ) perror( Could
not set socket into non-blocking mode )
void safeWriteToMany( int sock, int
nsock, char buf , int l) int i, retval,
again1, max0 int w fd_set writable
w (int)malloc( nsock ) memset( w, 0,
sizeof(int)nsock ) while( again 1 )
again 0
39Socket options
- Two ways to set socket options
- setsockopt( int sock, int level, int
optname, void optvalue, int optlen ) - Options for reading and writing all types of
sockets - Often used insetsockopt( request_sock,
SOL_SOCKET, SO_REUSEADDR, i,
sizeof(i)) - fcntl( int sock, int optname, long optvalue )
- Options for reading and writing all types of
sockets, files, pipes, user input, graphical
output - Often used in fcntl( socki, F_SETFL,
O_NONBLOCK)
40Summary
- Names and addresses
- IP addresses
- Fully qualified hostnames
- Domain Name System
- How to use it
- Connections in TCP and UDP
- Functions
- The select function call
- Several functions for reading and writing
- Writing really safe
- To one
- To many