sedna 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/{CHANGES → CHANGES.rdoc} +9 -0
  2. data/{README → README.rdoc} +23 -25
  3. data/Rakefile +32 -9
  4. data/ext/{extconf.rb → sedna/extconf.rb} +33 -21
  5. data/ext/{sedna.c → sedna/sedna.c} +48 -40
  6. data/test/sedna_test.rb +9 -9
  7. data/vendor/sedna/AUTHORS +18 -0
  8. data/vendor/sedna/COPYRIGHT +90 -0
  9. data/vendor/sedna/LICENSE +202 -0
  10. data/vendor/sedna/Makefile.include +423 -0
  11. data/vendor/sedna/Makefile.platform +31 -0
  12. data/vendor/sedna/depend.sed +48 -0
  13. data/vendor/sedna/driver/c/Makefile +98 -0
  14. data/vendor/sedna/driver/c/libsedna.c +1998 -0
  15. data/vendor/sedna/driver/c/libsedna.h +199 -0
  16. data/vendor/sedna/driver/c/sednamt.def +21 -0
  17. data/vendor/sedna/driver/c/sp_defs.h +186 -0
  18. data/vendor/sedna/kernel/common/FastXptrHash.cpp +101 -0
  19. data/vendor/sedna/kernel/common/IntHash.h +314 -0
  20. data/vendor/sedna/kernel/common/IntList.h +224 -0
  21. data/vendor/sedna/kernel/common/Makefile +30 -0
  22. data/vendor/sedna/kernel/common/SSMMsg.cpp +459 -0
  23. data/vendor/sedna/kernel/common/SSMMsg.h +142 -0
  24. data/vendor/sedna/kernel/common/XptrHash.h +435 -0
  25. data/vendor/sedna/kernel/common/argtable.c +972 -0
  26. data/vendor/sedna/kernel/common/argtable.h +896 -0
  27. data/vendor/sedna/kernel/common/base.cpp +339 -0
  28. data/vendor/sedna/kernel/common/base.h +226 -0
  29. data/vendor/sedna/kernel/common/bit_set.cpp +157 -0
  30. data/vendor/sedna/kernel/common/bit_set.h +55 -0
  31. data/vendor/sedna/kernel/common/commutil.h +67 -0
  32. data/vendor/sedna/kernel/common/config.h +62 -0
  33. data/vendor/sedna/kernel/common/counted_ptr.h +74 -0
  34. data/vendor/sedna/kernel/common/errdbg/ErrorCodes.java +1056 -0
  35. data/vendor/sedna/kernel/common/errdbg/Makefile +34 -0
  36. data/vendor/sedna/kernel/common/errdbg/assert.c +133 -0
  37. data/vendor/sedna/kernel/common/errdbg/d_printf.c +150 -0
  38. data/vendor/sedna/kernel/common/errdbg/d_printf.h +91 -0
  39. data/vendor/sedna/kernel/common/errdbg/error.codes +1743 -0
  40. data/vendor/sedna/kernel/common/errdbg/error_codes.c +531 -0
  41. data/vendor/sedna/kernel/common/errdbg/error_codes.h +549 -0
  42. data/vendor/sedna/kernel/common/errdbg/error_codes_scm.scm +527 -0
  43. data/vendor/sedna/kernel/common/errdbg/event_log.c +956 -0
  44. data/vendor/sedna/kernel/common/errdbg/event_log.h +226 -0
  45. data/vendor/sedna/kernel/common/errdbg/exceptions.cpp +155 -0
  46. data/vendor/sedna/kernel/common/errdbg/exceptions.h +559 -0
  47. data/vendor/sedna/kernel/common/errdbg/gen_error_codes +0 -0
  48. data/vendor/sedna/kernel/common/errdbg/gen_error_codes.c +345 -0
  49. data/vendor/sedna/kernel/common/gmm.cpp +192 -0
  50. data/vendor/sedna/kernel/common/gmm.h +29 -0
  51. data/vendor/sedna/kernel/common/ipc_ops.cpp +435 -0
  52. data/vendor/sedna/kernel/common/ipc_ops.h +51 -0
  53. data/vendor/sedna/kernel/common/lfsGlobals.h +12 -0
  54. data/vendor/sedna/kernel/common/lm_base.h +90 -0
  55. data/vendor/sedna/kernel/common/mmgr/Makefile +11 -0
  56. data/vendor/sedna/kernel/common/mmgr/aset.c +1185 -0
  57. data/vendor/sedna/kernel/common/mmgr/mcxt.c +741 -0
  58. data/vendor/sedna/kernel/common/mmgr/memnodes.h +70 -0
  59. data/vendor/sedna/kernel/common/mmgr/memutils.h +145 -0
  60. data/vendor/sedna/kernel/common/mmgr/se_alloc.h +321 -0
  61. data/vendor/sedna/kernel/common/mmgr/track.c +214 -0
  62. data/vendor/sedna/kernel/common/pping.cpp +672 -0
  63. data/vendor/sedna/kernel/common/pping.h +119 -0
  64. data/vendor/sedna/kernel/common/rcv_test.cpp +273 -0
  65. data/vendor/sedna/kernel/common/rcv_test.h +19 -0
  66. data/vendor/sedna/kernel/common/sedna.c +128 -0
  67. data/vendor/sedna/kernel/common/sedna.h +49 -0
  68. data/vendor/sedna/kernel/common/sedna_ef.h +52 -0
  69. data/vendor/sedna/kernel/common/sm_vmm_data.h +144 -0
  70. data/vendor/sedna/kernel/common/sp.c +93 -0
  71. data/vendor/sedna/kernel/common/sp.h +36 -0
  72. data/vendor/sedna/kernel/common/st/Makefile +20 -0
  73. data/vendor/sedna/kernel/common/st/os_linux/stacktrace.c +213 -0
  74. data/vendor/sedna/kernel/common/st/os_nt/stacktrace.c +338 -0
  75. data/vendor/sedna/kernel/common/st/os_other/stacktrace.c +39 -0
  76. data/vendor/sedna/kernel/common/st/stacktrace.h +72 -0
  77. data/vendor/sedna/kernel/common/st/stacktrfmt.c +64 -0
  78. data/vendor/sedna/kernel/common/tr_debug.cpp +112 -0
  79. data/vendor/sedna/kernel/common/tr_debug.h +22 -0
  80. data/vendor/sedna/kernel/common/u/Makefile +14 -0
  81. data/vendor/sedna/kernel/common/u/u.c +268 -0
  82. data/vendor/sedna/kernel/common/u/u.h +715 -0
  83. data/vendor/sedna/kernel/common/u/uatomic.h +12 -0
  84. data/vendor/sedna/kernel/common/u/udl.h +31 -0
  85. data/vendor/sedna/kernel/common/u/uevent.c +406 -0
  86. data/vendor/sedna/kernel/common/u/uevent.h +71 -0
  87. data/vendor/sedna/kernel/common/u/ugnames.cpp +330 -0
  88. data/vendor/sedna/kernel/common/u/ugnames.h +134 -0
  89. data/vendor/sedna/kernel/common/u/uhash_map.h +77 -0
  90. data/vendor/sedna/kernel/common/u/uhdd.c +1018 -0
  91. data/vendor/sedna/kernel/common/u/uhdd.h +206 -0
  92. data/vendor/sedna/kernel/common/u/ummap.cpp +268 -0
  93. data/vendor/sedna/kernel/common/u/ummap.h +60 -0
  94. data/vendor/sedna/kernel/common/u/umutex.c +145 -0
  95. data/vendor/sedna/kernel/common/u/umutex.h +65 -0
  96. data/vendor/sedna/kernel/common/u/upipe.cpp +244 -0
  97. data/vendor/sedna/kernel/common/u/upipe.h +74 -0
  98. data/vendor/sedna/kernel/common/u/uprocess.c +767 -0
  99. data/vendor/sedna/kernel/common/u/uprocess.h +91 -0
  100. data/vendor/sedna/kernel/common/u/usafesync.h +41 -0
  101. data/vendor/sedna/kernel/common/u/usecurity.c +150 -0
  102. data/vendor/sedna/kernel/common/u/usecurity.h +55 -0
  103. data/vendor/sedna/kernel/common/u/usem.c +891 -0
  104. data/vendor/sedna/kernel/common/u/usem.h +83 -0
  105. data/vendor/sedna/kernel/common/u/ushm.c +222 -0
  106. data/vendor/sedna/kernel/common/u/ushm.h +46 -0
  107. data/vendor/sedna/kernel/common/u/usocket.c +541 -0
  108. data/vendor/sedna/kernel/common/u/usocket.h +118 -0
  109. data/vendor/sedna/kernel/common/u/usystem.c +57 -0
  110. data/vendor/sedna/kernel/common/u/usystem.h +46 -0
  111. data/vendor/sedna/kernel/common/u/uthread.c +259 -0
  112. data/vendor/sedna/kernel/common/u/uthread.h +95 -0
  113. data/vendor/sedna/kernel/common/u/utime.c +65 -0
  114. data/vendor/sedna/kernel/common/u/utime.h +40 -0
  115. data/vendor/sedna/kernel/common/u/uutils.c +142 -0
  116. data/vendor/sedna/kernel/common/u/uutils.h +65 -0
  117. data/vendor/sedna/kernel/common/ugc.cpp +156 -0
  118. data/vendor/sedna/kernel/common/ugc.h +15 -0
  119. data/vendor/sedna/kernel/common/utils.cpp +156 -0
  120. data/vendor/sedna/kernel/common/utils.h +133 -0
  121. data/vendor/sedna/kernel/common/version.c +16 -0
  122. data/vendor/sedna/kernel/common/version.h +21 -0
  123. data/vendor/sedna/kernel/common/wustructures.h +18 -0
  124. data/vendor/sedna/kernel/common/wutypes.h +34 -0
  125. data/vendor/sedna/kernel/common/xptr.cpp +17 -0
  126. data/vendor/sedna/kernel/common/xptr.h +211 -0
  127. data/vendor/sedna/ver +1 -0
  128. metadata +142 -14
@@ -0,0 +1,83 @@
1
+ /*
2
+ * File: usem.h
3
+ * Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
4
+ */
5
+
6
+
7
+ #ifndef _USEM_H
8
+ #define _USEM_H
9
+
10
+ #include "common/u/u.h"
11
+ #include "common/u/usecurity.h"
12
+
13
+ #ifdef _WIN32
14
+
15
+ typedef HANDLE USemaphore; // named semaphore (interprocess semaphore)
16
+ typedef HANDLE *USemaphoreArr; // named array of semaphores (interprocess semaphore)
17
+ typedef HANDLE UUnnamedSemaphore; // unnamed semaphore (intraprocess semaphore)
18
+
19
+ #else
20
+
21
+ #include <sys/sem.h>
22
+ #include <pthread.h>
23
+
24
+ typedef int USemaphore; // named semaphore (interprocess semaphore)
25
+ typedef int USemaphoreArr; // named array of semaphores (interprocess semaphore)
26
+ typedef struct {
27
+ pthread_mutex_t mutex;
28
+ pthread_cond_t condition;
29
+ int count;
30
+ } UUnnamedSemaphore; // unnamed semaphore (intraprocess semaphore)
31
+
32
+
33
+ #endif
34
+
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ // functions return 0 on success
41
+ // sa - Security Attributes for the new semaphore
42
+ int USemaphoreCreate(USemaphore *sem, int init_value, int max_value, global_name name, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun);
43
+ int USemaphoreOpen(USemaphore *sem, global_name name, sys_call_error_fun fun);
44
+ int USemaphoreRelease(USemaphore sem, sys_call_error_fun fun);
45
+ int USemaphoreClose(USemaphore sem, sys_call_error_fun fun);
46
+ int USemaphoreDown(USemaphore sem, sys_call_error_fun fun);
47
+ // down with timout
48
+ // return values: 0 - success
49
+ // 1 - falure
50
+ // 2 - timeout
51
+ int USemaphoreDownTimeout(USemaphore sem, unsigned int millisec, sys_call_error_fun fun);
52
+ int USemaphoreUp(USemaphore sem, sys_call_error_fun fun);
53
+
54
+
55
+ // sa - Security Attributes for the new semaphores
56
+ int USemaphoreArrCreate(USemaphoreArr *sem, int size, const int *init_values, global_name name, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun);
57
+ int USemaphoreArrOpen(USemaphoreArr *sem, int size, global_name name, sys_call_error_fun fun);
58
+ int USemaphoreArrRelease(USemaphoreArr sem, int size, sys_call_error_fun fun);
59
+ int USemaphoreArrClose(USemaphoreArr sem, int size, sys_call_error_fun fun);
60
+ int USemaphoreArrDown(USemaphoreArr sem, int i, sys_call_error_fun fun);
61
+ int USemaphoreArrDownTimeout(USemaphoreArr sem, int i, unsigned int millisec, sys_call_error_fun fun);
62
+ int USemaphoreArrUp(USemaphoreArr sem, int i, sys_call_error_fun fun);
63
+
64
+
65
+
66
+ // sa - Security Attributes for the new unnamed semaphore
67
+ int UUnnamedSemaphoreCreate(UUnnamedSemaphore *sem, int init_value, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun);
68
+ int UUnnamedSemaphoreRelease(UUnnamedSemaphore *sem, sys_call_error_fun fun);
69
+ int UUnnamedSemaphoreDown(UUnnamedSemaphore *sem, sys_call_error_fun fun);
70
+ // down with timout
71
+ // return values: 0 - success
72
+ // 1 - falure
73
+ // 2 - timeout
74
+ int UUnnamedSemaphoreDownTimeout(UUnnamedSemaphore *sem, unsigned int millisec, sys_call_error_fun fun);
75
+ int UUnnamedSemaphoreUp(UUnnamedSemaphore *sem, sys_call_error_fun fun);
76
+
77
+ #ifdef __cplusplus
78
+ }
79
+ #endif
80
+
81
+ #endif
82
+
83
+
@@ -0,0 +1,222 @@
1
+ /*
2
+ * File: ushm.cpp
3
+ * Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
4
+ */
5
+
6
+
7
+ #include "common/u/ushm.h"
8
+ #include "common/errdbg/d_printf.h"
9
+ #include "common/u/ugnames.h"
10
+
11
+ //#define RIGHTS 0666
12
+
13
+ int uCreateShMem(UShMem *id, global_name name, int size, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
14
+ #ifdef _WIN32
15
+ {
16
+ char buf[128];
17
+ const char *wName = NULL;
18
+
19
+ wName = UWinIPCNameFromGlobalName(name,buf,128);
20
+
21
+ //printf("uCreateShMem name = %s\n", name);
22
+ *id = CreateFileMapping(INVALID_HANDLE_VALUE,
23
+ sa,
24
+ PAGE_READWRITE,
25
+ 0,
26
+ size,
27
+ wName
28
+ );
29
+
30
+ //printf("CreateFileMapping %s\n", name);
31
+
32
+ if (*id == NULL)
33
+ {
34
+ sys_call_error("CreateFileMapping");
35
+ return 1;
36
+ }
37
+
38
+ return 0;
39
+ }
40
+ #else
41
+ {
42
+ key_t key = IPC_PRIVATE;
43
+ key = USys5IPCKeyFromGlobalName(name);
44
+
45
+ USECURITY_ATTRIBUTES shm_access_mode = U_SEDNA_SHMEM_ACCESS_PERMISSIONS_MASK;
46
+ if (sa) shm_access_mode = *sa;
47
+ *id = shmget(key, size, IPC_CREAT | IPC_EXCL | shm_access_mode);
48
+
49
+ if(*id == -1)
50
+ {
51
+ sys_call_error("shmget");
52
+ return 1;
53
+ }
54
+ /*
55
+ shmid_ds info;
56
+
57
+ info.shm_perm.uid = geteuid();
58
+ info.shm_perm.gid = getegid();
59
+ info.shm_perm.mode |= 0600;
60
+
61
+ if (shmctl(*id, IPC_SET, &info) == -1)
62
+ {
63
+ printf("uCreateShMem failed\n");
64
+ return 1;
65
+ }
66
+ */
67
+ return 0;
68
+ }
69
+ #endif
70
+
71
+ int uOpenShMem(UShMem *id, global_name name, int size, sys_call_error_fun fun)
72
+ #ifdef _WIN32
73
+ {
74
+ char buf[128];
75
+ const char *wName = NULL;
76
+
77
+ wName = UWinIPCNameFromGlobalName(name,buf,128);
78
+
79
+ //printf("uOpenShMem name = %s\n", name);
80
+ *id = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
81
+ FALSE, // Do not inherit the name
82
+ wName // of the mapping object.
83
+ );
84
+
85
+ //printf("OpenFileMapping %s\n", name);
86
+
87
+ if (*id == NULL)
88
+ {
89
+ sys_call_error("OpenFileMapping");
90
+ return 1;
91
+ }
92
+
93
+ return 0;
94
+ }
95
+ #else
96
+ {
97
+ key_t key = IPC_PRIVATE;
98
+ key = USys5IPCKeyFromGlobalName(name);
99
+
100
+ //d_printf2("name = %d\n", name);
101
+ *(id) = shmget(key, size, 0);
102
+
103
+ if(*id == -1)
104
+ {
105
+ sys_call_error("shmget");
106
+ return 1;
107
+ }
108
+
109
+ return 0;
110
+ }
111
+ #endif
112
+
113
+ int uReleaseShMem(UShMem id, sys_call_error_fun fun)
114
+ #ifdef _WIN32
115
+ {
116
+ BOOL res = 0;
117
+ res = CloseHandle(id);
118
+ if (res == 0)
119
+ {
120
+ sys_call_error("CloseHandle");
121
+ return 1;
122
+ }
123
+
124
+ return 0;
125
+ }
126
+ #else
127
+ {
128
+ if(id < 0)
129
+ {
130
+ d_printf1("uReleaseShMem failed\n");
131
+ //d_printf2("Error %d\n", perror(semget));
132
+ return 1;
133
+ }
134
+ else
135
+ {
136
+ if (shmctl(id, IPC_RMID, NULL) < 0)
137
+ {
138
+ // if shared memory already destroyed don't raise an error
139
+ if (errno == EINVAL) return 0;
140
+
141
+ sys_call_error("shmctl");
142
+ return 1;
143
+ }
144
+ }
145
+ return 0;
146
+ }
147
+ #endif
148
+
149
+ int uCloseShMem(UShMem id, sys_call_error_fun fun)
150
+ #ifdef _WIN32
151
+ {
152
+ BOOL res = 0;
153
+ res = CloseHandle(id);
154
+ if (res == 0)
155
+ {
156
+ sys_call_error("CloseHandle");
157
+ return 1;
158
+ }
159
+
160
+ return 0;
161
+ }
162
+ #else
163
+ {
164
+ return 0;
165
+ }
166
+ #endif
167
+
168
+ void* uAttachShMem(UShMem id, void *ptr, int size, sys_call_error_fun fun)
169
+ #ifdef _WIN32
170
+ {
171
+ void *res = NULL;
172
+ res = MapViewOfFileEx(id, // Handle to mapping object.
173
+ FILE_MAP_ALL_ACCESS, // Read/write permission.
174
+ 0,
175
+ 0,
176
+ size,
177
+ ptr
178
+ );
179
+
180
+ if (res == NULL)
181
+ {
182
+ sys_call_error("MapViewOfFileEx");
183
+ return NULL;
184
+ }
185
+
186
+ return res;
187
+ }
188
+ #else
189
+ {
190
+ void *res = NULL;
191
+ if ((int)(res = shmat(id, ptr, 0)) == -1)
192
+ {
193
+ sys_call_error("shmat");
194
+ return NULL;
195
+ }
196
+ return res;
197
+ }
198
+ #endif
199
+
200
+ int uDettachShMem(UShMem id, void * ptr, sys_call_error_fun fun)
201
+ #ifdef _WIN32
202
+ {
203
+ BOOL res = 0;
204
+ res = UnmapViewOfFile(ptr);
205
+ if (res == 0)
206
+ {
207
+ sys_call_error("UnmapViewOfFile");
208
+ return 1;
209
+ }
210
+
211
+ return 0;
212
+ }
213
+ #else
214
+ {
215
+ if(shmdt(ptr) < 0)
216
+ {
217
+ sys_call_error("shmdt");
218
+ return 1;
219
+ }
220
+ return 0;
221
+ }
222
+ #endif
@@ -0,0 +1,46 @@
1
+ /*
2
+ * File: ushm.h
3
+ * Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
4
+ */
5
+
6
+ #ifndef _USHMEM_H
7
+ #define _USHMEM_H
8
+
9
+ #include "common/u/u.h"
10
+ #include "common/u/usecurity.h"
11
+
12
+ #ifdef _WIN32
13
+
14
+ typedef HANDLE UShMem;
15
+
16
+ #else
17
+
18
+ #include <sys/shm.h>
19
+
20
+ typedef int UShMem;
21
+
22
+ #endif
23
+
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ int uCreateShMem(UShMem *id, global_name name, int size, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun);
30
+
31
+ int uOpenShMem(UShMem *id, global_name key, int size, sys_call_error_fun fun);
32
+
33
+ int uReleaseShMem(UShMem id, sys_call_error_fun fun);
34
+
35
+ int uCloseShMem(UShMem id, sys_call_error_fun fun);
36
+
37
+ void* uAttachShMem(UShMem id, void *ptr, int size, sys_call_error_fun fun);
38
+
39
+ int uDettachShMem(UShMem id, void * ptr, sys_call_error_fun fun);
40
+
41
+ #ifdef __cplusplus
42
+ }
43
+ #endif
44
+
45
+ #endif
46
+
@@ -0,0 +1,541 @@
1
+ /*
2
+ * File: usocket.c
3
+ * Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
4
+ */
5
+
6
+ #ifndef _WIN32
7
+ #include <netdb.h>
8
+ #include <string.h>
9
+ #include <sys/types.h>
10
+ #include <netinet/tcp.h>
11
+ #include <netinet/in.h>
12
+ #include <arpa/inet.h>
13
+ #else
14
+ #include <Winsock2.h>
15
+ #endif
16
+
17
+
18
+ #include "common/u/usocket.h"
19
+ #include "common/errdbg/d_printf.h"
20
+
21
+
22
+ /* returns zero if succeeded
23
+ returns U_SOCKET_ERROR if failed */
24
+ int uSocketInit(sys_call_error_fun fun)
25
+ {
26
+ #ifdef _WIN32
27
+ WORD wVersionRequested;
28
+ WSADATA wsaData;
29
+ int err;
30
+
31
+ wVersionRequested = MAKEWORD(2, 2);
32
+
33
+ err = WSAStartup(wVersionRequested, &wsaData);
34
+ if (err != 0)
35
+ {
36
+ sys_call_error2("WSAStartup", &err);
37
+ return U_SOCKET_ERROR;
38
+ }
39
+
40
+ if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
41
+ {
42
+ if (WSACleanup() != 0) sys_call_error("WSACleanup");
43
+
44
+ return U_SOCKET_ERROR;
45
+ }
46
+
47
+ return 0;
48
+ #else
49
+ return 0;
50
+ #endif
51
+ }
52
+
53
+ /* returns zero if succeeded
54
+ returns U_SOCKET_ERROR if failed */
55
+ int uSocketCleanup(sys_call_error_fun fun)
56
+ {
57
+ #ifdef _WIN32
58
+ int res = WSACleanup();
59
+ if (res != 0) sys_call_error("WSACleanup");
60
+ return res;
61
+ #else
62
+ return 0;
63
+ #endif
64
+ }
65
+
66
+
67
+ /* returns U_INVALID_SOCKET if failed */
68
+ USOCKET usocket(int af, int type, int protocol, sys_call_error_fun fun)
69
+ {
70
+ #ifdef _WIN32
71
+ USOCKET res = socket(af, type, protocol);
72
+ if (res == INVALID_SOCKET) sys_call_error("socket");
73
+ return res;
74
+ #else
75
+ int param = 1, res = 0;
76
+ int sockfd = socket(af, type, protocol);
77
+ if (sockfd == U_SOCKET_ERROR)
78
+ {
79
+ sys_call_error("socket");
80
+ return U_INVALID_SOCKET;
81
+ }
82
+
83
+ #if defined(DARWIN) /// Under DARWIN this is the only way not to get damn SIGPIPE!
84
+ res = setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &param, sizeof(int));
85
+ if(-1 == res)
86
+ {
87
+ sys_call_error("setsockopt");
88
+ close(sockfd);
89
+ return U_INVALID_SOCKET;
90
+ }
91
+ #endif /* DARWIN */
92
+
93
+ return sockfd;
94
+ #endif
95
+ }
96
+
97
+ /* returns zero if succeeded
98
+ returns U_SOCKET_ERROR if failed */
99
+ int ubind_tcp(USOCKET s, int port, sys_call_error_fun fun)
100
+ {
101
+ #ifdef _WIN32
102
+ struct hostent *hp;
103
+ struct sockaddr_in ownaddr;
104
+
105
+ if ((hp = gethostbyname("0.0.0.0")) == NULL)
106
+ {
107
+ sys_call_error("gethostbyname");
108
+ return U_SOCKET_ERROR;
109
+ }
110
+
111
+ memset(&ownaddr, 0, sizeof ownaddr);
112
+ ownaddr.sin_family = AF_INET;
113
+ ownaddr.sin_port = htons(port);
114
+ memcpy(&ownaddr.sin_addr, hp->h_addr, hp->h_length);
115
+
116
+ if (bind(s, (struct sockaddr *) &ownaddr, sizeof ownaddr) != 0)
117
+ {
118
+ sys_call_error("bind");
119
+ return U_SOCKET_ERROR;
120
+ }
121
+ return 0;
122
+ #else
123
+ struct hostent *hp;
124
+ struct sockaddr_in ownaddr;
125
+
126
+ int t_reuse = 1;
127
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &t_reuse, sizeof(int));
128
+
129
+ if ((hp = gethostbyname("0.0.0.0")) == NULL)
130
+ {
131
+ sys_call_error("gethostbyname");
132
+ return U_SOCKET_ERROR;
133
+ }
134
+
135
+ memset(&ownaddr, 0, sizeof ownaddr);
136
+ ownaddr.sin_family = AF_INET;
137
+ ownaddr.sin_port = htons(port);
138
+ memcpy(&ownaddr.sin_addr, hp->h_addr, hp->h_length);
139
+
140
+ if (bind(s, (struct sockaddr *) &ownaddr, sizeof ownaddr) != 0)
141
+ {
142
+ sys_call_error("bind");
143
+ return U_SOCKET_ERROR;
144
+ }
145
+ return 0;
146
+ #endif
147
+ }
148
+
149
+ /* returns zero if succeeded
150
+ returns U_SOCKET_ERROR if failed */
151
+ int uconnect_tcp(USOCKET s, int port, const char *hostname, sys_call_error_fun fun)
152
+ {
153
+ #ifdef _WIN32
154
+ struct hostent *hp;
155
+ struct sockaddr_in ownaddr;
156
+
157
+ if ((hp = gethostbyname(hostname)) == NULL)
158
+ {
159
+ sys_call_error("gethostbyname");
160
+ return U_SOCKET_ERROR;
161
+ }
162
+
163
+ memset(&ownaddr, 0, sizeof ownaddr);
164
+ ownaddr.sin_family = AF_INET;
165
+ ownaddr.sin_port = htons(port);
166
+ memcpy(&ownaddr.sin_addr, hp->h_addr, hp->h_length);
167
+
168
+ if (connect(s, (struct sockaddr *) &ownaddr, sizeof ownaddr) != 0)
169
+ {
170
+ sys_call_error("connect");
171
+ return U_SOCKET_ERROR;
172
+ }
173
+ return 0;
174
+ #else
175
+ struct hostent *hp;
176
+ struct sockaddr_in ownaddr;
177
+
178
+
179
+ if ((hp = gethostbyname(hostname)) == NULL)
180
+ {
181
+ sys_call_error("gethostbyname");
182
+ return U_SOCKET_ERROR;
183
+ }
184
+
185
+ memset(&ownaddr, 0, sizeof ownaddr);
186
+ ownaddr.sin_family = AF_INET;
187
+ ownaddr.sin_port = htons(port);
188
+ memcpy(&ownaddr.sin_addr, hp->h_addr, hp->h_length);
189
+
190
+ if (connect(s, (struct sockaddr *) &ownaddr, sizeof ownaddr) != 0)
191
+ {
192
+ sys_call_error("connect");
193
+ return U_SOCKET_ERROR;
194
+ }
195
+ return 0;
196
+ #endif
197
+ }
198
+
199
+ /* returns zero if succeeded
200
+ returns U_SOCKET_ERROR if failed */
201
+ int usetsockopt(USOCKET s, int level, int optname, const void* optval, unsigned int optlen, sys_call_error_fun fun)
202
+ {
203
+ #ifdef _WIN32
204
+ int res = setsockopt(s, level, optname, (const char*)optval, optlen);
205
+ if (res == U_SOCKET_ERROR) sys_call_error("setsockopt");
206
+ return res;
207
+ #else
208
+ int res = setsockopt(s, level, optname, optval, optlen);
209
+ if (res == U_SOCKET_ERROR) sys_call_error("setsockopt");
210
+ return res;
211
+ #endif
212
+ }
213
+
214
+ /* returns zero if succeeded
215
+ returns U_SOCKET_ERROR if failed */
216
+ int ugetsockopt(USOCKET s, int level, int optname, void* optval, unsigned int optlen, sys_call_error_fun fun)
217
+ {
218
+ #ifdef _WIN32
219
+ int res = getsockopt(s, level, optname, (char*)optval, &optlen);
220
+ if (res == U_SOCKET_ERROR) sys_call_error("getsockopt");
221
+ return res;
222
+ #else
223
+ int res = getsockopt(s, level, optname, optval, &optlen);
224
+ if (res == U_SOCKET_ERROR) sys_call_error("getsockopt");
225
+ return res;
226
+ #endif
227
+ }
228
+
229
+ /* returns zero if succeeded
230
+ returns U_SOCKET_ERROR if failed */
231
+ int ulisten(USOCKET s, int backlog, sys_call_error_fun fun)
232
+ {
233
+ #ifdef _WIN32
234
+ int res = listen(s, backlog);
235
+ if (res == U_SOCKET_ERROR) sys_call_error("listen");
236
+ return res;
237
+ #else
238
+ int res = listen(s, backlog);
239
+ if (res == U_SOCKET_ERROR) sys_call_error("listen");
240
+ return res;
241
+ #endif
242
+ }
243
+
244
+ /* returns U_INVALID_SOCKET if failed */
245
+ USOCKET uaccept(USOCKET s, sys_call_error_fun fun)
246
+ {
247
+ #ifdef _WIN32
248
+ struct sockaddr_in commonaddr;
249
+ int commonlen;
250
+ USOCKET socknew;
251
+ memset(&commonaddr, 0, sizeof commonaddr);
252
+ commonlen = sizeof commonaddr;
253
+ socknew = accept(s, (struct sockaddr *) &commonaddr, &commonlen);
254
+ if (socknew == INVALID_SOCKET)
255
+ {
256
+ sys_call_error("accept");
257
+ return U_INVALID_SOCKET;
258
+ }
259
+ return socknew;
260
+ #else
261
+ int res = 0, param = 1;
262
+ struct sockaddr_in commonaddr;
263
+ unsigned int commonlen;
264
+ USOCKET socknew;
265
+ memset(&commonaddr, 0, sizeof commonaddr);
266
+ commonlen = sizeof commonaddr;
267
+ socknew = accept(s, (struct sockaddr *) &commonaddr, &commonlen);
268
+ if (socknew == U_INVALID_SOCKET)
269
+ {
270
+ sys_call_error("accept");
271
+ return U_INVALID_SOCKET;
272
+ }
273
+ #if defined(DARWIN) /// Under DARWIN this is the only way not to get damn SIGPIPE!
274
+ res = setsockopt(socknew, SOL_SOCKET, SO_NOSIGPIPE, &param, sizeof(int));
275
+ if(-1 == res)
276
+ {
277
+ sys_call_error("setsockopt");
278
+ close(socknew);
279
+ return U_INVALID_SOCKET;
280
+ }
281
+ #endif /* DARWIN */
282
+ return socknew;
283
+ #endif
284
+ }
285
+
286
+ /* return value indicates number of bytes received
287
+ returns zero if connection was gracefully closed
288
+ returns U_SOCKET_ERROR in the case of error */
289
+ int urecv(USOCKET s, char *buf, int len, sys_call_error_fun fun)
290
+ {
291
+ #ifdef _WIN32
292
+ int res_len = recv(s, buf, len, 0);
293
+ if (res_len == U_SOCKET_ERROR) sys_call_error("recv");
294
+ return res_len;
295
+ #else
296
+ int res_len;
297
+
298
+ while (1)
299
+ {
300
+ res_len = recv(s, buf, len, 0);
301
+
302
+ if (res_len == U_SOCKET_ERROR)
303
+ if (errno == EINTR)
304
+ continue;
305
+ else
306
+ {
307
+ sys_call_error("recv");
308
+ return U_SOCKET_ERROR;
309
+ }
310
+ else
311
+ return res_len;
312
+ }
313
+ #endif
314
+ }
315
+
316
+ /* return value indicates number of bytes send
317
+ returns U_SOCKET_ERROR in the case of error */
318
+ int usend(USOCKET s, const char *buf, int len, sys_call_error_fun fun)
319
+ {
320
+ #ifdef _WIN32
321
+ int res_len;
322
+ res_len = send(s, buf, len, 0);
323
+ if (res_len == U_SOCKET_ERROR)
324
+ {
325
+ sys_call_error("send");
326
+ return U_SOCKET_ERROR;
327
+ }
328
+
329
+ return res_len;
330
+ #else
331
+ int res_len;
332
+
333
+ while (1)
334
+ {
335
+ res_len = send(s, buf, len, U_MSG_NOSIGNAL);
336
+ if (res_len == U_SOCKET_ERROR)
337
+ if (errno == EINTR)
338
+ continue;
339
+ else
340
+ {
341
+ sys_call_error("send");
342
+ return U_SOCKET_ERROR;
343
+ }
344
+ else
345
+ return res_len;
346
+ }
347
+ #endif
348
+ }
349
+
350
+ /* returns zero if succeeded
351
+ returns U_SOCKET_ERROR if failed */
352
+ int uclose_socket(USOCKET s, sys_call_error_fun fun)
353
+ {
354
+ #ifdef _WIN32
355
+ int res = closesocket(s);
356
+ if (res == U_SOCKET_ERROR) sys_call_error("closesocket");
357
+ return res;
358
+ #else
359
+ int res = close(s);
360
+ if (res == U_SOCKET_ERROR) sys_call_error("close");
361
+ return res;
362
+ #endif
363
+ }
364
+
365
+ /* returns zero if succeeded
366
+ returns U_SOCKET_ERROR if failed */
367
+ int ushutdown_close_socket(USOCKET s, sys_call_error_fun fun)
368
+ {
369
+ #ifdef _WIN32
370
+ int res = shutdown(s, SD_BOTH);
371
+ if (res != 0)
372
+ {
373
+ sys_call_error("shutdown");
374
+ return U_SOCKET_ERROR;
375
+ }
376
+ res = closesocket(s);
377
+ if (res == U_SOCKET_ERROR) sys_call_error("closesocket");
378
+ return res;
379
+ #else
380
+ int res = shutdown(s, SHUT_RDWR);
381
+ if (res != 0)
382
+ {
383
+ if(errno != ENOTCONN)
384
+ {
385
+ sys_call_error("shutdown");
386
+ return U_SOCKET_ERROR;
387
+ }
388
+ }
389
+ res = close(s);
390
+ if (res == U_SOCKET_ERROR) sys_call_error("close");
391
+ return res;
392
+ #endif
393
+ }
394
+
395
+ /* returns zero if succeeded
396
+ returns U_SOCKET_ERROR if failed */
397
+ int ushutdown_socket(USOCKET s, sys_call_error_fun fun)
398
+ {
399
+ #ifdef _WIN32
400
+ int res = shutdown(s, SD_BOTH);
401
+ if (res != 0)
402
+ {
403
+ sys_call_error("shutdown");
404
+ return U_SOCKET_ERROR;
405
+ }
406
+ return res;
407
+ #else
408
+ int res = shutdown(s, SHUT_RDWR);
409
+ if (res != 0)
410
+ {
411
+ if(errno != ENOTCONN)
412
+ {
413
+ sys_call_error("shutdown");
414
+ return U_SOCKET_ERROR;
415
+ }
416
+ }
417
+ return res;
418
+ #endif
419
+ }
420
+
421
+ /* returns 1 (number of sockets ready to recv) if there is data pending in network connection
422
+ returns 0 if timeout
423
+ returns U_SOCKET_ERROR if failed */
424
+ int uselect_read(USOCKET s, struct timeval *timeout, sys_call_error_fun fun)
425
+ {
426
+ #ifdef _WIN32
427
+ fd_set socks;
428
+ int res = 0;
429
+
430
+ FD_ZERO(&socks);
431
+ FD_SET(s, &socks);
432
+ res = select(1, &socks, (fd_set *) NULL, (fd_set *) NULL, timeout);
433
+ if (res == U_SOCKET_ERROR) sys_call_error("select");
434
+ return res;
435
+ #else
436
+ fd_set socks;
437
+ int res = 0;
438
+
439
+ FD_ZERO(&socks);
440
+ FD_SET(s, &socks);
441
+
442
+ while (1)
443
+ {
444
+ res = select(s + 1, &socks, (fd_set *) NULL, (fd_set *) NULL, timeout);
445
+
446
+ if (res == U_SOCKET_ERROR)
447
+ if (errno == EINTR)
448
+ continue;
449
+ else
450
+ {
451
+ sys_call_error("select");
452
+ return U_SOCKET_ERROR;
453
+ }
454
+ else
455
+ return res;
456
+ }
457
+ #endif
458
+ }
459
+
460
+ /* returns number of sockets ready to recv if there is data pending in network connection
461
+ (s is changed and contains result)
462
+ returns 0 if timeout
463
+ returns U_SOCKET_ERROR if failed */
464
+ int uselect_read_arr(U_SSET *s, int maxfd, struct timeval *timeout, sys_call_error_fun fun)
465
+ {
466
+ #ifdef _WIN32
467
+ int res = 0, i;
468
+
469
+ res = select(1, s, (fd_set *) NULL, (fd_set *) NULL, timeout);
470
+ if (res == U_SOCKET_ERROR) sys_call_error("select");
471
+
472
+ return res;
473
+ #else
474
+ int res = 0, i;
475
+
476
+ while (1)
477
+ {
478
+ res = select(maxfd + 1, s, (fd_set *) NULL, (fd_set *) NULL, timeout);
479
+
480
+ if (res == U_SOCKET_ERROR)
481
+ if (errno == EINTR)
482
+ {
483
+ continue;
484
+ }
485
+ else
486
+ {
487
+ sys_call_error("select");
488
+ return U_SOCKET_ERROR;
489
+ }
490
+ else
491
+ return res;
492
+ }
493
+
494
+ #endif
495
+ }
496
+
497
+ /* retrieves the last socket error description */
498
+ const char *usocket_error_translator()
499
+ {
500
+ #ifdef _WIN32
501
+ static char se_socket_error_buf[1024];
502
+
503
+ if (!FormatMessage(
504
+ FORMAT_MESSAGE_FROM_SYSTEM |
505
+ FORMAT_MESSAGE_IGNORE_INSERTS,
506
+ NULL,
507
+ WSAGetLastError(),
508
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
509
+ (LPTSTR)se_socket_error_buf,
510
+ 1024,
511
+ NULL ))
512
+ {
513
+ strcpy(se_socket_error_buf, "unknown error");
514
+ }
515
+ return se_socket_error_buf;
516
+ #else
517
+ return strerror(errno);
518
+ #endif
519
+
520
+ }
521
+
522
+ /* when connect has failed, utry_connect_again checks if it is reasonable to reconnect
523
+ returns 1 if it is reasonable, zero if it is not */
524
+ int utry_connect_again()
525
+ {
526
+ #ifdef _WIN32
527
+ int sock_error;
528
+ sock_error = WSAGetLastError();
529
+ if ((sock_error == WSAENETUNREACH) || (sock_error == WSAETIMEDOUT))
530
+ return 1;
531
+ else
532
+ return 0;
533
+ #else
534
+ int sock_error;
535
+ sock_error = errno;
536
+ if ((sock_error == ENETUNREACH) || (sock_error == ETIMEDOUT))
537
+ return 1;
538
+ else
539
+ return 0;
540
+ #endif
541
+ }