Operating Systems II Unit OS A: Networking A.2. Windows Sockets - - PowerPoint PPT Presentation

operating systems ii unit os a networking a 2 windows
SMART_READER_LITE
LIVE PREVIEW

Operating Systems II Unit OS A: Networking A.2. Windows Sockets - - PowerPoint PPT Presentation

Operating Systems II Unit OS A: Networking A.2. Windows Sockets Programming Prof. Dr. Andreas Polze, Andreas Grapentin, Bernhard Rabe Roadmap for Sec.on A.2 General Concepts - Berkeley Sockets


slide-1
SLIDE 1

Operating Systems II Unit OS A: Networking A.2. Windows Sockets Programming

  • Prof. Dr. Andreas Polze,

Andreas Grapentin, Bernhard Rabe

slide-2
SLIDE 2

2 ¡

Roadmap ¡for ¡Sec.on ¡A.2 ¡

¡

  • General ¡Concepts ¡-­‑ ¡Berkeley ¡Sockets ¡
  • Crea.ng ¡a ¡socket ¡
  • Binding ¡an ¡address ¡
  • Accep.ng ¡connec.ons ¡
  • Exchanging ¡data ¡
  • Closing ¡a ¡connec.on ¡
  • Managing ¡mul.ple ¡connec.ons ¡with ¡select() ¡
slide-3
SLIDE 3

3 ¡

Winsock ¡Features ¡

  • Support ¡for ¡scaHer-­‑gather ¡and ¡asynchronous ¡applica.on ¡I/O ¡
  • Quality ¡of ¡service ¡(QoS) ¡conven.ons ¡so ¡that ¡applica.ons ¡can ¡nego.ate ¡

latency ¡and ¡bandwidth ¡requirements ¡when ¡the ¡underlying ¡network ¡ supports ¡QoS ¡

  • Extensibility ¡so ¡that ¡Winsock ¡can ¡be ¡used ¡with ¡protocols ¡other ¡than ¡those ¡

Windows ¡requires ¡it ¡to ¡support ¡

  • Support ¡for ¡integrated ¡namespaces ¡other ¡than ¡those ¡defined ¡by ¡a ¡protocol ¡

an ¡applica.on ¡is ¡using ¡with ¡Winsock. ¡A ¡server ¡can ¡publish ¡its ¡name ¡in ¡ Ac.ve ¡Directory, ¡for ¡example, ¡and ¡using ¡namespace ¡extensions, ¡a ¡client ¡ can ¡look ¡up ¡the ¡server's ¡address ¡in ¡Ac.ve ¡Directory ¡

  • Support ¡for ¡mul.point ¡messages ¡where ¡messages ¡transmit ¡to ¡mul.ple ¡

receivers ¡simultaneously ¡

slide-4
SLIDE 4

4 ¡

Windows ¡Socket ¡Programming ¡

Berkeley ¡Socket ¡programs ¡will ¡port ¡to ¡Window ¡Sockets ¡ Excep.ons: ¡

  • Call ¡WSAStartup() ¡to ¡ini.alize ¡Windows ¡Socket ¡DLL ¡
  • Use ¡ioctlsocket() ¡(non-­‑portable) ¡to ¡configure ¡the ¡socket ¡
  • _read() ¡and ¡_write() ¡can ¡be ¡used ¡on ¡sockets, ¡but ¡only ¡aZer ¡conver.ng ¡

the ¡socket ¡descriptor ¡to ¡a ¡file ¡handle ¡via ¡_open_os6andle() ¡

  • Use ¡closesocket() ¡(non-­‑portable) ¡rather ¡than ¡close ¡to ¡close ¡a ¡socket ¡
  • Call ¡WSACleanup() ¡to ¡shut ¡down ¡the ¡DLL ¡
slide-5
SLIDE 5

5 ¡

Berkeley ¡4.3 ¡UNIX ¡Sockets ¡– ¡ connec.on-­‑oriented ¡

Server ¡

socket() ¡ bind() ¡ listen() ¡ accept() ¡ read() ¡ write() ¡ socket() ¡

Client ¡

connect() ¡ write() ¡ read() ¡ connec.on ¡establishment ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡connec.on ¡ from ¡client ¡

slide-6
SLIDE 6

6 ¡

Berkeley ¡4.3 ¡UNIX ¡Sockets ¡-­‑ ¡ connec.onless ¡

Server ¡

socket() ¡ bind() ¡ recvfrom() ¡ sendto() ¡ socket() ¡

Client ¡

sendto() ¡ recvfrom() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡data ¡ received ¡from ¡client ¡

slide-7
SLIDE 7

7 ¡

Unix ¡SYS ¡V.3 ¡Transport ¡Layer ¡Interface ¡ – ¡connec.on-­‑oriented ¡

Server ¡

t_open() ¡ t_bind() ¡ t_listen() ¡ t_rcv() ¡ t_snd() ¡ t_open() ¡

Client ¡

t_connect() ¡ t_snd() ¡ t_rcv() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡connec.on ¡ from ¡client ¡ t_alloc() ¡ t_bind() ¡ t_alloc() ¡ t_accept() ¡ connec.on ¡establishment ¡

slide-8
SLIDE 8

8 ¡

Unix ¡SYS ¡V.3 ¡Transport ¡Layer ¡Interface ¡

  • ­‑ ¡connec.onless ¡

Server ¡

t_open() ¡ t_bind() ¡ t_rcvudata() ¡ t_sndudata() ¡ t_open() ¡

Client ¡

t_sndudata() ¡ t_rcvudata() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡data ¡ received ¡from ¡client ¡ t_alloc() ¡ t_bind() ¡ t_alloc() ¡

slide-9
SLIDE 9

9 ¡

Create ¡a ¡socket ¡

  • af: ¡An ¡address ¡format ¡specifica.on. ¡The ¡only ¡format ¡currently ¡

supported ¡is ¡AF_INET, ¡which ¡is ¡the ¡ARPA ¡Internet ¡address ¡

  • format. ¡ ¡
  • type: ¡A ¡type ¡specifica.on ¡for ¡the ¡new ¡socket. ¡ ¡
  • protocol: ¡A ¡par.cular ¡protocol ¡to ¡be ¡used ¡with ¡the ¡socket, ¡or ¡

0 ¡if ¡the ¡caller ¡does ¡not ¡wish ¡to ¡specify ¡a ¡protocol. ¡ ¡

#include ¡<winsock.h> ¡ ¡ SOCKET ¡socket ¡( ¡ ¡ ¡int ¡af, ¡int ¡type, ¡int ¡protocol ¡); ¡

slide-10
SLIDE 10

10 ¡

Accept ¡a ¡connec.on ¡on ¡a ¡socket ¡

  • s: ¡ ¡A ¡descriptor ¡iden.fying ¡a ¡socket ¡which ¡is ¡listening ¡for ¡connec.ons ¡aZer ¡

a ¡listen(). ¡ ¡

  • addr: ¡ ¡An ¡op.onal ¡pointer ¡to ¡a ¡buffer ¡which ¡receives ¡the ¡address ¡of ¡the ¡

connec.ng ¡en.ty, ¡as ¡known ¡to ¡the ¡communica.ons ¡layer. ¡The ¡exact ¡ format ¡of ¡the ¡addr ¡argument ¡is ¡determined ¡by ¡the ¡address ¡family ¡ established ¡when ¡the ¡socket ¡was ¡created. ¡ ¡

  • addrlen: ¡An ¡op.onal ¡pointer ¡to ¡an ¡integer ¡which ¡contains ¡the ¡length ¡of ¡

the ¡address ¡addr. ¡ ¡

#include ¡<winsock.h> ¡ ¡ SOCKET ¡accept ¡( ¡ ¡ ¡SOCKET ¡s, ¡struct ¡sockaddr ¡FAR ¡* ¡addr, ¡ ¡ ¡int ¡FAR ¡* ¡addrlen ¡); ¡

slide-11
SLIDE 11

11 ¡

struct ¡sockaddr ¡

  • From ¡winsock.h ¡(Windows) ¡or ¡/usr/include/

sys/socket.h ¡(UNIX) ¡ ¡

/* * Structure used by kernel to store most * addresses. */ struct sockaddr { u_char sa_len; /* total length */ u_char sa_family; /* address family */ char sa_data[14]; /* actually longer; address value*/ }; #define SOCK_MAXADDRLEN 255 /* longest possible addresses */

slide-12
SLIDE 12

12 ¡

Associate ¡a ¡local ¡address ¡with ¡a ¡socket ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡an ¡unbound ¡socket. ¡ ¡
  • name: ¡The ¡address ¡to ¡assign ¡to ¡the ¡socket. ¡
  • namelen: ¡length ¡of ¡the ¡name ¡ ¡

struct ¡sockaddr ¡{ ¡ ¡ u_short ¡sa_family; ¡ ¡ char ¡sa_data[14]; ¡ ¡ }; ¡ #include ¡<winsock.h> ¡ ¡ int ¡bind ¡( ¡ ¡ ¡SOCKET ¡s, ¡const ¡struct ¡sockaddr ¡FAR ¡* ¡name, ¡ ¡ ¡int ¡namelen ¡); ¡

slide-13
SLIDE 13

13 ¡

Internet ¡address ¡family ¡

  • In ¡the ¡Internet ¡address ¡family, ¡a ¡name ¡consists ¡of ¡several ¡components. ¡ ¡
  • For ¡SOCK_DGRAM ¡and ¡SOCK_STREAM, ¡the ¡name ¡consists ¡of ¡three ¡parts: ¡ ¡

– a ¡host ¡address, ¡the ¡protocol ¡number ¡(set ¡implicitly ¡to ¡UDP ¡or ¡TCP, ¡respec.vely), ¡and ¡a ¡ port ¡number ¡which ¡iden.fies ¡the ¡applica.on. ¡ ¡ – If ¡an ¡applica.on ¡does ¡not ¡care ¡what ¡address ¡is ¡assigned ¡to ¡it, ¡it ¡may ¡specify ¡an ¡Internet ¡ address ¡equal ¡to ¡INADDR_ANY, ¡a ¡port ¡equal ¡to ¡0, ¡or ¡both. ¡ ¡ – If ¡the ¡Internet ¡address ¡is ¡equal ¡to ¡INADDR_ANY, ¡any ¡appropriate ¡network ¡interface ¡ will ¡be ¡used; ¡this ¡simplifies ¡applica.on ¡programming ¡in ¡the ¡presence ¡of ¡mul.-­‑homed ¡

  • hosts. ¡ ¡

– If ¡the ¡port ¡is ¡specified ¡as ¡0, ¡the ¡Windows ¡Sockets ¡implementa.on ¡will ¡assign ¡a ¡unique ¡ port ¡to ¡the ¡applica.on ¡with ¡a ¡value ¡between ¡1024 ¡and ¡5000. ¡ ¡

  • The ¡applicaDon ¡may ¡use ¡getsockname() ¡aJer ¡bind() ¡to ¡learn ¡the ¡address ¡that ¡has ¡been ¡

assigned ¡to ¡it ¡ – getsockname() ¡will ¡not ¡necessarily ¡fill ¡in ¡the ¡Internet ¡address ¡un.l ¡the ¡socket ¡is ¡ connected; ¡several ¡Internet ¡addresses ¡may ¡be ¡valid ¡if ¡the ¡host ¡is ¡mul.-­‑homed. ¡

slide-14
SLIDE 14

14 ¡

Example: ¡bind ¡to ¡an ¡reserved ¡port ¡

SOCKADDR_IN sin; SOCKET s; u_short alport = IPPORT_RESERVED; /* 1024 */ sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; for (;;) { sin.sin_port = htons(alport); if (bind(s, (LPSOCKADDR)&sin, sizeof (sin)) == 0) { /* it worked */ } if ( GetLastError() != WSAEADDRINUSE) { /* fail */ } alport--; if (alport == IPPORT_RESERVED/2 ) { /* fail--all unassigned reserved ports are in use.*/ } }

slide-15
SLIDE 15

15 ¡

Close ¡a ¡socket ¡

  • This ¡func.on ¡closes ¡a ¡socket. ¡ ¡

– releases ¡the ¡socket ¡descriptor ¡s, ¡so ¡that ¡further ¡references ¡to ¡s ¡will ¡fail ¡with ¡ the ¡error ¡WSAENOTSOCK. ¡ ¡ – If ¡this ¡is ¡the ¡last ¡reference ¡to ¡the ¡underlying ¡socket, ¡the ¡associated ¡naming ¡ informa.on ¡and ¡queued ¡data ¡are ¡discarded. ¡

  • Seman.cs ¡influenced ¡by ¡socket ¡op.ons: ¡

#include ¡<winsock.h> ¡ ¡ int ¡closesocket ¡( ¡SOCKET ¡s ¡); ¡

Option Interval Type of close Wait for close? SO_DONTLINGER Don‘t care Graceful No SO_LINGER Zero Hard No SO_LINGER Non-zero Graceful Yes

slide-16
SLIDE 16

16 ¡

Establish ¡a ¡connec.on ¡to ¡a ¡peer ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡an ¡unconnected ¡socket. ¡ ¡
  • name: ¡The ¡name ¡of ¡the ¡peer ¡to ¡which ¡the ¡socket ¡is ¡to ¡be ¡connected. ¡ ¡
  • namelen: ¡The ¡length ¡of ¡the ¡name. ¡ ¡
  • create ¡a ¡connec.on ¡to ¡the ¡specified ¡foreign ¡associa.on. ¡The ¡parameter ¡s ¡

specifies ¡an ¡unconnected ¡datagram ¡or ¡stream ¡socket ¡

#include ¡<winsock.h> ¡ ¡ int ¡connect ¡( ¡SOCKET ¡s, ¡ ¡ ¡ ¡const ¡struct ¡sockaddr ¡* ¡name, ¡ ¡ ¡ ¡int ¡namelen ¡); ¡

slide-17
SLIDE 17

17 ¡

Establish ¡a ¡socket ¡to ¡listen ¡for ¡incoming ¡ connec.on ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡a ¡bound, ¡unconnected ¡socket. ¡ ¡
  • backlog: ¡The ¡maximum ¡length ¡to ¡which ¡the ¡queue ¡of ¡pending ¡connec.ons ¡may ¡
  • grow. ¡ ¡
  • typically ¡used ¡by ¡servers ¡that ¡could ¡have ¡more ¡than ¡one ¡connec.on ¡request ¡at ¡a ¡

.me: ¡ ¡

– if ¡a ¡connec.on ¡request ¡arrives ¡with ¡the ¡queue ¡full, ¡the ¡client ¡will ¡receive ¡an ¡error ¡with ¡ an ¡indica.on ¡of ¡WSAECONNREFUSED ¡

#include ¡<winsock.h> ¡ ¡ int ¡listen ¡( ¡SOCKET ¡s, ¡int ¡backlog ¡); ¡

slide-18
SLIDE 18

18 ¡

Receiving ¡data ¡from ¡a ¡socket ¡ (connec.on-­‑oriented) ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡a ¡connected ¡socket. ¡ ¡
  • buf: ¡A ¡buffer ¡for ¡the ¡incoming ¡data. ¡ ¡
  • len: ¡The ¡length ¡of ¡buf. ¡ ¡
  • flags: ¡Specifies ¡the ¡way ¡in ¡which ¡the ¡call ¡is ¡made. ¡ ¡

#include ¡<winsock.h> ¡ ¡ int ¡recv ¡( ¡SOCKET ¡s, ¡ ¡ ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags ¡); ¡

slide-19
SLIDE 19

19 ¡

Receive ¡a ¡datagram ¡and ¡store ¡the ¡source ¡ address ¡(connec.onless) ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡a ¡bound ¡socket. ¡ ¡
  • buf: ¡A ¡buffer ¡for ¡the ¡incoming ¡data. ¡ ¡
  • len: ¡The ¡length ¡of ¡buf. ¡ ¡
  • flags: ¡Specifies ¡the ¡way ¡in ¡which ¡the ¡call ¡is ¡made. ¡ ¡
  • from: ¡An ¡op.onal ¡pointer ¡to ¡a ¡buffer ¡which ¡will ¡hold ¡the ¡source ¡address ¡

upon ¡return. ¡ ¡

  • fromlen: ¡An ¡op.onal ¡pointer ¡to ¡the ¡size ¡of ¡the ¡from ¡buffer. ¡ ¡

#include ¡<winsock.h> ¡ ¡ int ¡recvfrom ¡( ¡SOCKET ¡s, ¡ ¡ ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags, ¡ ¡ ¡struct ¡sockaddr ¡* ¡from, ¡int ¡* ¡fromlen ¡); ¡ ¡

slide-20
SLIDE 20

20 ¡

Determine ¡the ¡status ¡of ¡one ¡or ¡more ¡ sockets, ¡wai.ng ¡if ¡necessary. ¡

  • nfds: ¡ ¡

This ¡argument ¡is ¡ignored ¡and ¡included ¡only ¡for ¡the ¡sake ¡of ¡compa.bility. ¡ ¡

  • readfds: ¡

An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡readability. ¡ ¡

  • writefds: ¡ ¡

An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡writability ¡ ¡

  • excepOds: ¡ ¡

An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡errors. ¡ ¡

  • Dmeout: ¡ ¡

The ¡maximum ¡.me ¡for ¡select() ¡to ¡wait, ¡or ¡NULL ¡for ¡blocking ¡opera.on. ¡ #include ¡<winsock.h> ¡ ¡ int ¡select ¡( ¡int ¡nfds, ¡fd_set ¡* ¡readfds, ¡ ¡ ¡ ¡fd_set ¡* ¡writefds, ¡fd_set ¡* ¡excepuds, ¡ ¡ ¡ ¡const ¡struct ¡.meval ¡* ¡.meout ¡); ¡

slide-21
SLIDE 21

21 ¡

Send ¡data ¡on ¡a ¡connected ¡socket ¡ (connec.on-­‑oriented) ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡a ¡connected ¡socket. ¡ ¡
  • buf: ¡A ¡buffer ¡containing ¡the ¡data ¡to ¡be ¡transmiHed. ¡ ¡
  • len: ¡The ¡length ¡of ¡the ¡data ¡in ¡buf. ¡ ¡
  • flags: ¡Specifies ¡the ¡way ¡in ¡which ¡the ¡call ¡is ¡made. ¡

#include ¡<winsock.h> ¡ ¡ int ¡send ¡( ¡SOCKET ¡s, ¡ ¡ const ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags ¡); ¡ ¡

slide-22
SLIDE 22

22 ¡

Send ¡data ¡to ¡a ¡specific ¡des.na.on ¡ (connec.onless) ¡

  • s: ¡A ¡descriptor ¡iden.fying ¡a ¡socket. ¡ ¡
  • buf: ¡A ¡buffer ¡containing ¡the ¡data ¡to ¡be ¡transmiHed. ¡ ¡
  • len: ¡The ¡length ¡of ¡the ¡data ¡in ¡buf. ¡ ¡
  • flags: ¡Specifies ¡the ¡way ¡in ¡which ¡the ¡call ¡is ¡made. ¡ ¡
  • to: ¡An ¡op.onal ¡pointer ¡to ¡the ¡address ¡of ¡the ¡target ¡socket. ¡ ¡
  • tolen: ¡The ¡size ¡of ¡the ¡address ¡in ¡to. ¡ ¡

#include ¡<winsock.h> ¡ ¡ int ¡sendto ¡( ¡SOCKET ¡s, ¡ ¡ ¡const ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags, ¡ ¡ ¡const ¡struct ¡sockaddr ¡* ¡to, ¡int ¡tolen ¡); ¡

slide-23
SLIDE 23

Outlook: ¡IPv6 ¡

  • new ¡socket ¡domain: ¡AF_INET6 ¡
  • struct ¡sockaddr_in6 ¡replaces ¡struct ¡sockaddr_in ¡
  • read ¡`man ¡ipv6` ¡

s ¡= ¡socket(AF_INET6, ¡SOCK_STREAM, ¡0); ¡ ¡ struct ¡sockaddr_in6 ¡{ ¡ ¡sa_family_t ¡ ¡sin6_family; ¡/* ¡AF_INET6 ¡*/ ¡ ¡in_port_t ¡ ¡sin6_port; ¡/* ¡port ¡number ¡*/ ¡ ¡uint32_t ¡ ¡ ¡sin6_flowinfo; ¡/* ¡IPv6 ¡flow ¡informa.on ¡*/ ¡ ¡struct ¡in6_addr ¡ ¡sin6_addr; ¡/* ¡IPv6 ¡address ¡*/ ¡ ¡ ¡uint32_t ¡ ¡ ¡sin6_scope_id; ¡/* ¡Scope ¡ID ¡(new ¡in ¡2.4) ¡*/ ¡ }; ¡ ¡ struct ¡in6_addr ¡{ ¡ ¡unsigned ¡char ¡s6_addr[16]; ¡/* ¡IPv6 ¡address ¡*/ ¡ }; ¡

slide-24
SLIDE 24

24 ¡

Further ¡Reading ¡

  • Mark ¡E. ¡Russinovich ¡and ¡David ¡A. ¡Solomon, ¡ ¡

MicrosoZ ¡Windows ¡Internals, ¡4th ¡Edi.on, ¡MicrosoZ ¡Press, ¡ 2004; ¡

– Windows ¡Sockets ¡(from ¡pp. ¡791) ¡ ¡

  • Abraham ¡Silberschatz, ¡Peter ¡B. ¡Galvin, ¡Opera.ng ¡System ¡

Concepts, ¡John ¡Wiley ¡& ¡Sons, ¡6th ¡Ed., ¡2003; ¡

– Chapter ¡15 ¡-­‑ ¡Distributed ¡System ¡Structures ¡

  • W. ¡Richard ¡Stevens, ¡Unix ¡Network ¡Programming, ¡Pren.ce ¡Hall ¡

SoZware ¡Series, ¡1990; ¡(The ¡Book) ¡

– Chapter ¡6 ¡-­‑ ¡Berkeley ¡Sockets ¡