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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gnetworking.h"
26 /* For Windows XP run-time compatibility */
27 #include "gwin32networking.h"
29 GWin32WinsockFuncs ws2funcs
= {0};
34 * @title: gnetworking.h
35 * @short_description: System networking includes
36 * @include: gio/gnetworking.h
38 * The `<gio/gnetworking.h>` header can be included to get
39 * various low-level networking-related system headers, automatically
40 * taking care of certain portability issues for you.
42 * This can be used, for example, if you want to call setsockopt()
45 * Note that while WinSock has many of the same APIs as the
46 * traditional UNIX socket API, most of them behave at least slightly
47 * differently (particularly with respect to error handling). If you
48 * want your code to work under both UNIX and Windows, you will need
49 * to take these differences into account.
51 * Also, under GNU libc, certain non-portable functions are only visible
52 * in the headers if you define %_GNU_SOURCE before including them. Note
53 * that this symbol must be defined before including any headers, or it
54 * may not take effect.
60 * Initializes the platform networking libraries (eg, on Windows, this
61 * calls WSAStartup()). GLib will call this itself if it is needed, so
62 * you only need to call it if you directly call system networking
63 * functions (without calling any GLib networking functions first).
68 g_networking_init (void)
71 static volatile gsize inited
= 0;
73 if (g_once_init_enter (&inited
))
76 HMODULE ws2dll
, iphlpapidll
;
78 if (WSAStartup (MAKEWORD (2, 0), &wsadata
) != 0)
79 g_error ("Windows Sockets could not be initialized");
81 /* We want to use these functions if they are available, but
82 * still need to make sure the code still runs on Windows XP
84 ws2dll
= LoadLibraryW (L
"ws2_32.dll");
85 iphlpapidll
= LoadLibraryW (L
"iphlpapi.dll");
90 (PFN_InetNtop
) GetProcAddress (ws2dll
, "inet_ntop");
92 (PFN_InetPton
) GetProcAddress (ws2dll
, "inet_pton");
97 ws2funcs
.pInetNtop
= NULL
;
98 ws2funcs
.pInetPton
= NULL
;
101 if (iphlpapidll
!= NULL
)
103 ws2funcs
.pIfNameToIndex
=
104 (PFN_IfNameToIndex
) GetProcAddress (iphlpapidll
, "if_nametoindex");
105 FreeLibrary (iphlpapidll
);
108 ws2funcs
.pIfNameToIndex
= NULL
;
110 g_once_init_leave (&inited
, 1);