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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "rpc-socket.h"
#include "rpcsvc.h"
#include "dict.h"
#include "logging.h"
#include "byte-order.h"
#include "common-utils.h"
#include "compat-errno.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
static int
nfs_rpcsvc_socket_server_get_local_socket (int addrfam, char *listenhost,
uint16_t listenport,
struct sockaddr *addr,
socklen_t *addr_len)
{
struct addrinfo hints, *res = 0;
char service[NI_MAXSERV];
int ret = -1;
memset (service, 0, sizeof (service));
sprintf (service, "%d", listenport);
memset (&hints, 0, sizeof (hints));
addr->sa_family = hints.ai_family = addrfam;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
ret = getaddrinfo(listenhost, service, &hints, &res);
if (ret != 0) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR,
"getaddrinfo failed for host %s, service %s (%s)",
listenhost, service, gai_strerror (ret));
ret = -1;
goto err;
}
memcpy (addr, res->ai_addr, res->ai_addrlen);
*addr_len = res->ai_addrlen;
freeaddrinfo (res);
ret = 0;
err:
return ret;
}
int
nfs_rpcsvc_socket_listen (int addrfam, char *listenhost, uint16_t listenport)
{
int sock = -1;
struct sockaddr_storage sockaddr;
socklen_t sockaddr_len;
int flags = 0;
int ret = -1;
int opt = 1;
ret = nfs_rpcsvc_socket_server_get_local_socket (addrfam, listenhost,
listenport,
SA (&sockaddr),
&sockaddr_len);
if (ret == -1)
return ret;
sock = socket (SA (&sockaddr)->sa_family, SOCK_STREAM, 0);
if (sock == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "socket creation failed"
" (%s)", strerror (errno));
goto err;
}
flags = fcntl (sock, F_GETFL);
if (flags == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "cannot get socket flags"
" (%s)", strerror(errno));
goto close_err;
}
ret = fcntl (sock, F_SETFL, flags | O_NONBLOCK);
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "cannot set socket "
"non-blocking (%s)", strerror (errno));
goto close_err;
}
ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "setsockopt() for "
"SO_REUSEADDR failed (%s)", strerror (errno));
goto close_err;
}
ret = bind (sock, (struct sockaddr *)&sockaddr, sockaddr_len);
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "binding socket failed:"
" %s", strerror (errno));
if (errno == EADDRINUSE)
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "Port is already"
" in use");
goto close_err;
}
ret = listen (sock, 10);
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "could not listen on"
" socket (%s)", strerror (errno));
goto close_err;
}
return sock;
close_err:
close (sock);
sock = -1;
err:
return sock;
}
int
nfs_rpcsvc_socket_accept (int listenfd)
{
int new_sock = -1;
struct sockaddr_storage new_sockaddr = {0, };
socklen_t addrlen = sizeof (new_sockaddr);
int flags = 0;
int ret = -1;
int on = 1;
new_sock = accept (listenfd, SA (&new_sockaddr), &addrlen);
if (new_sock == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR,"accept on socket failed");
goto err;
}
flags = fcntl (new_sock, F_GETFL);
if (flags == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "cannot get socket flags"
" (%s)", strerror(errno));
goto close_err;
}
ret = fcntl (new_sock, F_SETFL, flags | O_NONBLOCK);
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "cannot set socket "
"non-blocking (%s)", strerror (errno));
goto close_err;
}
#ifdef TCP_NODELAY
ret = setsockopt(new_sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "cannot set no-delay "
" socket option");
}
#endif
return new_sock;
close_err:
close (new_sock);
new_sock = -1;
err:
return new_sock;
}
ssize_t
nfs_rpcsvc_socket_read (int sockfd, char *readaddr, size_t readsize)
{
ssize_t dataread = 0;
ssize_t readlen = -1;
if (!readaddr)
return -1;
while (readsize > 0) {
readlen = read (sockfd, readaddr, readsize);
if (readlen == -1) {
if (errno != EAGAIN) {
dataread = -1;
break;
} else
break;
} else if (readlen == 0)
break;
dataread += readlen;
readaddr += readlen;
readsize -= readlen;
}
return dataread;
}
ssize_t
nfs_rpcsvc_socket_write (int sockfd, char *buffer, size_t size)
{
size_t writelen = -1;
ssize_t written = 0;
if (!buffer)
return -1;
while (size > 0) {
writelen = write (sockfd, buffer, size);
if (writelen == -1) {
if (errno != EAGAIN) {
written = -1;
break;
} else
break;
} else if (writelen == 0)
break;
written += writelen;
size -= writelen;
buffer += writelen;
}
return written;
}
int
nfs_rpcsvc_socket_peername (int sockfd, char *hostname, int hostlen)
{
struct sockaddr sa;
socklen_t sl = sizeof (sa);
int ret = EAI_FAIL;
if (!hostname)
return ret;
ret = getpeername (sockfd, &sa, &sl);
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "Failed to get peer name:"
" %s", strerror (errno));
ret = EAI_FAIL;
goto err;
}
ret = getnameinfo (&sa, sl, hostname, hostlen, NULL, 0, 0);
if (ret != 0)
goto err;
err:
return ret;
}
int
nfs_rpcsvc_socket_peeraddr (int sockfd, char *addrstr, int addrlen,
struct sockaddr *returnsa, socklen_t sasize)
{
struct sockaddr sa;
int ret = EAI_FAIL;
if (returnsa)
ret = getpeername (sockfd, returnsa, &sasize);
else {
sasize = sizeof (sa);
ret = getpeername (sockfd, &sa, &sasize);
}
if (ret == -1) {
gf_log (GF_RPCSVC_SOCK, GF_LOG_ERROR, "Failed to get peer addr:"
" %s", strerror (errno));
ret = EAI_FAIL;
goto err;
}
/* If caller did not specify a string into which the address can be
* stored, dont bother getting it.
*/
if (!addrstr) {
ret = 0;
goto err;
}
if (returnsa)
ret = getnameinfo (returnsa, sasize, addrstr, addrlen, NULL, 0,
NI_NUMERICHOST);
else
ret = getnameinfo (&sa, sasize, addrstr, addrlen, NULL, 0,
NI_NUMERICHOST);
err:
return ret;
}
int
nfs_rpcsvc_socket_block_tx (int sockfd)
{
int ret = -1;
int on = 1;
#ifdef TCP_CORK
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, &on, sizeof(on));
#endif
#ifdef TCP_NOPUSH
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof(on));
#endif
return ret;
}
int
nfs_rpcsvc_socket_unblock_tx (int sockfd)
{
int ret = -1;
int off = 0;
#ifdef TCP_CORK
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, &off, sizeof(off));
#endif
#ifdef TCP_NOPUSH
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NOPUSH, &off, sizeof(off));
#endif
return ret;
}
|