Operating Systems Sockets ENCE 360.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
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.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Tutorial 8 Socket Programming
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
Socket Option.
UNIX Sockets COS 461 Precept 1.
Chapter4 Elementary TCP Socket
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
UDP Sockets Programming
Socket Programming in C
TCP Sockets Programming
TCP/IP Socket Programming in C
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Operating Systems Sockets ENCE 360

Outline Introduction Details Example code Socket options+

(TCP=Transport Control Protocol, IP=Internet Protocol) Socket Overview Socket - An end-point for connection to another process (remote or local) What application layer “plugs into” User sees descriptor - integer index/handle Like: file index from open() Returned by socket() call (more later) Programmer cares about Application Programming Interface (API)  similar to file I/O Application Sockets Protocol A Protocol B Protocol C Network (TCP=Transport Control Protocol, IP=Internet Protocol)

Connection Endpoints End point determined by two things: Host address: e.g., IP address Port number Two end-points determine connection  socket pair Client Port Connection Sever message agreed port any port socket Internet address = 138.37.88.249 Internet address = 138.37.94.248 other ports client server

Ports Each host has 65,536 ports 16-bit integer Some ports are reserved for specific apps (/etc/services) FTP 20,21 Telnet 23 HTTP 80 Ports below 1024 are reserved User level 1024+ Ports 1024-5000 ephemeral Assigned in outgoing connection Ports 5001+ services port 65535 server port client app app app app data port port port port Network Packet port data Packet

Two Main Network Transport Protocols Today UDP: User Datagram Protocol no acknowledgements no retransmissions out of order, duplicates possible Connectionless SOCK_DGRAM TCP: Transmission Control Protocol reliable (in order, all arrive, no duplicates) flow control connection-based SOCK_STREAM Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..) TCP ~95% of all flows and packets on Internet (What applications may use UDP?) More in a networks course!

Socket Descriptor Data Structure Descriptor Table Socket Data Structure Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726 1 2 3 received queue sent queue 4 Service is the Transport Protocol IP (Internet Protocol) - address of computer Port - specifies which process on computer

Outline Introduction (done) Details (next) Example code Socket options+ Unix Network Programming, W. Richard Stevens, 2nd edition, 1998, Prentice Hall Beej’s Guide to Network Programming, Brian Hall, 2015, self-published, http://beej.us/guide/bgnet/

Addresses and Sockets Structure to hold address information Functions pass info (e.g., address) from user to OS bind() connect() sendto() Functions pass info (e.g., address) from OS to user accept() recvfrom()

Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* length of structure */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* TCP/UDP port number */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8];/* unused */ Also “generic” and “IPv6” socket structures Length field makes it easier for OS to handle

TCP Client-Server Client Server socket() “well-known” bind() port listen() Client accept() socket() (Block until connection) “Handshake” connect() Data (request) send() recv() Data (reply) send() recv() End-of-File close() recv() close()

socket() family is one of type is one of int socket(int family, int type, int protocol); Create socket, giving access to transport layer service family is one of AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), AF_ROUTE (access to routing tables), AF_KEY (for encryption) type is one of SOCK_STREAM (TCP), SOCK_DGRAM (UDP) SOCK_RAW (for special IP packets, PING, etc. Must be root) setuid bit (-rwsr-xr-x root 2014 /sbin/ping*) protocol is 0 (used for some raw socket options) upon success returns socket descriptor Integer, like file descriptor  index used internally Return -1 if failure

bind() sockfd is socket descriptor from socket() int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign local protocol address (“name”) to socket sockfd is socket descriptor from socket() myaddr is pointer to address struct with: port number and IP address if port is 0, then host will pick ephemeral port not usually for server (exception RPC port-map) IP address == INADDR_ANY (unless multiple nics) addrlen is length of structure returns 0 if ok, -1 on error EADDRINUSE (“Address already in use”)

Change socket state (to passive) for TCP server listen() int listen(int sockfd, int backlog); Change socket state (to passive) for TCP server sockfd is socket descriptor from socket() backlog is maximum number of incomplete connections historically 5 rarely above 15 even on moderately busy Web server! sockets default to active (for client) change to passive so OS will accept connection

accept() Return next completed connection blocking call (by default) int accept(int sockfd, struct sockaddr * cliaddr, socklen_t *addrlen); Return next completed connection blocking call (by default) sockfd is socket descriptor from socket() cliaddr and addrlen return protocol address from client returns brand new descriptor, created by OS note, if create new process or thread, can create concurrent server

close() Close socket for use int close(int sockfd); sockfd is socket descriptor from socket() closes socket for reading/writing returns (doesn’t block) attempts to send any unsent data socket option SO_LINGER block until data sent or discard any remaining data returns -1 if error

TCP Client-Server Client Server socket() “well-known” bind() port listen() Client accept() socket() (Block until connection) “Handshake” connect() Data (request) send() recv() Data (reply) send() recv() End-of-File close() recv() close()

connect() Connect to server int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server sockfd is socket descriptor from socket() servaddr is pointer to structure with: port number and IP address must be specified (unlike bind()) addrlen is length of structure client doesn’t need bind() OS will pick ephemeral port returns socket descriptor if ok, -1 on error

Sending and Receiving Same as read() and write() but with flags int recv(int sockfd, void *buff, size_t mbytes, int flags); int send(int sockfd, void *buff, size_t mbytes, int flags); Same as read() and write() but with flags MSG_DONTWAIT (this send non-blocking) MSG_OOB (out of band data, 1 byte sent ahead) MSG_PEEK (look, but don’t remove) MSG_WAITALL (don’t return less than mbytes) MSG_DONTROUTE (bypass routing table)

UDP Client-Server Server Client socket() bind() recvfrom() socket() “well-known” port bind() Client recvfrom() socket() (Block until receive datagram) Data (request) sendto() sendto() recvfrom() Data (reply) close() - No “connection”, no “handshake” - No simultaneous close

Sending and Receiving Same as recv() and send() but with addr 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 flags, const struct sockaddr *to, socklen_t addrlen); Same as recv() and send() but with addr recvfrom fills in address of where packet came from sendto requires address of where sending packet to

Can connect() with UDP Record address and port of peer Datagrams to/from others are not allowed Does not do three way handshake, or connection So, “connect” a misnomer, here. Should be setpeername() Use send() instead of sendto() Use recv() instead of recvfrom() Can change connect or unconnect by repeating connect() call (Can do similar with bind() on receiver)

Outline Introduction (done) Details (done) Example code (next) Socket options+

Example Code Server Client See: “listen-tcp.c” “talk-tcp.c” 1 3 2 4 % listen-tcp listen-tcp - server to accept TCP connections usage: listen-tcp <port> <port> - port to listen on % listen-tcp 7500 Listen activating. Trying to create socket at port 7500... Socket ready to go! Accepting connections.... received: 'Hello, world!' received: 'Networking is awesome!' server exiting 1 % talk-tcp talk-tcp - client to try TCP connection to server usage: talk-tcp <host> <port> <host> - Internet name of server host <port> - port % talk-tcp localhost 7500 Talk activated. Trying to connect to server localhost at port 7500... Looking up localhost... Found it. Setting port connection to 7500... Done. Creating socket... Created. Trying connection to server... Connection established! Type in messages to send to server. Hello, world! sending: 'Hello, world!' Networking is awesome! sending: 'Networking is awesome!' 3 2 4 See: “listen-tcp.c” “talk-tcp.c”

Outline Introduction (done) Details (done) Example code (done) Socket options+ (next)

Socket Options (General) setsockopt(), getsockopt() SO_LINGER Upon close, discard data or block until sent SO_RCVBUF, SO_SNDBUF Change buffer sizes For TCP is “pipeline”, for UDP is “discard” SO_RCVLOWAT, SO_SNDLOWAT How much data before “readable” via select() SO_RCVTIMEO, SO_SNDTIMEO Timeouts

Socket Options (TCP) TCP_KEEPALIVE TCP_MAXRT TCP_NODELAY Idle time before close (2 hours, default) TCP_MAXRT Set timeout value TCP_NODELAY Disable Nagle’s Algorithm Won’t buffer data for larger chunk, but sends immediately

fcntl() ‘File control’ - manipulate file descriptor So, used for sockets, too Get/Set socket owner Get/Set socket lock Set socket non-blocking flags = fcntl(sockfd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); Beware not getting flags before setting!

Connecting to the Internet If one computer, connect to server?

Internet Connecting to You If one computer, connect to server? Server connect to your computer?

Firewall to the Rescue! Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”)

Firewall Too Restrictive Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”) What if want connection? (Examples?)

Firewall – Port Forwarding Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”) What if want connection? (Examples?) Open port. Aka: port forwarding

Connecting to the Internet, Take 2 Single address, multiple computers? 104.45.147.81 (World) 202.36.179.10 ? (Home)

Connecting to the Internet, Take 2 Single address, multiple computers? Assign one box to route packets to all But how to differentiate incoming? 104.45.147.81 (World) 202.36.179.10 202.36.179.10 192.168.1.1 ? (Home) 192.168.1.2 192.168.1.4 192.168.1.3

Network Address Translation (NAT) Single address, multiple computers? Assign one box to route packets to all But how to differentiate incoming? Based on port Setup when connecting 104.45.147.81 (World) 202.36.179.10 80 202.36.179.10 192.168.1.1 192.168.1.2 9991 (Home) 192.168.1.2 192.168.1.4 Network Address Translation 192.168.1.3

NAT Tables Table maps local IP + port to Internet IP + port https://microchip.wdfiles.com/local--files/tcpip:nat-translation-table/nat_table.JPG Table maps local IP + port to Internet IP + port Setup when connected by client (punch out) Pre-setup by administrator when connecting in (local service/server)

Outline Introduction (done) Details (done) Example code (done) Socket options+ (done)