Title: IPC with sockets
1IPC with sockets
Linux Internals
- sockets in the linux operating system
Filip Polsakiewicz
December, 18th 2001
2topic overview
- motivation why sockets?
- what exactly are sockets?
- working with sockets
- how does the linux kernel implement sockets?
- summary
3topic overview
- motivation why sockets?
- what exactly are sockets?
- working with sockets
- how does the linux kernel implement sockets?
- summary
4topic overview
- motivation why sockets?
- what exactly are sockets?
- working with sockets
- how does the linux kernel implement sockets?
- summary
5topic overview
- motivation why sockets?
- what exactly are sockets?
- working with sockets
- how does the linux kernel implement sockets?
- summary
6topic overview
- motivation why sockets?
- what exactly are sockets?
- working with sockets
- how does the linux kernel implement sockets?
- summary
7motivation why sockets?
- UNIX-Operating systems at first only supported
using directional pipes for ipc - only related processes on the same machine were
able to communicate - only one-way communication
8motivation why sockets?
- UNIX-Operating systems at first only supported
using pipes for ipc - only related processes on the same machine were
able to communicate - only one-way communication
- networking was difficult since there were no
standards for network interfaces - developing applications for network jobs was
difficult (each network interface had to be
implemented seperately)
9motivation why sockets?
- many research facilities developed solutions for
these both problems - bidirectional pipes, named pipes
- one of these solutions solved both Problems
sockets
10what are sockets
- sockets are a connection-based type of ipc
- sockets are an abstraction of the network
interface and the network protocol - developers do not have to care about implementing
hardware-specific details
11what are sockets
- sockets are a connection-based type of ipc
- sockets are an abstraction of the network
interface and the network protocol - developers do not have to care about implementing
hardware-specific details
socket layer
Stream Socket
TCP/IP Protocol
network protocols
network interfaces
10 MBit/s Ethernet
12what are sockets
process 1
process 2
KERNEL
socket
socket
13general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
14general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
15general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
16general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
17general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
18general properties of sockets
- connection-based
- can have an address bound to them
- use packets for data transmission
- allow checking for errors
- UNIX_STREAM and TCP
- allow IPC in a network enviroment
- allrounders
- many protocols such as IPX, AX_25 and Appletalk
are supported
19different types of sockets
SOCKET
20different types of sockets
- UNIX Domain sockets
- realized in memory
- can be bound to an address which appears in the
local filesystem (e.g. /tmp/mysocket) - if client and server are on the same machine,
twice as fast as INET-sockets (see X-Server) - Internet (INET) sockets
- implemented through the network
- using a combination of 32-bit numbers and a
port-number as address (e.g. 192.168.0.18088) - both types have in common once they are bound to
an address they can be reached by other sockets
21different types of sockets
- UNIX Domain sockets
- realized in memory
- can be bound to an address which is a file
appears in the local filesystem (e.g.
/tmp/mysocket) - if client and server are on the same machine,
twice as fast as INET-sockets (see X-Server) - Internet (INET) sockets
- implemented through the network
- using a combination of 32-bit numbers and a
port-number as address (e.g. 192.168.0.18088) - both types have in common once they are bound to
an address they can be reached by other sockets
address
port
22different types of sockets
- UNIX Domain sockets
- realized in memory
- can be bound to an address which appears in the
local filesystem (e.g. /tmp/mysocket) - if client and server are on the same machine,
twice as fast as INET-sockets (see X-Server) - Internet (INET) sockets
- implemented through the network
- using a combination of 32-bit numbers and a
port-number as address (e.g. 192.168.0.18088) - both types have in common once they are bound to
an address they can be reached by other sockets
23different types of sockets
- UNIX Domain sockets
- realized in memory
- can be bound to an address which appears in the
local filesystem (e.g. /tmp/mysocket) - if client and server are on the same machine,
twice as fast as INET-sockets (see X-Server) - Internet (INET) sockets
- implemented through the network
- using a combination of six 32-bit numbers and a
port-number as address - both types have in common once they are bound to
an address they can be reached by other sockets
We will concentrate on UNIX Domain sockets since
INET Sockets will be handled later
24working with sockets (server)
sun_family sun_path
tells the kernel that the socket is now ready to
accept connections
listen(int fd, int backlog)
on an incoming connection
accept(int fd, struct sockaddr upeer_sockaddr,
int upeer_addrlen)
25working with sockets (client)
26communication
SERVER
CLIENT
socket()
bind()
listen()
socket()
connect()
accept()
write
read
read
write
close()
close()
27some additional functions
- socketpair()
- creates two socket file descriptors and connects
them to each other - similar to bidirectional pipes
- shutdown(int fd, int how)
- closes the socket file descriptor
28some additional functions
- socketpair()
- creates two socket file descriptors and connects
them to each other - simmilar to bidirectional pipes
- shutdown(int fd, int how)
- closes the socket file descriptor
29netstat -a
Active UNIX domain sockets (servers and
established)Proto RefCnt Flags
Type State I-Node
Pathunix 1 STREAM
CONNECTED 2523 _at_0000000bunix 1
STREAM CONNECTED 2689
_at_0000000e unix 0 ACC STREAM
LISTENING 331 /var/run/mysqld/mysqld.so
ckunix 0 ACC STREAM LISTENING
46678 /dev/logunix 1
STREAM CONNECTED 2698
_at_00000010unix 1 STREAM
CONNECTED 225 _at_00000001unix 1
STREAM CONNECTED 237
_at_00000003unix 0 ACC STREAM
LISTENING 2686 /tmp/.X11-unix/X0unix
0 ACC STREAM LISTENING
2514 /var/lib/zope/var/Z2-pcgi.socunix 0
ACC STREAM LISTENING 395
/tmp/.font-unix/fs7100unix 1
STREAM CONNECTED 2699
/tmp/.X11-unix/X0unix 1
STREAM CONNECTED 2693 /tmp/.X11-unix/X0u
nix 1 STREAM
CONNECTED 2541 /dev/log
30how are sockets implemented in the kernel
- each of the socket functions introduced in
earlier has a corresponding function in the
kernel - socket() ? sys_socket()
- bind ? sys_bind()
-
- when the process calls a function the kernel
executes the corresponding function which itself
calls other functions
31how are sockets implemented in the kernel
- each of the socket functions introduced in
earlier has a corresponding function in the
kernel - socket() ? sys_socket()
- bind ? sys_bind()
-
- when the process calls a function the kernel
executes the corresponding function which itself
calls other functions
sock_write()
unix_sendmsg()
sys_write()
stream_sendmsg()
write()
32data-structures
type
proto_ops
data
33data-structures
type
proto_ops
data
34data-structures
type
proto_ops
data
35data-structures
type
proto_ops
data
36summary
- sockets are an abstraction of the actual hardware
and protocol layers - sockets are connection-based
- after a socket was bound to an address it can be
reached by other sockets - communication of two sockets can be accomplished
by using standard file IO functions
37summary
- sockets are an abstraction of the actual hardware
and protocol layers - sockets are connection-based
- after a socket was bound to an address it can be
reached by other sockets - communication of two sockets can be accomplished
by using standard file IO functions
38summary
- sockets are an abstraction of the actual hardware
and protocol layers - sockets are connection-based
- after a socket was bound to an address it can be
reached by other sockets - communication of two sockets can be accomplished
by using standard file IO functions
39summary
- sockets are an abstraction of the actual hardware
and protocol layers - sockets are connection-based
- after a socket was bound to an address it can be
reached by other sockets - communication of two sockets can be accomplished
by using standard file IO functions
40thank you for your attention