1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2011 Red Hat, Inc.
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "gnetworking.h"
26 #include "gnetworkingprivate.h"
30 * @title: gnetworking.h
31 * @short_description: System networking includes
32 * @include: gio/gnetworking.h
34 * The `<gio/gnetworking.h>` header can be included to get
35 * various low-level networking-related system headers, automatically
36 * taking care of certain portability issues for you.
38 * This can be used, for example, if you want to call setsockopt()
41 * Note that while WinSock has many of the same APIs as the
42 * traditional UNIX socket API, most of them behave at least slightly
43 * differently (particularly with respect to error handling). If you
44 * want your code to work under both UNIX and Windows, you will need
45 * to take these differences into account.
47 * Also, under GNU libc, certain non-portable functions are only visible
48 * in the headers if you define %_GNU_SOURCE before including them. Note
49 * that this symbol must be defined before including any headers, or it
50 * may not take effect.
56 * Initializes the platform networking libraries (eg, on Windows, this
57 * calls WSAStartup()). GLib will call this itself if it is needed, so
58 * you only need to call it if you directly call system networking
59 * functions (without calling any GLib networking functions first).
64 g_networking_init (void)
67 static gsize inited = 0;
69 if (g_once_init_enter (&inited))
73 if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
74 g_error ("Windows Sockets could not be initialized");
76 g_once_init_leave (&inited, 1);
82 g_getservbyname_ntohs (const char *name, const char *proto, guint16 *out_port)
84 struct servent *result;
86 #ifdef HAVE_GETSERVBYNAME_R
87 struct servent result_buf;
91 r = getservbyname_r (name, proto, &result_buf, buf, sizeof (buf), &result);
92 if (r != 0 || result != &result_buf)
95 result = getservbyname (name, proto);
100 *out_port = g_ntohs (result->s_port);