1996-09-04 07:17:43 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
1996-09-05 06:32:25 -07:00
|
|
|
#include "unixsupport.h"
|
2012-09-25 21:18:08 -07:00
|
|
|
#include <mswsock.h> // for SO_OPENTYPE and SO_SYNCHRONOUS_NONALERT
|
1996-09-04 07:17:43 -07:00
|
|
|
|
|
|
|
int socket_domain_table[] = {
|
2012-07-13 05:15:57 -07:00
|
|
|
PF_UNIX, PF_INET,
|
|
|
|
#if defined(HAS_IPV6)
|
|
|
|
PF_INET6
|
|
|
|
#else
|
|
|
|
0
|
|
|
|
#endif
|
1996-09-04 07:17:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
int socket_type_table[] = {
|
|
|
|
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET
|
|
|
|
};
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_socket(domain, type, proto)
|
1996-09-04 07:17:43 -07:00
|
|
|
value domain, type, proto;
|
|
|
|
{
|
|
|
|
SOCKET s;
|
2013-07-01 01:29:51 -07:00
|
|
|
int oldvalue, oldvaluelen, newvalue, retcode;
|
2012-01-30 08:07:18 -08:00
|
|
|
|
2012-07-13 05:15:57 -07:00
|
|
|
#ifndef HAS_IPV6
|
2012-01-30 08:07:18 -08:00
|
|
|
/* IPv6 requires WinSock2, we must raise an error on PF_INET6 */
|
|
|
|
if (Int_val(domain) >= sizeof(socket_domain_table)/sizeof(int)) {
|
|
|
|
win32_maperr(WSAEPFNOSUPPORT);
|
|
|
|
uerror("socket", Nothing);
|
|
|
|
}
|
2012-07-13 05:15:57 -07:00
|
|
|
#endif
|
2012-01-30 08:07:18 -08:00
|
|
|
|
2013-07-01 01:29:51 -07:00
|
|
|
oldvaluelen = sizeof(oldvalue);
|
|
|
|
retcode = getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
|
|
|
|
(char *) &oldvalue, &oldvaluelen);
|
|
|
|
if (retcode == 0) {
|
|
|
|
/* Set sockets to synchronous mode */
|
|
|
|
newvalue = SO_SYNCHRONOUS_NONALERT;
|
|
|
|
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
|
|
|
|
(char *) &newvalue, sizeof(newvalue));
|
|
|
|
}
|
1996-09-04 07:17:43 -07:00
|
|
|
s = socket(socket_domain_table[Int_val(domain)],
|
|
|
|
socket_type_table[Int_val(type)],
|
|
|
|
Int_val(proto));
|
2013-07-01 01:29:51 -07:00
|
|
|
if (retcode == 0) {
|
|
|
|
/* Restore initial mode */
|
|
|
|
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
|
|
|
|
(char *) &oldvalue, oldvaluelen);
|
|
|
|
}
|
2001-12-03 02:33:39 -08:00
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
win32_maperr(WSAGetLastError());
|
|
|
|
uerror("socket", Nothing);
|
|
|
|
}
|
2002-04-30 08:00:48 -07:00
|
|
|
return win_alloc_socket(s);
|
1996-09-04 07:17:43 -07:00
|
|
|
}
|