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
|
/***********************************************************************/
/* */
/* Objective Caml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
/* $Id$ */
#include <mlvalues.h>
#include <alloc.h>
#include <fail.h>
#include "unixsupport.h"
#ifdef HAS_SOCKETS
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "socketaddr.h"
#ifndef SO_DEBUG
#define SO_DEBUG (-1)
#endif
#ifndef SO_BROADCAST
#define SO_BROADCAST (-1)
#endif
#ifndef SO_REUSEADDR
#define SO_REUSEADDR (-1)
#endif
#ifndef SO_KEEPALIVE
#define SO_KEEPALIVE (-1)
#endif
#ifndef SO_DONTROUTE
#define SO_DONTROUTE (-1)
#endif
#ifndef SO_OOBINLINE
#define SO_OOBINLINE (-1)
#endif
#ifndef SO_ACCEPTCONN
#define SO_ACCEPTCONN (-1)
#endif
#ifndef SO_SNDBUF
#define SO_SNDBUF (-1)
#endif
#ifndef SO_RCVBUF
#define SO_RCVBUF (-1)
#endif
#ifndef SO_ERROR
#define SO_ERROR (-1)
#endif
#ifndef SO_TYPE
#define SO_TYPE (-1)
#endif
#ifndef SO_RCVLOWAT
#define SO_RCVLOWAT (-1)
#endif
#ifndef SO_SNDLOWAT
#define SO_SNDLOWAT (-1)
#endif
#ifndef SO_LINGER
#define SO_LINGER (-1)
#endif
#ifndef SO_RCVTIMEO
#define SO_RCVTIMEO (-1)
#endif
#ifndef SO_SNDTIMEO
#define SO_SNDTIMEO (-1)
#endif
static int sockopt_bool[] = {
SO_DEBUG, SO_BROADCAST, SO_REUSEADDR, SO_KEEPALIVE,
SO_DONTROUTE, SO_OOBINLINE, SO_ACCEPTCONN };
static int sockopt_int[] = {
SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE, SO_RCVLOWAT, SO_SNDLOWAT };
static int sockopt_optint[] = { SO_LINGER };
static int sockopt_float[] = { SO_RCVTIMEO, SO_SNDTIMEO };
CAMLexport value getsockopt_int(int *sockopt, value socket,
int level, value option)
{
int optval;
socklen_param_type optsize;
optsize = sizeof(optval);
if (getsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &optval, &optsize) == -1)
uerror("getsockopt", Nothing);
return Val_int(optval);
}
CAMLexport value setsockopt_int(int *sockopt, value socket, int level,
value option, value status)
{
int optval = Int_val(status);
if (setsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &optval, sizeof(optval)) == -1)
uerror("setsockopt", Nothing);
return Val_unit;
}
CAMLprim value unix_getsockopt_bool(value socket, value option) {
value res = getsockopt_int(sockopt_bool, socket, SOL_SOCKET, option);
return Val_bool(Int_val(res));
}
CAMLprim value unix_setsockopt_bool(value socket, value option, value status)
{
return setsockopt_int(sockopt_bool, socket, SOL_SOCKET, option, status);
}
CAMLprim value unix_getsockopt_int(value socket, value option) {
return getsockopt_int(sockopt_int, socket, SOL_SOCKET, option);
}
CAMLprim value unix_setsockopt_int(value socket, value option, value status)
{
return setsockopt_int(sockopt_int, socket, SOL_SOCKET, option, status);
}
CAMLexport value getsockopt_optint(int *sockopt, value socket,
int level, value option)
{
struct linger optval;
socklen_param_type optsize;
value res = Val_int(0); /* None */
optsize = sizeof(optval);
if (getsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &optval, &optsize) == -1)
uerror("getsockopt_optint", Nothing);
if (optval.l_onoff != 0) {
res = alloc_small(1, 0);
Field(res, 0) = Val_int(optval.l_linger);
}
return res;
}
CAMLexport value setsockopt_optint(int *sockopt, value socket, int level,
value option, value status)
{
struct linger optval;
optval.l_onoff = Is_block (status);
if (optval.l_onoff)
optval.l_linger = Int_val (Field (status, 0));
if (setsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &optval, sizeof(optval)) == -1)
uerror("setsockopt_optint", Nothing);
return Val_unit;
}
CAMLprim value unix_getsockopt_optint(value socket, value option)
{
return getsockopt_optint(sockopt_optint, socket, SOL_SOCKET, option);
}
CAMLprim value unix_setsockopt_optint(value socket, value option, value status)
{
return setsockopt_optint(sockopt_optint, socket, SOL_SOCKET, option, status);
}
CAMLexport value getsockopt_float(int *sockopt, value socket,
int level, value option)
{
struct timeval tv;
socklen_param_type optsize;
optsize = sizeof(tv);
if (getsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &tv, &optsize) == -1)
uerror("getsockopt_float", Nothing);
return copy_double((double) tv.tv_sec + (double) tv.tv_usec / 1e6);
}
CAMLexport value setsockopt_float(int *sockopt, value socket, int level,
value option, value status)
{
struct timeval tv;
double tv_f;
tv_f = Double_val(status);
tv.tv_sec = (int)tv_f;
tv.tv_usec = (int) (1e6 * (tv_f - tv.tv_sec));
if (setsockopt(Int_val(socket), level, sockopt[Int_val(option)],
(void *) &tv, sizeof(tv)) == -1)
uerror("setsockopt_float", Nothing);
return Val_unit;
}
CAMLprim value unix_getsockopt_float(value socket, value option)
{
return getsockopt_float(sockopt_float, socket, SOL_SOCKET, option);
}
CAMLprim value unix_setsockopt_float(value socket, value option, value status)
{
return setsockopt_float(sockopt_float, socket, SOL_SOCKET, option, status);
}
#else
CAMLprim value unix_getsockopt_bool(value socket, value option)
{ invalid_argument("getsockopt not implemented"); }
CAMLprim value unix_setsockopt_bool(value socket, value option, value status)
{ invalid_argument("setsockopt not implemented"); }
CAMLprim value unix_getsockopt_int(value socket, value option)
{ invalid_argument("getsockopt_int not implemented"); }
CAMLprim value unix_setsockopt_int(value socket, value option, value status)
{ invalid_argument("setsockopt_int not implemented"); }
CAMLprim value unix_getsockopt_optint(value socket, value option)
{ invalid_argument("getsockopt_optint not implemented"); }
CAMLprim value unix_setsockopt_optint(value socket, value option, value status)
{ invalid_argument("setsockopt_optint not implemented"); }
CAMLprim value unix_getsockopt_float(value socket, value option)
{ invalid_argument("getsockopt_float not implemented"); }
CAMLprim value unix_setsockopt_float(value socket, value option, value status)
{ invalid_argument("setsockopt_float not implemented"); }
#endif
|