1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2011 Red Hat, Inc.
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 *
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.
13 *
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.
18 *
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/>.
21 */
22
23 #include "config.h"
24
25 #include "gnetworking.h"
26 #include "gnetworkingprivate.h"
27
28 /**
29 * g_networking_init:
30 *
31 * Initializes the platform networking libraries (eg, on Windows, this
32 * calls WSAStartup()). GLib will call this itself if it is needed, so
33 * you only need to call it if you directly call system networking
34 * functions (without calling any GLib networking functions first).
35 *
36 * Since: 2.36
37 */
38 void
39 g_networking_init (void)
40 {
41 #ifdef G_OS_WIN32
42 static gsize inited = 0;
43
44 if (g_once_init_enter (&inited))
45 {
46 WSADATA wsadata;
47
48 if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0)
49 g_error ("Windows Sockets could not be initialized");
50
51 g_once_init_leave (&inited, 1);
52 }
53 #endif
54 }
55
56 gboolean
57 g_getservbyname_ntohs (const char *name, const char *proto, guint16 *out_port)
58 {
59 struct servent *result;
60
61 #ifdef HAVE_GETSERVBYNAME_R
62 struct servent result_buf;
63 char buf[2048];
64 int r;
65
66 r = getservbyname_r (name, proto, &result_buf, buf, sizeof (buf), &result);
67 if (r != 0 || result != &result_buf)
68 result = NULL;
69 #else
70 result = getservbyname (name, proto);
71 #endif
72
73 if (!result)
74 return FALSE;
75 *out_port = g_ntohs (result->s_port);
76 return TRUE;
77 }