~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/ikvm/internal/Winsock.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2010-2011 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
 
 
23
*/
 
24
 
 
25
package ikvm.internal;
 
26
 
 
27
import cli.System.Net.EndPoint;
 
28
import cli.System.Net.IPAddress;
 
29
import cli.System.Net.IPEndPoint;
 
30
import cli.System.Net.Sockets.AddressFamily;
 
31
import cli.System.Net.Sockets.IOControlCode;
 
32
import cli.System.Net.Sockets.IPv6MulticastOption;
 
33
import cli.System.Net.Sockets.LingerOption;
 
34
import cli.System.Net.Sockets.MulticastOption;
 
35
import cli.System.Net.Sockets.ProtocolType;
 
36
import cli.System.Net.Sockets.SocketFlags;
 
37
import cli.System.Net.Sockets.SocketOptionName;
 
38
import cli.System.Net.Sockets.SocketOptionLevel;
 
39
import cli.System.Net.Sockets.SocketShutdown;
 
40
import cli.System.Net.Sockets.SocketType;
 
41
import ikvm.lang.CIL;
 
42
 
 
43
@ikvm.lang.Internal
 
44
public final class Winsock
 
45
{
 
46
    private Winsock() { }
 
47
 
 
48
    // remember the last error code
 
49
    @cli.System.ThreadStaticAttribute.Annotation
 
50
    private static int lastError;
 
51
 
 
52
    // Error Codes
 
53
    public static final int WSA_NOT_ENOUGH_MEMORY = 8;
 
54
    public static final int WSA_OPERATION_ABORTED = 995;
 
55
    public static final int WSAEINTR = 10004;
 
56
    public static final int WSAEACCES = 10013;
 
57
    public static final int WSAEFAULT = 10014;
 
58
    public static final int WSAEINVAL = 10022;
 
59
    public static final int WSAEMFILE = 10024;
 
60
    public static final int WSAEWOULDBLOCK = 10035;
 
61
    public static final int WSAEINPROGRESS = 10036;
 
62
    public static final int WSAEALREADY = 10037;
 
63
    public static final int WSAENOTSOCK = 10038;
 
64
    public static final int WSAEDESTADDRREQ = 10039;
 
65
    public static final int WSAEMSGSIZE = 10040;
 
66
    public static final int WSAEPROTOTYPE = 10041;
 
67
    public static final int WSAENOPROTOOPT = 10042;
 
68
    public static final int WSAEPROTONOSUPPORT = 10043;
 
69
    public static final int WSAESOCKTNOSUPPORT = 10044;
 
70
    public static final int WSAEOPNOTSUPP = 10045;
 
71
    public static final int WSAEPFNOSUPPORT = 10046;
 
72
    public static final int WSAEAFNOSUPPORT = 10047;
 
73
    public static final int WSAEADDRINUSE = 10048;
 
74
    public static final int WSAEADDRNOTAVAIL = 10049;
 
75
    public static final int WSAENETDOWN = 10050;
 
76
    public static final int WSAENETUNREACH = 10051;
 
77
    public static final int WSAENETRESET = 10052;
 
78
    public static final int WSAECONNABORTED = 10053;
 
79
    public static final int WSAECONNRESET = 10054;
 
80
    public static final int WSAENOBUFS = 10055;
 
81
    public static final int WSAEISCONN = 10056;
 
82
    public static final int WSAENOTCONN = 10057;
 
83
    public static final int WSAESHUTDOWN = 10058;
 
84
    public static final int WSAETIMEDOUT = 10060;
 
85
    public static final int WSAECONNREFUSED = 10061;
 
86
    public static final int WSAEHOSTDOWN = 10064;
 
87
    public static final int WSAEHOSTUNREACH = 10065;
 
88
    public static final int WSAEPROCLIM = 10067;
 
89
    public static final int WSASYSNOTREADY = 10091;
 
90
    public static final int WSAVERNOTSUPPORTED = 10092;
 
91
    public static final int WSANOTINITIALISED = 10093;
 
92
    public static final int WSAEDISCON = 10101;
 
93
    public static final int WSATYPE_NOT_FOUND = 10109;
 
94
    public static final int WSAHOST_NOT_FOUND = 11001;
 
95
    public static final int WSATRY_AGAIN = 11002;
 
96
    public static final int WSANO_RECOVERY = 11003;
 
97
    public static final int WSANO_DATA = 11004;
 
98
 
 
99
    // Other constants
 
100
    public static final int SOCKET_ERROR = -1;
 
101
    public static final cli.System.Net.Sockets.Socket INVALID_SOCKET = null;
 
102
 
 
103
    public static final int AF_INET = AddressFamily.InterNetwork;
 
104
    public static final int AF_INET6 = AddressFamily.InterNetworkV6;
 
105
 
 
106
    public static final int SOCK_STREAM = SocketType.Stream;
 
107
    public static final int SOCK_DGRAM = SocketType.Dgram;
 
108
 
 
109
    public static final int SD_RECEIVE = SocketShutdown.Receive;
 
110
    public static final int SD_SEND = SocketShutdown.Send;
 
111
    public static final int SD_BOTH = SocketShutdown.Both;
 
112
 
 
113
    public static final int SOL_SOCKET = SocketOptionLevel.Socket;
 
114
    public static final int IPPROTO_TCP = SocketOptionLevel.Tcp;
 
115
    public static final int IPPROTO_IP = SocketOptionLevel.IP;
 
116
    public static final int IPPROTO_IPV6 = SocketOptionLevel.IPv6;
 
117
 
 
118
    public static final int TCP_NODELAY = SocketOptionName.NoDelay;
 
119
    public static final int SO_OOBINLINE = SocketOptionName.OutOfBandInline;
 
120
    public static final int SO_LINGER = SocketOptionName.Linger;
 
121
    public static final int SO_SNDBUF = SocketOptionName.SendBuffer;
 
122
    public static final int SO_RCVBUF = SocketOptionName.ReceiveBuffer;
 
123
    public static final int SO_KEEPALIVE = SocketOptionName.KeepAlive;
 
124
    public static final int SO_REUSEADDR = SocketOptionName.ReuseAddress;
 
125
    public static final int SO_BROADCAST = SocketOptionName.Broadcast;
 
126
    public static final int SO_RCVTIMEO = SocketOptionName.ReceiveTimeout;
 
127
    public static final int SO_ERROR = SocketOptionName.Error;
 
128
    public static final int IP_MULTICAST_IF = SocketOptionName.MulticastInterface;
 
129
    public static final int IP_MULTICAST_LOOP = SocketOptionName.MulticastLoopback;
 
130
    public static final int IP_TOS = SocketOptionName.TypeOfService;
 
131
    public static final int IP_MULTICAST_TTL = SocketOptionName.MulticastTimeToLive;
 
132
    public static final int IP_ADD_MEMBERSHIP = SocketOptionName.AddMembership;
 
133
    public static final int IP_DROP_MEMBERSHIP = SocketOptionName.DropMembership;
 
134
    public static final int IPV6_MULTICAST_IF = SocketOptionName.MulticastInterface;
 
135
    public static final int IPV6_MULTICAST_LOOP = SocketOptionName.MulticastLoopback;
 
136
    public static final int IPV6_MULTICAST_HOPS = SocketOptionName.MulticastTimeToLive;
 
137
    public static final int IPV6_ADD_MEMBERSHIP = SocketOptionName.AddMembership;
 
138
    public static final int IPV6_DROP_MEMBERSHIP = SocketOptionName.DropMembership;
 
139
    public static final int IPV6_V6ONLY = 27;
 
140
 
 
141
    public static final int SIO_UDP_CONNRESET = 0x9800000C;
 
142
 
 
143
    public static final int MSG_PEEK = SocketFlags.Peek;
 
144
    public static final int MSG_OOB = SocketFlags.OutOfBand;
 
145
 
 
146
    public static final int FIONREAD = (int)IOControlCode.DataToRead;
 
147
    public static final int FIONBIO = (int)IOControlCode.NonBlockingIO;
 
148
 
 
149
    public static int WSAGetLastError()
 
150
    {
 
151
        return lastError;
 
152
    }
 
153
 
 
154
    public static void WSASetLastError(int err)
 
155
    {
 
156
        lastError = err;
 
157
    }
 
158
 
 
159
    public static int WSASendDisconnect(cli.System.Net.Sockets.Socket socket)
 
160
    {
 
161
        if (socket == null)
 
162
        {
 
163
            lastError = WSAENOTSOCK;
 
164
            return SOCKET_ERROR;
 
165
        }
 
166
        try
 
167
        {
 
168
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
169
            if (false) throw new cli.System.ObjectDisposedException("");
 
170
            socket.Shutdown(SocketShutdown.wrap(SocketShutdown.Send));
 
171
            return 0;
 
172
        }
 
173
        catch (cli.System.Net.Sockets.SocketException x)
 
174
        {
 
175
            lastError = x.get_ErrorCode();
 
176
            return SOCKET_ERROR;
 
177
        }
 
178
        catch (cli.System.ObjectDisposedException _)
 
179
        {
 
180
            lastError = WSAENOTSOCK;
 
181
            return SOCKET_ERROR;
 
182
        }
 
183
    }
 
184
 
 
185
    public static int WSAIoctl(cli.System.Net.Sockets.Socket socket, int ioControlCode, boolean optionInValue)
 
186
    {
 
187
        byte[] in = new byte[4];
 
188
        in[0] = optionInValue ? (byte)1 : (byte)0;
 
189
        byte[] out = new byte[4];
 
190
        return WSAIoctl(socket, ioControlCode, in, out);
 
191
    }
 
192
 
 
193
    public static int WSAIoctl(cli.System.Net.Sockets.Socket socket, int ioControlCode, byte[] optionInValue, byte[] optionOutValue)
 
194
    {
 
195
        if (socket == null)
 
196
        {
 
197
            lastError = WSAENOTSOCK;
 
198
            return SOCKET_ERROR;
 
199
        }
 
200
        try
 
201
        {
 
202
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
203
            if (false) throw new cli.System.ObjectDisposedException("");
 
204
            if (false) throw new cli.System.InvalidOperationException();
 
205
            if (ioControlCode == FIONBIO)
 
206
            {
 
207
                // it's illegal to meddle with the blocking mode via IOControl
 
208
                socket.set_Blocking(optionInValue[0] == 0);
 
209
            }
 
210
            else if (ioControlCode == FIONREAD)
 
211
            {
 
212
                int avail = socket.get_Available();
 
213
                optionOutValue[0] = (byte)(avail >> 0);
 
214
                optionOutValue[1] = (byte)(avail >> 8);
 
215
                optionOutValue[2] = (byte)(avail >> 16);
 
216
                optionOutValue[3] = (byte)(avail >> 24);
 
217
            }
 
218
            else
 
219
            {
 
220
                socket.IOControl(ioControlCode, optionInValue, optionOutValue);
 
221
            }
 
222
            return 0;
 
223
        }
 
224
        catch (cli.System.Net.Sockets.SocketException x)
 
225
        {
 
226
            lastError = x.get_ErrorCode();
 
227
            return SOCKET_ERROR;
 
228
        }
 
229
        catch (cli.System.ObjectDisposedException _)
 
230
        {
 
231
            lastError = WSAENOTSOCK;
 
232
            return SOCKET_ERROR;
 
233
        }
 
234
        catch (cli.System.InvalidOperationException _)
 
235
        {
 
236
            lastError = WSAEINVAL;
 
237
            return SOCKET_ERROR;
 
238
        }
 
239
    }
 
240
 
 
241
    public static int ioctlsocket(cli.System.Net.Sockets.Socket s, int cmd, int[] argp)
 
242
    {
 
243
        byte[] in = cli.System.BitConverter.GetBytes(argp[0]);
 
244
        byte[] out = new byte[4];
 
245
        int ret = WSAIoctl(s, cmd, in, out);
 
246
        argp[0] = cli.System.BitConverter.ToInt32(out, 0);
 
247
        return ret;
 
248
    }
 
249
 
 
250
    public static int ioctlsocket(cli.System.Net.Sockets.Socket s, int cmd, int arg)
 
251
    {
 
252
        byte[] in = cli.System.BitConverter.GetBytes(arg);
 
253
        return WSAIoctl(s, cmd, in, in);
 
254
    }
 
255
 
 
256
    public static cli.System.Net.Sockets.Socket socket(int af, int type, int protocol)
 
257
    {
 
258
        try
 
259
        {
 
260
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
261
            if (false) throw new cli.System.ObjectDisposedException("");
 
262
            return new cli.System.Net.Sockets.Socket(AddressFamily.wrap(af), SocketType.wrap(type), ProtocolType.wrap(protocol));
 
263
        }
 
264
        catch (cli.System.Net.Sockets.SocketException x)
 
265
        {
 
266
            lastError = x.get_ErrorCode();
 
267
            return INVALID_SOCKET;
 
268
        }
 
269
        catch (cli.System.ObjectDisposedException _)
 
270
        {
 
271
            lastError = WSAENOTSOCK;
 
272
            return INVALID_SOCKET;
 
273
        }
 
274
    }
 
275
 
 
276
    public static int closesocket(cli.System.Net.Sockets.Socket socket)
 
277
    {
 
278
        if (socket == null)
 
279
        {
 
280
            lastError = WSAENOTSOCK;
 
281
            return SOCKET_ERROR;
 
282
        }
 
283
        socket.Close();
 
284
        return 0;
 
285
    }
 
286
 
 
287
    public static final class linger
 
288
    {
 
289
        public int l_onoff;
 
290
        public int l_linger;
 
291
 
 
292
        LingerOption ToLingerOption()
 
293
        {
 
294
            return new LingerOption(l_onoff != 0, l_linger);
 
295
        }
 
296
    }
 
297
 
 
298
    public static final class ip_mreq
 
299
    {
 
300
        public final in_addr imr_multiaddr = new in_addr();
 
301
        public final in_addr imr_interface = new in_addr();
 
302
 
 
303
        MulticastOption ToMulticastOption()
 
304
        {
 
305
            return new MulticastOption(imr_multiaddr.ToIPAddress(), imr_interface.ToIPAddress());
 
306
        }
 
307
    }
 
308
 
 
309
    public static final class in_addr
 
310
    {
 
311
        public int s_addr;
 
312
 
 
313
        IPAddress ToIPAddress()
 
314
        {
 
315
            return new IPAddress(s_addr & 0xFFFFFFFFL);
 
316
        }
 
317
    }
 
318
 
 
319
    public static final class ipv6_mreq
 
320
    {
 
321
        public in6_addr ipv6mr_multiaddr;
 
322
        public int ipv6mr_interface;
 
323
 
 
324
        IPv6MulticastOption ToIPv6MulticastOption()
 
325
        {
 
326
            return new IPv6MulticastOption(ipv6mr_multiaddr.addr, ipv6mr_interface & 0xFFFFFFFFL);
 
327
        }
 
328
    }
 
329
 
 
330
    public static final class in6_addr
 
331
    {
 
332
        IPAddress addr;
 
333
 
 
334
        public byte[] s6_bytes()
 
335
        {
 
336
            return addr == null ? new byte[16] : addr.GetAddressBytes();
 
337
        }
 
338
 
 
339
        public static in6_addr FromSockAddr(IIPEndPointWrapper ep)
 
340
        {
 
341
            in6_addr addr = new in6_addr();
 
342
            addr.addr = ep.get().get_Address();
 
343
            return addr;
 
344
        }
 
345
    }
 
346
 
 
347
    public static int getsockopt(cli.System.Net.Sockets.Socket socket, int level, int optname, Object optval)
 
348
    {
 
349
        if (socket == null)
 
350
        {
 
351
            lastError = WSAENOTSOCK;
 
352
            return SOCKET_ERROR;
 
353
        }
 
354
        try
 
355
        {
 
356
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
357
            if (false) throw new cli.System.ObjectDisposedException("");
 
358
            Object val = socket.GetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname));
 
359
            if (val instanceof cli.System.Int32)
 
360
            {
 
361
                if (optval instanceof in_addr)
 
362
                {
 
363
                    ((in_addr)optval).s_addr = CIL.unbox_int(val);
 
364
                }
 
365
                else
 
366
                {
 
367
                    ((cli.System.Array)optval).SetValue(val, 0);
 
368
                }
 
369
            }
 
370
            else if (val instanceof LingerOption)
 
371
            {
 
372
                LingerOption lo = (LingerOption)val;
 
373
                linger ling = (linger)optval;
 
374
                ling.l_onoff = lo.get_Enabled() ? 1 : 0;
 
375
                // FXBUG the linger time is treated as a signed short instead of an unsiged short
 
376
                ling.l_linger = lo.get_LingerTime() & 0xFFFF;
 
377
            }
 
378
            else
 
379
            {
 
380
                lastError = WSAEINVAL;
 
381
                return SOCKET_ERROR;
 
382
            }
 
383
            return 0;
 
384
        }
 
385
        catch (cli.System.Net.Sockets.SocketException x)
 
386
        {
 
387
            lastError = x.get_ErrorCode();
 
388
            return SOCKET_ERROR;
 
389
        }
 
390
        catch (cli.System.ObjectDisposedException _)
 
391
        {
 
392
            lastError = WSAENOTSOCK;
 
393
            return SOCKET_ERROR;
 
394
        }
 
395
    }
 
396
 
 
397
    public static int connect(cli.System.Net.Sockets.Socket socket, IIPEndPointWrapper epw)
 
398
    {
 
399
        if (socket == null)
 
400
        {
 
401
            lastError = WSAENOTSOCK;
 
402
            return SOCKET_ERROR;
 
403
        }
 
404
        try
 
405
        {
 
406
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
407
            if (false) throw new cli.System.ObjectDisposedException("");
 
408
            IPEndPoint ep = epw.get();
 
409
            if (ep == null)
 
410
            {
 
411
                // it is a disconnect request, we must connect to the Any address
 
412
                if (socket.get_AddressFamily().Value == AddressFamily.InterNetwork)
 
413
                {
 
414
                    ep = new IPEndPoint(cli.System.Net.IPAddress.Any, 0);
 
415
                }
 
416
                else
 
417
                {
 
418
                    ep = new IPEndPoint(cli.System.Net.IPAddress.IPv6Any, 0);
 
419
                }
 
420
            }
 
421
            else
 
422
            {
 
423
                ep = v4mapped(socket, ep);
 
424
            }
 
425
            if (socket.get_SocketType().Value == SocketType.Dgram)
 
426
            {
 
427
                // NOTE we use async connect to work around the issue that the .NET Socket class disallows sync Connect after the socket has received WSAECONNRESET
 
428
                socket.EndConnect(socket.BeginConnect(ep, null, null));
 
429
            }
 
430
            else
 
431
            {
 
432
                socket.Connect(ep);
 
433
            }
 
434
            return 0;
 
435
        }
 
436
        catch (cli.System.Net.Sockets.SocketException x)
 
437
        {
 
438
            lastError = x.get_ErrorCode();
 
439
            return SOCKET_ERROR;
 
440
        }
 
441
        catch (cli.System.ObjectDisposedException _)
 
442
        {
 
443
            lastError = WSAENOTSOCK;
 
444
            return SOCKET_ERROR;
 
445
        }
 
446
    }
 
447
 
 
448
    private static IPEndPoint v4mapped(cli.System.Net.Sockets.Socket socket, IPEndPoint ep)
 
449
    {
 
450
        // when binding an IPv6 socket to an IPv4 address, we need to use a mapped v4 address
 
451
        if (socket.get_AddressFamily().Value == AF_INET6 && ep.get_AddressFamily().Value == AF_INET)
 
452
        {
 
453
            byte[] v4 = ep.get_Address().GetAddressBytes();
 
454
            if (v4[0] == 0 && v4[1] == 0 && v4[2] == 0 && v4[3] == 0)
 
455
            {
 
456
                return new IPEndPoint(IPAddress.IPv6Any, ep.get_Port());
 
457
            }
 
458
            else
 
459
            {
 
460
                byte[] v6 = new byte[16];
 
461
                v6[10] = -1;
 
462
                v6[11] = -1;
 
463
                v6[12] = v4[0];
 
464
                v6[13] = v4[1];
 
465
                v6[14] = v4[2];
 
466
                v6[15] = v4[3];
 
467
                return new IPEndPoint(new IPAddress(v6), ep.get_Port());
 
468
            }
 
469
        }
 
470
        return ep;
 
471
    }
 
472
 
 
473
    public static int bind(cli.System.Net.Sockets.Socket socket, IIPEndPointWrapper ep)
 
474
    {
 
475
        if (socket == null)
 
476
        {
 
477
            lastError = WSAENOTSOCK;
 
478
            return SOCKET_ERROR;
 
479
        }
 
480
        try
 
481
        {
 
482
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
483
            if (false) throw new cli.System.ObjectDisposedException("");
 
484
            socket.Bind(v4mapped(socket, ep.get()));
 
485
            return 0;
 
486
        }
 
487
        catch (cli.System.Net.Sockets.SocketException x)
 
488
        {
 
489
            lastError = x.get_ErrorCode();
 
490
            return SOCKET_ERROR;
 
491
        }
 
492
        catch (cli.System.ObjectDisposedException _)
 
493
        {
 
494
            lastError = WSAENOTSOCK;
 
495
            return SOCKET_ERROR;
 
496
        }
 
497
    }
 
498
 
 
499
    public static int listen(cli.System.Net.Sockets.Socket socket, int count)
 
500
    {
 
501
        if (socket == null)
 
502
        {
 
503
            lastError = WSAENOTSOCK;
 
504
            return SOCKET_ERROR;
 
505
        }
 
506
        try
 
507
        {
 
508
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
509
            if (false) throw new cli.System.ObjectDisposedException("");
 
510
            socket.Listen(count);
 
511
            return 0;
 
512
        }
 
513
        catch (cli.System.Net.Sockets.SocketException x)
 
514
        {
 
515
            lastError = x.get_ErrorCode();
 
516
            return SOCKET_ERROR;
 
517
        }
 
518
        catch (cli.System.ObjectDisposedException _)
 
519
        {
 
520
            lastError = WSAENOTSOCK;
 
521
            return SOCKET_ERROR;
 
522
        }
 
523
    }
 
524
 
 
525
    public static int shutdown(cli.System.Net.Sockets.Socket socket, int how)
 
526
    {
 
527
        if (socket == null)
 
528
        {
 
529
            lastError = WSAENOTSOCK;
 
530
            return SOCKET_ERROR;
 
531
        }
 
532
        try
 
533
        {
 
534
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
535
            if (false) throw new cli.System.ObjectDisposedException("");
 
536
            socket.Shutdown(SocketShutdown.wrap(how));
 
537
            return 0;
 
538
        }
 
539
        catch (cli.System.Net.Sockets.SocketException x)
 
540
        {
 
541
            lastError = x.get_ErrorCode();
 
542
            return SOCKET_ERROR;
 
543
        }
 
544
        catch (cli.System.ObjectDisposedException _)
 
545
        {
 
546
            lastError = WSAENOTSOCK;
 
547
            return SOCKET_ERROR;
 
548
        }
 
549
    }
 
550
 
 
551
    public static final class fd_set
 
552
    {
 
553
        cli.System.Collections.ArrayList list = new cli.System.Collections.ArrayList();
 
554
    }
 
555
 
 
556
    public static final class timeval
 
557
    {
 
558
        public long tv_sec;
 
559
        public long tv_usec;
 
560
    }
 
561
 
 
562
    public static void FD_ZERO(fd_set set)
 
563
    {
 
564
        set.list.Clear();
 
565
    }
 
566
 
 
567
    public static void FD_SET(cli.System.Net.Sockets.Socket socket, fd_set set)
 
568
    {
 
569
        set.list.Add(socket);
 
570
    }
 
571
 
 
572
    public static boolean FD_ISSET(cli.System.Net.Sockets.Socket socket, fd_set set)
 
573
    {
 
574
        return set.list.Contains(socket);
 
575
    }
 
576
 
 
577
    private static cli.System.Collections.ArrayList copy(fd_set set)
 
578
    {
 
579
        return set == null ? null : (cli.System.Collections.ArrayList)set.list.Clone();
 
580
    }
 
581
 
 
582
    public static int select(fd_set readfds, fd_set writefds, fd_set exceptfds, timeval timeout)
 
583
    {
 
584
        long expiration;
 
585
        long current = cli.System.DateTime.get_UtcNow().get_Ticks();
 
586
        if (timeout == null)
 
587
        {
 
588
            // FXBUG documentation says that -1 will block forever, but in fact it returns immediately,
 
589
            // so we simulate timeout with a large expiration
 
590
            expiration = Long.MAX_VALUE;
 
591
        }
 
592
        else
 
593
        {
 
594
            long timeout100nanos = Math.min(Long.MAX_VALUE / 10, timeout.tv_usec) * 10 + Math.min(Long.MAX_VALUE / 10000000, timeout.tv_sec) * 10000000;
 
595
            expiration = current + Math.min(Long.MAX_VALUE - current, timeout100nanos);
 
596
        }
 
597
        try
 
598
        {
 
599
            if (false) throw new cli.System.ArgumentNullException();
 
600
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
601
            if (false) throw new cli.System.ObjectDisposedException("");
 
602
            for (; ; )
 
603
            {
 
604
                cli.System.Collections.ArrayList checkRead = copy(readfds);
 
605
                cli.System.Collections.ArrayList checkWrite = copy(writefds);
 
606
                cli.System.Collections.ArrayList checkError = copy(exceptfds);
 
607
                int microSeconds = (int)Math.min(Integer.MAX_VALUE, (expiration - current) / 10);
 
608
                cli.System.Net.Sockets.Socket.Select(checkRead, checkWrite, checkError, microSeconds);
 
609
                int count = 0;
 
610
                if (checkRead != null)
 
611
                {
 
612
                    count += checkRead.get_Count();
 
613
                }
 
614
                if (checkWrite != null)
 
615
                {
 
616
                    count += checkWrite.get_Count();
 
617
                }
 
618
                if (checkError != null)
 
619
                {
 
620
                    count += checkError.get_Count();
 
621
                }
 
622
                current = cli.System.DateTime.get_UtcNow().get_Ticks();
 
623
                if (count != 0 || current >= expiration)
 
624
                {
 
625
                    if (readfds != null)
 
626
                    {
 
627
                        readfds.list = checkRead;
 
628
                    }
 
629
                    if (writefds != null)
 
630
                    {
 
631
                        writefds.list = checkWrite;
 
632
                    }
 
633
                    if (exceptfds != null)
 
634
                    {
 
635
                        exceptfds.list = checkError;
 
636
                    }
 
637
                    return count;
 
638
                }
 
639
            }
 
640
        }
 
641
        catch (cli.System.ArgumentNullException _)
 
642
        {
 
643
            lastError = WSAEINVAL;
 
644
            return SOCKET_ERROR;
 
645
        }
 
646
        catch (cli.System.Net.Sockets.SocketException x)
 
647
        {
 
648
            lastError = x.get_ErrorCode();
 
649
            return SOCKET_ERROR;
 
650
        }
 
651
        catch (cli.System.ObjectDisposedException _)
 
652
        {
 
653
            lastError = WSAENOTSOCK;
 
654
            return SOCKET_ERROR;
 
655
        }
 
656
    }
 
657
 
 
658
    public static int send(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags)
 
659
    {
 
660
        return send(socket, buf, 0, len, flags);
 
661
    }
 
662
 
 
663
    public static int send(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags)
 
664
    {
 
665
        if (socket == null)
 
666
        {
 
667
            lastError = WSAENOTSOCK;
 
668
            return SOCKET_ERROR;
 
669
        }
 
670
        try
 
671
        {
 
672
            if (false) throw new cli.System.ArgumentException();
 
673
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
674
            if (false) throw new cli.System.ObjectDisposedException("");
 
675
            return socket.Send(buf, off, len, SocketFlags.wrap(flags));
 
676
        }
 
677
        catch (cli.System.ArgumentException _)
 
678
        {
 
679
            lastError = WSAEINVAL;
 
680
            return SOCKET_ERROR;
 
681
        }
 
682
        catch (cli.System.Net.Sockets.SocketException x)
 
683
        {
 
684
            lastError = x.get_ErrorCode();
 
685
            return SOCKET_ERROR;
 
686
        }
 
687
        catch (cli.System.ObjectDisposedException _)
 
688
        {
 
689
            lastError = WSAENOTSOCK;
 
690
            return SOCKET_ERROR;
 
691
        }
 
692
    }
 
693
 
 
694
    public static int recv(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags)
 
695
    {
 
696
        return recv(socket, buf, 0, len, flags);
 
697
    }
 
698
 
 
699
    public static int recv(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags)
 
700
    {
 
701
        if (socket == null)
 
702
        {
 
703
            lastError = WSAENOTSOCK;
 
704
            return SOCKET_ERROR;
 
705
        }
 
706
        try
 
707
        {
 
708
            if (false) throw new cli.System.ArgumentException();
 
709
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
710
            if (false) throw new cli.System.ObjectDisposedException("");
 
711
            return socket.Receive(buf, off, len, SocketFlags.wrap(flags));
 
712
        }
 
713
        catch (cli.System.ArgumentException _)
 
714
        {
 
715
            lastError = WSAEINVAL;
 
716
            return SOCKET_ERROR;
 
717
        }
 
718
        catch (cli.System.Net.Sockets.SocketException x)
 
719
        {
 
720
            lastError = x.get_ErrorCode();
 
721
            return SOCKET_ERROR;
 
722
        }
 
723
        catch (cli.System.ObjectDisposedException _)
 
724
        {
 
725
            lastError = WSAENOTSOCK;
 
726
            return SOCKET_ERROR;
 
727
        }
 
728
    }
 
729
 
 
730
    public static int sendto(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags, IIPEndPointWrapper to)
 
731
    {
 
732
        if (socket == null)
 
733
        {
 
734
            lastError = WSAENOTSOCK;
 
735
            return SOCKET_ERROR;
 
736
        }
 
737
        try
 
738
        {
 
739
            if (false) throw new cli.System.ArgumentException();
 
740
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
741
            if (false) throw new cli.System.ObjectDisposedException("");
 
742
            if (to == null)
 
743
            {
 
744
                return socket.Send(buf, off, len, SocketFlags.wrap(flags));
 
745
            }
 
746
            else
 
747
            {
 
748
                return socket.SendTo(buf, off, len, SocketFlags.wrap(flags), v4mapped(socket, to.get()));
 
749
            }
 
750
        }
 
751
        catch (cli.System.ArgumentException _)
 
752
        {
 
753
            lastError = WSAEINVAL;
 
754
            return SOCKET_ERROR;
 
755
        }
 
756
        catch (cli.System.Net.Sockets.SocketException x)
 
757
        {
 
758
            // on Linux we get a WSAECONNREFUSED when sending to an unreachable port/destination, so ignore that
 
759
            if (x.get_ErrorCode() == WSAECONNREFUSED)
 
760
            {
 
761
                return 0;
 
762
            }
 
763
            lastError = x.get_ErrorCode();
 
764
            return SOCKET_ERROR;
 
765
        }
 
766
        catch (cli.System.ObjectDisposedException _)
 
767
        {
 
768
            lastError = WSAENOTSOCK;
 
769
            return SOCKET_ERROR;
 
770
        }
 
771
    }
 
772
 
 
773
    public static int recvfrom(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags, IIPEndPointWrapper from)
 
774
    {
 
775
        return recvfrom(socket, buf, 0, len, flags, from);
 
776
    }
 
777
 
 
778
    public static int recvfrom(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags, IIPEndPointWrapper from)
 
779
    {
 
780
        if (socket == null)
 
781
        {
 
782
            lastError = WSAENOTSOCK;
 
783
            return SOCKET_ERROR;
 
784
        }
 
785
        try
 
786
        {
 
787
            if (false) throw new cli.System.ArgumentException();
 
788
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
789
            if (false) throw new cli.System.ObjectDisposedException("");
 
790
            EndPoint[] ep = new EndPoint[] { socket.get_AddressFamily().Value == AF_INET6 ? new IPEndPoint(IPAddress.IPv6Any, 0) : new IPEndPoint(0, 0) };
 
791
            try
 
792
            {
 
793
                return socket.ReceiveFrom(buf, off, len, SocketFlags.wrap(flags), ep);
 
794
            }
 
795
            finally
 
796
            {
 
797
                if (from != null)
 
798
                {
 
799
                    from.set((IPEndPoint)ep[0]);
 
800
                }
 
801
            }
 
802
        }
 
803
        catch (cli.System.ArgumentException _)
 
804
        {
 
805
            lastError = WSAEINVAL;
 
806
            return SOCKET_ERROR;
 
807
        }
 
808
        catch (cli.System.Net.Sockets.SocketException x)
 
809
        {
 
810
            lastError = x.get_ErrorCode();
 
811
            return SOCKET_ERROR;
 
812
        }
 
813
        catch (cli.System.ObjectDisposedException _)
 
814
        {
 
815
            lastError = WSAENOTSOCK;
 
816
            return SOCKET_ERROR;
 
817
        }
 
818
    }
 
819
 
 
820
    public static int setsockopt(cli.System.Net.Sockets.Socket s, int level, int optname, Object optval)
 
821
    {
 
822
        if (s == null)
 
823
        {
 
824
            lastError = WSAENOTSOCK;
 
825
            return SOCKET_ERROR;
 
826
        }
 
827
        try
 
828
        {
 
829
            if (false) throw new cli.System.ArgumentException();
 
830
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
831
            if (false) throw new cli.System.ObjectDisposedException("");
 
832
            if (optval instanceof Boolean)
 
833
            {
 
834
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((Boolean)optval).booleanValue());
 
835
            }
 
836
            else if (optval instanceof Integer)
 
837
            {
 
838
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((Integer)optval).intValue());
 
839
            }
 
840
            else if (optval instanceof linger)
 
841
            {
 
842
                s.set_LingerState(((linger)optval).ToLingerOption());
 
843
            }
 
844
            else if (optval instanceof ip_mreq)
 
845
            {
 
846
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((ip_mreq)optval).ToMulticastOption());
 
847
            }
 
848
            else if (optval instanceof ipv6_mreq)
 
849
            {
 
850
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((ipv6_mreq)optval).ToIPv6MulticastOption());
 
851
            }
 
852
            else if (optval instanceof in_addr)
 
853
            {
 
854
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((in_addr)optval).s_addr);
 
855
            }
 
856
            else
 
857
            {
 
858
                lastError = WSAEINVAL;
 
859
                return SOCKET_ERROR;
 
860
            }
 
861
            return 0;
 
862
        }
 
863
        catch (cli.System.ArgumentException _)
 
864
        {
 
865
            lastError = WSAEINVAL;
 
866
            return SOCKET_ERROR;
 
867
        }
 
868
        catch (cli.System.Net.Sockets.SocketException x)
 
869
        {
 
870
            lastError = x.get_ErrorCode();
 
871
            return SOCKET_ERROR;
 
872
        }
 
873
        catch (cli.System.ObjectDisposedException _)
 
874
        {
 
875
            lastError = WSAENOTSOCK;
 
876
            return SOCKET_ERROR;
 
877
        }
 
878
    }
 
879
 
 
880
    public static cli.System.Net.Sockets.Socket accept(cli.System.Net.Sockets.Socket s, IIPEndPointWrapper ep)
 
881
    {
 
882
        if (s == null)
 
883
        {
 
884
            lastError = WSAENOTSOCK;
 
885
            return INVALID_SOCKET;
 
886
        }
 
887
        try
 
888
        {
 
889
            if (false) throw new cli.System.InvalidOperationException();
 
890
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
891
            if (false) throw new cli.System.ObjectDisposedException("");
 
892
            cli.System.Net.Sockets.Socket remote = s.Accept();
 
893
            if (ep != null)
 
894
            {
 
895
                ep.set((IPEndPoint)remote.get_RemoteEndPoint());
 
896
            }
 
897
            return remote;
 
898
        }
 
899
        catch (cli.System.ObjectDisposedException _)
 
900
        {
 
901
            lastError = WSAENOTSOCK;
 
902
            return INVALID_SOCKET;
 
903
        }
 
904
        catch (cli.System.InvalidOperationException _)
 
905
        {
 
906
            lastError = WSAEINVAL;
 
907
            return INVALID_SOCKET;
 
908
        }
 
909
        catch (cli.System.Net.Sockets.SocketException x)
 
910
        {
 
911
            lastError = x.get_ErrorCode();
 
912
            return INVALID_SOCKET;
 
913
        }
 
914
    }
 
915
 
 
916
    public interface IIPEndPointWrapper
 
917
    {
 
918
        void set(IPEndPoint value);
 
919
        IPEndPoint get();
 
920
    }
 
921
 
 
922
    public static int getsockname(cli.System.Net.Sockets.Socket s, IIPEndPointWrapper name)
 
923
    {
 
924
        if (s == null)
 
925
        {
 
926
            lastError = WSAENOTSOCK;
 
927
            return SOCKET_ERROR;
 
928
        }
 
929
        try
 
930
        {
 
931
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
932
            if (false) throw new cli.System.ObjectDisposedException("");
 
933
            IPEndPoint ep = (IPEndPoint)s.get_LocalEndPoint();
 
934
            if (ep == null)
 
935
            {
 
936
                lastError = WSAEINVAL;
 
937
                return SOCKET_ERROR;
 
938
            }
 
939
            name.set(ep);
 
940
            return 0;
 
941
        }
 
942
        catch (ClassCastException _)
 
943
        {
 
944
            lastError = WSAEOPNOTSUPP;
 
945
            return SOCKET_ERROR;
 
946
        }
 
947
        catch (cli.System.ObjectDisposedException _)
 
948
        {
 
949
            lastError = WSAENOTSOCK;
 
950
            return SOCKET_ERROR;
 
951
        }
 
952
        catch (cli.System.Net.Sockets.SocketException x)
 
953
        {
 
954
            lastError = x.get_ErrorCode();
 
955
            return SOCKET_ERROR;
 
956
        }
 
957
    }
 
958
 
 
959
    public static int ntohl(int address)
 
960
    {
 
961
        return Integer.reverseBytes(address);
 
962
    }
 
963
 
 
964
    public static int htonl(int address)
 
965
    {
 
966
        return Integer.reverseBytes(address);
 
967
    }
 
968
 
 
969
    public static int ntohs(int port)
 
970
    {
 
971
        return Short.reverseBytes((short)port) & 0xFFFF;
 
972
    }
 
973
 
 
974
    public static int htons(int port)
 
975
    {
 
976
        return Short.reverseBytes((short)port) & 0xFFFF;
 
977
    }
 
978
}