Advanced Sockets Programming

Slides:



Advertisements
Similar presentations
Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
Advertisements

Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
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.
Multicasting - Part II© 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.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Lecture 19 Advanced Sockets Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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.
Socket Options Jari Kellokoski. Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable.
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
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Socket option Getsockopt ou setsockopt fcntl ioctl.
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.
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.
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:
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.
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.
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.
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,
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Chapter 11 Advanced Name and Address Conversion. Introduction gethostbyname, gethostbyaddr: protocol dependent getaddrinfo: –a function providing protocol.
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 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.
IPv4 socket option Level => IPPROTO_IP
Socket Option.
Operating Systems Sockets ENCE 360.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
Advanced Computer Networks
Transport Layer.
CS 105 “Tour of the Black Holes of Computing”
Class A Addresses The 1st bit of a class A address is 0
Week 13 - Friday CS222.
Socket Programming in C
Name and Address Conversions Part I
Client-side Networking CSE 333 Spring 2018
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
UDP Sockets Programming
Chapter 07. Socket Options.
Socket Programming in C
Advanced Network Programming spring 2007
Chapter 11 Name and Address Conversions (Part II)
Client-side Networking CSE 333 Summer 2018
Client-side Networking CSE 333 Autumn 2018
Client-side Networking CSE 333 Winter 2019
Socket options: a summary
Transport Layer 9/22/2019.
Advanced UNIX programming
Client-side Networking CSE 333 Spring 2019
Presentation transcript:

Advanced Sockets Programming Ref: Chapter 7,11,21,22

 Socket Options.  Posix name/address conversion  Out-of-Band Data  Socket Options  Posix name/address conversion  Out-of-Band Data  Signal Driven I/O It's important to know about some of these topics, although it might not be apparent how and when to use them. Details are in the book - we are just trying to get some idea of what can be done.

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.

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).

Setting and Getting option values getsockopt() gets the current value of a socket option. setsockopt() is used to set the value of a socket option.

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).

int setsockopt( int sockfd, int level, int optname, const 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).

General Options Protocol independent options. Handled by the generic socket system code. Some options are supported only by specific types of sockets (SOCK_DGRAM, SOCK_STREAM).

Some Generic Options SO_BROADCAST SO_DONTROUTE SO_ERROR SO_KEEPALIVE SO_LINGER SO_RCVBUF,SO_SNDBUF SO_REUSEADDR

SO_BROADCAST Boolean option: enables/disables sending of broadcast messages. 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).

SO_DONTROUTE Boolean option: enables bypassing of normal routing. Used by routing daemons.

SO_ERROR Integer value option. Value is an error indicator value as used by errno. Readable (get’able) only! Reading (by calling getsockopt()) clears any pending error.

SO_KEEPALIVE Boolen option: enabled means that STREAM sockets should send a probe to peer if no data flow for a “long time”. 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.

SO_LINGER Value is: struct linger { int l_onoff; /* 0 = off */ int l_linger; /* time in seconds */ }; Used to control whether and how long a call to close will wait for pending ACKS. connection-oriented sockets only.

SO_LINGER 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() !).

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.

SO_REUSEADDR Boolean option: enables binding to an address (port) that is already in use. 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)

IP Options (IPv4) IP_HDRINCL: used on raw IP sockets when 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.

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 algorithm that delays sending small packets if there is unACK’d data pending.

This was just an overview there are many details associated with the options described. There are many options that haven’t been described.

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()

getaddrinfo, getnameinfo These functions provide name/address conversions as part of the sockets library. In the future it will be important to write code that can run on many protocols (IPV4, IPV6), but for now these functions are not widely available. It's worth seeing how they work even though we probably can't use them yet!

getaddrinfo() Puts protocol dependence in library (where it belongs). Same code can be used for many protocols, we will see examples when we talk about IPV6 re-entrant function - gethostbyname is not! Important to threaded applications.

getaddrinfo() int getaddrinfo( const char *hostname, const char *service, const struct addrinfo* hints, struct addrinfo **result); getaddrinfo() replaces both gethostbyname() and getservbyname()

getaddrinfo() Hostname is a hostname or an address string (dotted decimal string for IP). Service is a service name or a decimal port number string.

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; }; Linked list!

getaddrinfo() 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.)

getaddrinfo It is possible to get multiple structures: 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.

addrinfo usage Used in call to socket() ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next Used in call to bind(), connect() or sendto() 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

Out-of-Band Date Ever been on a date, gone to a dance club 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 term solutions (until the number of bands can be increased).

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: normal data TCP PORT A TCP PORT B special data

TCP OOB Data TCP supports something like OOB data using URGENT MODE (a bit is send 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). The details are not important to us (we just want to see how to use this in our program).

send(sd,buff,1,MSG_OOB); Sending OOB Data send(sd,buff,1,MSG_OOB); Can 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. The TCP layer on the receiving end treats the data marked as urgent in a special way.

Receiving OOB Data The TCP layer generates a SIGURG signal in the receiving process. Select() will tell you an exception condition is present. Depending on how things are set up: the data can be read using recv() with a MSG_OOB flag set. The data can be read inline and the receiving process can moniter the out-of-band-mark for the connection (using sockatmark())

So what? OOB Data might be used: a heartbeat between the client and server to detect early failure (example in the book). A way to communicate an exceptional condition to a peer even when flow control has stopped the sender.

Singles Driven IOU Another problem with Internet Dating services is the lack of single drivers in many metropolitan areas. Neither participant can pick up the other. Dating protocols degrade to involve online communication only. Proxy drivers (running TAXI protocol) get overloaded and refuse IOU packets.

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

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.

Signal Driven TCP 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