Blame gio/gnetworking.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit ae235b
Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gnetworking.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
/* For Windows XP run-time compatibility */
Packit ae235b
#include "gwin32networking.h"
Packit ae235b
Packit ae235b
GWin32WinsockFuncs ws2funcs = {0};
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gnetworking
Packit ae235b
 * @title: gnetworking.h
Packit ae235b
 * @short_description: System networking includes
Packit ae235b
 * @include: gio/gnetworking.h
Packit ae235b
 *
Packit ae235b
 * The `<gio/gnetworking.h>` header can be included to get
Packit ae235b
 * various low-level networking-related system headers, automatically
Packit ae235b
 * taking care of certain portability issues for you.
Packit ae235b
 *
Packit ae235b
 * This can be used, for example, if you want to call setsockopt()
Packit ae235b
 * on a #GSocket.
Packit ae235b
 *
Packit ae235b
 * Note that while WinSock has many of the same APIs as the
Packit ae235b
 * traditional UNIX socket API, most of them behave at least slightly
Packit ae235b
 * differently (particularly with respect to error handling). If you
Packit ae235b
 * want your code to work under both UNIX and Windows, you will need
Packit ae235b
 * to take these differences into account.
Packit ae235b
 *
Packit ae235b
 * Also, under GNU libc, certain non-portable functions are only visible
Packit ae235b
 * in the headers if you define %_GNU_SOURCE before including them. Note
Packit ae235b
 * that this symbol must be defined before including any headers, or it
Packit ae235b
 * may not take effect.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_networking_init:
Packit ae235b
 *
Packit ae235b
 * Initializes the platform networking libraries (eg, on Windows, this
Packit ae235b
 * calls WSAStartup()). GLib will call this itself if it is needed, so
Packit ae235b
 * you only need to call it if you directly call system networking
Packit ae235b
 * functions (without calling any GLib networking functions first).
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_networking_init (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  static volatile gsize inited = 0;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&inited))
Packit ae235b
    {
Packit ae235b
      WSADATA wsadata;
Packit ae235b
      HMODULE ws2dll, iphlpapidll;
Packit ae235b
Packit ae235b
      if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
Packit ae235b
        g_error ("Windows Sockets could not be initialized");
Packit ae235b
Packit ae235b
      /* We want to use these functions if they are available, but
Packit ae235b
       * still need to make sure the code still runs on Windows XP
Packit ae235b
       */
Packit ae235b
      ws2dll = LoadLibraryW (L"ws2_32.dll");
Packit ae235b
      iphlpapidll = LoadLibraryW (L"iphlpapi.dll");
Packit ae235b
Packit ae235b
      if (ws2dll != NULL)
Packit ae235b
        {
Packit ae235b
          ws2funcs.pInetNtop =
Packit ae235b
            (PFN_InetNtop) GetProcAddress (ws2dll, "inet_ntop");
Packit ae235b
          ws2funcs.pInetPton =
Packit ae235b
            (PFN_InetPton) GetProcAddress (ws2dll, "inet_pton");
Packit ae235b
          FreeLibrary (ws2dll);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          ws2funcs.pInetNtop = NULL;
Packit ae235b
          ws2funcs.pInetPton = NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (iphlpapidll != NULL)
Packit ae235b
        {
Packit ae235b
          ws2funcs.pIfNameToIndex =
Packit ae235b
            (PFN_IfNameToIndex) GetProcAddress (iphlpapidll, "if_nametoindex");
Packit ae235b
          FreeLibrary (iphlpapidll);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        ws2funcs.pIfNameToIndex = NULL;
Packit ae235b
      
Packit ae235b
      g_once_init_leave (&inited, 1);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
}