0% found this document useful (0 votes)
47 views11 pages

SCTP in Go

This document describes research combining the Stream Control Transmission Protocol (SCTP) and the Go programming language. SCTP is a reliable, message-oriented transport protocol that offers improvements over TCP, including support for multihoming and resistance to denial-of-service attacks. Go is a concurrent, statically typed language developed by Google. The author extended Go's network library to support SCTP, allowing software engineers to take advantage of SCTP's features. The implementation was tested on FreeBSD and Mac OS X, which have up-to-date SCTP specifications.

Uploaded by

gabork
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views11 pages

SCTP in Go

This document describes research combining the Stream Control Transmission Protocol (SCTP) and the Go programming language. SCTP is a reliable, message-oriented transport protocol that offers improvements over TCP, including support for multihoming and resistance to denial-of-service attacks. Go is a concurrent, statically typed language developed by Google. The author extended Go's network library to support SCTP, allowing software engineers to take advantage of SCTP's features. The implementation was tested on FreeBSD and Mac OS X, which have up-to-date SCTP specifications.

Uploaded by

gabork
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

SCTP in Go
Olivier Van Acker
Department of Computer Science and Information Systems
Birkbeck University of London
London, United Kingdom
Email: [email protected]

Abstract—This paper describes a successful attempt to com- browser this year implementing the WebRTC standard. Go as
bine two relatively new technologies: Stream Control Trans- a language is very new and it is too early to say what impact
mission Protocol (SCTP) and the programming language Go, it will have. It does however receive a lot of media attention
achieved by extending the existing Go network library with
SCTP. since it is developed by Google. It is also growing in popularity
SCTP is a reliable, message-oriented transport layer protocol, because of its ease of use as a concurrent system language [1].
similar to TCP and UDP. It offers sequenced delivery of messages
over multiple streams, network fault tolerance via multihoming
support, resistance against flooding and masquerade attacks B. Outline
and congestion avoidance procedures. It has improvements over
wider-established network technologies and is gradually gaining Section II presents an overview of Go and SCTP, followed
traction in the telecom and Internet industries. by (section III) a description of how the TCP socket API
Go is an open source, concurrent, statically typed, compiled is integrated in the Go networking library. This is a starting
and garbage-collected language, developed by Google Inc. Go’s
main design goals are simplicity and ease of use and it has a
point for the design of an SCTP extension to the network
syntax broadly similar to C. Go has good support for networked library in Go, described in section IV. Section V explains
and multicore computing and as a system language is often used the implementation. Section VI analysis the results and VII
for networked applications, however it doesn’t yet support SCTP. concludes.
By combining SCTP and Go, software engineers can exploit the
advantages of both technologies. The implementation of SCTP
extending the Go network library was done on FreeBSD and II. T ECHNOLOGY OVERVIEW
Mac OS X - the two operating systems that contain the most up
to date implementation of the SCTP specification. In this section I will give an overview of the main features
Index Terms—Stream Control Transmission Protocol (SCTP); of SCTP and Go.
Transmission Control Protocol (TCP); Go; Networking;

A. SCTP
I. I NTRODUCTION
SCTP was initially developed in response to the demands
This paper will look into combining two relatively new
of the telecoms industry, which was not satisfied with the
technologies: a network protocol called SCTP and the pro-
reliability and performance of TCP [10, p. 15]. During the
gramming language Go. Both technologies claim to offer
design phase the decision was made to make SCTP a less
improvements over existing technologies: SCTP does away
telephony-centric IP protocol [10, p. 16] so that it could also
with the limitations of the widely used TCP protocol [8]; and
be used for more generic data transport purposes.
Go was designed with simplicity and minimized programmer
effort in mind, thereby preventing a forest of features getting 1) Comparison with TCP: It is fruitful to compare TCP
in the way of program design: Less is exponentially more [6]. and SCTP, as TCP is the most widely-used protocol [5] and
The current version of the Go network library does not support SCTP is very similar to it:
the SCTP protocol and this paper examines how easy it is to 2) Multihoming: A host is said to be multihomed if it has
extend the Go network library with this protocol. The work in multiple IP addresses which can be associated with one or
this paper is based on the dissertation submitted for an MSc more physical interfaces connected to the same or different
in Computer Science at Birkbeck University in London and is networks[2]. TCP can only bind to one network address at
available as an open source project. each end of the connection. In the event of a network failure
there is no way to stay connected and send the data over
another physical path without setting up a new connection.
A. Relevance SCTP natively supports multihoming at the transport layer.
After ten years SCTP as a technology is becoming more This makes it possible to connect and transfer data to and
and more relevant. One notable implementation is the use of from multihomed hosts, so that when one network path fails,
SCTP as a data channel in the Web Real-Time Communication the connection seamlessly fails over using the remaining paths.
(WebRTC) standard [11], a HTML 5 extension to enable real And together with concurrent multipath transfer (CMT) [4] it
time video and audio communication in browsers. Google inc. is possible to increase data throughput by transferring data
and the Mozilla Foundation are each planning to release a simultaneously over multiple links.
2

3) In-built message boundaries: In TCP there are no mark- Figure 2: Head of line blocking
ers to indicate the start or end of a specific piece of data (a 9 user messages ready to be send
user message). All data sent over the socket is converted into
a byte stream and an application must add its own mechanism 9 8 7 6 5 4 3 2 1

to be able to reconstruct the messages. In SCTP the message


1 to 7 are send, message 4 does not arrive
boundaries of the data sent are preserved in the protocol. In the
event of the message being larger than the maximum package
9 8 7 6 5 3 2 1
size a notofication is sent to the application layer more is on
its way. 7 to 4 are resend
4) Protection against Denial of Service (DOS) attacks:
Another disadvantage of TCP is that it is open to ’SYN flood 9 8 7 6 5 4

attack’, a specific type of attack which can drain the server


of resources, resulting in a denial of service (nothing else can
connect). To prevent this, SCTP uses a four-way handshake to
Figure 3: Associations and streams
establish the connection, whereas TCP only uses a three-way
handshake. With a four-way handshake the originator has to Application Application

double-acknowledge itself (”is it really you?”) by resending


a cookie it previously received from the destination server Streams Streams

before that server will assign resources to the connection. This


Port Multiple IP addresses Port
prevents timeouts on the server side and thus makes this type SCTP SCTP
of denial of service impossible. To reduce start up delay, actual
data can also be sent during the second part of the handshake. IP IP

Figure 1: Initiating a network connection IP network

(a) TCP handshake (b) SCTP handshake

SCTP association

multiple incoming associations, meaning that multiple clients


can connect to a single server listening on a single socket.
The one-to-one model can only have a single association per
socket. The one-to-one model is for easy migration of existing
TCP applications, it maps one-to-one to the system calls TCP
makes to establish a connection. But it can only have one
connection per association, and thus only a single client can
5) SCTP Multistreaming: SCTP user messages in a single connect to a server. The one-to-one interface makes migrating
SCTP socket connection can be sent and delivered to the an existing application a relatively painless exercise. If you
application layer over independent streams. In case of two sets want to upgrade your existing TCP application to a one-to-
of user messages A and B, each set delivered sequentially, the many SCTP application, significant retooling is needed. [7, p.
messages of set A can be sent over a different stream than B. 267]
And in case a messages in set A gets missing and part of the
sequence needs to be resent this will not effect the data of set Figure 4: Socket API
B if it is sent over a different stream. This tackels the ’head
of line blocking’ problem (figure 2) where messages already Networked application protocol Networked
User process application layer
delivered need to be redelivered because they have to arrive client server

in order and becuase one message did not arrive. Socket API
6) Associations: A connection between a SCTP server and TCP Transport protocol TCP
UDP UDP transport layer
client is called an association. An association gets initiated SCTP SCTP

by a request from a SCTP client. So a listening server can Kernel

accept incoming requests from multiple clients. Messages sent IP


IP protocol
IP network layer

over the assocation have an association id attached to them.


to make it possible to know where they come from. Within Ethernet protocol
Ethernet Ethernet
datalink layer
a single association you can have multiple streams for data driver driver

transmission (See figure 3)


7) Two programming models: SCTP has two interfaces for
Ethernet
implementation in a networked application: one-to-one and
one-to-many. A single socket in a one-to-many model can have
3

8) Socket API: Figure 4 is an overview of two applications Figure 6: Ancillary data embedded in sendmsg() data structure
communicating over an IP network using a transport protocol
msghdr
(TCP, UDP or SCTP). The software engineer writing the client
SCTP_SNDINFO
and server in the application layer only has to know about msg_name sockaddr
snd_sid
the function calls exposing the functionality in the transport msg_iov iovec
snd_flags
layer. This collection of functions is called the socket API. A
msg_control snd_ppid
socket is an endpoint for networked communication; multiple
processes can open sockets via the API and data written into snd_context
cmsghdr
one socket can be read from another. The socket API consist cmsg_level sctp_sndinfo snd_assoc_id
&
of functions which can open and close the socket, send data cmsg_type
over it and set the behavior of a socket using ’socket options’.
An example of such behavior is the enabling or disabling of flags
data buffering before sending to reduce the number of packets
to sent over the network and therefore improve efficiency
(Nagle’s algorithm).
Table I: Ancillary data mappings
Figure 5: TCP and SCTP socket API sid stream identifier
ssn stream sequence number
TCP SCTP ppid identifier set by peer
aid association id
Server Client Server Client

socket() socket()

bind() bind() B. Go language overview

listen() socket() listen() socket()


In this section I will give a short overview of the main
accept() connect() features and characteristics of the Go language and provide
more detail where relevant to the project.
recv() send() recvmsg() sendmsg()
1) Data: Go has a syntax broadly similar to the C and
send() recv() sendmsg() recvmsg()
Java languages. Variables are declared either by variable name
followed by data type, or by name only with data type inferred
close() close() close() close()
(known as ’type inference’).’ With the initialization operator :=
variables can be declared and initialized in a single statement.
If not initialized explicitly, the variable is set to a default value.
9) SCTP Socket API: Figure 5 is an overview of both the Here are some examples:
TCP and SCTP socket APIs and gives the order in which the
system calls would be used in a simple client server application
sending and receiving messages. The server first creates a var number i n t
socket, this returns a file descriptor which can be used by the var f i r s t _ n a m e , last_name s t r i n g
var g r e e t i n g = h e l l o ( " o l i v i e r " )
bind function to to make a request to assign a address to it. f r a c t a l : = make ( [ ] uint64 , 10)
After this the server can start listening to incoming connections
with the listen() function. After this point TCP is different from
Go has two ways to create data structures: i) with ’new’,
SCTP. The TCP client actively connects to the peer and the
where the data structure is initialized to zero; ii) with ’make’,
server accepts the connection with the accept() and connect()
where the data structure is initialized to a specified value.
functions. With SCTP the connection set up hand handshake
happens implicitly when sending and receiving a message. At
this point the server and client can exchange messages and
var p ∗ [ ] i n t = new ( [ ] i n t )
finally the connection terminates with the close() function. var v [ ] i n t = make ( [ ] i n t , 100)
10) Socket I/O functions and ancillary data: The sendmsg()
and recvmsg() functions (see appendix D for definition) are the
An asterisk indicates that the variable is a pointer.
most general of all socket I/O functions [7, p. 390] and can be
used in combination with all protocols in the transport layer. 2) Functions: Unlike most commonly-used languages Go
Both functions have room for message data and ancillary data functions and methods can return multiple values. The follow-
(see appendix E). SCTP adds extra meta data which contains ing function expects a string and integer as input parameters
information about the message being sent and its connection. and returns a string and an error. If the count integer is zero
Table I gives an overview of the ancillary data mapped to the the function will return a nil and an error, otherwise it returns
return variables. the greeting and nil as error.
4

it talks and then gets cast (type changed) to an animal


func h e l l o ( name s t r i n g , count i n t ) ( g r e e t i n g object:
string , e r r e r r o r ) {
type animal i n t e r f a c e {
i f count = 0 {
Talk ( )
r e t u r n n i l , e r r o r s . New( " Cannot say
}
h e l l o zero t i m e s " )
}
type Cat
g r e e t i n g = " H e l l o " + name , n i l
return
func ( c Cat ) T a l k ( ) {
}
f m t . P r i n t l n ( "Meow" )
}
This hello function can be be called in the following func main ( ) {
manner: var c Cat
c . Talk ( )
a : = animal ( c ) / / Cast from c a t t o animal
a . Talk ( )
g r e e t i n g , e r r : = h e l l o ( " paard " , 0 ) }
i f err != n i l {
println ( " error ! " )
} else { 5) Pointers: Although Go has pointers (references to mem-
println ( greeting ) ory locations), they are implemented differently than in a
}
language like C. Pointer arithmetic is not possible so Go
pointers can only be used to pass values by reference. At a
Or if we are not interested in one of the return values, a lower level Go pointers can be converted to C pointers.
blank identifier _ can be used to discard the value: 6) Concurrency: Go has a basic primitive for concur-
rency called a goroutine. The name is not only a play on
g r e e t i n g , _ : = h e l l o ( " paard " , 1 ) a programming component called coroutine but is also its
implementation of it in the language. Coroutines are methods
3) Object and methods: In Go data structures (objects) are which call on each other, data comes in, gets processed and
defined as follows: gets passed to a the next coroutine. One of the advantages of
coroutines is they are generally easier to create and understand
[Knuth V1 p193]. But the main advantage is that it lends
type Person s t r u c t {
name s t r i n g
itself very well for distributed computing where data gets
age i n t passed around from one processor to another, maybe even on
}
a different computer.
In Go every function can become a goroutine by simple
Functionality can be added to an object by defining methods putting go in front of it. A gorouting can than communicate
associated with it. A difference between Go and other object- its input and output via channels. This concept is called
oriented languages like Java is that in Go this functionality is Communicating sequential processes (CSP) and is surprisingly
defined outside of the object, for example: versatile in its use. [3] Here an example:

func ( p Person ) SayHello ( name S t r i n g ) { 1 package main


r e t u r n " H e l l o " + name " , my name i s " + p . name 2
} 3 func receiveChan ( c i chan i n t ) {
4 for {
5 i : = <− c i
In the above example the SayHello method has a string as 6 println ( i )
input and is associated with a Person object p. 7 }
8 }
Methods can be associated with any type, not just objects. 9
In the following example the Add() method is associated with 10 func main ( ) {
11 c i : = make ( chan i n t )
an Array of strings: 12 go receiveChan ( c i )
13
14 f o r i : = 0 ; i < 1 0 ; i ++ {
15 c i <− i
type NameArray [ ] s t r i n g
16 }
func ( na NameArray ) Add ( name s t r i n g ) [ ] s t r i n g {
17 }
...
}
The receiveChan() function has as input a channel of
It’s worth noting that Go has objects but not inheritance. integers. On line 4 an endless for loop starts where line 5
4) Interfaces : As with other object oriented languages, waits for an integer to come in on the channel. The main
you can specify behavior of an object by using interfaces functions first creates a channel of integers. Line 12 starts the
and also implement multiple interfaces per object. The in- function receiveChan as a Go routine in the background. This
terfaces does not have to be explicitly named: as soon as is followed by a loop sending 10 integers over the channel to
the type implements the methods of the interface the com- the receiveChan function.
piler knows the relation between the two. In the follow- 7) Much more: There is more to the language like garbage
ing example, in function main, a cat object gets created, collection, first class functions, arrays, slices, however the
5

explanation of this falls outside the scope of this paper. More


information can be found on the Go website 1 . 1 package main
2 import " n e t "
3
4 func main ( ) {
5 l i s t e n , e r r : = n e t . L i s t e n ( " t c p " , " l o c a l h o s t :1234 "
)
III. G O NETWORKING 6 i f err != n i l {
7 return
8 }
9 b u f f e r : = make ( [ ] byte , 1024)
The following section contains the findings of my research 10 for {
11 conn , e r r : = l i s t e n . Accept ( )
on how Go combines the system level TCP socket API into 12 i f err != n i l {
an easy-to-use network library and how that works internally. 13 continue
14 }
Much of the TCP functionality is very similar to the SCTP 15 conn . Read ( b u f f e r )
stack and this will serve as an example of how to implement 16 println ( string ( buffer ) )
17 }
SCTP in Go. 18 }

The server gets created with the Listen() method and returns
an object which implements the Listener interface. On line 9
A. The network library a byte array buffer gets created for the received data. The
following for loop (10 - 17) will continuously wait to accept
Go provides a portable interface for network I/O. Basic an incoming connection (11), check for errors after connect,
interaction is provided by the Dial, Listen, ListenPacket and read from it (15) and convert the byte array to a string before
Accept functions which return implementations of the Conn, printing it (16).
PacketConn and Listener interfaces. The Listen function is
for byte stream data connections and the ListenPacket is for B. Under the hood
data transfer which includes messages boundaries, like UDP Go’s network communication library uses the same socket
or Unix domain sockets. These functions simplify the access API as any C program. In this section I will examine what
to the socket API, but if needed the application developer can happens when a network connection is set up and which socket
still access the socket API in more detail. API calls are made at what point. The example uses TCP. To
Here is an example of a typical TCP client and server illustrate how Go accesses the socket API I will take the TCP
application. The client sends a single message and the server client described in the previous section as an example and
waits to receive a message, prints it out after receiving and show how it interacts with the kernel by opening a connection.
starts listening again. First a simple client: Socket and Connect: For a TCP client to create a connection
and send some data the following system calls need to be made
in this order:
1 package main
2 import " n e t " 1) resolve IP address
3 2) socket()
4 func main ( ) {
5 conn , e r r : = n e t . D i a l ( " t c p " , " l o c a l h o s t :1234 " ) 3) setsockopt() (optional)
6 i f err != n i l { 4) connect()
7 return
8 } In Go all this is wrapped in the net.Dial() call.
9 defer conn . Close ( ) Figure 8 shows a sequence diagram of method calls after a
10 conn . W r i t e ( [ ] byte ( " H e l l o w o r l d ! " ) )
11 } client calls Dial(). First the hostname:port gets parsed and re-
solved to an IP address (1.1)2 . Net.dialAddr() (1.3) determines
the transport protocol type and calls the relevant method,
The function main() is the entry point of the program.
net.DialTCP() in this case (1.3.1). Next net.internetSocket()
The first step the client performs is ’dialing’ a server with
gets called which internally calls socket() in the syscall pack-
the TCP protocol (line 5). The Dial() function returns a
age. Syscall.socket() is an auto-generated method based on C
connection which is an implementation of the Conn interface.
header files which describe the socket API.
After checking for errors (6-8) the defer keyword before the
Every network protocol in Go has its own connection type.
connection close command indicates the connection can be
As you can see in figure 8 the generic Dial() method eventually
finished as soon as it is no longer needed. In this case it
reaches the specific DialTCP() method which returns a TCP-
happens immediately after the write so it does not make much
specific connection type. This type gets cast to the more
sense to defer it, but in larger programs with multiple exit
generic Conn type which can be used in the client application.
points you only need a single (deferred) close statement, which
If TCP-specific functionality is needed the Conn type can be
makes it easier to understand and maintain the program.
recast to a TCPConn which then makes it possible to access
Next the server: TCP-specific functionality.
2 There are more method calls behind this but they are not relevant for this
1 http://golang.org/doc/effective_go.html example
6

C. Auto-generation A. Mapping APIs


Native Go functions to access kernel system calls and data The SCTP socket API will follow closely the wrapping of
structures can be auto-generated via scripts. In FreeBSD, if the the TCP socket API in the Go network library as described
description of the system call is present in the syscalls.master in section III-B. Figure 7 show the mapping of the socket
file 3 , the function will be available through the syscall API to the different functions and methods. At the server side
package. Most system calls which are used in Go are wrapped the socket(), bind() and listen() functions are bundled into the
in methods to make them fit better the language. The structures ListenPacket() function which resides in the network package
which are passed to the system calls are created in the same (net). The ListenPacket() function returns an implementation
way. The source code directory of the syscall package contains of the PacketConn interface (See appendix B). The client
a file with a link pointing to a header file which describes the wraps the socket() function into the Dail() function. This
structures. functions returns a Conn (connection) interface which can
used for writing messages (Conn.WriteTo()) and these user
D. Go non-blocking networking messages can be received vie the ReadFrom() method on the
All networking in Go is non blocking which is not the PacketConn interface.
default in the TCP/IP model. As soon as a application tries
to retrieve data from a socket, e.g. via readmsg(), it will not
block until some data comes in, instead it will immediately B. SCTP specific
move on in the program. Reading data from the socket is 1) Receive SCTP specific information : To access SCTP
normally done in an endless loop. To make Go actually wait specific functionality, such as which stream the message has
for sending data back to the application Go implements the been sent on, or the association id, net.ListenSCTP() can be
reactor design pattern. The implementation of this software used. This method returns a SCTP specific type (SCTPConn)
pattern makes use of a mechanism in the kernel where you which has the method ReadFromSCTP() and WriteToSCTP()
can ask the kernel to send a signal back to the application added to it. These methods return and set the information
if certain events occur whilst you keep doing other things in contained by the SCTP receive information structure, added
the program. For example data being written to the socket as ancillary data when the system call recvmsg() returns.
descriptor. On BSD the system call kqeueu() and kevent() are
2) Send SCTP specific information: To be able to set SCTP
used to register and queue events. The functionality of this is
specific send information such as stream id or association id
handled by Go’s runtime.
via the SCTP Send Information Structure, the several methods
on the SCTPConn object can be used (See table II):
IV. D ESIGN
In the following section I will describe how the different Table II: Initialization parameters
Go network methods will map to the SCTP socket API.
Number of output streams (*SCTPConn) InitNumStreams(n int) error
Max number of input streams (*SCTPConn) InitMaxInStream(n int) error
Figure 7: Go SCTP network API Max number of attempts to connect (*SCTPConn) InitMaxAttempts(n int) error
Timeout (*SCTPConn) InitMaxInitTimeout(n int) error
Server Client

net.ListenPacket() A typical server which has access to SCTP specific func-


socket() tionality would look like this:

bind()
package main
import (
net.Dail()
" net "
listen() socket() " strconv "
)

PacketConn.ReadFrom() func main ( ) {


Conn.WriteTo() addr , _ : = n e t . ResolveSCTPAddr ( " s c t p " , " l o c a l h o s t :4242 " )
recvmsg() sendmsg() conn , _ : = n e t . ListenSCTP ( " s c t p " , addr )
defer conn . Close ( )
for {
PacketConn.WriteTo() Conn.WriteTo() message : = make ( [ ] byte , 1024)
_ , _ , stream , _ : = conn . ReadFromSCTP ( message )
sendmsg() recvmsg() p r i n t l n ( " stream " + s t r c o n v . I t o a ( i n t ( stream ) ) + " : "
+ s t r i n g ( message ) )
}
PacketConn.Close() Conn.Close() }

close() close()
In this program ListenSCTP returns a SCTP connection
type. This type implements Conn and PacketConn interface
3 System call name/number master file: and has the ReadFromSCTP method added to it. The println()
http://fxr.watson.org/fxr/source/kern/syscalls.master functions prints the stream id and the user message.
7

V. I MPLEMENTATION used. This method returns a SCTP-specific type (SCTPConn)


which has the method ReadFromSCTP() added to it:
In this section I will describe how the SCTP network
functionality can fit into the existing Go network framework.
The main goal of the SCTP application programming interface ( ∗SCTPConn ) . ReadFromSCTP ( message ∗ s t r i n g ) ( s i d
(API) design is to combine lower-level system calls in an i n t , ssn i n t , p p i d i n t , a i d i n t , addr
SCTPAddr , e r r e r r o r )
easy-to-use framework. This framework hides the underlying
complexity of the socket API but if needed gives access to
The ReadFromSCTP() method returns the information con-
all the functionality provided by the protocol. To make sure
tained by the SCTP receive information structure, added as
SCTP fits in the Go design philosophy, less is more, I will
ancillary data when the system call recvmsg() returns. A
make use as much as possible of the existing components and
typical server which has access to SCTP-specific functionality
interfaces in the Go network package. In the following section
would look like this:
I’ll e

package main
A. Server import (
" net "
For a server to be able to set up a SCTP association it " strconv "
)
needs to create a socket, bind to it, optionally set some socket
options and start listening to it. A typical server will access func main ( ) {
addr , _ : = n e t . ResolveSCTPAddr ( " s c t p " , " l o c a l h o s t
the socket API in the following sequence: :4242 " )
1) socket() conn , _ : = n e t . ListenSCTP ( " s c t p " , addr )
defer conn . Close ( )
2) bind() for {
3) listen() message : = make ( [ ] byte , 1024)
_ , _ , stream , _ : = conn . ReadFromSCTP ( message )
4) recvmsg() p r i n t l n ( " stream " + s t r c o n v . I t o a ( i n t ( stream ) )
+ " : " + s t r i n g ( message ) )
The socket(), bind() and listen() functions will be wrapped }
into a Listen() method which returns a connection type. }
There are three variations: net.Listen(), net.ListenPacket()
and net.ListenSCTP(). The Go network library provides the In this program ListenSCTP() returns a SCTP connection
net.ListenPacket() method for packet-oriented networking like type. This type implements the Conn and PacketConn inter-
UDP, and net.Listen() for stream-oriented networking like faces and adds the ReadFromSCTP() method.
TCP. SCTP which is packet-oriented, can also make use
of the net.ListenPacket() method. ListenPacket() returns an
B. Client
implementation of the PacketConn interface which can be used
to read and write messages (recvmsg()). A simple SCTP echo In Go a client connection sets itself up with a call to the
server using the PacketConn interface might look like this: Dial() method. The Dial() method returns the generic Conn
interface. Every network protocol in Go has its own Dial()
method which returns a protocol-specific connection type. In
package main
import " n e t " the case of SCTP this is the PacketConn type which has
underneath it a specific SCTP connection type (SCTPConn).
func main ( ) { A simple SCTP client sending a single message would look
conn , _ : = n e t . L i s t e n P a c k e t ( " s c t p " , "
l o c a l h o s t :4242 " ) like this:
defer conn . Close ( )
message : = make ( [ ] byte , 1024)
conn . ReadFrom ( message ) package main
p r i n t ( s t r i n g ( message ) ) import " n e t "
}
func main ( ) {
addr , _ : = n e t . ResolveSCTPAddr ( " s c t p " , " l o c a l h o s t
:4242 " )
After the the main entry point a connection object is created conn , _ : = n e t . DialSCTP ( " s c t p " , n i l , addr )
via the ListenPacket() method. The parameters of this method defer conn . Close ( )
message : = [ ] byte ( " paard " )
indicate that the connection should use the SCTP protocol conn . WriteTo ( message , addr )
and listen on localhost port 4242. The next line defers the }
closing of the connection when it is not needed anymore. After
creating a byte array buffer to store incoming data a message is The DialSCTP() method creates the socket and sets default
read from the connection. The ReadFrom() method will block socket options. Sending the message via WriteTo() will im-
until a message is received. Finally the message is printed and plicitly set up the connection.
the program ends. Send SCTP-specific information: To be able to set SCTP-
Receive SCTP-specific information: To access SCTP- specific send information such as stream id or association id
specific functionality, such as which stream the message has via the SCTP Send Information Structure, the WriteToSCTP()
been sent on, or the association id, net.ListenSCTP() can be method can be used:
8

Go source code repository. Building up to the official release


(∗SCTPConn ) . WriteToSCTP ( message ∗ s t r i n g , addr the Go development team did a considerable amount of work.
SCTPAddr , s i d i n t , ssn i n t , p p i d i n t , a i d i n t ,
err error ) With the 1.0 release a large number of changes had to be
incorporated into my own branch. As there were many changes
Creating and binding of the socket : The sequence diagram to the internals of Go, this resulted in many merge conflicts
in figure 9 gives an overview of how a socket is created and in certain areas, specifically around the implementation of the
bind to. At point 1.1.3 in this figure net.internetSocket() returns generic Dial and Listen interfaces. Most of the work in this
a socket descriptor which is used to create the actual SCTP area had to be redone.
connection type. At this point the SCTP initialization structure
is set to its default values together with the NODELAY socket D. Extensions
option. The ’no delay’ socket option disables buffering of There are many extensions to SCTP described in multiple
bytes sent over the connection. This is also the default setting RFCs. A complete SCTP implementation should include [9]:
for the TCP implementation in Go.
1) RFC4960 (basic SCTP)
2) RFC3758 (partial reliability)
VI. A NALYSIS
3) RFC4895 (authentication)
A. Performance 4) RFC5061 (dynamic addresses)
For performance testing a client-server application was The last three in this list are not included in this implementa-
designed, with the client sending messages and the server tion.
receiving them. Both a C and a Go version of this application
were created and compared and run against each other. The VII. C ONCLUSION
data throughput of an SCTP client-server application written in
Because of its similarity to existing protocols available
Go is approximately twice as high as the same program written
in the Go networking library, SCTP fits easily into it. The
in C. Most of the delay happens in the Go client. Unlike the C
biggest challenges of this project were the ongoing work on
implementation, the Go application needs to go through some
the SCTP specification and Go itself which made successful
additional method calls before it reaches the system call which
implementation a moving target. More recently (mid-way
sends the data. Go is also garbage-collected, which causes an
2012) the APIs of Go and SCTP have been stabilized. It
extra delay because several data structures are redeclared each
should be noted however that there are many extensions to
time a message is sent. Another factor to consider is that the
SCTP described in multiple RFCs. The work in this paper
implementation of SCTP in Go is against version 1.0.2 of
only looks at the bare minimum needed to make SCTP work
Go. This version does not have a compiler which is good at
in Go.
optimizing. Newer versions of Go address this issue.

B. Fitting like a glove A. Future work


Since Go already provides methods and interfaces for User land implementation of SCTP in Go: Not all operating
message-based data transmission that could be reused, and systems support the SCTP protocol natively. It is however
because of the SCTP socket API’s similarity to the TCP socket possible to have SCTP running on top of the UDP protocol,
API, making SCTP available in the Go network library was a outside the kernel (user land). To make this work a user land
relatively straightforward task. I was able to reuse the ancillary implementation of SCTP on top of UDP needs to be added
data structure from the Unix socket API and had only to add to Go. Once this is done SCTP could become available on all
the SCTP-specific ancillary data to the structure. It was easy different platforms supported by the Go language.
to follow the design philosophy ’less is exponentially more’:
the SCTP socket API could be wrapped in an easier-to-use R EFERENCES
API, just as it is done with TCP. This resulted in a Go SCTP [1] Why we need go - OReilly radar.
API which can be used in the most simple way, hiding all http://radar.oreilly.com/2012/09/golang.html.
[2] R. Braden. Requirements for internet hosts - communication layers.
complexity of the protocol or, if needed, it is possible to dive http://tools.ietf.org/html/rfc1122.
deeper and make use of more specific SCTP functionality. [3] C. A. R. Hoare. Communicating sequential processes. Communications
of the ACM, 21(8):666–677, August 1978.
[4] Janardhan R. Iyengar, Paul D. Amer, and Randall Stewart. Concurrent
C. Issues during implementation multipath transfer using SCTP multihoming over independent end-to-
end paths. IEEE/ACM Trans. Netw., 14(5):951–964, October 2006.
During implementation there were two major changes that [5] B. Penoff, A. Wagner, M. Tuxen, and I. Rungeler. Portable and perfor-
caused unexpected setbacks. The first, as previously men- mant userspace SCTP stack. In 2012 21st International Conference on
tioned, was the changing SCTP socket API, which made large Computer Communications and Networks (ICCCN), pages 1 –9, August
2012.
parts of the implementation already in place obsolete, forcing [6] Rob Pike. command center: Less is exponentially more.
me to rewrite the majority of the implementation. The second http://commandcenter.blogspot.co.uk/2012/06/less-is-exponentially-
issue was the first official release (1.0) of Go. Until that release more.html, June 2012.
[7] W. Richard Stevens, Bill Fenner, and Andrew M. Rudoff. Unix Network
I had sporadically synchronized my version of Go with the Programming: Sockets Networking API v. 1. Addison Wesley, 3 edition,
latest changes of the main development tree of the canonical November 2003.
9

[8] R. Stewart. RFC 4960 - stream control transmission protocol, motivation. E. Message header structure and ancillary data
http://tools.ietf.org/html/rfc4960#section-1.1.
[9] Randall Stewart, Michael Tuexen, and Peter Lei. SCTP: what is it, and
how to use it? BSDCan 2008, 2008. s t r u c t msghdr {
[10] Randall R. Stewart and Qiaobing Xie. Stream Control Transmission void ∗msg_name ; /∗ o p t i o n a l address ∗ /
Protocol (SCTP): A Reference Guide. Addison Wesley, 1 edition, socklen_t msg_namelen ; /∗ s i z e o f address ∗ /
s t r u c t i o v e c ∗msg_iov ; /∗ s c a t t e r / gather array ∗/
October 2001. int msg_iovlen ; /∗ # elements i n msg_iov ∗ /
[11] Michael Tuexen, Salvatore Loreto, and Randell Jesup. RTCWeb void ∗ msg_control ; /∗ a n c i l l a r y data ∗ /
datagram connection. http://tools.ietf.org/html/draft-ietf-rtcweb-data- socklen_t msg_controllen ; /∗ a n c i l l a r y data b u f f e r l e n ∗ /
channel-00. int msg_flags ; /∗ f l a g s on message ∗ /
};

The msg_control argument, which has length


A PPENDIX msg_controllen, points to a buffer for other
A. Conn interface protocol control related messages or other miscel-
laneous ancillary data. The messages are of the
form:
type Conn i n t e r f a c e { s t r u c t cmsghdr {
Read ( b [ ] byte ) ( n i n t , e r r e r r o r ) s o c k l e n _ t cmsg_len ; / ∗ data b y t e count , i n c l u d i n g hdr ∗ /
int cmsg_level ; / ∗ o r i g i n a t i n g p r o t o c o l ∗ /
W r i t e ( b [ ] byte ) ( n i n t , e r r e r r o r ) int cmsg_type ; / ∗ p r o t o c o l −s p e c i f i c t y p e ∗ /
/ ∗ f o l l o w e d by u_char cmsg_data [ ] ; ∗ /
Close ( ) e r r o r };
LocalAddr ( ) Addr

RemoteAddr ( ) Addr

SetDeadline ( t t i m e . Time ) e r r o r

SetReadDeadline ( t t i m e . Time ) e r r o r

S e t W r i t e D e a d l i n e ( t t i m e . Time ) e r r o r
}

B. PacketConn interface

type PacketConn i n t e r f a c e {

ReadFrom ( b [ ] byte ) ( n i n t , addr Addr , e r r e r r o r )

WriteTo ( b [ ] byte , addr Addr ) ( n i n t , e r r e r r o r )

Close ( ) e r r o r

LocalAddr ( ) Addr

SetDeadline ( t t i m e . Time ) e r r o r

SetReadDeadline ( t t i m e . Time ) e r r o r

S e t W r i t e D e a d l i n e ( t t i m e . Time ) e r r o r
}

C. Listener interface

type L i s t e n e r i n t e r f a c e {
Accept ( ) ( c Conn , e r r e r r o r )

Close ( ) e r r o r

Addr ( ) Addr
}

D. Socket IO function definition

s s i z e _ t sendmsg ( i n t s , const s t r u c t msghdr ∗msg , i n t f l a g s )

s s i z e _ t recvmsg ( i n t s , s t r u c t msghdr ∗msg , i n t f l a g s )


10

Figure 8: TCP Client setting up connection

net.Dial() net.resolveNetAddr() net.ResolveTCPAddr() net.dialAddr() net.DialTCP() net.internetSockets() syscall.socket()

1:
1.1:
1.1.1:

1.1.2: TCPAddr
1.2: Addr

1.3:
1.3.1:
1.3.1.1:
1.3.1.1.1:

1.3.1.1.2: int

1.3.1.2: netFD
1.3.2: TCPConn

1.4: Conn
11

Figure 9: Creating and bind

net.ListenPacket() net.ListenSCTP() net.internetSocket() net.socket() syscall.Socket() syscall.socket() syscall.Bind()

1:
1.1:
1.1.1: resolve address

1.1.2:
1.1.2.1:

1.1.2.1.1:
1.1.2.1.1.1:

1.1.2.1.1.2:

1.1.2.1.2:

1.1.2.1.3:

1.1.2.1.4:

1.1.2.2:
1.1.3:

1.1.4: listen
1.1.5:

You might also like