Network Programming with Sockets ECE 255 Douglas C. Schmidt Http

Network Programming with Sockets ECE 255 Douglas C. Schmidt Http

Intro duction The So cket Interface cont'd So ckets are a lo cal and remote OS IPC abstraction de ned in 4.2 BSD UNIX and beyond Originally, so ckets were implemented as a set of system calls { Now part of most major op erating systems, in- cluding Windows and Win32 systems { For eciency, they were tightly-coupled with the BSD networking architecture in the OS kernel So ckets were originally develop ed for TCP/IP proto cols Recent versions of so ckets are implemented { Later generalized to include other proto col fam- as a library of functions in user-space ilies { e.g., SVR4 UNIX and Win32 e.g., Novell IPX/SPX, TP4, ATM User-space implementations improve ex- So cket routine control communication be- tween pro cesses and proto cols ibility and portability at the exp ense of some p erformance { Also provide bu ering between synchronous ap- plication domain and the asynchronous kernel domain 2 4 The So cket Interface cont'd work Programming with Net DISTRIBUTEDAPPLICATION DISTRIBUTEDAPPLICATION DISTRIBUTEDAPPLICATION PROCESS 3592 PROCESS 4183 PROCESS 8729 APPLICATION 1 APPLICATION 2 APPLICATION 3 So ckets USER SPACE 255 ECE BSDSOCKET SOCKET SSYSTEMYSTEM V APIAPI TLITLI APIAPI OS KERNEL KERNEL C. Schmidt Douglas PROTOCOL SERVICES SPACE (TCP/IP, OSI, ETC.) http://www.ece.uci.edu/schmidt/ NETWORK INTERFACE [email protected] University of California, Irvine An application pro cess using TCP/IP pro- to cols resides in its own address space 1 3 Communication Domains cont'd Internet domain or TCP/IP PF INET Communication Domains cont'd { Communicate across network or on same ma- chine uses \dotted-decimal Internet addresses" e.g., "128.195.1.1 @ port 21" UNIX domain PF UNIX { Communicate only with a pro cess on the same { General-purp ose addressing, but existing ver- machine sions don't scale well due to xed-sized ad- dressing Uses UNIX lenames for rendezvous between This is xed in IPv6 client and server pro cesses { e.g., TCP, UDP, IP, ftp, rlogin, telnet { Really a form of intra-machine IPC, similar to SVR4 STREAM pip es STREAM and Supp orts b oth reliable SOCK Xerox XNS later evolved into Novell IPX unreliable SOCK DGRAM lo cal IPC { SPP, PEX, IDP Used for lo cal X-windows trac::: ISO OSI { e.g., TP4-TP1, CLNS, CONS 8 6 Communication Domains Communication domains are a key struc- turing concept in the BSD networking ar- chitecture Communication Domains cont'd { e.g., Internet domain and UNIX domain UNIX cont'd UNIX domain PF Domains sp ecify: { 4.3 BSD and SunOS 4.1.x implement pip es via \lob otomized" connection-oriented Unix domain 1. The scop e over which two pro cesses may com- so cket proto col implementations municate { SVR4-based UNIX systems use the STREAMS { e.g., lo cal only vs. lo cal/remote facility 2. How names and addresses are formed and in- In general, UNIX domain so ckets have b een terpreted in subsequent so cket calls subsumed by STREAM-pip es and connld in SVR4 { e.g., pathnames vs. IP/p ort numb ers { Not surprisingly, Win32 do es not supp ort UNIX domain so ckets Most so cket implementations provide sev- eral domains represented as \proto col fam- ilies" { The socket interface is used for all these pro- to col family domains 7 5 Stream So cket Typ e of service Reliably-delivered Message So cket { Reliable i.e., sequenced, non-duplicated, non- corrupted bi-directional delivery of byte-stream data Typ e of service { Reliable datagram Metaphor { A \network pip e" Metaphor { Sending a registered letter e.g., INET, SOCK STREAM, 0; int s = so cket PF e.g., /* Note, s is an internal id:::*/ int s = so cket PF NS, SOCK RDM, 0; Note, we'll use int as the so cket typ e, al- though Win32 uses SOCKET:: : 12 10 So cket Typ es Datagram So cket There are ve Typ es of So ckets 1. Stream So cket Typ e of service { Unreliable, unsequenced datagram 2. Datagram So cket 3. Reliably-delivered Message So cket Metaphor 4. Sequenced Packet Stream So cket { Sending a letter 5. Raw So ckets e.g., SOCK STREAM and SOCK DGRAM are INET, SOCK DGRAM, 0; int s = so cket PF the most common typ es of so ckets::: 9 11 So cket Addresses cont'd Raw So ckets General Format Typ e of service { Allows user-de ned proto cols that interface with struct so ckaddr f u short sa family; char sa data[14]; g; IP UNIX Domain { Requires ro ot access struct so ckaddr un f short sun family; char sun path[108]; g; Metaphor { Playing with an erector set:::;- Internet Domain addr f unsigned long s addr; g; struct in struct so ckaddr in f e.g., short sin family; u short sin port; struct in addr sin addr; char sin zero[8]; INET, SOCK RAW, 0; int s = so cket PF g; 14 16 So cket Addresses UNIX supp orts multiple communication do- Sequenced Packet Stream So cket mains, proto col families, and address fam- ilies Typ e of service { The so cket API provides a single address in- terface for all these families { Reliable, bi-directional delivery of record-oriented data The typ e of sockaddr structure used with Metaphor accept, bind, connect, sendto, and recvfrom di ers according to the domain UNIX vs. { Record-oriented TCP e.g., TP4 and XTP Internet vs. XNS e.g., The addressing API has a somewhat con- fusing and error-prone design int s = so cket PF NS, SOCK SEQPACKET, 0; { Motivation was to save space for the \common case":: : 15 13 So cket Op erations Connection-oriented So cket Lo cal context management Usage int so cket int domain, int typ e, int proto col; bind int fd, struct so ckaddr *, int len; int 2: ACTIVE 1: PASSIVE listen int fd, int backlog; int ROLE ROLE close int fd; int socket() socket() getp eername int fd, struct so ckaddr *, int *len; int bind() (optional) bind() getso ckname int fd, struct so ckaddr *, int *len; int connect() listen() accept() send()/recv() Connection establishment and termination send()/recv() close() 3: SERVICE PROCESSING close() int connect int fd, struct so ckaddr *, int len; int accept int fd, struct so ckaddr *, int *len; int shutdown int fd, int how; CLIENT SERVER Option management NETWORK int io ctl int fd, int request, char *arg; int fcntl int fd, int cmd, int arg; int getso ckopt int, int, int, char *, int *; int setso ckopt int, int, int, char *, int; 18 20 So cket Op erations So cket Addresses cont'd Data transfer General usage for Internet-domain service: int read int fd, void *buf, int len; struct so ckaddr in addr; int write int fd, void *buf, int len; int send int fd, void *buf, int len, int ags; memset &addr, 0, sizeof addr; int recv int fd, void *buf, int len, int * ags; addr.sin family = AF INET; int readv int fd, struct iovec [], int len; addr.sin port = htons p ort numb er; int writev int fd, struct iovec [], int len; addr.sin addr.s addr = htonl INADDR ANY; int sendto int fd, void *buf, int len, int ags, struct so ckaddr *, int len; int recvfrom int fd, void *buf, int len, int ags, if bind sd, struct so ckaddr * &addr, sizeof addr struct so ckaddr *, int *len; == 1 int sendmsg int fd, struct msghdr *msg, int ags; :::; int recvmsg int fd, struct msghdr *msg, int ags; Note the use of a cast Event demultiplexing { In C++, this whole mess can be cleaned-up via inheritance and dynamic binding! int select int maxfdp1, fd set *rdfds, fd set *wrfds, fd set *exfds, struct timeval *; 17 19 Client and Server Op erations Client and Server Op erations cont'd close socket { Close a so cket { Creates and op ens a so cket and returns a de- { int close int s; scriptor Note, there are subtle semantics related to { int s = so cket int domain, int typ e, int pro- \grace termination:::" of proto cols to col; domain ! PF UNIX, PF INET shutdown { Shutdown part or all of full-duplex connection typ e of service ! SOCK STREAM, SOCK DGRAM { int shutdown int s, int how; proto col ! generally 0, but could be TCP, how is 0, then further receives will be disal- VMTP, NETBLT, XTP lowed how is 1, then further sends will be disal- lowed Note, this call only lls in the rst part of how is 2, then further sends and receives will the 5-tuple asso ciation be disallowed { Note, shutdown do es not close the descriptor::: 24 22 Client and Server Op erations cont'd Connectionless So cket Usage bind Asso ciates a lo cal address e.g., an IP address, 2: ACTIVE { family, and port numb er to an un- ROLE 1: PASSIVE address so cket socket() ROLE named socket() bind() (optional) int bind int s, struct so ckaddr *addr, int ad- bind() { sendto()/ drlen; recvfrom() sendto()/ addr ! lo cal address e.g., p oints to an In- recvfrom() addr or a UNIX domain addr close() 3: SERVICE ternet PROCESSING close() addrlen ! length of address { Note CLIENT SERVER bind is not necessary for clients which im- NETWORK plicitly allo cate transient port numb ers ANY is a wildcard for The address INADDR any server host/network interface Always \zero-out" the address structure be- fore using it::: 21 23 Typical Server Op erations accept Typical Client Op erations { Returns a unique descriptor to the next avail- able completed connection from the connec- tion queue connect { int accept int s, struct so ckaddr *addr, int *addrlenptr; { Sp ecify foreign/remote destination address e.g., IP/p ort numb ers and joins two so ckets for addr ! address of remote server I/O: addrlenptr ! ptr to length of address { int connect int s, struct so ckaddr *addr, int addrlen; Returns new so cket descriptor sp ecifying the addr ! address of remote client full asso ciation addrlen ! length of address { Notes: 1.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us