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:
- Create the socket with
g_socket_listen()to start receiving messages.
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.