summaryrefslogtreecommitdiff
path: root/server/net-utils.c
blob: 7cad5342de285e328dffdeaee3c3ce4f0acbfa72 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2009, 2017 Red Hat, Inc.

   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, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#ifndef _WIN32
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#endif

#include <common/log.h>

#include "net-utils.h"
#include "sys-socket.h"

#if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE) && defined(__APPLE__)
#define TCP_KEEPIDLE TCP_KEEPALIVE
#endif

#if defined(EOPNOTSUPP) && EOPNOTSUPP != ENOTSUP
#define NOTSUP_ERROR(err) ((err) == ENOTSUP || (err) == EOPNOTSUPP)
#else
#define NOTSUP_ERROR(err) ((err) == ENOTSUP)
#endif

static inline bool
darwin_einval_on_unix_socket(int fd, int err)
{
#if defined(__APPLE__) || defined(__FreeBSD__)
    if (err == EINVAL) {
        union {
            struct sockaddr sa;
            char buf[1024];
        } addr;
        socklen_t len = sizeof(addr);

        if (getsockname(fd, &addr.sa, &len) == 0 && addr.sa.sa_family == AF_UNIX) {
            return true;
        }
    }
#endif
    return false;
}

/**
 * red_socket_set_keepalive:
 * @fd: a socket file descriptor
 * @keepalive: whether to enable keepalives on @fd
 *
 * Returns: #true if the operation succeeded, #false otherwise.
 */
bool red_socket_set_keepalive(int fd, bool enable, int timeout)
{
    int keepalive = !!enable;

    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) {
        if (!NOTSUP_ERROR(errno) && !darwin_einval_on_unix_socket(fd, errno)) {
            g_warning("setsockopt for keepalive failed, %s", strerror(errno));
            return false;
        }
    }

    if (!enable) {
        return true;
    }

#ifdef TCP_KEEPIDLE
    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout)) == -1) {
        if (!NOTSUP_ERROR(errno) && !darwin_einval_on_unix_socket(fd, errno)) {
            g_warning("setsockopt for keepalive timeout failed, %s", strerror(errno));
            return false;
        }
    }
#endif

    return true;
}

/**
 * red_socket_set_no_delay:
 * @fd: a socket file descriptor
 * @no_delay: whether to enable TCP_NODELAY on @fd
 *
 * Returns: #true if the operation succeeded, #false otherwise.
 */
bool red_socket_set_no_delay(int fd, bool no_delay)
{
    int optval = no_delay;

    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
                   &optval, sizeof(optval)) != 0) {
        if (!NOTSUP_ERROR(errno) && errno != ENOPROTOOPT &&
            !darwin_einval_on_unix_socket(fd, errno)) {
            spice_warning("setsockopt failed, %s", strerror(errno));
            return false;
        }
    }

    return true;
}

/**
 * red_socket_set_non_blocking:
 * @fd: a socket file descriptor
 * @non_blocking: whether to enable O_NONBLOCK on @fd
 *
 * Returns: #true if the operation succeeded, #false otherwise.
 */
bool red_socket_set_non_blocking(int fd, bool non_blocking)
{
#ifdef _WIN32
    u_long ioctl_nonblocking = 1;

    if (ioctlsocket(fd, FIONBIO, &ioctl_nonblocking) != 0) {
        spice_warning("ioctlsocket(FIONBIO) failed, %d", WSAGetLastError());
        return false;
    }
    return true;
#else
    int flags;

    if ((flags = fcntl(fd, F_GETFL)) == -1) {
        spice_warning("fnctl(F_GETFL) failed, %s", strerror(errno));
        return false;
    }

    if (non_blocking) {
        flags |= O_NONBLOCK;
    } else {
        flags &= ~O_NONBLOCK;
    }

    if (fcntl(fd, F_SETFL, flags) == -1) {
        spice_warning("fnctl(F_SETFL) failed, %s", strerror(errno));
        return false;
    }

    return true;
#endif
}

/**
 * red_socket_get_no_delay:
 * @fd: a socket file descriptor
 *
 * Returns: The current value of TCP_NODELAY for @fd, -1 if an error occurred
 */
int red_socket_get_no_delay(int fd)
{
    int delay_val;
    socklen_t opt_size = sizeof(delay_val);

    if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &delay_val,
                   &opt_size) == -1) {
            spice_warning("getsockopt failed, %s", strerror(errno));
            return -1;
    }

    return delay_val;
}

/**
 * red_socket_set_nosigpipe
 * @fd: a socket file descriptor
 */
void red_socket_set_nosigpipe(int fd, bool enable)
{
#if defined(SO_NOSIGPIPE) && defined(__APPLE__)
    int val = !!enable;
    setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const void *) &val, sizeof(val));
#endif
}