Socket options - PowerPoint PPT Presentation

About This Presentation
Title:

Socket options

Description:

But applications usually can't alter code in an operating system's protected kernel ... Then we will be ready to compile and run our bindtoif' demo-program ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 13
Provided by: cru6
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: Socket options


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
FTP, HTTP, SSH, DHCP, etc
The sockets API
Transport layer
TCP, UDP, etc
Network layer
IP, ICMP, etc
Link layer
ARP, RARP, etc
Physical layer
DSL, FDDI, etc
3
Encapsulation
message
data
Application
segment
data
UDP Header
Transport
datagram
data
UDP Header
IP Header
Network
frame
data
UDP Header
IP Header
Frame Header
Frame CRC
Link
stream-of-bits
data
UDP Header
IP Header
Frame Headerd
Frame CRC
preamble
Inter-Frame Gap
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_in saddr socklen_t salen
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
Internets standard big-endian byte-order,
rather than the little-endian byte-order
employed within Intels 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 cant alter code in an
    operating systems 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 systems
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
    classrooms Local Area Network as you can
    confirm using our nicwatch tool
  • And -- the user can choose the interface!
  • Normally an application wouldnt 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 networks 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
Write a Comment
User Comments (0)
About PowerShow.com