Lecture 19 Advanced Sockets Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.

Slides:



Advertisements
Similar presentations
Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Advertisements

Netprog Advanced Sockets Programming1 Advanced Sockets Programming Ref: Chapter 7,11,21,22.
I/O Models Satish Krishnan. I/O Models Blocking I/O Non-blocking I/O I/O Multiplexing Signal driven I/O Asynchronous I/O.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Netprog: daemons and inetd1 Daemons & inetd Refs: Chapter 13.
Socket Programming.
TDC561 Network Programming Camelia Zlatea, PhD Week 5: Client/Server Programming Aspects  Client side:
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Unix Network Programming Chapter 13: Daemon processes and the inetd superserver Jani Peusaari.
Socket Options Jari Kellokoski. Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable.
Daemon Processes and inetd Superserver
1 Advanced Name and Address Conversions getaddrinfo, getnameinfo, gai_strerror, freeaddrinfo host_serv, tcp_connect, tcp_listen, udp_client, udp_connect,
Computer Network Architecture and Programming
Socket option Getsockopt ou setsockopt fcntl ioctl.
UNIX Sockets COS 461 Precept 1.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
ECE 4110 – Internetwork Programming Client-Server Model.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Client-Side Network Programming
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Chapter 2 Applications and Layered Architectures Sockets.
Network Programming Eddie Aronovich mail:
Nezer J. Zaidenberg.  Advanced programming for the unix environment (chapters about processes)
More on Socket API. More on Socket I/O Functions  Scatter read and gather write.
Netprog: Advanced Sockets Programming1 Advanced Sockets Programming Ref: Chapter 7,11,21,22.
Advanced Sockets API-II Vinayak Jagtap
1 Socket Options Ref: Chapter 7. 2 Socket Options Various attributes that are used to determine the behavior of sockets.Various attributes that are used.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket options: a summary –Tcp options, ip options and general socket options can be examined and sometimes modified. getsockopt, setsockopt routines Some.
1 Daemons & inetd Refs: Chapter Daemons A daemon is a process that: –runs in the background –not associated with any terminal Unix systems typically.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Today’s topic: UDP Reliable communication over UDP.
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 12. Daemon Processes and inetd Superserver.
Chapter 11 Advanced Name and Address Conversion. Introduction gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: –a function providing protocol.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
CSE 333 – SECTION 8 Client-Side Network Programming.
Socket Option.
UNIX Sockets COS 461 Precept 1.
Class A Addresses The 1st bit of a class A address is 0
Name and Address Conversions Part I
Client-side Networking CSE 333 Spring 2018
Lecture 4 Socket Programming Issues
UDP Sockets Programming
Chapter 07. Socket Options.
Advanced Network Programming spring 2007
Advanced Sockets Programming
Chapter 11 Name and Address Conversions (Part II)
Daemons & inetd Refs: Chapter 12.
Client-side Networking CSE 333 Summer 2018
Client-side Networking CSE 333 Winter 2019
Socket options: a summary
Advanced UNIX programming
Presentation transcript:

Lecture 19 Advanced Sockets Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger

Advanced Sockets Programming  Socket Options  Posix name/address conversion  Out-of-Band Data  Signal Driven I/O  Deamons  inetd  It's important to know about some of these topics,  it might not be apparent how and when to use them  Details are in the book Advanced Sockets Programming 2

Socket Options  Various attributes that are used to determine the behavior of sockets.  Setting options tells the OS/Protocol Stack the behavior we want.  Support for  generic options (apply to all sockets) and  protocol specific options. Advanced Sockets Programming 3

Option types  Many socket options are Boolean flags indicating whether some feature is  enabled (1) or disabled (0)  Other options are associated with more complex types including  int, timeval, in_addr, sockaddr, etc.  Some options are readable only  we can’t set the value Advanced Sockets Programming 4

Setting and Getting option values getsockopt()  gets the current value of a socket option. setsockopt()  used to set the value of a socket option. #include Advanced Sockets Programming 5

getsockopt() int getsockopt( int sockfd, int level, int optname, void *opval, socklen_t *optlen);  level specifies whether the option is a general option or a protocol specific option  what level of code should interpret the option Advanced Sockets Programming 6

setsockopt() int setsockopt( int sockfd, int level, int optname, const void *opval, socklen_t optlen); Advanced Sockets Programming 7

General Options  Protocol independent options.  Handled by the generic socket system code.  Some general options are supported only by specific types of sockets  SOCK_DGRAM  SOCK_STREAM Advanced Sockets Programming 8

Some Generic Options SO_BROADCAST SO_DONTROUTE SO_ERROR SO_KEEPALIVE SO_LINGER SO_RCVBUF,SO_SNDBUF SO_REUSEADDR Advanced Sockets Programming 9

SO_BROADCAST  enables/disables sending of broadcast msgs  Boolean option  Underlying DL layer must support broadcasting!  Applies only to SOCK_DGRAM sockets.  Prevents applications from inadvertently sending broadcasts  OS looks for this flag when broadcast address is specified Advanced Sockets Programming 10

SO_DONTROUTE  Boolean option  Enables bypassing of normal routing.  Used by routing daemons. Advanced Sockets Programming 11

SO_ERROR  Integer value option.  The value is an error indicator value  similar to errno  Readable only!  by calling getsockopt()  Reading clears any pending error. Advanced Sockets Programming 12

SO_KEEPALIVE  Enabled means that STREAM sockets should send a probe to peer if no data flow for a “long time”  Boolean option  Used by TCP  Allows a process to determine whether peer process/host has crashed  Consider what would happen to an open telnet connection without keepalive Advanced Sockets Programming 13

SO_LINGER  Used to control whether and how long a call to close will wait for pending ACKS  connection-oriented sockets only  By default, calling close() on a TCP socket will return immediately.  The closing process has no way of knowing whether or not the peer received all data  Setting SO_LINGER means the closing process can determine that the peer machine has received the data  but not that the data has been read() Advanced Sockets Programming 14

SO_RCVBUF & SO_SNDBUF  Integer values options  Change the receive and send buffer sizes.  Can be used with STREAM and DGRAM sockets.  With TCP, this option effects the window size used for flow control  must be established before connection is made. Advanced Sockets Programming 15

SO_REUSEADDR  Enables binding to an address (port) that is already in use  Boolean option  Used by servers that are transient  allows binding a passive socket to a port currently in use (with active sockets) by other processes.  Can be used to establish separate servers for the same service on different interfaces  or different IP addresses on the same interface  Virtual Web Servers can work this way Advanced Sockets Programming 16

IP Options (IPv4)  IP_HDRINCL: used on raw IP sockets  when we want to build the IP header ourselves  IP_TOS: Sets the “Type-of-service” field in IP header.  IP_TTL: Sets the “Time-to-live” field in IP header. Advanced Sockets Programming 17

TCP socket options  TCP_KEEPALIVE: set the idle time used when SO_KEEPALIVE is enabled.  TCP_MAXSEG: set the maximum segment size sent by a TCP socket.  TCP_NODELAY: can disable TCP’s Nagle alg.  Nagle algorithm delays sending small packets if there is unACK’d data pending.  also disables delayed ACKS Advanced Sockets Programming 18

Socket Options Summary  This was just an overview  There are many details associated with the options described.  There are many options that haven’t been described.  Our text is one of the best sources of information about socket options. Advanced Sockets Programming 19

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() Advanced Sockets Programming 20

getaddrinfo, getnameinfo  These functions provide name/address conversions as part of the sockets library  It is important to write code that can run on many protocols  IPV4, IPV6  Getaddrinfo puts protocol dependence in library  Same code can be used for many protocols IPV4, IPV6  re-entrant function Important to threaded applications. gethostbyname is not! Advanced Sockets Programming 21

getaddrinfo() int getaddrinfo( const char *hostname, const char *service, const struct addrinfo* hints, struct addrinfo **result);  hostname is a hostname or an address string  dotted decimal string for IP  service is a service name or a decimal port number string Advanced Sockets Programming 22

getaddrinfo() hints  hints is an addrinfo * (can be NULL ) that can contain:  ai_flags ( AI_PASSIVE, AI_CANONNAME )  ai_family ( AF_XXX )  ai_socktype ( SOCK_XXX )  ai_protocol (IPPROTO_TCP, etc.) Advanced Sockets Programming 23

getaddrinfo() result  result is returned with the address of a pointer to an addrinfo structure that is the head of a linked list.  It is possible to get multiple structures:  multiple addresses associated with the hostname.  The service is provided for multiple socket types. Advanced Sockets Programming 24

addrinfo usage Advanced Sockets Programming 25 ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next socket() Used in call to socket() bind(), connect() Used in call to bind(), connect() or sendto() ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next

getnameinfo() int getnameinfo( const struct sockaddr *sockaddr, socklen_t addrlen char *host, size_t hostlen, char *serv, size_t servlen, int flags);  getnameinfo() looks up a hostname and a service name given a sockaddr Advanced Sockets Programming 26

Out-of-Band Data  TCP (and other transport layers) provide a mechanism for delivery of "high priority" data ahead of "normal data".  We can almost think of this as 2 streams: Advanced Sockets Programming 28 TCP PORT A TCP PORT B normal data special data

TCP OOB Data  TCP supports something like OOB data using URGENT MODE  a bit is set in a TCP segment header  A TCP segment header field contains an indication of the location of the urgent data in the stream  the byte number Advanced Sockets Programming 29

Sending OOB Data send(sd,buff,1,MSG_OOB);  Use send() to put a single byte of urgent data in a TCP stream.  The TCP layer adds some segment header info to let the other end know there is some OOB data. Advanced Sockets Programming 30

Receiving OOB Data  TCP layer generates a SIGURG signal in the receiving process.  select() will indicate an exception condition is present  Reading URG data  the data can be read using recv() with a MSG_OOB flag set  The data can be read inline and the receiving process can monitor the out-of-band-mark for the connection using sockatmark() Advanced Sockets Programming 31

So what?  OOB Data might be used:  a heartbeat between the client and server to detect early failure  A way to communicate an exceptional condition to a peer even when flow control has stopped the sender Advanced Sockets Programming 32

Signal Driven I/O  We can tell the kernel to send us a SIGIO signal whenever something happens to a socket descriptor.  The signal handler must determine what conditions caused the signal and take appropriate action. Advanced Sockets Programming 33

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 other tasks and still watch for incoming UDP messages.  Eg: NTP Network Time Protocol  Record timestamp of arrival of UDP datagram Advanced Sockets Programming 34

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 buffer space  asynchronous error Advanced Sockets Programming 35

daemons and inetd 37 Daemons  A daemon is a process that:  runs in the background  not associated with any terminal output doesn't end up in another session. terminal generated signals (^C) aren't received.  Unix systems typically have many daemon processes.  Most servers run as a daemon process

daemons and inetd 38 Common Daemons  Web server (httpd)  Mail server (sendmail)  SuperServer (inetd)  System logging (syslogd)  Print server (lpd)  router process (routed, gated)

daemons and inetd 39 Daemon Output  No terminal  must use something else: file system central logging facility  Syslog is often used  provides central repository for system logging.  syslogd daemon provides system logging services to "clients”  Simple API for "clients"  A library provided by O.S.

daemons and inetd 40 Centralized Administration  A system administrator can control logging functions by specifying:  where messages should go  what kinds of messages are important  what can be ignored  Examples  Sysadmin could set LOG_EMERG messages to be sent to the console  low priority messages from lpr could be thrown away  Medium priority message from the mail server could be saved in a file

daemons and inetd 41 syslogd UDP socket port 514 Unix domain socket /dev/log /dev/klog Filesystem /var/log/messages Remote syslogd Console

daemons and inetd 42 Syslog messages  A server that accepts messages.  Each message includes a number of fields  Message: (level,facility,string)  a level indicating the importance (8 levels) LOG_EMERG highest priority LOG_DEBUG lowest priority  a facility that indicates the type of process that sent the message: LOG_MAIL, LOG_AUTH, LOG_USER, LOG_KERN, LOG_LPR,...  A text string

daemons and inetd 43 /etc/syslog.conf  Syslogd reads a configuration file that specifies  how various messages should be handled  where they should go  The sysadmin controls all logged messages by editing this file.

daemons and inetd 44 Sending a message to syslogd  Standard programming interface provided by syslog () function: #include void syslog( int priority, const char *message,... );  Works like printf()

daemons and inetd 45 Syslog client/server  Clients send messages to local syslogd through a unix domain (datagram) socket  All the details are handled by syslog()  syslogd sends/receives messages to/from other hosts using UDP.

daemons and inetd 46 Daemon initialization  To force a process to run in the background,  just fork() and have the parent exit  There are a number of ways to disassociate a process from any controlling terminal  Call setsid() and then fork() again.  Should close all unnecessary descriptors  often including stdin, stdout, stderr.  Set up for using syslog  Call openlog()  Often change working directory.

daemons and inetd 47 Too many daemons?  There can be many servers running as daemons  and idle most of the time.  Much of the startup code is the same for these servers.  Most of the servers are asleep most of the time,  but use up space in the process table.

daemons and inetd 48  Most Unix systems provide a “SuperServer” that solves the problem:  executes the startup code required by a bunch of servers.  Waits for incoming requests destined for the same bunch of servers.  When a request arrives starts up the right server and gives it the request. SuperServer

daemons and inetd 49 inetd  The SuperServer is named inetd.  This single daemon creates multiple sockets and waits for (multiple) incoming requests.  inetd typically uses select to watch multiple sockets for input.  When a request arrives, inetd will fork and the child process handles the client.

daemons and inetd 50 inetd children  Child process closes all unnecessary sockets  The child dup ’s the client socket to descriptors 0, 1 and 2 ( stdin,stdout,stderr)  The child exec ’s the real server program, which handles the request and exits.  Servers started by inetd assume that the socket holding the request is already established  TCP servers started by inetd don’t call accept.

daemons and inetd 51 /etc/inetd.conf  inetd reads a configuration file that lists all the services it should handle.  inetd creates a socket for each listed service, and adds the socket to a fd_set given to select().  For each service, inetd needs to know:  the port number and transport protocol  wait/nowait flag.  login name the process should run as.  pathname of real server program.  command line arguments to server program

daemons and inetd 52 # comments start with # echo stream tcp nowait root internal echo dgram udp wait root internal chargen stream tcp nowait root internal chargen dgram udp wait root internal ftp stream tcp nowait root /usr/sbin/ftpd ftpd -l telnet stream tcp nowait root /usr/sbin/telnetd telnetd finger stream tcp nowait root /usr/sbin/fingerd fingerd # Authentication auth stream tcp nowait nobody /usr/sbin/in.identd in.identd -l -e -o # TFTP tftp dgram udp wait root /usr/sbin/tftpd tftpd -s /tftpboot example /etc/inetd.conf

daemons and inetd 53 wait/nowait  Specifying WAIT means that inetd should not look for new clients for the service until the child (the real server) has terminated.  TCP servers usually specify nowait  this means inetd can start multiple copies of the TCP server program providing concurrency!

daemons and inetd 54 UDP & wait/nowait  Most UDP services run with inetd told to wait until the child server has died.  What would happen if:  inetd did not wait for a UDP server to die.  inetd gets a time slice before the real server reads the request datagram?  Some UDP servers hang out for a while,  handling multiple clients before exiting.  inetd was told to wait  it ignores the socket until the UDP server exits

daemons and inetd 55 Servers  Servers that are expected to deal with frequent requests are typically not run from inetd: mail, web, NFS.  Many servers are written so that a command line option can be used to run the server from inetd.

daemons and inetd 56 xinetd  Some versions of Unix provide a service very similar to inetd called xinetd.  configuration scheme is different  basic idea (functionality) is the same…