Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: Re: [Wireshark-dev] Portable sockets

From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Wed, 10 Feb 2016 10:10:45 -0800
On Feb 10, 2016, at 7:51 AM, Dario Lombardo <dario.lombardo.ml@xxxxxxxxx> wrote:

> I'm working on a new extcap that listens on a port for incoming packets produced by capture devices. I have to create a small, standard udp server. I was wandering which functions I am supposed to use to be portable. Are POSIX sockets ok on Windows

Sort of.  There's Winsock:

	https://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx

but there might have to be some "#ifdef _WIN32/#else/#endif" in your code to handle, for example, socket descriptors being regular file descriptors in UN*X but being of type "SOCKET" in Winsock.

> (I suppose they're fine on OS X)?

Given that

	1) OS X is a Single UNIX Specification-compliant OS (i.e., compliant with the current version of POSIX);

	2) OS X's socket code is a derivative of the 4.4-Lite socket code, which is a descendant of the original BSD socket code;

sockets work the same as on other BSD-flavored UN*Xes. (That means some things might be a little different than on, say, Linux, or Solaris, or..., in ways that aren't specified by the Single UNIX Specification - Linux's select(), for example, modifies the timeout argument passed to it, which is allowed, but not required, by the SUS, but other UN*X's select() generally doesn't. However, as long as you carefully follow the SUS, the code should work on current versions of SUS-compliant UNIXes and probably also work on *BSD and Linux.  For example, for select(), neither depend on the timeout being modified nor depend on it *not* being modified; set the timeout variable before each select() call, rather than initializing it once and using that value in a loop, and don't use the value of the variable after select() returns.)

> Or should I go with something like GSocket?
> 
> https://developer.gnome.org/gio/stable/GSocket.html

If you only expect the server to run on machines with Wireshark, that's probably good enough; they seem to indicate that it papers over the differences between UN*X sockets and Winsock, and requiring GLib isn't a problem if you only expect it to be run on machines with Wireshark or TShark installed, given that they require GLib.

The only reason *not* to go with GSocket might be if you don't want to require GLib on the target platform.