openbase 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. data/History.txt +50 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +53 -0
  4. data/README.txt +35 -0
  5. data/Rakefile +126 -0
  6. data/examples/example.rb +37 -0
  7. data/ext/CommAPI.c +643 -0
  8. data/ext/Headers/CommAPI.h +1 -0
  9. data/ext/Headers/NetClient.h +42 -0
  10. data/ext/Headers/NetClientLib.h +41 -0
  11. data/ext/Headers/OB_Memory.h +69 -0
  12. data/ext/Headers/OpenBaseAdmin.h +227 -0
  13. data/ext/Headers/OpenBaseConnection.h +337 -0
  14. data/ext/Headers/OpenBaseEncoding.h +302 -0
  15. data/ext/Headers/OpenBasePrepare.h +1 -0
  16. data/ext/Headers/OpenBaseSupport.h +1 -0
  17. data/ext/Headers/Platform_Carbon_Header.h +5 -0
  18. data/ext/Headers/Platform_Classic_Header.h +6 -0
  19. data/ext/Headers/Platform_Linux.h +4 -0
  20. data/ext/Headers/Platform_Mach_Header.h +4 -0
  21. data/ext/Headers/Platform_Windows.h +3 -0
  22. data/ext/Headers/conversion.h +1 -0
  23. data/ext/Headers/datetime.h +26 -0
  24. data/ext/Headers/longlong.h +46 -0
  25. data/ext/Headers/platform.h +67 -0
  26. data/ext/Headers/stringConversion.h +15 -0
  27. data/ext/NetClient.c +888 -0
  28. data/ext/OpenBaseAdmin.c +1884 -0
  29. data/ext/OpenBaseConnection.c +1841 -0
  30. data/ext/OpenBaseEncoding.c +993 -0
  31. data/ext/OpenBaseEncoding_DOS.c +1 -0
  32. data/ext/OpenBaseEncoding_ISO8859.c +1 -0
  33. data/ext/OpenBaseEncoding_MacOS.c +1 -0
  34. data/ext/OpenBaseEncoding_Windows.c +1150 -0
  35. data/ext/OpenBasePrepare.c +1 -0
  36. data/ext/OpenBaseSupport.c +1 -0
  37. data/ext/conversion.c +1 -0
  38. data/ext/datetime.c +816 -0
  39. data/ext/depend +1 -0
  40. data/ext/extconf.rb +10 -0
  41. data/ext/longlong.c +1 -0
  42. data/ext/openbase.c +980 -0
  43. data/ext/stringConversion.c +169 -0
  44. data/lib/ruby-openbase/version.rb +9 -0
  45. data/scripts/txt2html +67 -0
  46. data/setup.rb +1585 -0
  47. data/test/test_helper.rb +2 -0
  48. data/test/test_openbase.rb +241 -0
  49. data/website/index.html +114 -0
  50. data/website/index.txt +59 -0
  51. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  52. data/website/stylesheets/screen.css +133 -0
  53. data/website/template.rhtml +47 -0
  54. metadata +105 -0
@@ -0,0 +1,15 @@
1
+ // -----------------------------------------------------------------------------------
2
+ // stringConversion.h
3
+ // ------------------------------------------------------------------------------
4
+ //
5
+ // Copyright (c) 2004 OpenBase International Ltd.
6
+ // All rights reserved.
7
+ //
8
+ // ------------------------------------------------------------------------------
9
+
10
+ void initSQLCodeString();
11
+
12
+ void encodeQUOTEDString(const unsigned char *string, unsigned char *buffer, int len);
13
+ void encodeSQLString(const unsigned char *string, unsigned char *buffer, int len);
14
+ void encodeSQLStringConvertSlashes(const unsigned char *string, unsigned char *buffer, int len);
15
+ void decodeSQLString(const unsigned char *string, unsigned char *buffer, int *len);
@@ -0,0 +1,888 @@
1
+ #include "platform.h"
2
+
3
+ #ifdef __cplusplus
4
+ using namespace std;
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <ctype.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+
13
+ #ifdef i386
14
+ #define SWAPENDIAN 1
15
+ #endif
16
+
17
+ #if WINNT || LINUX || MACOSX_UNIX
18
+
19
+ #include <sys/types.h>
20
+ #include <fcntl.h>
21
+ #include <sys/stat.h>
22
+
23
+ #ifdef WINNT
24
+ #import <System/winsock.h>
25
+ #include <io.h>
26
+ //#define RECEIVE_TIMOUTS_ACTIVATED
27
+ #endif
28
+
29
+ #endif
30
+
31
+ #if LINUX || MACOSX_UNIX || RHAPSODY || SOLARIS
32
+
33
+ #import <libc.h>
34
+ #import <netdb.h>
35
+ #import <netinet/in_systm.h>
36
+ #import <netinet/in.h>
37
+ #import <netinet/ip.h>
38
+ #import <netinet/ip_icmp.h>
39
+ #import <netinet/tcp.h>
40
+ #include <sys/socket.h>
41
+ #import <errno.h>
42
+
43
+ #ifdef RHAPSODY
44
+ #include <unistd.h>
45
+ #endif
46
+
47
+ #ifdef MACOSX_UNIX
48
+ #define SEND_TIMOUTS_ACTIVATED
49
+ #define RECEIVE_TIMOUTS_ACTIVATED
50
+ #endif
51
+
52
+ #ifdef LINUX
53
+ #define RECEIVE_TIMOUTS_ACTIVATED
54
+ #define USE_UNIX_DOMAIN
55
+ #endif
56
+
57
+ #ifdef SOLARIS
58
+ #define RECEIVE_TIMOUTS_ACTIVATED
59
+ #define USE_UNIX_DOMAIN
60
+ #endif
61
+
62
+ #endif
63
+
64
+ #ifdef MACOSX_CARBON
65
+ #include <OpenTransport.h>
66
+ #include <OpenTptInternet.h>
67
+ #include <size_t.h>
68
+ #include <cctype>
69
+ #include <cstdlib>
70
+
71
+ static bool OTinited = false ;
72
+ static OTClientContextPtr gOTClientContextPtr;
73
+
74
+ #endif
75
+
76
+ #include "NetClient.h"
77
+
78
+ #ifndef YES
79
+ #define YES 1
80
+ #endif
81
+ #ifndef NO
82
+ #define NO 0
83
+ #endif
84
+
85
+ #ifdef DEBUG
86
+ #define DEBUG_CONNECT_ADDRESS 1
87
+ //#define DEBUG_CONNECT
88
+ //#define COMM_PRINT
89
+ #warning *************** DEBUG MODE ***************
90
+ #endif
91
+
92
+ #define KEEPALIVE_CLIENT
93
+ #define SEND_TIMEOUT_VALUE 10
94
+ #define RECEIVE_TIMEOUT_VALUE 10
95
+
96
+
97
+
98
+ // ====================================================================================================================
99
+ // ====================================================================================================================
100
+ // ====================================================================================================================
101
+ int _windowsStartup() ;
102
+ void initOT() ;
103
+
104
+ // ====================================================================================================================
105
+ // ====================================================================================================================
106
+ // ====================================================================================================================
107
+ void initOT()
108
+ {
109
+ #ifdef MACOSX_CARBON
110
+ InitOpenTransportInContext(kInitOTForExtensionMask, &gOTClientContextPtr);
111
+
112
+ OTinited = true ;
113
+ #endif
114
+ }
115
+
116
+ // ====================================================================================================================
117
+ // ====================================================================================================================
118
+ // ====================================================================================================================
119
+ int _windowsStartup()
120
+ {
121
+
122
+ #ifdef WINNT
123
+ WSADATA WSAData;
124
+ static int windowsDidStart = 0;
125
+ int status;
126
+
127
+ if (!windowsDidStart) {
128
+ status = WSAStartup(MAKEWORD(1,1), &WSAData);
129
+ if (status != 0)
130
+ return NO;
131
+ if (HIBYTE(WSAData.wVersion) != 1 || LOBYTE(WSAData.wVersion) != 1) {
132
+ return NO;
133
+ }
134
+ windowsDidStart = YES;
135
+ }
136
+ #endif
137
+
138
+ return YES;
139
+ }
140
+
141
+
142
+ // ====================================================================================================================
143
+ // ====================================================================================================================
144
+ // ====================================================================================================================
145
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
146
+
147
+ int netConnect(int port, const char *hostName)
148
+ {
149
+ int ret = NO;
150
+ unsigned int keepa = 1;
151
+ int newsock;
152
+
153
+ #ifdef USE_UNIX_DOMAIN
154
+ struct sockaddr sockAddr;
155
+ #else
156
+ struct sockaddr_in sockAddr;
157
+ #endif
158
+
159
+ _windowsStartup(); // note this does nothing on non-windows platforms !
160
+
161
+ #if DEBUG_CONNECT_ADDRESS
162
+ printf("CONNECT TO NAME <%s>\n", hostName);
163
+ #endif
164
+
165
+ memset((struct sockaddr*) &sockAddr, 0, sizeof(sockAddr));
166
+ sockAddr.sin_family = AF_INET;
167
+ sockAddr.sin_port = htons(port);
168
+ sockAddr.sin_addr.s_addr = inet_addr(hostName);
169
+ if (sockAddr.sin_addr.s_addr == INADDR_NONE) {
170
+ struct hostent *hostEntP;
171
+
172
+ #if WINNT || SOLARIS
173
+ hostEntP = gethostbyname(hostName);
174
+ #else
175
+ hostEntP = gethostbyname2(hostName, AF_INET);
176
+ #endif
177
+
178
+ if (!hostEntP)
179
+ {
180
+ return -1;
181
+ }
182
+ memcpy(&sockAddr.sin_addr, hostEntP->h_addr, hostEntP->h_length);
183
+ }
184
+
185
+ newsock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); //PF_INET
186
+ #ifdef CLOSE_FD_ON_EXEC
187
+ fcntl(newsock, F_SETFD, 1); //test
188
+ #endif
189
+
190
+ if (newsock == -1)
191
+ {
192
+ return -1;
193
+ }
194
+
195
+ ret = connect(newsock, (struct sockaddr*) &sockAddr, sizeof(sockAddr));
196
+ if (ret == -1)
197
+ {
198
+ OBSocketClose(newsock);
199
+ return -1;
200
+ }
201
+
202
+ #ifdef KEEPALIVE_CLIENT
203
+ setsockopt(newsock, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepa, sizeof(keepa));
204
+ #endif
205
+
206
+ return newsock;
207
+
208
+ }
209
+ #endif
210
+
211
+ // ====================================================================================================================
212
+
213
+ #if MACOSX_CARBON
214
+
215
+ #ifdef TARGET_API_MAC_CARBON
216
+ EndpointRef netConnect(int inServerPort, const char * inServerName, OTClientContextPtr outClientContext)
217
+ #else
218
+ EndpointRef netConnect(int inServerPort, const char * inServerName, OTClientContextPtr)
219
+ #endif
220
+ {
221
+ EndpointRef theEndpoint;
222
+ OSStatus err;
223
+ TCall sndCall;
224
+ DNSAddress hostDNSAddress;
225
+ char hostAddress[255];
226
+
227
+
228
+ if (!OTinited) initOT() ;
229
+
230
+ #ifdef TARGET_API_MAC_CARBON
231
+ theEndpoint = ::OTOpenEndpointInContext(::OTCreateConfiguration(kTCPName), 0, nil, &err, outClientContext);
232
+ #else
233
+ theEndpoint = ::OTOpenEndpoint(::OTCreateConfiguration(kTCPName), 0, nil, &err);
234
+ #endif
235
+
236
+ if(err == noErr)
237
+ {
238
+ ::OTSetSynchronous(theEndpoint);
239
+ ::OTSetBlocking(theEndpoint);
240
+ ::OTUseSyncIdleEvents(theEndpoint, true);
241
+ err = ::OTBind(theEndpoint, nil, nil);
242
+ }
243
+
244
+ if(err == noErr)
245
+ {
246
+ ::OTMemzero(&sndCall, sizeof(TCall));
247
+ sprintf(hostAddress, "%s:%d", inServerName, inServerPort);
248
+ sndCall.addr.buf = (UInt8 *)&hostDNSAddress;
249
+ sndCall.addr.len = ::OTInitDNSAddress(&hostDNSAddress, hostAddress);
250
+
251
+ err = ::OTConnect(theEndpoint, &sndCall, nil);
252
+ }
253
+
254
+ if(err != noErr)
255
+ {
256
+ netClose(theEndpoint);
257
+ theEndpoint = kOTInvalidEndpointRef;
258
+ }
259
+
260
+ return(theEndpoint);
261
+ }
262
+ #endif
263
+
264
+
265
+ // ====================================================================================================================
266
+ // ====================================================================================================================
267
+ // ====================================================================================================================
268
+ int copyAndFixString(unsigned char **tostring, int *tosize, int position, const unsigned char *str, int length, unsigned char *codedmap)
269
+ {
270
+ char *strdest;
271
+
272
+ // make sure the string is long enough
273
+ if ((length + position) > (*tosize - 10))
274
+ {
275
+ *tosize = *tosize * 2;
276
+ // if doubling size isn't enough
277
+ if ((length + position) > (*tosize - 10))
278
+ {
279
+ *tosize = (length + position + 10);
280
+ }
281
+ //*tostring = (unsigned char *)NSZoneRealloc(NSDefaultMallocZone(), *tostring, *tosize);
282
+ *tostring = (unsigned char *)realloc(*tostring, *tosize);
283
+ }
284
+
285
+ strdest = (char *)(*tostring + position);
286
+
287
+ // copy the size of the string to the buffer
288
+ ob_intToBytes((unsigned char *)strdest, length);
289
+ strdest = strdest + sizeof(int);
290
+
291
+ // copy the string itself to the buffer
292
+ if (!codedmap)
293
+ {
294
+ memcpy(strdest, str, length);
295
+ }
296
+ else
297
+ { // encode during copy
298
+ int ct;
299
+
300
+ for (ct=0; ct != length; ct++)
301
+ {
302
+ strdest[ct] = codedmap[(unsigned int)str[ct]];
303
+ }
304
+ }
305
+
306
+ strdest[length] = '\0';
307
+ //printf("SEND TOKEN(%d) = %s\n", length, strdest);
308
+ return (length + sizeof(int));
309
+ }
310
+
311
+
312
+ // ====================================================================================================================
313
+ // ====================================================================================================================
314
+ // ====================================================================================================================
315
+ void printBuffer(char *string, int len)
316
+ {
317
+ int ct;
318
+
319
+ for (ct=0; ct < len ;ct++)
320
+ {
321
+ switch(string[ct])
322
+ {
323
+ case '\n':
324
+ printf("\\n\n");
325
+ break;
326
+ case '\r':
327
+ printf("\\r\n");
328
+ break;
329
+ case '\0':
330
+ printf("\\0");
331
+ break;
332
+ default:
333
+ printf("%c",string[ct]);
334
+ }
335
+ }
336
+ }
337
+
338
+ // ====================================================================================================================
339
+ // ====================================================================================================================
340
+ // ====================================================================================================================
341
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
342
+
343
+ void netClose(unsigned int socket)
344
+ {
345
+ OBSocketClose(socket); //close(socket);
346
+ }
347
+
348
+ #endif
349
+
350
+ // ====================================================================================================================
351
+
352
+ #if MACOSX_CARBON
353
+
354
+ void netClose(EndpointRef inEndpoint)
355
+ {
356
+ if (!OTinited) initOT() ;
357
+
358
+ if(inEndpoint != kOTInvalidEndpointRef)
359
+ {
360
+ OTUnbind(inEndpoint);
361
+ OTCloseProvider(inEndpoint);
362
+ }
363
+ }
364
+ #endif
365
+
366
+ // ====================================================================================================================
367
+ // ====================================================================================================================
368
+ // ====================================================================================================================
369
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
370
+
371
+ int netSndRaw(unsigned char *rawdata, size_t len, int socket)
372
+ {
373
+ int ret=0;
374
+
375
+ if (socket == -1)
376
+ return 0;
377
+ if (len==0)
378
+ return 1;
379
+
380
+ errno = 0;
381
+
382
+ #ifdef COMM_PRINT
383
+ printf("SOCKET %d SEND ==================\n",socket);
384
+ printBuffer((char *)rawdata,len);
385
+ #endif
386
+
387
+ // send the data
388
+ ret = OBSocketWrite(socket, rawdata, len);
389
+ //ret = write(socket, rawdata, len);
390
+ if (errno == ETIMEDOUT)
391
+ {
392
+ //printf("switch(errno) -- socket failed, ret = %d, len = %d, errno = %d\n", (int)ret, (int)len, (int)errno);
393
+ printBuffer((char *)rawdata,len);
394
+ return 0;
395
+ }
396
+
397
+ /* switch(errno) {
398
+ case ENETDOWN:
399
+ case ENETUNREACH:
400
+ case ENETRESET:
401
+ case ECONNABORTED:
402
+ case ECONNRESET:
403
+ case ENOTCONN:
404
+ case ESHUTDOWN:
405
+ case ETIMEDOUT:
406
+ case ECONNREFUSED:
407
+ printf("switch(errno) -- socket failed, ret = %d, len = %d, errno = %d\n", (int)ret, (int)len, (int)errno);
408
+ printBuffer((char *)rawdata,len);
409
+ return 0;
410
+ }*/
411
+
412
+ //if (errno!=0) {
413
+ // printf("(errno!=0) -- socket failed, ret = %d, len = %d, errno = %d\n", (int)ret, (int)len, (int)errno);
414
+ //}
415
+
416
+ if ((ret == -1) || (ret != len))
417
+ {
418
+ //printf("socket failed, ret = %d, len = %d, errno = %d\n", (int)ret, (int)len, (int)errno);
419
+ return 0;
420
+ }
421
+
422
+ return 1;
423
+ }
424
+ #endif
425
+
426
+ // ====================================================================================================================
427
+
428
+ #if MACOSX_CARBON
429
+
430
+ int netSndRaw (UInt8 * inRawdata, size_t inLength, EndpointRef inEndpoint)
431
+ {
432
+ int theResult;
433
+
434
+ if (!OTinited) initOT() ;
435
+
436
+ if(inLength == OTSnd(inEndpoint, inRawdata, inLength, 0))
437
+ {
438
+ theResult = 1;
439
+ }
440
+ else
441
+ {
442
+ theResult = 0;
443
+ }
444
+ return theResult;
445
+ }
446
+ #endif
447
+
448
+ // ====================================================================================================================
449
+ // ====================================================================================================================
450
+ // ====================================================================================================================
451
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
452
+
453
+ long netRcvToken(unsigned char **buffer, int *bufsize, int socket, unsigned char *decodemap, int *flag)
454
+
455
+ /* Waits for data on the NetConnection's socket. */
456
+ /* Returns an ISAM error code, and if no error the marshall buffer is */
457
+ /* full with the received data */
458
+ {
459
+ int cc;
460
+ int tokenLength = 0;
461
+
462
+ if (OBSocketRead(socket, *buffer, sizeof(int), flag) != sizeof(int))
463
+ {
464
+ //printf("OBSocketRead FAILED (1)\n");
465
+ return -1L;
466
+ }
467
+ ob_bytesToInt(&tokenLength, *buffer);
468
+
469
+ if (tokenLength > 150000000)
470
+ { // greater than 150 megabytes
471
+ return -1L;
472
+ }
473
+
474
+ //memcpy(&tokenLength, *buffer, sizeof(int));
475
+ if (tokenLength == -1)
476
+ {
477
+ return -2L; // end of token stream
478
+ }
479
+
480
+ // reallocate buffer if it needs expanding
481
+ if (tokenLength > (*bufsize-10))
482
+ {
483
+ *bufsize = *bufsize * 2;
484
+ if (tokenLength > (*bufsize-10))
485
+ {
486
+ *bufsize = tokenLength + 10;
487
+ }
488
+ // *buffer = (unsigned char *)NSZoneRealloc(NSDefaultMallocZone(), *buffer, *bufsize);
489
+ *buffer = (unsigned char *)realloc(*buffer, *bufsize);
490
+ }
491
+
492
+ // read token block
493
+ if ((cc = OBSocketRead(socket, *buffer, tokenLength, flag)) != tokenLength)
494
+ {
495
+ // printf("OBSocketRead FAILED (2) %d but supposed to be %d\n", cc, tokenLength);
496
+ return -1L;
497
+ }
498
+ *(char *)(*buffer + cc) = '\0';
499
+
500
+ // decode result
501
+ if (decodemap)
502
+ {
503
+ for (cc = 0; cc != tokenLength; cc++)
504
+ {
505
+ *(unsigned char *)(*buffer+cc) = decodemap[*(unsigned char *)(*buffer+cc)];
506
+ }
507
+ }
508
+
509
+ //printf("RECEIVE TOKEN = <%s>\n", *buffer);
510
+ return tokenLength;
511
+ }
512
+ #endif
513
+
514
+ // ====================================================================================================================
515
+
516
+ #if MACOSX_CARBON
517
+
518
+ long netRcvToken (UInt8 **buffer, int *bufsize, EndpointRef inEndpoint, UInt8 *decodemap, int *flag)
519
+ {
520
+ // Waits for data on the NetConnection's socket.
521
+ // Returns an ISAM error code, and if no error the marshall buffer is
522
+ // full with the received data
523
+
524
+ UInt32 tokenLength = 0;
525
+ UInt32 cc;
526
+
527
+ if (!OTinited) initOT() ;
528
+
529
+ if (sizeof (UInt32) != OBSocketRead (inEndpoint, (void *) &tokenLength, sizeof (UInt32), flag))
530
+ {
531
+ return -1L;
532
+ }
533
+
534
+ if (tokenLength == -1)
535
+ {
536
+ return -2L; // end of token stream
537
+ }
538
+ else
539
+ {
540
+ if (tokenLength < 0)
541
+ {
542
+ return -1L; // error
543
+ }
544
+ }
545
+
546
+
547
+ // reallocate TOKEN buffer if it needs expanding
548
+ if (tokenLength > (*bufsize-10))
549
+ {
550
+ *bufsize = *bufsize * 2;
551
+
552
+ if (tokenLength > (*bufsize-10))
553
+ {
554
+ *bufsize = tokenLength + 10;
555
+ }
556
+
557
+ // WAS: *buffer = (UInt8 *) std::realloc (*buffer, *bufsize);
558
+ // free(*buffer);
559
+ ob_free(*buffer);
560
+ // *buffer = (UInt8*)malloc(*bufsize);
561
+ *buffer = (UInt8*)ob_malloc(*bufsize);
562
+ }
563
+
564
+ // read token block
565
+ if (tokenLength != (cc = OBSocketRead (inEndpoint, *buffer, tokenLength, flag)))
566
+ {
567
+ return -1L;
568
+ }
569
+
570
+ *(char *)(*buffer + cc) = '\0'; // EOS
571
+
572
+
573
+ if (NULL != decodemap) // decode result ?
574
+ {
575
+ for (cc = 0; cc != tokenLength; cc++)
576
+ {
577
+ *(UInt8*)(*buffer+cc) = decodemap[*(UInt8*)(*buffer+cc)];
578
+ }
579
+ }
580
+
581
+ return(tokenLength);
582
+ }
583
+
584
+ #endif
585
+
586
+
587
+ // ====================================================================================================================
588
+ // ====================================================================================================================
589
+ // ====================================================================================================================
590
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
591
+
592
+ /* Creates a server socket on the given port which will be used to wait */
593
+ /* for incoming client connections. */
594
+ /* Returns the socket handle, or INVALID_SOCKET in case of failure. */
595
+ int netSvrCreate(int port)
596
+ {
597
+ int sock;
598
+ int shouldReuse = 1;
599
+ struct sockaddr_in sockAddr;
600
+
601
+ _windowsStartup();
602
+
603
+ /* make sure no session is open and global mainSock is set to INVALID */
604
+ //netSvrDestroy();
605
+
606
+ memset(&sockAddr, 0, sizeof(sockAddr));
607
+ sockAddr.sin_family = AF_INET;
608
+ sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
609
+ sockAddr.sin_port = htons(port);
610
+
611
+ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_IP); // PF_INET
612
+
613
+ #ifdef CLOSE_FD_ON_EXEC
614
+ fcntl(sock, F_SETFD, 1); //test
615
+ #endif
616
+
617
+ if (sock == -1)
618
+ return -1;
619
+
620
+
621
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&shouldReuse, sizeof(shouldReuse));
622
+
623
+ if (bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) == (-1))
624
+ return -1;
625
+
626
+ if (listen(sock, 5) == (-1))
627
+ return -1;
628
+
629
+ return sock;
630
+ }
631
+ #endif
632
+
633
+ // ====================================================================================================================
634
+ // ====================================================================================================================
635
+ // ====================================================================================================================
636
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
637
+ int netListen(int parentSock)
638
+
639
+ /* Waits to accept incoming socket connections on the given server socket. */
640
+ /* Returns a new child server-side socket which is attached to the */
641
+ /* incoming client connection. */
642
+
643
+ {
644
+ struct sockaddr_in clientSockAddr;
645
+ unsigned int sockLen;
646
+ int newSock;
647
+
648
+ #ifdef RECEIVE_TIMOUTS_ACTIVATED
649
+ struct timeval tm;
650
+ #endif
651
+
652
+ sockLen = sizeof(struct sockaddr_in);
653
+
654
+ do
655
+ {
656
+ newSock = accept(parentSock, (struct sockaddr *) &clientSockAddr, &sockLen);
657
+ #ifdef WINNT
658
+ } while (newSock == -1);
659
+ #else
660
+ } while (newSock == -1 && OMNI_ERRNO() == EINTR);
661
+ #endif
662
+
663
+ if (newSock < 0)
664
+ return -1;
665
+
666
+ #ifdef RECEIVE_TIMOUTS_ACTIVATED
667
+ tm.tv_sec = RECEIVE_TIMEOUT_VALUE;
668
+ tm.tv_usec = RECEIVE_TIMEOUT_VALUE;
669
+ setsockopt(newSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tm, sizeof(tm));
670
+ #endif
671
+
672
+ #ifdef CLOSE_FD_ON_EXEC
673
+ fcntl(newSock, F_SETFD, 1); //test
674
+ #endif
675
+
676
+
677
+ #ifdef DEBUG_CONNECT
678
+ printf("NEW SOCKET = %d\n", newSock);
679
+ #endif
680
+
681
+ return newSock;
682
+ }
683
+ #endif
684
+
685
+ // ====================================================================================================================
686
+ // ====================================================================================================================
687
+ // ====================================================================================================================
688
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
689
+ int setupClientHandle(int inSocket)
690
+ {
691
+ int socket;
692
+ unsigned int keepa;
693
+ int param ;
694
+
695
+ #ifdef WINNT
696
+ struct linger slinger;
697
+ unsigned int nodelay;
698
+
699
+ nodelay = TRUE;
700
+ setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
701
+
702
+ // TURN ON LINGER
703
+ slinger.l_onoff = 0;
704
+ slinger.l_linger = 0;
705
+ setsockopt(socket, SOL_SOCKET, SO_LINGER, (char *)&slinger, sizeof(slinger));
706
+
707
+ // TURN ON KEEP ALIVE
708
+ keepa = TRUE;
709
+ setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepa, sizeof(keepa));
710
+ #else
711
+ #ifdef SEND_TIMOUTS_ACTIVATED
712
+ unsigned int len;
713
+ struct timeval sendTimeout;
714
+ #endif
715
+
716
+ socket = inSocket;
717
+
718
+ // SEND TIMEOUT
719
+ getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&sendTimeout, &len);
720
+ sendTimeout.tv_sec = SEND_TIMEOUT_VALUE;
721
+ sendTimeout.tv_usec = SEND_TIMEOUT_VALUE;
722
+ setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&sendTimeout, sizeof(sendTimeout));
723
+ #endif
724
+
725
+ // TURN ON NOSIGPIPE
726
+ #ifdef MACOSX_UNIX
727
+ #ifndef SO_NOSIGPIPE
728
+ #define SO_NOSIGPIPE 0x1022 /* APPLE: No SIGPIPE on EPIPE */
729
+ #endif
730
+
731
+ param = 1;
732
+ setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&param, sizeof(param));
733
+ #endif
734
+
735
+ // TURN ON KEEP ALIVE
736
+ keepa = TRUE;
737
+ setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepa, sizeof(keepa));
738
+
739
+ return 1;
740
+ }
741
+ #endif
742
+
743
+ // ====================================================================================================================
744
+ // ====================================================================================================================
745
+ // ====================================================================================================================
746
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
747
+
748
+ int ob_threadNeedsDisconnect()
749
+ {
750
+ // put code here to check a flag to abort a connection.
751
+ return 0;
752
+ }
753
+
754
+ #endif
755
+
756
+ // ====================================================================================================================
757
+ // ====================================================================================================================
758
+ // ====================================================================================================================
759
+ #if MACOSX_UNIX || WINNT || LINUX || SOLARIS || RHAPSODY
760
+
761
+ int ob_socketread(int socketFD, unsigned char *buffer, size_t byteCount, int *flag)
762
+ {
763
+ int ct = 0, ret = 0;
764
+ int err_hold = 0;
765
+ while (ct < byteCount)
766
+ {
767
+ ret = recv(socketFD, &buffer[ct], (byteCount - ct), MSG_WAITALL);
768
+ err_hold = errno;
769
+ if (ret == 0)
770
+ break;
771
+
772
+ if ((err_hold == EAGAIN) || (err_hold == EDEADLK))
773
+ {
774
+ //printf("socket: EAGAIN (%d) ret =%d\n", socketFD, ret);
775
+ if (ret < 0)
776
+ {
777
+ ret = 0;
778
+ //if (ob_threadNeedsDisconnect()) return 0;
779
+ }
780
+ }
781
+ #ifdef WINNT
782
+ if (((err_hold == EINTR) || (err_hold == EFAULT) || (err_hold == EPIPE) || (err_hold == EBADF)) || ((ret == -1) && (err_hold == 0)))
783
+ #endif
784
+ #if MACOSX_UNIX || LINUX || SOLARIS || RHAPSODY
785
+ if (/*(errno == EINTR) ||*/ (errno == EFAULT) || (errno == ENOTSOCK) || (errno == EPIPE) || (errno == EBADF))
786
+ #endif
787
+ {
788
+ //printf("(errno == EINTR) -- TERMINATE CONNECTION!!, errno = %d, ret = %d, (ct:%d != byteCount:%d)\n", err_hold, ret, ct, byteCount);
789
+ return 0;
790
+ }
791
+ if (*flag)
792
+ {
793
+ //printf("!flag -- TERMINATE CONNECTION!!, errno = %d, ret = %d, (ct:%d != byteCount:%d)\n", err_hold, ret, ct, byteCount);
794
+ return 0;
795
+ }
796
+ if (ret < 0)
797
+ {
798
+ //printf("SOCKET RETURNED %d, setting to 0, errno=%d\n", ret, err_hold);
799
+ //NSLog([NSString stringWithFormat:@"SOCKET RETURNED %d, setting to 0, errno=%d\n", ret, errno]);
800
+ ret = 0;
801
+ }
802
+
803
+ ct = ct + ret;
804
+ }
805
+ //if (ct != byteCount)
806
+ //{
807
+ // printf("(ct != byteCount) -- TERMINATE CONNECTION!!, errno = %d, ret = %d, (ct:%d != byteCount:%d)\n", err_hold, ret, ct, byteCount);
808
+ //}
809
+
810
+ return ct;
811
+ }
812
+ #endif
813
+
814
+ // ====================================================================================================================
815
+ #if MACOSX_CARBON
816
+ int ob_socketread (EndpointRef inEndpoint, void* ioBuffer, size_t recvSize, int *flag)
817
+ {
818
+ #pragma unused (flag)
819
+
820
+ OTFlags theJunkFlags = NULL; // there were not initialized
821
+ int theReceivedByteCount = NULL; //[jsh] 11/09/2003 signed type
822
+ void* thePacket = (Ptr) ioBuffer;
823
+ UInt32 xferSize = 0L;
824
+ UInt32 theSize = recvSize;
825
+
826
+ if (!OTinited) initOT() ;
827
+
828
+ // WAS: theReceivedByteCount = ::OTRcv(inEndpoint, ioBuffer, inByteCount, &theJunkFlags);
829
+
830
+ while(recvSize > 0) // read all expected bytes
831
+ {
832
+ // YieldToAnyThread(); // not needed if called elsewhere
833
+ thePacket = (void*)((UInt32)ioBuffer + xferSize); // advance ptr into incoming buffer
834
+ theReceivedByteCount = ::OTRcv(inEndpoint, thePacket, theSize, &theJunkFlags);
835
+ //[jsh] 11/09/2003 Negative return value indicates error code.
836
+ if (theReceivedByteCount < 0) return(theReceivedByteCount);
837
+ recvSize -= theReceivedByteCount; // reduce amount to read
838
+ xferSize += theReceivedByteCount; // advance ptr into incoming buffer
839
+ theSize = recvSize; // ready for next usage
840
+ }
841
+
842
+ theReceivedByteCount = xferSize;
843
+
844
+ return(theReceivedByteCount);
845
+ }
846
+
847
+ #endif
848
+
849
+ // ====================================================================================================================
850
+ // ====================================================================================================================
851
+ // ====================================================================================================================
852
+ void ob_intToBytes(unsigned char *buffer, int number)
853
+ {
854
+ #ifdef SWAPENDIAN
855
+ unsigned char *i;
856
+
857
+ i = (unsigned char *)&number;
858
+ buffer[0] = i[3];
859
+ buffer[1] = i[2];
860
+ buffer[2] = i[1];
861
+ buffer[3] = i[0];
862
+ #else
863
+ memcpy(buffer, &number, sizeof(int));
864
+ #endif
865
+ }
866
+
867
+
868
+ // ====================================================================================================================
869
+ // ====================================================================================================================
870
+ // ====================================================================================================================
871
+ void ob_bytesToInt(int *number, unsigned char *buffer)
872
+ {
873
+ #ifdef SWAPENDIAN
874
+ unsigned char *i;
875
+ i = (unsigned char *)number;
876
+ i[0] = buffer[3];
877
+ i[1] = buffer[2];
878
+ i[2] = buffer[1];
879
+ i[3] = buffer[0];
880
+ #else
881
+ memcpy(number, buffer, sizeof(int));
882
+ #endif
883
+ }
884
+
885
+
886
+ #ifdef __cplusplus
887
+ }
888
+ #endif