summaryrefslogtreecommitdiff
path: root/lib/gibber/gibber-sockets.c
blob: bcedeb53bf3e3ec41480681ee5934a47f6d3021e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * gibber-sockets.c - basic portability wrappers for BSD/Winsock differences
 *
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include "gibber-sockets.h"

#include <errno.h>

#define DEBUG_FLAG DEBUG_NET
#include "gibber-debug.h"

gboolean
gibber_connect_errno_requires_retry (void)
{
#ifdef G_OS_WIN32
  int err = WSAGetLastError ();

  return (err == WSAEINPROGRESS || err == WSAEALREADY);
#else
  return (errno == EINPROGRESS || errno == EALREADY);
#endif
}

gint
gibber_socket_errno (void)
{
#ifdef G_OS_WIN32
  return WSAGetLastError ();
#else
  return errno;
#endif
}

const gchar *
gibber_socket_strerror (void)
{
#ifdef G_OS_WIN32
  return "[no strerror() in winsock :-(]";
#else
  return g_strerror (errno);
#endif
}

gboolean
gibber_socket_errno_is_eafnosupport (void)
{
#ifdef G_OS_WIN32
  return (WSAGetLastError () == WSAEAFNOSUPPORT);
#else
  return (errno == EAFNOSUPPORT);
#endif
}

gboolean
gibber_socket_errno_is_eaddrinuse (void)
{
#ifdef G_OS_WIN32
  return (WSAGetLastError () == WSAEADDRINUSE);
#else
  return (errno == EADDRINUSE);
#endif
}

void
gibber_socket_set_error (GError **error, const gchar *context,
    GQuark domain, gint code)
{
  gint err = gibber_socket_errno ();
  const gchar *str = gibber_socket_strerror ();

  DEBUG ("%s: #%d %s", context, err, str);
  g_set_error (error, domain, code, "%s: #%d %s", context, err, str);
}

GIOChannel *
gibber_io_channel_new_from_socket (gint sockfd)
{
#ifdef G_OS_WIN32
  return g_io_channel_win32_new_socket (sockfd);
#else
  return g_io_channel_unix_new (sockfd);
#endif
}

void
gibber_socket_set_nonblocking (gint sockfd)
{
#ifdef G_OS_WIN32
  u_long please_dont_block = 1;

  ioctlsocket (sockfd, FIONBIO, &please_dont_block);
#else
  fcntl (sockfd, F_SETFL, O_NONBLOCK);
#endif
}