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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/* $%BEGINLICENSE%$
Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc
This program 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; version 2 of the License.
This program 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$%ENDLICENSE%$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef _WIN32
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h> /* writev */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_FILIO_H
/**
* required for FIONREAD on solaris
*/
#include <sys/filio.h>
#endif
#include <arpa/inet.h> /** inet_ntoa */
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#else
#include <winsock2.h>
#include <io.h>
#define ioctl ioctlsocket
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_WRITEV
#define USE_BUFFERED_NETIO
#else
#undef USE_BUFFERED_NETIO
#endif
#ifdef _WIN32
#define E_NET_CONNRESET WSAECONNRESET
#define E_NET_CONNABORTED WSAECONNABORTED
#define E_NET_WOULDBLOCK WSAEWOULDBLOCK
#define E_NET_INPROGRESS WSAEINPROGRESS
#else
#define E_NET_CONNRESET ECONNRESET
#define E_NET_CONNABORTED ECONNABORTED
#define E_NET_INPROGRESS EINPROGRESS
#if EWOULDBLOCK == EAGAIN
/**
* some system make EAGAIN == EWOULDBLOCK which would lead to a
* error in the case handling
*
* set it to -1 as this error should never happen
*/
#define E_NET_WOULDBLOCK -1
#else
#define E_NET_WOULDBLOCK EWOULDBLOCK
#endif
#endif
#include "network-socket.h"
#include "network-mysqld-proto.h"
#include "network-mysqld-packet.h"
#ifndef DISABLE_DEPRECATED_DECL
network_socket *network_socket_init() {
return network_socket_new();
}
#endif
network_socket *network_socket_new() {
network_socket *s;
s = g_new0(network_socket, 1);
s->send_queue = network_queue_new();
s->recv_queue = network_queue_new();
s->recv_queue_raw = network_queue_new();
s->default_db = g_string_new(NULL);
s->fd = -1;
s->socket_type = SOCK_STREAM; /* let's default to TCP */
s->packet_id_is_reset = TRUE;
s->src = network_address_new();
s->dst = network_address_new();
return s;
}
void network_socket_free(network_socket *s) {
if (!s) return;
network_queue_free(s->send_queue);
network_queue_free(s->recv_queue);
network_queue_free(s->recv_queue_raw);
if (s->response) network_mysqld_auth_response_free(s->response);
if (s->challenge) network_mysqld_auth_challenge_free(s->challenge);
network_address_free(s->dst);
network_address_free(s->src);
event_del(&(s->event));
if (s->fd != -1) {
closesocket(s->fd);
}
g_string_free(s->default_db, TRUE);
g_free(s);
}
/**
* portable 'set non-blocking io'
*
* @param sock a socket
* @return NETWORK_SOCKET_SUCCESS on success, NETWORK_SOCKET_ERROR on error
*/
network_socket_retval_t network_socket_set_non_blocking(network_socket *sock) {
int ret;
#ifdef _WIN32
int ioctlvar;
ioctlvar = 1;
ret = ioctlsocket(sock->fd, FIONBIO, &ioctlvar);
#else
ret = fcntl(sock->fd, F_SETFL, O_NONBLOCK | O_RDWR);
#endif
if (ret != 0) {
#ifdef _WIN32
errno = WSAGetLastError();
#endif
g_critical("%s.%d: set_non_blocking() failed: %s (%d)",
__FILE__, __LINE__,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
return NETWORK_SOCKET_SUCCESS;
}
/**
* accept a connection
*
* event handler for listening connections
*
* @param srv a listening socket
*
*/
network_socket *network_socket_accept(network_socket *srv) {
network_socket *client;
g_return_val_if_fail(srv, NULL);
g_return_val_if_fail(srv->socket_type == SOCK_STREAM, NULL); /* accept() only works on stream sockets */
client = network_socket_new();
if (-1 == (client->fd = accept(srv->fd, &client->src->addr.common, &(client->src->len)))) {
network_socket_free(client);
return NULL;
}
network_socket_set_non_blocking(client);
if (network_address_refresh_name(client->src)) {
network_socket_free(client);
return NULL;
}
/* the listening side may be INADDR_ANY, let's get which address the client really connected to */
if (-1 == getsockname(client->fd, &client->dst->addr.common, &(client->dst->len))) {
network_address_reset(client->dst);
} else if (network_address_refresh_name(client->dst)) {
network_address_reset(client->dst);
}
return client;
}
static network_socket_retval_t network_socket_connect_setopts(network_socket *sock) {
#ifdef WIN32
char val = 1; /* Win32 setsockopt wants a const char* instead of the UNIX void*...*/
#else
int val = 1;
#endif
/**
* set the same options as the mysql client
*/
#ifdef IP_TOS
val = 8;
setsockopt(sock->fd, IPPROTO_IP, IP_TOS, &val, sizeof(val));
#endif
val = 1;
setsockopt(sock->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val) );
val = 1;
setsockopt(sock->fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val) );
/* the listening side may be INADDR_ANY, let's get which address the client really connected to */
if (-1 == getsockname(sock->fd, &sock->src->addr.common, &(sock->src->len))) {
g_debug("%s: getsockname() failed: %s (%d)",
G_STRLOC,
g_strerror(errno),
errno);
network_address_reset(sock->src);
} else if (network_address_refresh_name(sock->src)) {
g_debug("%s: network_address_refresh_name() failed",
G_STRLOC);
network_address_reset(sock->src);
}
return NETWORK_SOCKET_SUCCESS;
}
/**
* finish the non-blocking connect()
*
* sets 'errno' as if connect() would have failed
*
*/
network_socket_retval_t network_socket_connect_finish(network_socket *sock) {
int so_error = 0;
network_socklen_t so_error_len = sizeof(so_error);
/**
* we might get called a 2nd time after a connect() == EINPROGRESS
*/
#ifdef _WIN32
/* need to cast to get rid of the compiler warning. otherwise identical to the UNIX version below. */
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &so_error_len)) {
errno = WSAGetLastError();
#else
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &so_error, &so_error_len)) {
#endif
/* getsockopt failed */
g_critical("%s: getsockopt(%s) failed: %s (%d)",
G_STRLOC,
sock->dst->name->str, g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
switch (so_error) {
case 0:
network_socket_connect_setopts(sock);
return NETWORK_SOCKET_SUCCESS;
default:
errno = so_error;
return NETWORK_SOCKET_ERROR_RETRY;
}
}
/**
* connect a socket
*
* the sock->addr has to be set before
*
* @param sock a socket
* @return NETWORK_SOCKET_SUCCESS on connected, NETWORK_SOCKET_ERROR on error, NETWORK_SOCKET_ERROR_RETRY for try again
* @see network_address_set_address()
*/
network_socket_retval_t network_socket_connect(network_socket *sock) {
g_return_val_if_fail(sock->dst, NETWORK_SOCKET_ERROR); /* our _new() allocated it already */
g_return_val_if_fail(sock->dst->name->len, NETWORK_SOCKET_ERROR); /* we want to use the ->name in the error-msgs */
g_return_val_if_fail(sock->fd < 0, NETWORK_SOCKET_ERROR); /* we already have a valid fd, we don't want to leak it */
g_return_val_if_fail(sock->socket_type == SOCK_STREAM, NETWORK_SOCKET_ERROR);
/**
* create a socket for the requested address
*
* if the dst->addr isn't set yet, socket() will fail with unsupported type
*/
if (-1 == (sock->fd = socket(sock->dst->addr.common.sa_family, sock->socket_type, 0))) {
#ifdef _WIN32
errno = WSAGetLastError();
#endif
g_critical("%s.%d: socket(%s) failed: %s (%d)",
__FILE__, __LINE__,
sock->dst->name->str, g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
/**
* make the connect() call non-blocking
*
*/
network_socket_set_non_blocking(sock);
if (-1 == connect(sock->fd, &sock->dst->addr.common, sock->dst->len)) {
#ifdef _WIN32
errno = WSAGetLastError();
#endif
/**
* in most TCP cases we connect() will return with
* EINPROGRESS ... 3-way handshake
*/
switch (errno) {
case E_NET_INPROGRESS:
case E_NET_WOULDBLOCK: /* win32 uses WSAEWOULDBLOCK */
return NETWORK_SOCKET_ERROR_RETRY;
default:
g_critical("%s.%d: connect(%s) failed: %s (%d)",
__FILE__, __LINE__,
sock->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
}
network_socket_connect_setopts(sock);
return NETWORK_SOCKET_SUCCESS;
}
/**
* connect a socket
*
* the con->dst->addr has to be set before
*
* @param con a socket
* @return NETWORK_SOCKET_SUCCESS on connected, NETWORK_SOCKET_ERROR on error
*
* @see network_address_set_address()
*/
network_socket_retval_t network_socket_bind(network_socket * con) {
#ifdef WIN32
char val = 1; /* Win32 setsockopt wants a const char* instead of the UNIX void*...*/
#else
int val = 1;
#endif
g_return_val_if_fail(con->fd < 0, NETWORK_SOCKET_ERROR); /* socket is already bound */
g_return_val_if_fail((con->socket_type == SOCK_DGRAM) || (con->socket_type == SOCK_STREAM), NETWORK_SOCKET_ERROR);
if (con->socket_type == SOCK_STREAM) {
g_return_val_if_fail(con->dst, NETWORK_SOCKET_ERROR);
g_return_val_if_fail(con->dst->name->len > 0, NETWORK_SOCKET_ERROR);
if (-1 == (con->fd = socket(con->dst->addr.common.sa_family, con->socket_type, 0))) {
g_critical("%s: socket(%s) failed: %s (%d)",
G_STRLOC,
con->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
if (con->dst->addr.common.sa_family == AF_INET ||
con->dst->addr.common.sa_family == AF_INET6) {
if (0 != setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))) {
g_critical("%s: setsockopt(%s, IPPROTO_TCP, TCP_NODELAY) failed: %s (%d)",
G_STRLOC,
con->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
if (0 != setsockopt(con->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) {
g_critical("%s: setsockopt(%s, SOL_SOCKET, SO_REUSEADDR) failed: %s (%d)",
G_STRLOC,
con->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
}
if (-1 == bind(con->fd, &con->dst->addr.common, con->dst->len)) {
g_critical("%s: bind(%s) failed: %s (%d)",
G_STRLOC,
con->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
if (-1 == listen(con->fd, 128)) {
g_critical("%s: listen(%s, 128) failed: %s (%d)",
G_STRLOC,
con->dst->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
} else {
/* UDP sockets bind the ->src address */
g_return_val_if_fail(con->src, NETWORK_SOCKET_ERROR);
g_return_val_if_fail(con->src->name->len > 0, NETWORK_SOCKET_ERROR);
if (-1 == (con->fd = socket(con->src->addr.common.sa_family, con->socket_type, 0))) {
g_critical("%s: socket(%s) failed: %s (%d)",
G_STRLOC,
con->src->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
if (-1 == bind(con->fd, &con->src->addr.common, con->src->len)) {
g_critical("%s: bind(%s) failed: %s (%d)",
G_STRLOC,
con->src->name->str,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
}
return NETWORK_SOCKET_SUCCESS;
}
/**
* read a data from the socket
*
* @param sock the socket
*/
network_socket_retval_t network_socket_read(network_socket *sock) {
gssize len;
if (sock->to_read > 0) {
GString *packet = g_string_sized_new(sock->to_read);
g_queue_push_tail(sock->recv_queue_raw->chunks, packet);
if (sock->socket_type == SOCK_STREAM) {
len = recv(sock->fd, packet->str, sock->to_read, 0);
} else {
/* UDP */
socklen_t dst_len = sizeof(sock->dst->addr.common);
len = recvfrom(sock->fd, packet->str, sock->to_read, 0, &(sock->dst->addr.common), &(dst_len));
sock->dst->len = dst_len;
}
if (-1 == len) {
#ifdef _WIN32
errno = WSAGetLastError();
#endif
switch (errno) {
case E_NET_CONNABORTED:
case E_NET_CONNRESET: /** nothing to read, let's let ioctl() handle the close for us */
case E_NET_WOULDBLOCK: /** the buffers are empty, try again later */
case EAGAIN:
return NETWORK_SOCKET_WAIT_FOR_EVENT;
default:
g_debug("%s: recv() failed: %s (errno=%d)", G_STRLOC, g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
}
} else if (len == 0) {
/**
* connection close
*
* let's call the ioctl() and let it handle it for use
*/
return NETWORK_SOCKET_WAIT_FOR_EVENT;
}
sock->to_read -= len;
sock->recv_queue_raw->len += len;
#if 0
sock->recv_queue_raw->offset = 0; /* offset into the first packet */
#endif
packet->len = len;
}
return NETWORK_SOCKET_SUCCESS;
}
#ifdef HAVE_WRITEV
/**
* write data to the socket
*
*/
static network_socket_retval_t network_socket_write_writev(network_socket *con, int send_chunks) {
/* send the whole queue */
GList *chunk;
struct iovec *iov;
gint chunk_id;
gint chunk_count;
gssize len;
int os_errno;
gint max_chunk_count;
if (send_chunks == 0) return NETWORK_SOCKET_SUCCESS;
chunk_count = send_chunks > 0 ? send_chunks : (gint)con->send_queue->chunks->length;
if (chunk_count == 0) return NETWORK_SOCKET_SUCCESS;
max_chunk_count = sysconf(_SC_IOV_MAX);
if (max_chunk_count < 0) { /* option is unknown */
#if defined(UIO_MAXIOV)
max_chunk_count = UIO_MAXIOV; /* as defined in POSIX */
#elif defined(IOV_MAX)
max_chunk_count = IOV_MAX; /* on older Linux'es */
#else
g_assert_not_reached(); /* make sure we provide a work-around in case sysconf() fails on us */
#endif
}
chunk_count = chunk_count > max_chunk_count ? max_chunk_count : chunk_count;
g_assert_cmpint(chunk_count, >, 0); /* make sure it is never negative */
iov = g_new0(struct iovec, chunk_count);
for (chunk = con->send_queue->chunks->head, chunk_id = 0;
chunk && chunk_id < chunk_count;
chunk_id++, chunk = chunk->next) {
GString *s = chunk->data;
if (chunk_id == 0) {
g_assert(con->send_queue->offset < s->len);
iov[chunk_id].iov_base = s->str + con->send_queue->offset;
iov[chunk_id].iov_len = s->len - con->send_queue->offset;
} else {
iov[chunk_id].iov_base = s->str;
iov[chunk_id].iov_len = s->len;
}
}
len = writev(con->fd, iov, chunk_count);
os_errno = errno;
g_free(iov);
if (-1 == len) {
switch (os_errno) {
case E_NET_WOULDBLOCK:
case EAGAIN:
return NETWORK_SOCKET_WAIT_FOR_EVENT;
case EPIPE:
case E_NET_CONNRESET:
case E_NET_CONNABORTED:
/** remote side closed the connection */
return NETWORK_SOCKET_ERROR;
default:
g_message("%s.%d: writev(%s, ...) failed: %s",
__FILE__, __LINE__,
con->dst->name->str,
g_strerror(errno));
return NETWORK_SOCKET_ERROR;
}
} else if (len == 0) {
return NETWORK_SOCKET_ERROR;
}
con->send_queue->offset += len;
con->send_queue->len -= len;
/* check all the chunks which we have sent out */
for (chunk = con->send_queue->chunks->head; chunk; ) {
GString *s = chunk->data;
if (con->send_queue->offset >= s->len) {
con->send_queue->offset -= s->len;
g_string_free(s, TRUE);
g_queue_delete_link(con->send_queue->chunks, chunk);
chunk = con->send_queue->chunks->head;
} else {
return NETWORK_SOCKET_WAIT_FOR_EVENT;
}
}
return NETWORK_SOCKET_SUCCESS;
}
#endif
/**
* write data to the socket
*
* use a loop over send() to be compatible with win32
*/
static network_socket_retval_t network_socket_write_send(network_socket *con, int send_chunks) {
/* send the whole queue */
GList *chunk;
if (send_chunks == 0) return NETWORK_SOCKET_SUCCESS;
for (chunk = con->send_queue->chunks->head; chunk; ) {
GString *s = chunk->data;
gssize len;
g_assert(con->send_queue->offset < s->len);
if (con->socket_type == SOCK_STREAM) {
len = send(con->fd, s->str + con->send_queue->offset, s->len - con->send_queue->offset, 0);
} else {
len = sendto(con->fd, s->str + con->send_queue->offset, s->len - con->send_queue->offset, 0, &(con->dst->addr.common), con->dst->len);
}
if (-1 == len) {
#ifdef _WIN32
errno = WSAGetLastError();
#endif
switch (errno) {
case E_NET_WOULDBLOCK:
case EAGAIN:
return NETWORK_SOCKET_WAIT_FOR_EVENT;
case EPIPE:
case E_NET_CONNRESET:
case E_NET_CONNABORTED:
/** remote side closed the connection */
return NETWORK_SOCKET_ERROR;
default:
g_message("%s: send(%s, %"G_GSIZE_FORMAT") failed: %s",
G_STRLOC,
con->dst->name->str,
s->len - con->send_queue->offset,
g_strerror(errno));
return NETWORK_SOCKET_ERROR;
}
} else if (len == 0) {
return NETWORK_SOCKET_ERROR;
}
con->send_queue->offset += len;
if (con->send_queue->offset == s->len) {
g_string_free(s, TRUE);
g_queue_delete_link(con->send_queue->chunks, chunk);
con->send_queue->offset = 0;
if (send_chunks > 0 && --send_chunks == 0) break;
chunk = con->send_queue->chunks->head;
} else {
return NETWORK_SOCKET_WAIT_FOR_EVENT;
}
}
return NETWORK_SOCKET_SUCCESS;
}
/**
* write a content of con->send_queue to the socket
*
* @param con socket to read from
* @param send_chunks number of chunks to send, if < 0 send all
*
* @returns NETWORK_SOCKET_SUCCESS on success, NETWORK_SOCKET_ERROR on error and NETWORK_SOCKET_WAIT_FOR_EVENT if the call would have blocked
*/
network_socket_retval_t network_socket_write(network_socket *con, int send_chunks) {
if (con->socket_type == SOCK_STREAM) {
#ifdef HAVE_WRITEV
return network_socket_write_writev(con, send_chunks);
#else
return network_socket_write_send(con, send_chunks);
#endif
} else {
return network_socket_write_send(con, send_chunks);
}
}
network_socket_retval_t network_socket_to_read(network_socket *sock) {
int b = -1;
#ifdef SO_NREAD
/* on MacOS X ioctl(..., FIONREAD) returns _more_ than what we have in the queue */
if (sock->socket_type == SOCK_DGRAM) {
socklen_t b_len = sizeof(b);
if (0 != getsockopt(sock->fd, SOL_SOCKET, SO_NREAD, &b, &b_len)) {
g_critical("%s: getsockopt(%d, SO_NREAD, ...) failed: %s (%d)",
G_STRLOC,
sock->fd,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
} else if (b < 0) {
g_critical("%s: getsockopt(%d, SO_NREAD, ...) succeeded, but is negative: %d",
G_STRLOC,
sock->fd,
b);
return NETWORK_SOCKET_ERROR;
} else {
sock->to_read = b;
return NETWORK_SOCKET_SUCCESS;
}
}
#endif
if (0 != ioctl(sock->fd, FIONREAD, &b)) {
g_critical("%s: ioctl(%d, FIONREAD, ...) failed: %s (%d)",
G_STRLOC,
sock->fd,
g_strerror(errno), errno);
return NETWORK_SOCKET_ERROR;
} else if (b < 0) {
g_critical("%s: ioctl(%d, FIONREAD, ...) succeeded, but is negative: %d",
G_STRLOC,
sock->fd,
b);
return NETWORK_SOCKET_ERROR;
} else {
sock->to_read = b;
return NETWORK_SOCKET_SUCCESS;
}
}
|