Upload
nivi-senthil
View
221
Download
0
Embed Size (px)
Citation preview
7/27/2019 Adv Sock Opt
1/46
Netprog: Advanced Sockets Programming 1
Advanced Sockets
Programming
7/27/2019 Adv Sock Opt
2/46
Netprog: Advanced Sockets Programming 2
It's important to know about some of
these topics, although it might not beapparent how and when to use them.
Details are available online (and inUnix Network Programming) - we arejust trying to get some idea of what canbe done.
Socket Options
Posix name/address conversion
Out-of-Band Data Signal Driven I/O
7/27/2019 Adv Sock Opt
3/46
Netprog: Advanced Sockets Programming 3
Socket Options
Various attributes that are used todetermine the behavior of sockets.
Setting options tells the OS/ProtocolStack the behavior we want.
Support for generic options (apply to allsockets) and protocol specific options.
7/27/2019 Adv Sock Opt
4/46
Netprog: Advanced Sockets Programming 4
Option types
Many socket options are Boolean flagsindicating whether some feature isenabled (1) or disabled (0).
Other options are associated with morecomplex types including int, timeval,
in_addr, sockaddr, etc.
7/27/2019 Adv Sock Opt
5/46
Netprog: Advanced Sockets Programming 5
Read-Only Socket Options
Some options are readable only (wecant set the value).
7/27/2019 Adv Sock Opt
6/46
Netprog: Advanced Sockets Programming 6
Setting and Getting option
valuesgetsockopt() gets the current value of
a socket option.
setsockopt() is used to set the valueof a socket option.
#include
7/27/2019 Adv Sock Opt
7/46
Netprog: Advanced Sockets Programming 7
int getsockopt( int sockfd,
int level,int optname,
void *opval,
socklen_t *optlen);
level specifies whether the option is ageneral option or a protocol specific
option (what level of code shouldinterpret the option).
getsockopt()
7/27/2019 Adv Sock Opt
8/46
Netprog: Advanced Sockets Programming 8
int setsockopt( int sockfd,
int level,
int optname,
const void *opval,
socklen_t optlen);
setsockopt()
7/27/2019 Adv Sock Opt
9/46
Netprog: Advanced Sockets Programming 9
General Options
Protocol independent options.
Handled by the generic socket system
code.
Some general options are supportedonly by specific types of sockets
(SOCK_DGRAM, SOCK_STREAM).
7/27/2019 Adv Sock Opt
10/46
Netprog: Advanced Sockets Programming 10
Some Generic Options
SO_BROADCAST
SO_DONTROUTE
SO_ERROR
SO_KEEPALIVE
SO_LINGER
SO_RCVBUF,SO_SNDBUF
SO_REUSEADDR
7/27/2019 Adv Sock Opt
11/46
Netprog: Advanced Sockets Programming 11
SO_BROADCAST
Boolean option: enables/disablessending of broadcast messages.
Underlying DL layer must support
broadcasting! Applies only to SOCK_DGRAM
sockets.
Prevents applications from inadvertentlysending broadcasts (OS looks for thisflag when broadcast address is
7/27/2019 Adv Sock Opt
12/46
Netprog: Advanced Sockets Programming 12
SO_DONTROUTE
Boolean option: enables bypassing ofnormal routing.
Used by routing daemons.
7/27/2019 Adv Sock Opt
13/46
Netprog: Advanced Sockets Programming 13
SO_ERROR
Integer value option.
The value is an error indicator value
(similar to errno). Readable (getable) only!
Reading (by calling
getsockopt())
clears any pending error.
7/27/2019 Adv Sock Opt
14/46
Netprog: Advanced Sockets Programming 14
SO_KEEPALIVE
Boolean option: enabled means thatSTREAM sockets should send a probeto peer if no data flow for a long time.
Used by TCP - allows a process todetermine whether peer process/hosthas crashed.
Consider what would happen to anopen telnet connection withoutkeepalive.
7/27/2019 Adv Sock Opt
15/46
Netprog: Advanced Sockets Programming 15
SO_LINGER
Value is of type:struct linger {
int l_onoff; /* 0 = off */
int l_linger; /* time in seconds */};
Used to control whether and how long acall to close will wait for pending ACKS.
connection-oriented sockets only.
7/27/2019 Adv Sock Opt
16/46
Netprog: Advanced Sockets Programming 16
SO_LINGERusage
By default, calling close() on a TCPsocket will return immediately.
The closing process has no way of
knowing whether or not the peerreceived all data.
Setting SO_LINGER means the closing
process can determine that the peermachine has received the data (but notthat the data has been read() !).
7/27/2019 Adv Sock Opt
17/46
Netprog: Advanced Sockets Programming 17
shutdown() vs
SO_LINGER
The book shows how you can useshutdown() to find out when the peerprocess has read all the sent data.
7/27/2019 Adv Sock Opt
18/46
Netprog: Advanced Sockets Programming 18
SO_RCVBUF
SO_SNDBUF Integer values options - change the
receive and send buffer sizes.
Can be used with STREAM andDGRAM sockets.
With TCP, this option effects the windowsize used for flow control - must beestablished before connection is made.
7/27/2019 Adv Sock Opt
19/46
Netprog: Advanced Sockets Programming 19
SO_REUSEADDR
Boolean option: enables binding to anaddress (port) that is already in use.
Used by servers that are transient -allows binding a passive socket to aport currently in use (with active
sockets) by other processes.
7/27/2019 Adv Sock Opt
20/46
Netprog: Advanced Sockets Programming 20
SO_REUSEADDR
Can be used to establish separateservers for the same service on different
interfaces (or different IP addresses onthe same interface).
Virtual Web Servers can work this way.
7/27/2019 Adv Sock Opt
21/46
Netprog: Advanced Sockets Programming 21
IP Options (IPv4)
IP_HDRINCL: used on raw IP socketswhen we want to build the IP header
ourselves. IP_TOS: allows us to set the Type-of-
service field in an IP header.
IP_TTL: allows us to set the Time-to-live field in an IP header.
7/27/2019 Adv Sock Opt
22/46
Netprog: Advanced Sockets Programming 22
TCP socket options
TCP_KEEPALIVE: set the idle timeused when SO_KEEPALIVE is enabled.
TCP_MAXSEG: set the maximumsegment size sent by a TCP socket.
7/27/2019 Adv Sock Opt
23/46
Netprog: Advanced Sockets Programming 23
another TCP socket option
TCP_NODELAY: can disable TCPsNagle algorithm that delays sending
small packets if there is unACKd datapending.
TCP_NODELAY also disables delayedACKS (TCP ACKs are cumulative).
7/27/2019 Adv Sock Opt
24/46
Netprog: Advanced Sockets Programming 24
This was just an overview
there are many details associated with the
options described. There are many options that havent been
described.
Our text is one of the best sources of
information about socket options.
Socket Options Summary
7/27/2019 Adv Sock Opt
25/46
Netprog: Advanced Sockets Programming 25
Posix Name/Adress Conversion
We've seen gethostbyname and
gethostbyaddr - these are protocol
dependent. Not part of sockets library.
Posix includes protocol independent
functions:getaddrinfo() getnameinfo()
7/27/2019 Adv Sock Opt
26/46
Netprog: Advanced Sockets Programming 26
getaddrinfo, getnameinfo
These functions provide name/addressconversions as part of the socketslibrary.
In the future it will be important to writecode that can run on many protocols(IPV4, IPV6).
7/27/2019 Adv Sock Opt
27/46
Netprog: Advanced Sockets Programming 27
Why getaddrinfo()?
Puts protocol dependence in library(where it belongs).
Same code can be used for manyprotocols (IPV4, IPV6)
re-entrant function - gethostbyname isnot!
Important to threaded applications.
7/27/2019 Adv Sock Opt
28/46
Netprog: Advanced Sockets Programming 28
getaddrinfo()
int getaddrinfo(const char *hostname,
const char *service,
const struct addrinfo* hints,struct addrinfo **result);
getaddrinfo() replaces bothgethostbyname() and getservbyname()
7/27/2019 Adv Sock Opt
29/46
Netprog: Advanced Sockets Programming 29
getaddrinfo() parameters
hostnameis a hostname or an address string(dotted decimal string for IP).
serviceis a service name or a decimal portnumber string.
7/27/2019 Adv Sock Opt
30/46
Netprog: Advanced Sockets Programming 30
struct addrinfo
struct addrinfo {int ai_flags;int ai_family;int ai_socktype;
int ai_protocol;size_t ai_addrlen;char *canonname;
struct sockaddr *ai_addr;struct addrinfo *ai_next;};
Link
edlist!
7/27/2019 Adv Sock Opt
31/46
Netprog: Advanced Sockets Programming 31
getaddrinfo() hints
hintsis an addrinfo * (can beNULL) that cancontain:
ai_flags (AI_PASSIVE ,AI_CANONNAME ) ai_family (AF_XXX)
ai_socktype (SOCK_XXX)
ai_protocol (IPPROTO_TCP, etc.)
7/27/2019 Adv Sock Opt
32/46
Netprog: Advanced Sockets Programming 32
getaddrinfo() result
resultis returned with the address of apointer to an addrinfo structure that is thehead of a linked list.
It is possible to get multiple structures: multiple addresses associated with the
hostname.
The service is provided for multiple sockettypes.
7/27/2019 Adv Sock Opt
33/46
Netprog: Advanced Sockets Programming 33
addrinfo usage
ai_flagsai_familyai_socktypeai_protocolai_addrlenai_canonnameai_addrai_next
Used in call tosocket()
Used in call tobind(), connect()
or sendto()
ai_flagsai_family
ai_socktypeai_protocolai_addrlenai_canonnameai_addrai_next
7/27/2019 Adv Sock Opt
34/46
Netprog: Advanced Sockets Programming 34
getnameinfo()
int getnameinfo(const struct sockaddr *sockaddr,socklen_t addrlenchar *host,
size_t hostlen,char *serv,size_t servlen,
int flags);
getnameinfo()looks up a hostname and aservice name given a sockaddr
7/27/2019 Adv Sock Opt
35/46
Netprog: Advanced Sockets Programming 35
Out-of-Band Date
Ever been on a date, gone to a danceclub and the band doesn't show up?
This is becoming a serious problem:
The number of Internet dating services is
growing exponentially.
The number of bands is not growing.
RFC 90210 proposes some short termsolutions (until the number of bands can beincreased).
7/27/2019 Adv Sock Opt
36/46
Netprog: Advanced Sockets Programming 36
Out-of-Band Data
TCP (and other transport layers) providea mechanism for delivery of "high priority"data ahead of "normal data".
We can almost think of this as 2 streams:
TCP PORT
A
TCP PORT
B
normaldata
specialdata
7/27/2019 Adv Sock Opt
37/46
Netprog: Advanced Sockets Programming 37
TCP OOB Data
TCP supports something like OOB datausing URGENT MODE (a bit is set in aTCP segment header).
A TCP segment header field containsan indication of the location of theurgent data in the stream (the bytenumber).
7/27/2019 Adv Sock Opt
38/46
Netprog: Advanced Sockets Programming 38
Sending OOB Data
send(sd,buff,1,MSG_OOB);
Use send() to put a single byte of urgentdata in a TCP stream.
The TCP layer adds some segment headerinfo to let the other end know there is
some OOB data.
7/27/2019 Adv Sock Opt
39/46
Netprog: Advanced Sockets Programming 39
Receiving OOB Data
The TCP layer generates a SIGURGsignal in the receiving process.
select() will tell you an exception
condition is present.
7/27/2019 Adv Sock Opt
40/46
7/27/2019 Adv Sock Opt
41/46
Netprog: Advanced Sockets Programming 41
So what?
OOB Data might be used:
a heartbeat between the client and server
to detect early failure (example in thebook).
A way to communicate an exceptionalcondition to a peer even when flow control
has stopped the sender.
7/27/2019 Adv Sock Opt
42/46
Netprog: Advanced Sockets Programming 42
Singles Driven IOU
Another problem with Internet Datingservices is the lack of single drivers in
many metropolitan areas. Neither participant can pick up the other.
Dating protocols degrade to involve onlinecommunication only.
Proxy drivers (running TAXI protocol) getoverloaded and refuse IOU packets.
7/27/2019 Adv Sock Opt
43/46
Netprog: Advanced Sockets Programming 43
Signal Driven I/O
We can tell the kernel to send us aSIGIO signal whenever something
happens to a socket descriptor.
The signal handler must determine what
conditions caused the signal and takeappropriate action.
7/27/2019 Adv Sock Opt
44/46
Netprog: Advanced Sockets Programming 44
Signal Driven UDP
SIGIO occurs whenever:
an incoming datagram arrives.
An asynchronous error occurs. Could be ICMP error (unreachable, invalid
address, etc).
Could allow process to handle othertasks and still watch for incoming UDPmessages.
7/27/2019 Adv Sock Opt
45/46
Netprog: Advanced Sockets Programming 45
Signal-Driven Example
Real life signal-driven UDP exampledescribed in the book:
NTP Network Time Protocol. Used to record timestamp of arrival of
UDP datagram.
7/27/2019 Adv Sock Opt
46/46
Netprog: Advanced Sockets Programming 46
Signal Driven TCP (very rare)
SIGIO occurs whenever: an incoming connection has completed.
Disconnect request initiated.
Disconnect request completed. Half a connection shutdown.
Data has arrived.
Data has been sent (indicating there is bufferspace)
asynchronous error