SCTP in Theory and Practice-Sample
SCTP in Theory and Practice-Sample
Tsvetomir Dimitrov
This book is for sale at http://leanpub.com/sctp
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Who is this book for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
How to read the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Code samples and PCAP files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
THANK YOU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Association teardown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Association shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Key take-aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Chapter 5: Multi-homing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
How multi-homing works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Time to check the specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Path verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Primary path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Time to check the specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Association termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Key take-aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Obtaining association id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Getting local and remote association addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Sending and receiving data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Peel off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
One-to-one server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
One-to-many server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Peel-off server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
THANK YOU
The decision to buy this book and spare your precious time reading it means a lot to me! I hope you
will like it and will learn a lot from it. Don’t hesitate to contact me if you have got any questions,
you have found a mistake or just want to share your feedback. You can find my up to date contact
information on https://petanode.com¹.
¹https://petanode.com/
Chapter 1: Quick introduction to the
SCTP protocol
SCTP stands for Stream Control Transmission Protocol. The protocol is not very popular in the
Internet world, but plays an important role in the telecommunication networks. It is used as a
transport for the SIGTRAN² and it is the default transport protocol in 4G core network - EPC³.
SCTP is connection oriented (like TCP) and message oriented (like UDP) transport layer protocol.
Let’s see what these terms mean:
• Connection oriented protocol. Means that the protocol maintains logical connection. Usually
these protocols are reliable, they handle packet delivery confirmations, retransmissions, how
much data the receiver can handle and so on. Connection oriented protocols ensure that the
messages are delivered in-order without any losses. TCP is a classical example for such protocol.
• Message oriented protocol. The data is transmitted in separate messages and they are received
one by one from the peer. The counterpart of these protocols are called Stream oriented. They
transmit the data as a continuous stream without any logical separators. It’s a responsibility
of the upper layer to read enough data and to know how to interpret and separate it. These
protocols usually use predefined header which contains the length of the data or some sort
of separator. HTTP for example uses CRLF as separator and the Content-Length header field
specifies the length of the message body.
Back to SCTP. What does connection oriented and message oriented means? SCTP establishes
a logical connection between the client and server, handles rate at which the sender transmits
data to the receiver and handles the retransmissions of lost packets. At the same time SCTP
transmits messages (called chunks) between the peers and the receiver can easily distinguish them.
Additionally SCTP supports some useful features like multiple streams and multi-homing, but more
on this later.
especially when you do it for the first time. The document contains all details one might need to
create working SCTP implementation and it’s easy to get lost.
In this book I try to sacrifice some of the details and to produce an easy to follow guide to SCTP.
In each chapter I try to explain a concept about the protocol and point to the exact sections of the
specification where this topic is covered. I hope this approach will save you some time and get you
a smoother learning experience. Don’t feel obligated to read each section of the specification, unless
you really need the details. Just reading the book should be enough to understand how the protocol
works.
Chunk. This is a unit of information within an SCTP packet. Think about it as a specific protocol
message, which can represent a request/response from the peers, state update or user data transfer.
The structure of the chunk will be discussed later in this chapter.
Each time when I use a Wireshark screenshot I will also explicitly note the filename and packet
number (the first column in Wireshark) of the SCTP chunk I am referring to. This way you can
open the file by yourself and see some more context about the topic we are discussing.
On fig. 2 there is a common header and INIT chunk, which is collapsed for better readability. This
chunk in packet no. 1 from association.pcapng.
An SCTP chunk represents a protocol message, which can be used by the protocol itself (e.g. INIT,
which is the first step in association establishment), or can contain user data (DATA chunk). It has
got three fixed-length parameters and a variable length value. Each chunk is 4 byte aligned, which
means that the whole chunk size have to be multiple of 4 bytes. If it is not, zero byte padding is added.
The padding shouldn’t be more than 3 bytes (4 byte padding doesn’t make sense). Each chunk has
got the following fields:
• chunk type - 8 bits, can be considered as and identification of the SCTP message. E.g. INIT,
DATA, HEARTBEAT, etc.
• chunk flags - 8 bits, specific for each chunk type.
Chapter 1: Quick introduction to the SCTP protocol 7
• chunk length - 16 bits, the length of the chunk in bytes, including all header fields (type, flags,
length) plus the length of the value, excluding the padding.
• chunk value - variable length parameter. Contains various chunk parameters, which depend
on the chunk type.
Fig. 3 shows SACK message, which contains common packet header and SACK chunk. The chunk’s
size is 16 bytes, which means that there is no padding. The chunk is in packet no. 6 from
association.pcapng.
Chunk parameters
The value of each chunk has specific format too. It contains two fixed-sized parameters and variable
length value. Each parameter in the chunk must be 4 byte aligned, so the chunk parameter itself
may also contain padding. The logic is the same as for the chunk value. Here is the structure of each
parameter:
• chunk parameter type - 16 bits, the type of the parameter in the chunk.
• chunk parameter length - 16 bits, the length of the parameter, without the padding.
• chunk parameter value - the actual value for the specified parameter type.
Chapter 1: Quick introduction to the SCTP protocol 8
0000 11 5c c6 68 88 e5 e3 76 88 34 32 df 03 00 00 10
0010 8a 70 48 4a 00 01 9f fa 00 00 00 00
We see two lines of hex digits. Each line starts with four digits, followed by 16 groups of two digits,
separated by spaces.
A single hex digit represents 4 bits, so a group of two digits is 8 bits, which is 1 byte. Each line is
a fixed stream of bytes. The four digits in the beginning of each line is offset, which is also in hex.
0x10 is 16 in hex, which exactly corresponds to the 16 bytes (16 groups of two digits) we have got
on each line.
Convert these numbers to decimal and you will get the values from Wireshark.
• chunk type (8 bits): 03. A quick glance in Section 3.2¹¹ shows that this is a SACK chunk.
• chunk flags (8 bits): 00
• chunk length (16 bits): 00 10. This is the length of the chunk - 16 bytes or 128 bits. 128 is divisible
by 4 so there is no padding in this chunk. Don’t forget that each chunk should be aligned on 4
bits.
¹¹http://tools.ietf.org/html/rfc4960#section-3.2
Chapter 1: Quick introduction to the SCTP protocol 10
We already parsed Chunk type, flags and length. The flags value is 0x00. The flags description in
¹²http://tools.ietf.org/html/rfc4960#section-3.3.4
Chapter 1: Quick introduction to the SCTP protocol 11
Section 3.3.4¹³ says that the flags for SACK are always 0.
Key take-aways
SCTP is connection oriented and message oriented transport protocol, which uses IPv4/IPv6 as
network layer. The protocol is defined in RFC 4960¹⁵.
Each SCTP packet contains a common header (describing the association) and one or more chunks.
Each chunk contains its own common header and zero or more chunk parameters.
¹³http://tools.ietf.org/html/rfc4960#section-3.3.4
¹⁴http://tools.ietf.org/html/rfc4960#section-3.2
¹⁵https://tools.ietf.org/html/rfc4960
Chapter 2: Association initialisation
In this chapter we will see how SCTP association initialisation happens, what chunks are used and
what parameters they have got.
Section 4¹⁶ has got a detailed state diagram for a SCTP association. There are two main states
- CLOSED and ESTABLISHED. Two endpoints can exchange data only when the association
is in ESTABLISHED state. The transition between these two states is made by the association
initialisation procedure.
Association initialisation
Similarly to TCP, the peer initiating the association is called client and it is connecting to a server.
Figure 1 shows the flow which leads to an initialised association (ESTABLISHED state). The chunk
names are in bold, below them are their most important parameters. Xa/Xb, Ya/Yb are numeric
values for each parameter respectively on the client and the server. The exact values are not
important for now. Remember that a single SCTP packet can contain more than one chunk. Let’s
review each chunk in detail.
INIT chunk
The association initialisation starts with the client sending an INIT chunk to the server. On fig.
2 you can see a screenshot from Wireshark, showing such a chunk. This is packet no. 1 from
association.pcapng. It has the following parameters:
• Initiate tag. This value should be saved by the receiver and set in Verification tag field
in the SCTP common header for each message from the server to the client. Note that the
Verification tag in the message, containing the INIT chunk, is 0 because it is the first one in
the communication.
• Advertised Receiver Window Credit (a_rwnd) is the buffer size of the sender.
• Number of outbound streams. The requested number of streams from the client to the server.
The server may not accept this number. The procedure for stream count negotiation is described
later in the chapter. This value can’t be zero.
• Number of inbound streams. The maximum number of inbound streams that the client
supports. The server is obligated to respect this value, or error will occur. Also can’t be zero.
• Initial TSN. A random number between 0 and 4294967295. TSN stands for Transmission
Sequence Number. Each DATA chunk (user data) has attached a unique TSN which is used
for acknowledgement and duplicated chunk detection. This parameters states the first TSN
that will be used by the sender (the client in this case). For each new DATA chunk, the TSN is
incremented by one.
• Supported address types and IPv4 address parameters. They contain the IP addresses which
can be used to reach the client. They can be more than one, because of the SCTP’s multi-homing
feature.
• ECN parameter. ECN stands for Explicit congestion notification it is an extension, which is
not part of the base protocol and hasn’t got any relation to association establishment. It is out
of the scope of this book, so I will skip it. Check the Time to check the specification section for
more details.
• Forward TSN supported parameter. This parameter is from SCTP Partial Reliability Exten-
sion. It is also out of the scope of the book.
¹⁷http://tools.ietf.org/html/rfc4960#section-5
Chapter 2: Association initialisation 14
The Verification tag in the common header is set to 0x08fe2132 - the Initiate tag from the INIT.
Remember that only the INIT has got Verification tag set to 0. Each subsequent message will have
the Verification tag set to the Initiate tag of the receiver. The parameters of the INIT ACK chunk
are similar to the INIT ones on first sight, but they carry different meaning:
• Number of inbound streams. The maximum number of inbound streams, supported of the
server. If the Number of outbound stream parameters in the INIT chunk of the client is bigger
than this value, the client must adjust its maximum output streams number.
• Initial TSN. The first TSN that will be used by the server. What we discussed about this
parameter in INIT is valid for INIT ACK too.
• ECN parameter and Forward TSN supported parameter. Again they are not related to the
association establishment and we will skip them.
At this point both the client and the server know the number of input and output streams supported
by each other. This procedure is used to negotiate an acceptable number for both peers in each
direction. The protocol negotiates the maximum, not the actual number of streams in each direction.
This doesn’t mean that the SCTP user is obligated to use all of them.
There is one more parameter in the INIT ACK chunk, which was not present in the INIT - State
cookie. When INIT is received, the server generates TCB, which stands for Transmission Control
Block. It contains all the information about the association which is required by the server. The
State cookie parameter contains the TCB with MAC (Message Authentication Code). It is used for
integrity check and authentication of the TCB. After the state cookie is generated, the server deletes
all the information about the association. This means that the server doesn’t allocate any resources
for the association at this point. Later, the client responds with COOKIE ECHO (discussed next),
which contains the TCB with all the required information which the server needs to create the
association. The MAC guarantees that the TCB is not modified by the client so the server can use it
safely.
This approach is very clever from a security point of view. TCP is vulerable to DoS attacks, called
SYN flood. In nutshell TCP connection establishing is a three step process. The client sends SYN, the
server allocates resources for the connection, sends ACK and waits for SYN-ACK. This makes him
vulnerable to a malicious clients, which send lots of SYN packets and doesn’t establish or tear down
the connection. SCTP’s approach to association establishment mitigates this issue. If a malicious
client tries to flood the server with INIT messages the SCTP stack will only calculate state cookies
and send INIT ACKs, without keeping any state information. This way no resources will be wasted
and the DoS attack is way harder to perform.
• RFC 2104²⁷
• Hash-based message authentication code²⁸ article on Wikipedia
²⁷http://tools.ietf.org/html/rfc2104
²⁸http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
Chapter 2: Association initialisation 18
Key take-aways
SCTP association establishment is a four step process. Four SCTP chunks are exchanged between the
client and the server - INIT, INIT ACK, COOKIE ECHO, COOKIE ACK. COOKIE ECHO contains
²⁹http://tools.ietf.org/html/rfc4960#section-3.3.11
³⁰http://tools.ietf.org/html/rfc4960#section-5.1.5
³¹http://tools.ietf.org/html/rfc4960#section-3.3.12
Chapter 2: Association initialisation 19
TCB, which is all the information the server needs to establish the association. The client echoes
back the TCB with the COOKIE ACK chunk. This approach makes DoS attacks against the server
harder to carry on. Section 5³² describes the whole association establishment procedure.
³²http://tools.ietf.org/html/rfc4960#section-5
Chapter 3: Data transfer in SCTP
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
DATA chunk
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SACK chunk
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 4: Association teardown
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Failure detection
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Path Heartbeat
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 4: Association teardown 23
HEARTBEAT chunk
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Association teardown
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
ABORT chunk
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Association shutdown
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SHUTDOWN chunk
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 5: Multi-homing
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Path verification
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 5: Multi-homing 26
Primary path
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Association termination
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Example
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Normal operation
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 6: Sockets introduction
In Linux, you deal with the network stack via sockets. I assume you are familiar how they work
and you have got basic network programming knowledge in C. This includes what functions like
socket(), bind(), listen() do, how you should call them and how to implement a simple client-server
application. If you need a refreshment or you are new to Linux socket programming read some
tutorials on this topic. There are a lot of good ones out there, but my favourite is Beej’s Guide to
Network Programming³³.
The sample code for this and the following chapters is located here³⁴. Each directory in the project
contains source code for a specific chapter and one separate folder with common boilerplate code.
For a start checkout the project and go to ch6_one-to-one directory. Spend some time to familiarise
yourself with the code. It is a client-server application exchanging few messages over SCTP, but it
is not that different from any client server application written in C.
• socket()
• bind()
³³http://beej.us/guide/bgnet/
³⁴https://gitlab.com/petanode/sctp-sample-code
³⁵https://tools.ietf.org/html/rfc6458
Chapter 6: Sockets introduction 28
• listen()
• accept()
• send()/receive()
• close()
• socket()
• connect()
• send()/recv()
• close()
• socket()
• bind()
• listen()
• recvmsg()/sendmsg()
• close()
It’s worth mentioning that one-to-many style server doesn’t need to call accept() in order to create
an association. It is created on the fly for each new client.
And for a client:
• socket()
• sendmsg()/recvmsg()
• close()
Note that the client doesn’t need to issue a connect() in order to communicate with a server. Client
associations are also initiated on the fly. The socket type for one-to-many is SOCK_SEQPACKET
In this chapter we will see how one-to-one client/server application is implemented.
Chapter 6: Sockets introduction 29
1 cd sctp-sample-code/ch6_one-to-one/
CMake creates out of source build, which is a fancy name for “CMake uses separate build direcotry”.
Let’s create it:
1 mkdir build
2 cd build
And run cmake. The argument is the path to the source code, which in our case is the parent
directory:
1 cmake ..
1 make
That’s all. If everything is correct you should have got two binaries - server and client. Now start
wireshark and run the binaries.
Open two terminals and start the server in the first one:
1 ./server
1 ./client 127.0.0.1
1 Connecting...
2 OK
3 Sending message 1 of 5. Result: OK
4 Sending message 2 of 5. Result: OK
5 Sending message 3 of 5. Result: OK
6 Sending message 4 of 5. Result: OK
7 Sending message 5 of 5. Result: OK
8 Closing...
Now get back to the server terminal and check its output. You should see two new lines:
And finally check your PCAP trace to see the establishment of the SCTP association, the transfer of
the messages and the association tear down. If you don’t see anything, make sure you are listening
on the correct interface. By default the client and the server communicate via localhost.
Chapter 6: Sockets introduction 32
The code
There is a sample code for each chapter located in separate directories. Each sample project contains
similar set of files:
• defaults.h - contains various constants for the application. This file is almost identical for each
chapter.
• server.c - generic implementation for the server. It contains all function calls required to
establish one-to-one or one-to-many server.
• chapter specific source/header file. They contain function specific for the chapter. The are called
from the server and possibly from the client.
• client.c - this chapter and the next one contain respectively one-to-one and one-to-many style
clients. The following chapters omit the client unless it is necessary. You can use any client
with any server. The reason to skip the client code is that it’s usually the same.
In the root directory of the project there is a folder called common. It contains bolierplate code used
from all the servers/clients. This code is not SCTP related but required so I will not review it in the
book.
The code for this chapter looks like a typical TCP client/server implementation and this is intentional.
I want to show you how easy it is to convert a TCP client/server program to a SCTP one. As a proof,
let’s try to rework this program to use TCP instead SCTP. Open defaults.h and change the PROTO
constant. Its current value should be set to IPPROTO_SCTP, change it to IPPROTO_TCP. Now
rebuild everything (run make clean && make), run the binaries again and check the PCAP. You
should see that the whole communication is now over TCP. Don’t forget to revert the change before
you go on.
Let’s have a look at the code. I have intentionally skipped some error handling, so don’t be surprised
if it crashes with bad input data. What I want is to focus on the socket API and keep the code as
short and readable as possible. There are two C files for each binary - the client and the server. There
is also a header (defaults.h) with some common constants for both binaries.
Constants
In defaults.h there are some application wide parameters like default port number, address family,
socket type, protocol etc. You can easily adjust the whole behaviour of the application from this
header.
Chapter 6: Sockets introduction 33
1 #ifndef DEFAULTS_H_
2 #define DEFAULTS_H_
3
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <netinet/sctp.h>
7
8 enum {
9 SERVER_PORT = 4444,
10 ADDR_FAMILY = AF_INET,
11 SOCK_TYPE = SOCK_STREAM,
12 PROTO = IPPROTO_SCTP,
13 CLIENT_SEND_COUNT = 5,
14 SERVER_LISTEN_QUEUE_SIZE = 10,
15 };
16
17 #endif /* DEFAULTS_H_ */
The server
main() reads the parameters passed to the binary (if any). You can pass port number different from
the default one (4444). Then a SCTP socket is created (protocol is IPPROTO_SCTP). On success the
server binds on all possible IP addresses and listens for new connections.
New thread is spawned for each new connection. This approach is not scalable, but will do the job
for a demo program. The server waits for new connections in infinite loop. The only way to stop the
server is to kill it.
Let’s review server.c block by block. We will start with main(). Each important line is marked with
a number. All explanations are after the code listing.
1 #include "defaults.h"
2 #include "server_helpers.h"
3 #include "inc.h"
4
5
6 void* handle_connection(void *thread_data);
7
8 int main(int argc, char* argv[])
9 {
10 int server_port = handle_server_cmd_arg(argc, argv, SERVER_PORT); // 1
11 if(server_port == -1) {
12 return 1;
13 }
Chapter 6: Sockets introduction 34
14
15 struct sockaddr_in bind_addr = get_server_bind_sockaddr(ADDR_FAMILY,
16 server_port); // 2
17
18 int server_fd = 0;
19 if((server_fd = socket(ADDR_FAMILY, SOCK_TYPE, PROTO)) == -1) { // 3
20 perror("socket");
21 return 1;
22 }
23
24 if(bind(server_fd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) == -1) { // 4
25 perror("bind");
26 return 1;
27 }
28
29 if(listen(server_fd, SERVER_LISTEN_QUEUE_SIZE) != 0) { // 5
30 perror("listen");
31 return 1;
32 }
33
34 printf("Listening on port %d\n", server_port);
35
36 while(1) {
37 int* client_fd = malloc(sizeof(int)); // 6
38 if(client_fd == NULL) {
39 printf("malloc error\n");
40 return 1;
41 }
42
43 struct sockaddr client_addr;
44 unsigned int client_addr_len = sizeof(client_addr);
45
46 if((*client_fd = accept(server_fd, &client_addr,
47 &client_addr_len)) == -1) { // 7
48 perror("accept");
49 free(client_fd);
50 return 1;
51 }
52
53 log_address("Got connection from", (struct sockaddr_in*)&client_addr);
54
55 if(start_server_thread(&handle_connection, (void*)client_fd)) { // 8
56 free(client_fd);
Chapter 6: Sockets introduction 35
57 }
58 }
59
60 return 0;
61 }
62
63 void* handle_connection(void *thread_data) {
64 int socket = *(int*)thread_data;
65 free(thread_data);
66
67 while(1) {
68 char buf[1024];
69 int recv_len = 0;
70
71 if((recv_len = recv(socket, &buf, sizeof(buf), 0)) == -1) { // 9
72 perror("recv");
73 return NULL;
74 }
75
76 if(recv_len == 0) {
77 printf("Connection from closed by remote peer.\n");
78 close(socket);
79 return NULL;
80 }
81
82 printf("Message received: %s\n", buf);
83
84 if(send(socket, "OK", strlen("OK"), 0) == -1) { // 10
85 perror("send");
86 return NULL;
87 }
88 }
89 }
1. handle_server_cmd_arg() parses the command line parameters for the binary and returns the
port number on which the server should bind. This function is from common.
2. get_server_bind_sockaddr() prepares a sockaddr_in structure for our server. Also from
common.
3. A SCTP socket is created. The constants are defined in defaults.h. ADDR_FAMILY is AF_INET,
SOCK_TYPE - SOCK_STREAM and PROTO is IPPROTO_SCTP.
4. The socket is bound to the server port on all local addresses.
Chapter 6: Sockets introduction 36
The client
The client reads an IP address and optionally a port number from the command line. Then sends a
number of messages to the server (specified by the CLIENT_SEND_COUNT parameter), reads the
responses and exits. The code is quite similar to the server. Let’s see it:
1 #include "defaults.h"
2 #include "inc.h"
3 #include "client_helpers.h"
4
5
6 int main(int argc, char* argv[]) {
7 server_endpoint_t params;
8 if(handle_client_cmd_arg(argc, argv, SERVER_PORT, ¶ms)) { // 1
9 return 1;
10 }
11
12 int client_fd = 0;
13 if((client_fd = socket(ADDR_FAMILY, SOCK_TYPE, PROTO)) == -1) { // 2
14 perror("socket");
15 return 1;
16 }
17
18 struct sockaddr_in peer_addr;
19 if(get_peer_sockaddr(&peer_addr, ADDR_FAMILY, params.address,
20 params.port)) { // 3
21 return 1;
22 }
23
24 printf("Connecting...\n");
25
26 if(connect(client_fd, (struct sockaddr*)&peer_addr,
27 sizeof(peer_addr)) == -1) { // 4
Chapter 6: Sockets introduction 37
28 perror("connect");
29 return 1;
30 }
31
32 char buf[1024];
33 for(int i = 0; i < CLIENT_SEND_COUNT; ++i) {
34 printf("Sending message %d of %d. Result: ", i+1, CLIENT_SEND_COUNT);
35
36 memset(buf, 0, sizeof(buf));
37 snprintf(buf, sizeof(buf)-1, "DATA %d", i);
38
39 if(send(client_fd, &buf, strlen(buf), 0) == -1) { // 5
40 perror("send");
41 return 1;
42 }
43
44 memset(buf, 0, sizeof(buf));
45
46 if(recv(client_fd, &buf, sizeof(buf), 0) == -1) { // 6
47 perror("recv");
48 return 1;
49 }
50
51 printf("%s\n", buf);
52 }
53
54 printf("Closing...\n");
55 if(close(client_fd) == -1) { // 7
56 perror("close");
57 return 1;
58 }
59
60 return 0;
61 }
1. handle_client_params() reads the command line parameters for the client. Function from
common.
2. Creates a SCTP socket.
3. get_peer_sockaddr() creates a sockaddr_in structure. Function from common.
4. Connects to the server.
5. Sends a message.
Chapter 6: Sockets introduction 38
Key take-aways
SCTP one-to-one interface represents one to one relationship between a socket and an association.
This interface is quite similar to the TCP one. By changing some constants and minor code
adjustments you can easily convert a TCP application to SCTP one.
Chapter 7: SCTP Linux API:
One-to-many style interface
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
One-to-many in a nutshell
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The code
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Constants
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 7: SCTP Linux API: One-to-many style interface 40
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
onetomany.c
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The client
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 8: Working with SCTP
ancillary data
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Introduction
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 8: Working with SCTP ancillary data 43
The code
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
anc.c
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The client
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 9: SCTP notifications in Linux
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Introduction
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Notification structure
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Events
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 9: SCTP notifications in Linux 45
SCTP_ASSOC_CHANGE
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_PEER_ADDR_CHANGE
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_REMOTE_ERROR
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_SEND_FAILED
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_SHUTDOWN_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_ADAPTATION_INDICATION
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_PARTIAL_DELIVERY_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_AUTHENTICATION_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 9: SCTP notifications in Linux 46
SCTP_SENDER_DRY_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_NOTIFICATIONS_STOPPED_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
SCTP_SEND_FAILED_EVENT
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The code
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
notif.c
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Key take-aways
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 10: SCTP specific socket
functions in Linux
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Obtaining association id
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Peel off
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
One-to-one server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 10: SCTP specific socket functions in Linux 48
One-to-many server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Peel-off server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Conclusion
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Chapter 11: Multi-homing in Linux
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The server
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
The client
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.
Conclusion
This content is not available in the sample book. The book can be purchased on Leanpub at http:
//leanpub.com/sctp.