Download presentation
Presentation is loading. Please wait.
1
Socket options A way for network applications to ‘tweak’ the processing done at lower-levels of the TCP/IP stack
2
The TCP/IP stack Application layer Transport layer Network layer Link layer The sockets API Physical layer TCP, UDP, etc IP, ICMP, etc ARP, RARP, etc FTP, HTTP, SSH, DHCP, etc DSL, FDDI, etc
3
Encapsulation data message UDP Header UDP Header UDP Header UDP Header IP Header IP Header IP Header Frame Headerd Frame CRC Frame Header Frame CRC preamble Inter-Frame Gap segment datagram frame stream-of-bits Application Transport Network Link
4
Key library functions int socket() int bind(), int connect() int listen(), int accept() int write(), int read() int send(), int recv() int sendto(), int recvfrom() int shutdown(), int close()
5
Simple program-flow example int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); connect( sock, (sockaddr*)&saddr, sizeof( saddr ) ); write( sock, message, sizeof( message ) ); close( sock );
6
socket-address object struct sockaddr_insaddr; socklen_tsalen = sizeof( saddr ); bzero( &saddr, salen ); saddr.sin_family = AF_INET; saddr.sin_port = htons( port_number ); saddr.sin_addr.s_addr = htonl( peer_ip_address ); Using the sockets API requires allocating and initializing data-objects known as ‘socket-addresses’ (aka ‘socket-names’), with some numeric fields which require using the Internet’s standard ‘big-endian’ byte-order, rather than the ‘little-endian’ byte-order employed within Intel’s x86 processors. But helper- functions, like htons() and htonl(), will convert host-order into network-order.
7
Using socket ‘options’ The default behavior of the kernel routines for the lower-layers of the TCP/IP protocol stack may not be fully suitable for the aims of particular network application programs But applications usually can’t alter code in an operating system’s protected kernel The sockets API offers a ‘workaround’ for such situations, i.e.: int setsockopt()
8
Function prototypes int getsockopt( int sd, int level, int optname, void *optval, socklen_t *optlen ); int setsockopt( int sd, int level, int optname, void *optval, socklen_t optlen ); There are various socket options, which apply to various levels in the networking system’s software hierarchy, and which selectively apply to various types of sockets -- and which are implemented to varying degrees within different versions of popular operating systems. We will demonstrate use of two socket options available in Linux for datagram sockets: SO_BROADCAST and SO_BINDTODEVICE. Our demo also illustrate the use of ‘write()’ for a connected socket.
9
Demo: ‘bindtoif.cpp’ This program lets a privileged user write a ‘broadcast’ message to all of the hosts on our classroom’s Local Area Network – as you can confirm using our ‘nicwatch’ tool And -- the user can choose the interface! Normally an application wouldn’t be able to send a ‘broadcast’ message, nor be able to select which interface gets used
10
Overview Get the name of the desired network interface from the command-line Open an internet datagram socket Turn on the ‘SO_BROADCAST’ socket-option Bind the chosen network interface to that socket Connect the socket to the network‘s broadcast address Write a message to the connected socket Show the user a confirmation message
11
Programming details Now we take a ‘timeout’ from these slides to look carefully line-by-line at the source- code which will implement those steps Then we will be ready to compile and run our ‘bindtoif’ demo-program (using ‘sudo’) You should all be able to watch the arrival of the broadcast message at your desktop!
12
‘ifconfig’ Remember that your classroom computer leaves the ‘eth1’ interface disabled after a reboot – so you will need to use ‘ifconfig’ to enable that interface and also assign it an appropriate Internet Protocol address: $ sudo /sbin/ifconfig eth1 192.168.1.xxx up
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.