-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.c
1227 lines (1124 loc) · 26.5 KB
/
socket.c
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------
* Module: socket.c
*
* Description: This module contains functions for low level socket
* operations (connecting/reading/writing to the backend)
*
* Classes: SocketClass (Functions prefix: "SOCK_")
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#include "socket.h"
#ifdef USE_SSPI
#include "sspisvcs.h"
#endif /* USE_SSPI */
#ifdef USE_GSS
#include "gsssvcs.h"
#endif /* USE_GSS */
#ifdef USE_LIBPQ
#include <libpq-fe.h>
#ifdef USE_SSL
#include <openssl/ssl.h>
#endif /* USE_SSL */
#endif /* USE_LIBPQ */
#include "misc.h"
#include "loadlib.h"
#include "connection.h"
#ifdef WIN32
#include <time.h>
#include <Wspiapi.h> /* Ensure to support the backward-compatibility version
of getaddrinfo() etc */
#else
#include <stdlib.h>
#include <string.h> /* for memset */
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif /* HAVE_SYS_TIME_H */
#endif /* TIME_WITH__SYS_TIME */
#endif /* WIN32 */
extern GLOBAL_VALUES globals;
static int SOCK_get_next_n_bytes(SocketClass *s, int n, char *buf);
static void SOCK_set_error(SocketClass *s, int _no, const char *_msg)
{
int gerrno = SOCK_ERRNO;
s->errornumber = _no;
if (NULL != s->_errormsg_)
free(s->_errormsg_);
if (NULL != _msg)
s->_errormsg_ = strdup(_msg);
else
s->_errormsg_ = NULL;
mylog("(%d)%s ERRNO=%d\n", _no, _msg, gerrno);
}
SocketClass *
SOCK_Constructor(const ConnectionClass *conn)
{
SocketClass *rv;
rv = (SocketClass *) malloc(sizeof(SocketClass));
if (rv != NULL)
{
rv->socket = (SOCKETFD) -1;
#ifdef USE_SSPI
rv->ssd = NULL;
rv->sspisvcs = 0;
#endif /* USE_SSPI */
#ifdef USE_GSS
rv->gctx = NULL;
rv->gtarg_nam = NULL;
#endif /* USE_GSS */
#ifdef USE_LIBPQ
rv->via_libpq = FALSE;
#ifdef USE_SSL
rv->ssl = NULL;
#endif
rv->pqconn = NULL;
#endif /* USE_LIBPQ */
rv->pversion = 0;
rv->reslen = 0;
rv->buffer_filled_in = 0;
rv->buffer_filled_out = 0;
rv->buffer_read_in = 0;
if (conn)
{
rv->buffer_size = conn->connInfo.drivers.socket_buffersize;
rv->keepalive = !conn->connInfo.disable_keepalive;
rv->keepalive_idle = conn->connInfo.keepalive_idle;
rv->keepalive_interval = conn->connInfo.keepalive_interval;
}
else
{
rv->buffer_size = globals.socket_buffersize;
rv->keepalive = TRUE;
rv->keepalive_idle = -1;
rv->keepalive_interval = -1;
}
rv->buffer_in = (UCHAR *) malloc(rv->buffer_size);
if (!rv->buffer_in)
{
free(rv);
return NULL;
}
rv->buffer_out = (UCHAR *) malloc(rv->buffer_size);
if (!rv->buffer_out)
{
free(rv->buffer_in);
free(rv);
return NULL;
}
rv->_errormsg_ = NULL;
rv->errornumber = 0;
}
return rv;
}
void
SOCK_Destructor(SocketClass *self)
{
mylog("SOCK_Destructor\n");
if (!self)
return;
#ifdef USE_LIBPQ
if (self->pqconn)
{
if (self->via_libpq)
{
PQfinish(self->pqconn);
/* UnloadDelayLoadedDLLs(NULL != self->ssl); */
}
self->via_libpq = FALSE;
self->pqconn = NULL;
#ifdef USE_SSL
self->ssl = NULL;
#endif
}
else
#endif /* USE_LIBPQ */
{
if (self->socket != (SOCKETFD) -1)
{
SOCK_put_char(self, 'X');
SOCK_put_int(self, 4, 4);
SOCK_flush_output(self);
closesocket(self->socket);
}
#ifdef USE_SSPI
if (self->ssd)
{
ReleaseSvcSpecData(self, (UInt4) -1);
free(self->ssd);
self->ssd = NULL;
}
self->sspisvcs = 0;
#endif /* USE_SSPI */
#ifdef USE_GSS
pg_GSS_cleanup(self);
#endif /* USE_GSS */
}
if (self->buffer_in)
free(self->buffer_in);
if (self->buffer_out)
free(self->buffer_out);
if (self->_errormsg_)
free(self->_errormsg_);
free(self);
}
#ifdef WIN32
static inet_pton_func inet_pton_ptr = NULL;
static HMODULE ws2_hnd = NULL;
#else
static inet_pton_func inet_pton_ptr = inet_pton;
#endif /* WIN32 */
#if defined(_MSC_VER) && (_MSC_VER < 1300)
static freeaddrinfo_func freeaddrinfo_ptr = NULL;
static getaddrinfo_func getaddrinfo_ptr = NULL;
static getnameinfo_func getnameinfo_ptr = NULL;
#else
static freeaddrinfo_func freeaddrinfo_ptr = freeaddrinfo;
static getaddrinfo_func getaddrinfo_ptr = getaddrinfo;
#endif /* _MSC_VER */
static BOOL format_sockerr(char *errmsg, size_t buflen, int errnum, const char *cmd, const char *host, int portno)
{
BOOL ret = FALSE;
#ifdef WIN32
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
errnum, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
errmsg, (DWORD)buflen, NULL))
ret = TRUE;
#else
#if defined(POSIX_MULTITHREAD_SUPPORT) && defined(HAVE_STRERROR_R)
#ifdef STRERROR_R_CHAR_P
const char *pchar;
pchar = (const char *) strerror_r(errnum, errmsg, buflen);
if (NULL != pchar)
{
if (pchar != errmsg)
strncpy_null(errmsg, pchar, buflen);
ret = TRUE;
}
#else
if (0 == strerror_r(errnum, errmsg, buflen))
ret = TRUE;
#endif /* STRERROR_R_CHAR_P */
#else
strncpy_null(errmsg, strerror(errnum), buflen);
ret = TRUE;
#endif /* POSIX_MULTITHREAD_SUPPORT */
#endif /* WIN32 */
if (ret)
{
size_t tlen = strlen(errmsg);
errmsg += tlen;
buflen -= tlen;
snprintf(errmsg, buflen, " [%s:%d]", host, portno);
}
else
snprintf(errmsg, buflen, "%s failed for [%s:%d] ", cmd, host, portno);
return ret;
}
static int
is_numeric_address(const char *hostname)
{
if (inet_pton_ptr != NULL)
{
char unused[16];
return (inet_pton_ptr(AF_INET, hostname, &unused) ||
inet_pton_ptr(AF_INET6, hostname, &unused));
}
return (inet_addr(hostname) != INADDR_NONE);
}
char
SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname, long timeout)
{
struct addrinfo rest, *addrs = NULL, *curadr = NULL;
int family = 0;
char retval = 0;
int gerrno;
if (self->socket != (SOCKETFD) -1)
{
SOCK_set_error(self, SOCKET_ALREADY_CONNECTED, "Socket is already connected");
return 0;
}
#ifdef WIN32
if (ws2_hnd == NULL)
{
ws2_hnd = GetModuleHandle("ws2_32.dll");
if (inet_pton_ptr == NULL)
inet_pton_ptr = (inet_pton_func)GetProcAddress(ws2_hnd, "inet_pton");
}
#endif /* WIN32 */
#if defined(_MSC_VER) && (_MSC_VER < 1300)
if (freeaddrinfo_ptr == NULL)
freeaddrinfo_ptr = (freeaddrinfo_func)GetProcAddress(ws2_hnd, "freeaddrinfo");
if (getaddrinfo_ptr == NULL)
getaddrinfo_ptr = (getaddrinfo_func)GetProcAddress(ws2_hnd, "getaddrinfo");
if (getnameinfo_ptr == NULL)
getnameinfo_ptr = (getnameinfo_func)GetProcAddress(ws2_hnd, "getnameinfo");
#endif
/*
* Hostname lookup.
*/
if (hostname && hostname[0]
#ifndef WIN32
&& '/' != hostname[0]
#endif /* WIN32 */
)
{
char portstr[16];
int ret;
memset(&rest, 0, sizeof(rest));
rest.ai_socktype = SOCK_STREAM;
rest.ai_family = AF_UNSPEC;
snprintf(portstr, sizeof(portstr), "%d", port);
#ifdef AI_NUMERICSERV
rest.ai_flags |= AI_NUMERICSERV;
#endif /* AI_NUMERICSERV */
if (is_numeric_address(hostname))
/* don't resolve address in getaddrinfo() if not necessary */
rest.ai_flags |= AI_NUMERICHOST;
ret = getaddrinfo_ptr(hostname, portstr, &rest, &addrs);
if (ret || !addrs)
{
SOCK_set_error(self, SOCKET_HOST_NOT_FOUND, "Could not resolve hostname.");
if (addrs)
freeaddrinfo_ptr(addrs);
return 0;
}
curadr = addrs;
}
else
#ifdef HAVE_UNIX_SOCKETS
{
struct sockaddr_un *un = (struct sockaddr_un *) &(self->sadr_area);
family = un->sun_family = AF_UNIX;
/* passing NULL or '' means pg default "/tmp" */
UNIXSOCK_PATH(un, port, hostname);
self->sadr_len = UNIXSOCK_LEN(un);
}
#else
{
SOCK_set_error(self, SOCKET_HOST_NOT_FOUND, "Hostname isn't specified.");
return 0;
}
#endif /* HAVE_UNIX_SOCKETS */
retry:
if (curadr)
family = curadr->ai_family;
self->socket = socket(family, SOCK_STREAM, 0);
if (self->socket == (SOCKETFD) -1)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CREATE_SOCKET, "Could not create Socket.");
goto cleanup;
}
#ifdef TCP_NODELAY
if (family != AF_UNIX)
{
int i;
socklen_t len;
i = 1;
len = sizeof(i);
if (setsockopt(self->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &i, len) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to NODELAY.");
goto cleanup;
}
}
#endif /* TCP_NODELAY */
if (family != AF_UNIX && self->keepalive)
{
#ifdef WIN32
if (self->keepalive_idle < 0 && self->keepalive_interval < 0)
{
#ifdef SO_KEEPALIVE
int i;
socklen_t len;
i = 1;
len = sizeof(i);
if (setsockopt(self->socket, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, len) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to SO_KEEPALIVE.");
goto cleanup;
}
#endif /* SO_KEEPALIVE */
}
else
{
#ifdef SIO_KEEPALIVE_VALS
struct tcp_keepalive ka;
DWORD retsize;
int idle = 0;
int interval = 0;
if (self->keepalive_idle >= 0)
idle = self->keepalive_idle;
else
idle = 2 * 60 * 60; /* 2 hours = default */
if (self->keepalive_interval >= 0)
interval = self->keepalive_interval;
else
interval = 1; /* 1 second = default */
ka.onoff = 1;
ka.keepalivetime = idle * 1000;
ka.keepaliveinterval = interval * 1000;
if (WSAIoctl(self->socket, SIO_KEEPALIVE_VALS, (LPVOID) &ka, sizeof(ka), NULL, 0, &retsize, NULL, NULL) != 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to SO_KEEPALIVE VALS.");
goto cleanup;
}
#endif /* SIO_KEEPALIVE_VALS */
}
#else
#ifdef SO_KEEPALIVE
int i;
socklen_t len;
i = 1;
len = sizeof(i);
if (setsockopt(self->socket, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, len) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to SO_KEEPALIVE.");
goto cleanup;
}
#endif /* SO_KEEPALIVE */
/*
* Set the keepalive idle timer.
*/
if (self->keepalive_idle >= 0)
{
#ifdef TCP_KEEPIDLE
int idle = self->keepalive_idle;
if (setsockopt(self->socket, IPPROTO_TCP, TCP_KEEPIDLE,
(char *) &idle, sizeof(idle)) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to TCP_KEEPIDLE.");
goto cleanup;
}
#else
#ifdef TCP_KEEPALIVE
/* Darwin uses TCP_KEEPALIVE rather than TCP_KEEPIDLE */
int idle = self->keepalive_idle;
if (setsockopt(self->socket, IPPROTO_TCP, TCP_KEEPALIVE,
(char *) &idle, sizeof(idle)) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to TCP_KEEPALIVE.");
goto cleanup;
}
#endif /* TCP_KEEPALIVE */
#endif /* TCP_KEEPIDLE */
}
/*
* Set the keepalive interval.
*/
if (self->keepalive_interval >= 0)
{
#ifdef TCP_KEEPINTVL
int interval = self->keepalive_interval;
if (setsockopt(self->socket, IPPROTO_TCP, TCP_KEEPINTVL,
(char *) &interval, sizeof(interval)) < 0)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not set socket to TCP_KEEPINTVL.");
goto cleanup;
}
#endif /* TCP_KEEPINTVL */
}
#endif /* WIN32 */
}
#ifdef WIN32
{
long ioctlsocket_ret = 1;
/* Returns non-0 on failure, while fcntl() returns -1 on failure */
ioctlsocket(self->socket, FIONBIO, &ioctlsocket_ret);
}
#else
fcntl(self->socket, F_SETFL, O_NONBLOCK);
#endif
if (curadr)
{
struct sockaddr *in = (struct sockaddr *) &(self->sadr_area);
memset((char *) in, 0, sizeof(self->sadr_area));
memcpy(in, curadr->ai_addr, curadr->ai_addrlen);
self->sadr_len = (int) curadr->ai_addrlen;
}
if (connect(self->socket, (struct sockaddr *) &(self->sadr_area), self->sadr_len) < 0)
{
int ret, optval;
int wait_sec = 0;
#ifdef HAVE_POLL
struct pollfd fds;
#else
fd_set fds, except_fds;
struct timeval tm;
#endif /* HAVE_POLL */
socklen_t optlen = sizeof(optval);
time_t t_now, t_finish = 0;
BOOL tm_exp = FALSE;
gerrno = SOCK_ERRNO;
switch (gerrno)
{
case 0:
case EINPROGRESS:
case EINTR:
#ifdef EAGAIN
case EAGAIN:
#endif /* EAGAIN */
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
case EWOULDBLOCK:
#endif /* EWOULDBLOCK */
break;
default:
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not connect to remote socket immedaitely");
goto cleanup;
}
if (timeout > 0)
{
t_now = time(NULL);
t_finish = t_now + timeout;
wait_sec = timeout;
}
do {
#ifdef HAVE_POLL
fds.fd = self->socket;
fds.events = POLLOUT;
fds.revents = 0;
ret = poll(&fds, 1, timeout > 0 ? wait_sec * 1000 : -1);
#else
tm.tv_sec = wait_sec;
tm.tv_usec = 0;
FD_ZERO(&fds);
FD_ZERO(&except_fds);
FD_SET(self->socket, &fds);
FD_SET(self->socket, &except_fds);
ret = select((int) self->socket + 1, NULL, &fds, &except_fds, timeout > 0 ? &tm : NULL);
#endif /* HAVE_POLL */
gerrno = SOCK_ERRNO;
if (0 < ret)
break;
else if (0 == ret)
tm_exp = TRUE;
else if (EINTR != gerrno)
break;
else if (timeout > 0)
{
if (t_now = time(NULL), t_now >= t_finish)
tm_exp = TRUE;
else
wait_sec = (int) (t_finish - t_now);
}
} while (!tm_exp);
if (tm_exp)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not connect .. timeout occured.");
goto cleanup;
}
else if (0 > ret)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not connect .. select error occured.");
mylog("select error ret=%d ERROR=%d\n", ret, gerrno);
goto cleanup;
}
if (getsockopt(self->socket, SOL_SOCKET, SO_ERROR,
(char *) &optval, &optlen) == -1)
{
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, "Could not connect .. getsockopt error.");
}
else if (optval != 0)
{
char errmsg[256], host[64];
host[0] = '\0';
#if defined(_MSC_VER) && (_MSC_VER < 1300)
getnameinfo_ptr
#else
getnameinfo
#endif
((struct sockaddr *) &(self->sadr_area),
self->sadr_len, host, sizeof(host),
NULL, 0, NI_NUMERICHOST);
/* snprintf(errmsg, sizeof(errmsg), "connect getsockopt val %d addr=%s\n", optval, host); */
format_sockerr(errmsg, sizeof(errmsg), optval, "connect", host, port);
mylog(errmsg);
SOCK_set_error(self, SOCKET_COULD_NOT_CONNECT, errmsg);
}
else
retval = 1;
}
else
retval = 1;
cleanup:
if (0 == retval)
{
if (self->socket >= 0)
{
closesocket(self->socket);
self->socket = (SOCKETFD) -1;
}
if (curadr && curadr->ai_next)
{
curadr = curadr->ai_next;
goto retry;
}
}
else
SOCK_set_error(self, 0, NULL);
if (addrs)
freeaddrinfo_ptr(addrs);
return retval;
}
/*
* To handle EWOULDBLOCK etc (mainly for libpq non-blocking connection).
*/
static int SOCK_wait_for_ready(SocketClass *sock, BOOL output, BOOL nowait)
{
int ret, gerrno;
#ifdef HAVE_POLL
struct pollfd fds;
#else
fd_set fds, except_fds;
struct timeval tm;
#endif /* HAVE_POLL */
#ifdef USE_SSL
/* always wait if SSL is disabled. XXX: why? */
if (sock->ssl == NULL)
nowait = FALSE;
#endif /* USE_SSL */
do {
#ifdef HAVE_POLL
fds.fd = sock->socket;
fds.events = output ? POLLOUT : POLLIN;
fds.revents = 0;
ret = poll(&fds, 1, nowait ? 0 : -1);
mylog("!!! poll ret=%d revents=%x\n", ret, fds.revents);
#else
FD_ZERO(&fds);
FD_ZERO(&except_fds);
FD_SET(sock->socket, &fds);
FD_SET(sock->socket, &except_fds);
if (nowait)
{
tm.tv_sec = 0;
tm.tv_usec = 0;
}
ret = select((int) sock->socket + 1, output ? NULL : &fds, output ? &fds : NULL, &except_fds, nowait ? &tm : NULL);
#endif /* HAVE_POLL */
gerrno = SOCK_ERRNO;
} while (ret < 0 && EINTR == gerrno);
return ret;
}
static int SOCK_SSPI_recv(SocketClass *self, void *buffer, int len)
{
#ifdef USE_SSPI
if (self->sspisvcs && self->ssd)
return SSPI_recv(self, (char *) buffer, len);
else
#endif /* USE_SSPI */
return recv(self->socket, (char *) buffer, len, RECV_FLAG);
}
static int SOCK_SSPI_send(SocketClass *self, const void *buffer, int len)
{
#ifdef USE_SSPI
if (self->sspisvcs && self->ssd)
return SSPI_send(self, buffer, len);
else
#endif /* USE_SSPI */
return send(self->socket, (char *) buffer, len, SEND_FLAG);
}
#ifdef USE_SSL
/*
* The stuff for SSL.
*/
/* included in <openssl/ssl.h>
#define SSL_ERROR_NONE 0
#define SSL_ERROR_SSL 1
#define SSL_ERROR_WANT_READ 2
#define SSL_ERROR_WANT_WRITE 3
#define SSL_ERROR_WANT_X509_LOOKUP 4
#define SSL_ERROR_SYSCALL 5 // look at error stack/return value/errno
#define SSL_ERROR_ZERO_RETURN 6
#define SSL_ERROR_WANT_CONNECT 7
#define SSL_ERROR_WANT_ACCEPT 8
*/
/*
* recv more than 1 bytes using SSL.
*/
static int SOCK_SSL_recv(SocketClass *sock, void *buffer, int len)
{
CSTR func = "SOCK_SSL_recv";
int n, err, gerrno;
retry:
n = SSL_read(sock->ssl, buffer, len);
err = SSL_get_error(sock->ssl, len);
gerrno = SOCK_ERRNO;
inolog("%s: %d get_error=%d Lasterror=%d\n", func, n, err, gerrno);
switch (err)
{
case SSL_ERROR_NONE:
break;
case SSL_ERROR_WANT_READ:
if (SOCK_wait_for_ready(sock, FALSE, FALSE) >= 0)
goto retry;
n = -1;
break;
case SSL_ERROR_WANT_WRITE:
goto retry;
break;
case SSL_ERROR_SYSCALL:
if (-1 != n)
{
n = -1;
SOCK_ERRNO_SET(ECONNRESET);
}
break;
case SSL_ERROR_SSL:
case SSL_ERROR_ZERO_RETURN:
n = -1;
SOCK_ERRNO_SET(ECONNRESET);
break;
default:
n = -1;
}
return n;
}
/*
* send more than 1 bytes using SSL.
*/
static int SOCK_SSL_send(SocketClass *sock, void *buffer, int len)
{
CSTR func = "SOCK_SSL_send";
int n, err, gerrno;
retry:
n = SSL_write(sock->ssl, buffer, len);
err = SSL_get_error(sock->ssl, len);
gerrno = SOCK_ERRNO;
inolog("%s: %d get_error=%d Lasterror=%d\n", func, n, err, gerrno);
switch (err)
{
case SSL_ERROR_NONE:
break;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
if (SOCK_wait_for_ready(sock, TRUE, FALSE) >= 0)
goto retry;
n = -1;
break;
case SSL_ERROR_SYSCALL:
if (-1 != n)
{
n = -1;
SOCK_ERRNO_SET(ECONNRESET);
}
break;
case SSL_ERROR_SSL:
case SSL_ERROR_ZERO_RETURN:
n = -1;
SOCK_ERRNO_SET(ECONNRESET);
break;
default:
n = -1;
}
return n;
}
#endif /* USE_SSL */
int
SOCK_get_id(SocketClass *self)
{
int id;
if (!self)
return 0;
if (0 != self->errornumber)
return 0;
if (self->reslen > 0)
{
mylog("SOCK_get_id has to eat %d bytes\n", self->reslen);
/*
do
{
SOCK_get_next_byte(self, FALSE);
if (0 != self->errornumber)
return 0;
} while (self->reslen > 0);
*/
SOCK_get_next_n_bytes(self, self->reslen, NULL);
}
id = SOCK_get_next_byte(self, FALSE);
self->reslen = 0;
return id;
}
void
SOCK_get_n_char(SocketClass *self, char *buffer, Int4 len)
{
if (!self)
return;
if (!buffer)
{
SOCK_set_error(self, SOCKET_NULLPOINTER_PARAMETER, "get_n_char was called with NULL-Pointer");
return;
}
/*
for (lf = 0; lf < len; lf++)
{
if (0 != self->errornumber)
break;
buffer[lf] = SOCK_get_next_byte(self, FALSE);
}
*/
SOCK_get_next_n_bytes(self, len, buffer);
}
void
SOCK_put_n_char(SocketClass *self, const char *buffer, size_t len)
{
size_t lf;
if (!self)
return;
if (!buffer)
{
SOCK_set_error(self, SOCKET_NULLPOINTER_PARAMETER, "put_n_char was called with NULL-Pointer");
return;
}
for (lf = 0; lf < len; lf++)
{
if (0 != self->errornumber)
break;
SOCK_put_next_byte(self, (UCHAR) buffer[lf]);
}
}
/*
* bufsize must include room for the null terminator
* will read at most bufsize-1 characters + null.
* returns TRUE if truncation occurs.
*/
BOOL
SOCK_get_string(SocketClass *self, char *buffer, Int4 bufsize)
{
int lf;
for (lf = 0; lf < bufsize - 1; lf++)
if (!(buffer[lf] = SOCK_get_next_byte(self, FALSE)))
return FALSE;
buffer[bufsize - 1] = '\0';
return TRUE;
}
void
SOCK_put_string(SocketClass *self, const char *string)
{
size_t lf, len;
len = strlen(string) + 1;
for (lf = 0; lf < len; lf++)
{
if (0 != self->errornumber)
break;
SOCK_put_next_byte(self, (UCHAR) string[lf]);
}
}
int
SOCK_get_int(SocketClass *self, short len)
{
if (!self)
return 0;
switch (len)
{
case 2:
{
unsigned short buf;
SOCK_get_n_char(self, (char *) &buf, len);
return ntohs(buf);
}
case 4:
{
unsigned int buf;
SOCK_get_n_char(self, (char *) &buf, len);
return ntohl(buf);
}
default:
SOCK_set_error(self, SOCKET_GET_INT_WRONG_LENGTH, "Cannot read ints of that length");
return 0;
}
}
void
SOCK_put_int(SocketClass *self, int value, short len)
{
unsigned int rv;
unsigned short rsv;
if (!self)
return;
switch (len)
{
case 2:
rsv = htons((unsigned short) value);
SOCK_put_n_char(self, (char *) &rsv, 2);
return;
case 4:
rv = htonl((unsigned int) value);
SOCK_put_n_char(self, (char *) &rv, 4);
return;
default:
SOCK_set_error(self, SOCKET_PUT_INT_WRONG_LENGTH, "Cannot write ints of that length");
return;
}
}
Int4
SOCK_flush_output(SocketClass *self)
{
int written, pos = 0, ttlsnd = 0, gerrno;
if (!self)
return -1;
if (0 != self->errornumber)
return -1;
while (self->buffer_filled_out > 0)
{
#ifdef USE_SSL
if (self->ssl)
written = SOCK_SSL_send(self, (char *) self->buffer_out + pos, self->buffer_filled_out);
else
#endif /* USE_SSL */
{
written = SOCK_SSPI_send(self, (char *) self->buffer_out + pos, self->buffer_filled_out);
}
gerrno = SOCK_ERRNO;
if (written < 0)
{
switch (gerrno)
{
case EINTR:
continue;
break;
#ifdef EAGAIN
case EAGAIN:
#endif /* EAGAIN */
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
case EWOULDBLOCK:
#endif /* EWOULDBLOCK */
if (SOCK_wait_for_ready(self, TRUE, FALSE) >= 0)
continue;
break;
}
SOCK_set_error(self, SOCKET_WRITE_ERROR, "Could not flush socket buffer.");
return -1;
}
pos += written;
self->buffer_filled_out -= written;
ttlsnd += written;
}
return ttlsnd;
}
UCHAR
SOCK_get_next_byte(SocketClass *self, BOOL peek)
{
int gerrno;
BOOL maybeEOF = FALSE;