Outline F Socket basics F TCP sockets F Socket details F Socket options Computer Networks F Final notes Sockets Socket Basics Ports F An end-point for a IP network connection F Numbers (vary in BSD, Solaris): – what the application layer “plugs into” – 0-1023 “reserved”, must be root – programmer cares about Application Programming Interface (API) – 1024 - 5000 “ephemeral” F End point determined by two things: – however, many systems allow > 3977 ports u (50,000 is correct number) – Host address: IP address is Network Layer F /etc/services: – Port number: is Transport Layer ftp 21/tcp F Two end-points determine a connection: telnet 23/tcp socket pair finger 79/tcp – ex: 206.62.226.35,p21 + 198.69.10.2,p1500 snmp 161/udp – ex: 206.62.226.35,p21 + 198.69.10.2,p1499 Sockets and the OS Transport Layer F UDP: User Datagram Protocol User – no acknowledgements Socket – no retransmissions Operating System – out of order, duplicate possible (Transport Layer) – connectionless F TCP: Transmission Control Protocol F User sees “descriptor”, integer index – reliable (in order, all arrive, no duplicates) – like: FILE *, or file index – flow control – returned by socket() call (more later) – connection – duplex – (proj 2) 1 Socket Details Addresses and Sockets F Unix Network Programming, W. Richard Structure to hold address information Stevens, 2nd edition, ã1998, Prentice Hall F Functions pass address from user to OS – bind() – connect() F Socket address structure – sendto() F TCP client-server F Functions pass address from OS to user F Misc stuff – accept() – setsockopt(), getsockopt() – recvfrom() – fcntl() Server TCP Client-Server Socket Address Structure socket() “well-known” bind() struct in_addr { port in_addr_t s_addr; /* 32-bit IPv4 addresses */ listen() Client }; accept() struct sock_addr_in { socket() ) (Block until connection “Handshake” unit8_t sin_len; /* length of structure */ connect() sa_family_t sin_family; /* AF_INET */ Data (request) send() in_port_t sin_port; /* TCP/UDP Port num */ recv() struct in_addr sin_addr; /* IPv4 address */ Data (reply) send() char sin_zero[8]; /* unused */ recv() } End-of-File close() F Are also “generic” and “IPv6” socket structures recv() close() socket() bind() int socket(int family, int type, int protocol); int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Create a socket, giving access to transport layer service. Assign a local protocol address (“name”) to a socket. F family is one of F sockfd is socket descriptor from socket() – AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), F – AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) myaddr is a pointer to address struct with: F type is one of – port number and IP address – SOCK_STREAM (TCP), SOCK_DGRAM (UDP) – if port is 0, then host will pick ephemeral port – SOCK_RAW (for special IP packets, PING, etc. Must be root) u not usually for server (exception RPC port-map) u setuid bit (-rws--x--x root 1997 /sbin/ping*) – IP address != INADDR_ANY (multiple net cards) F protocol is 0 (used for some raw socket options) F addrlen is length of structure F upon success returns socket descriptor F – like file descriptor returns 0 if ok, -1 on error – -1 if failure – EADDRINUSE (“Address already in use”) 2 listen() accept() int accept(int sockfd, struct sockaddr cliaddr, int listen(int sockfd, int backlog); socklen_t *addrlen); Change socket state for TCP server. Return next completed connection. F sockfd is socket descriptor from socket() F sockfd is socket descriptor from socket() F backlog is maximum number of incomplete F cliaddr and addrlen return protocol address connections from client – historically 5 F returns brand new descriptor, created by OS – rarely above 15 on a even moderate web server! F if used with fork(), can create F Sockets default to active (for client) concurrent server (more later) – change to passive to OS will accept connection close() connect() int connect(int sockfd, const struct sockaddr int close(int sockfd); *servaddr, socklen_t addrlen); Close socket for use. Connect to server. F sockfd is socket descriptor from socket() F sockfd is socket descriptor from socket() F servaddr is a pointer to a structure with: F closes socket for reading/writing – port number and IP address – returns (doesn’t block) – must be specified (unlike bind()) – attempts to send any unsent data F addrlen is length of structure – socket option SO_LINGER F u block until data sent client doesn’t need bind() u or discard any remaining data – OS will pick ephemeral port – -1 if error F returns socket descriptor if ok, -1 on error Sending and Receiving Socket Options F setsockopt(), getsockopt() int recv(int sockfd, void *buff, F size_t mbytes, int flags); SO_LINGER int send(int sockfd, void *buff, – upon close, discard data or block until sent F size_t mbytes, int flags); SO_RCVBUF, SO_SNDBUF F Same as read() and write() but for flags – change buffer sizes – for TCP is “pipeline”, for UDP is “discard” – MSG_DONTWAIT (this send non-blocking) F – MSG_OOB (out of band data, 1 byte sent ahead) SO_RCVLOWAT, SO_SNDLOWAT – MSG_PEEK (look, but don’t remove) – how much data before “readable” via select() – MSG_WAITALL (don’t give me less than max) F SO_RCVTIMEO, SO_SNDTIMEO – MSG_DONTROUTE (bypass routing table) – timeouts 3 fcntl() Socket Options (TCP) F ‘File control’ but used for sockets, too F TCP_KEEPALIVE F Signal driven sockets – idle time before close (2 hours, default) F Set socket owner F TCP_MAXRT F Get socket owner – set timeout value F Set socket non-blocking F TCP_NODELAY flags = fcntl(sockfd, F_GETFL, 0); – disable Nagle Algorithm flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); F Beware not getting flags before setting! Concurrent TCP Server UDP Client-Server Server Text segment socket() sock = socket() Parent /* setup socket */ int sock; bind() “well-known” port Client while (1) { int newsock; newsock = accept(sock) recvfrom() socket() fork() (Block until receive datagram) Data (request) sendto() if child Child read(newsock) int sock; until exit int newsock; sendto() recvfrom() Data (reply) } close() - No “handshake” F Close sock in child, newsock in parent - No simultaneous close() F - Note, usually fork() for concurrent servers! Reference count for socket descriptor Called iterative server Sending and Receiving connect() with UDP int recvfrom(int sockfd, void *buff, size_t mbytes, int flags, struct sockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_t mbytes, int F Record address and port of peer flags, const struct sockaddr *to, socklen_t addrlen); – datagrams to/from others are not allowed F Same as recv() and send() but for addr – does not do three way handshake, or connection – recvfrom fills in address of where packet came – connect a misnomer, here. Should be from setpeername() – sento requires address of where sending packet to F Use send() instead of sendto() F Use recv() instead of recvfrom() F Can change connect (or unconnect) by repeating connect() call 4 Why use connected UDP? Why else about connected UDP? F Consider client: F Send two datagrams F Send two datagrams sendto() unconnected: connected: recvfrom() – connect the socket – connect the socket – NULL in recvfrom args could come from other – output first dgram – output first dgram – could fill in, or done in – unconnect the socket – output second dgram connect() – connect the socket F Asynchronous errors not returned – output second dgram – talk.udp reno // server down, unconncected – unconnect the socket – hey! // no response, error not returned to process vs. – talk.udp reno // server down, conncected – port unreachable // error returned Mcast Extensions to UDP Mcast is Group and Port Client Server join socket() Application Sendto Application socket() host = 224.0.1.1 setsockopt() port = 123 Frames port=123 sendto() UDP UDP recvfrom() IP IP host=224.0.1.1 Other pause clients play Data Link Data Link LAN - sendto() in server to multicast group - setsockopt() in client to join group to router IP_ADD_MEMBERSHIP sendto() and recvfrom() specify port and group Scope of IPv4 Addresses Scope TTL Addr node 0 link 1 224.0.0.0 to 224.0.0.255 site <32 239.255.0.0 to 239.255.255.255 org <32 239.192.0.0 to 239.195.255.255 global <255 224.0.1.0 to 238.255.255.255 F Use “link-local” addresses – only 255, but port numbers also unique F On garden, ernie and bert 5.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-