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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2011. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*
*/
#ifdef __WIN32__
#include <winsock2.h>
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
static unsigned long param_zero = 0;
static unsigned long param_one = 1;
#define SET_BLOCKING(Sock) ioctlsocket((Sock),FIONBIO,¶m_zero)
#define SET_NONBLOCKING(Sock) ioctlsocket((Sock),FIONBIO,¶m_one)
#define ERROR_WOULDBLOCK WSAEWOULDBLOCK
#define ERROR_TIMEDOUT WSAETIMEDOUT
#define ERROR_INPROGRESS WSAEINPROGRESS
#define GET_SOCKET_ERROR() WSAGetLastError()
#define MEANS_SOCKET_ERROR(Ret) ((Ret == SOCKET_ERROR))
#define IS_INVALID_SOCKET(Sock) ((Sock) == INVALID_SOCKET)
#elif VXWORKS
#include <vxWorks.h>
#include <hostLib.h>
#include <ifLib.h>
#include <sockLib.h>
#include <taskLib.h>
#include <inetLib.h>
#include <selectLib.h>
#include <sys/types.h>
#include <ioLib.h>
#include <unistd.h>
static unsigned long param_zero = 0;
static unsigned long param_one = 1;
#define SET_BLOCKING(Sock) ioctl((Sock),FIONBIO,(int)¶m_zero)
#define SET_NONBLOCKING(Sock) ioctl((Sock),FIONBIO,(int)¶m_one)
#define ERROR_WOULDBLOCK EWOULDBLOCK
#define ERROR_TIMEDOUT ETIMEDOUT
#define ERROR_INPROGRESS EINPROGRESS
#define GET_SOCKET_ERROR() (errno)
#define MEANS_SOCKET_ERROR(Ret) ((Ret) == ERROR)
#define IS_INVALID_SOCKET(Sock) ((Sock) < 0)
#else /* other unix */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifndef EWOULDBLOCK
#define ERROR_WOULDBLOCK EAGAIN
#else
#define ERROR_WOULDBLOCK EWOULDBLOCK
#endif
#define SET_BLOCKING(fd) fcntl((fd), F_SETFL, \
fcntl((fd), F_GETFL, 0) & ~O_NONBLOCK)
#define SET_NONBLOCKING(fd) fcntl((fd), F_SETFL, \
fcntl((fd), F_GETFL, 0) | O_NONBLOCK)
#define ERROR_TIMEDOUT ETIMEDOUT
#define ERROR_INPROGRESS EINPROGRESS
#define GET_SOCKET_ERROR() (errno)
#define MEANS_SOCKET_ERROR(Ret) ((Ret) < 0)
#define IS_INVALID_SOCKET(Sock) ((Sock) < 0)
#endif
/* common includes */
#include "eidef.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ei_portio.h"
#include "ei_internal.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#ifdef HAVE_WRITEV
static int ei_writev_t(int fd, struct iovec *iov, int iovcnt, unsigned ms)
{
int res;
if (ms != 0) {
fd_set writemask;
struct timeval tv;
tv.tv_sec = (time_t) (ms / 1000U);
ms %= 1000U;
tv.tv_usec = (time_t) (ms * 1000U);
FD_ZERO(&writemask);
FD_SET(fd,&writemask);
switch (select(fd+1, NULL, &writemask, NULL, &tv)) {
case -1 :
return -1; /* i/o error */
case 0:
return -2; /* timeout */
default:
if (!FD_ISSET(fd, &writemask)) {
return -1; /* Other error */
}
}
}
res = writev(fd, iov, iovcnt);
return (res < 0) ? -1 : res;
}
int ei_writev_fill_t(int fd, const struct iovec *iov, int iovcnt, unsigned ms)
{
int i;
int done;
struct iovec *iov_base = NULL;
struct iovec *current_iov;
int current_iovcnt;
int sum;
for (sum = 0, i = 0; i < iovcnt; ++i) {
sum += iov[i].iov_len;
}
if (ms != 0U) {
SET_NONBLOCKING(fd);
}
current_iovcnt = iovcnt;
current_iov = (struct iovec *) iov;
done = 0;
for (;;) {
i = ei_writev_t(fd, current_iov, current_iovcnt, ms);
if (i <= 0) { /* ei_writev_t should always return at least 1 */
if (ms != 0U) {
SET_BLOCKING(fd);
}
if (iov_base != NULL) {
free(iov_base);
}
return (i);
}
done += i;
if (done < sum) {
if (iov_base == NULL) {
iov_base = malloc(sizeof(struct iovec) * iovcnt);
if (iov_base == NULL) {
return -1;
}
memcpy(iov_base, iov, sizeof(struct iovec) * iovcnt);
current_iov = iov_base;
}
while (i > 0) {
if (i < current_iov[0].iov_len) {
char *p = (char*)current_iov[0].iov_base;
current_iov[0].iov_base = p + i;
current_iov[0].iov_len -= i;
i = 0;
} else {
i -= current_iov[0].iov_len;
current_iov++;
current_iovcnt--;
}
}
} else {
break;
}
}
if (ms != 0U) {
SET_BLOCKING(fd);
}
if (iov_base != NULL) {
free(iov_base);
}
return (sum);
}
#endif
int ei_connect_t(int fd, void *sinp, int sin_siz, unsigned ms)
{
int res;
int error;
int s_res;
struct timeval tv;
fd_set writefds;
fd_set exceptfds;
if (ms == 0) {
res = connect(fd, sinp, sin_siz);
return (res < 0) ? -1 : res;
} else {
SET_NONBLOCKING(fd);
res = connect(fd, sinp, sin_siz);
error = GET_SOCKET_ERROR();
SET_BLOCKING(fd);
if (!MEANS_SOCKET_ERROR(res)) {
return (res < 0) ? -1 : res;
} else {
if (error != ERROR_WOULDBLOCK &&
error != ERROR_INPROGRESS) {
return -1;
} else {
tv.tv_sec = (long) (ms/1000U);
ms %= 1000U;
tv.tv_usec = (long) (ms * 1000U);
FD_ZERO(&writefds);
FD_SET(fd,&writefds);
FD_ZERO(&exceptfds);
FD_SET(fd,&exceptfds);
s_res = select(fd + 1, NULL, &writefds, &exceptfds, &tv);
switch (s_res) {
case 0:
return -2;
case 1:
if (FD_ISSET(fd, &exceptfds)) {
return -1;
} else {
return 0; /* Connect completed */
}
default:
return -1;
}
}
}
}
}
int ei_accept_t(int fd, void *addr, void *addrlen, unsigned ms)
{
int res;
if (ms != 0) {
fd_set readmask;
struct timeval tv;
tv.tv_sec = (time_t) (ms / 1000U);
ms %= 1000U;
tv.tv_usec = (time_t) (ms * 1000U);
FD_ZERO(&readmask);
FD_SET(fd,&readmask);
switch (select(fd+1, &readmask, NULL, NULL, &tv)) {
case -1 :
return -1; /* i/o error */
case 0:
return -2; /* timeout */
default:
if (!FD_ISSET(fd, &readmask)) {
return -1; /* Other error */
}
}
}
res = (int) accept(fd,addr,addrlen);
return (res < 0) ? -1 : res;
}
static int ei_read_t(int fd, char* buf, int len, unsigned ms)
{
int res;
if (ms != 0) {
fd_set readmask;
struct timeval tv;
tv.tv_sec = (time_t) (ms / 1000U);
ms %= 1000U;
tv.tv_usec = (time_t) (ms * 1000U);
FD_ZERO(&readmask);
FD_SET(fd,&readmask);
switch (select(fd+1, &readmask, NULL, NULL, &tv)) {
case -1 :
return -1; /* i/o error */
case 0:
return -2; /* timeout */
default:
if (!FD_ISSET(fd, &readmask)) {
return -1; /* Other error */
}
}
}
res = readsocket(fd, buf, len);
return (res < 0) ? -1 : res;
}
static int ei_write_t(int fd, const char* buf, int len, unsigned ms)
{
int res;
if (ms != 0) {
fd_set writemask;
struct timeval tv;
tv.tv_sec = (time_t) (ms / 1000U);
ms %= 1000U;
tv.tv_usec = (time_t) (ms * 1000U);
FD_ZERO(&writemask);
FD_SET(fd,&writemask);
switch (select(fd+1, NULL, &writemask, NULL, &tv)) {
case -1 :
return -1; /* i/o error */
case 0:
return -2; /* timeout */
default:
if (!FD_ISSET(fd, &writemask)) {
return -1; /* Other error */
}
}
}
res = writesocket(fd, buf, len);
return (res < 0) ? -1 : res;
}
/*
* Fill buffer, return buffer length, 0 for EOF, < 0 (and sets errno)
* for error. */
int ei_read_fill_t(int fd, char* buf, int len, unsigned ms)
{
int i,got=0;
do {
i = ei_read_t(fd, buf+got, len-got, ms);
if (i <= 0)
return (i);
got += i;
} while (got < len);
return (len);
} /* read_fill */
int ei_read_fill(int fd, char* buf, int len)
{
return ei_read_fill_t(fd, buf, len, 0);
}
/* write entire buffer on fd or fail (setting errno)
*/
int ei_write_fill_t(int fd, const char *buf, int len, unsigned ms)
{
int i,done=0;
if (ms != 0U) {
SET_NONBLOCKING(fd);
}
do {
i = ei_write_t(fd, buf+done, len-done, ms);
if (i <= 0) {
if (ms != 0U) {
SET_BLOCKING(fd);
}
return (i);
}
done += i;
} while (done < len);
if (ms != 0U) {
SET_BLOCKING(fd);
}
return (len);
}
int ei_write_fill(int fd, const char *buf, int len)
{
return ei_write_fill_t(fd, buf, len, 0);
}
|