/src/glib/gio/gnetworking.c
Line | Count | Source (jump to first uncovered line) |
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 | | * SECTION:gnetworking |
30 | | * @title: gnetworking.h |
31 | | * @short_description: System networking includes |
32 | | * @include: gio/gnetworking.h |
33 | | * |
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. |
37 | | * |
38 | | * This can be used, for example, if you want to call setsockopt() |
39 | | * on a #GSocket. |
40 | | * |
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. |
46 | | * |
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. |
51 | | */ |
52 | | |
53 | | /** |
54 | | * g_networking_init: |
55 | | * |
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). |
60 | | * |
61 | | * Since: 2.36 |
62 | | */ |
63 | | void |
64 | | g_networking_init (void) |
65 | 0 | { |
66 | | #ifdef G_OS_WIN32 |
67 | | static gsize inited = 0; |
68 | | |
69 | | if (g_once_init_enter (&inited)) |
70 | | { |
71 | | WSADATA wsadata; |
72 | | |
73 | | if (WSAStartup (MAKEWORD (2, 0), &wsadata) != 0) |
74 | | g_error ("Windows Sockets could not be initialized"); |
75 | | |
76 | | g_once_init_leave (&inited, 1); |
77 | | } |
78 | | #endif |
79 | 0 | } |
80 | | |
81 | | gboolean |
82 | | g_getservbyname_ntohs (const char *name, const char *proto, guint16 *out_port) |
83 | 0 | { |
84 | 0 | struct servent *result; |
85 | |
|
86 | 0 | #ifdef HAVE_GETSERVBYNAME_R |
87 | 0 | struct servent result_buf; |
88 | 0 | char buf[2048]; |
89 | 0 | int r; |
90 | |
|
91 | 0 | r = getservbyname_r (name, proto, &result_buf, buf, sizeof (buf), &result); |
92 | 0 | if (r != 0 || result != &result_buf) |
93 | 0 | result = NULL; |
94 | | #else |
95 | | result = getservbyname (name, proto); |
96 | | #endif |
97 | |
|
98 | 0 | if (!result) |
99 | 0 | return FALSE; |
100 | 0 | *out_port = g_ntohs (result->s_port); |
101 | 0 | return TRUE; |
102 | 0 | } |