Posted by: Airtower | 2010-06-30

Using SCTP in GLib

A while ago, I wrote about using a UDP socket as event source in GLib. Now, I’m migrating my program to SCTP. The main reason is that SCTP provides reliability for sequential messages, but some of the advanced features might become useful later. Surprisingly few changes were necessary in my program:

The relevant code parts now look like this:

GSocket *sock;
GError *err = NULL;
sock = g_socket_new(G_SOCKET_FAMILY_IPV6,
                    G_SOCKET_TYPE_SEQPACKET,
                    G_SOCKET_PROTOCOL_SCTP,
                    &err);
g_assert(err == NULL);

g_socket_bind(sock,
              G_SOCKET_ADDRESS(addr),
              TRUE,
              &err);
g_assert(err == NULL);

int fd = g_socket_get_fd(sock);
GIOChannel* channel = g_io_channel_unix_new(fd);
guint source = g_io_add_watch(channel, G_IO_IN,
                              (GIOFunc) callback_function, data);
g_io_channel_unref(channel);

g_socket_listen(sock, &err);
g_assert(err == NULL);

Sadly, it seems like there are no functions for GSocket to use lower level SCTP features, like getting the list of peer and local IP addresses in the association. I suppose this is possible by using the normal low level functions (e.g. sctp_getpaddrs()) on the file descriptor returned by g_socket_get_fd(), but I haven’t tried yet.

Advertisements

Responses

  1. […] in stream mode SCTP sockets can operate in two modes: SOCK_SEQPACKET or SOCK_STREAM. When migrating my server from UDP to SCTP, I initially used the sequential packet mode. However, changing to stream mode offered me two […]


Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: