1 /* w32-afunix.c - AF_UNIX emulation for Windows (Client only).
2 * Copyright (C) 2004, 2006 g10 Code GmbH
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Use of this code is preprecated - you better use the sockt wrappers
26 #define WIN32_LEAN_AND_MEAN
33 #include "w32-afunix.h"
37 /* The buffer for NONCE needs to be at least 16 bytes. Returns 0 on
40 read_port_and_nonce (const char *fname
, unsigned short *port
, char *nonce
)
47 fp
= fopen (fname
, "rb");
50 nread
= fread (buffer
, 1, sizeof buffer
- 1, fp
);
59 if (aval
< 1 || aval
> 65535)
64 *port
= (unsigned int)aval
;
65 for (p
=buffer
; nread
&& *p
!= '\n'; p
++, nread
--)
67 if (*p
!= '\n' || nread
!= 17)
73 memcpy (nonce
, p
, 16);
82 int rc
= closesocket (fd
);
83 if (rc
&& WSAGetLastError () == WSAENOTSOCK
)
90 _w32_sock_new (int domain
, int type
, int proto
)
92 if (domain
== AF_UNIX
|| domain
== AF_LOCAL
)
94 return socket (domain
, type
, proto
);
99 _w32_sock_connect (int sockfd
, struct sockaddr
*addr
, int addrlen
)
101 struct sockaddr_in myaddr
;
102 struct sockaddr_un
*unaddr
;
109 unaddr
= (struct sockaddr_un
*)addr
;
110 if (read_port_and_nonce (unaddr
->sun_path
, &port
, nonce
))
113 myaddr
.sin_family
= AF_INET
;
114 myaddr
.sin_port
= htons (port
);
115 myaddr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
117 /* Set return values. */
118 unaddr
->sun_family
= myaddr
.sin_family
;
119 unaddr
->sun_port
= myaddr
.sin_port
;
120 unaddr
->sun_addr
.s_addr
= myaddr
.sin_addr
.s_addr
;
122 ret
= connect (sockfd
, (struct sockaddr
*)&myaddr
, sizeof myaddr
);
125 /* Send the nonce. */
126 ret
= send (sockfd
, nonce
, 16, 0);
127 if (ret
>= 0 && ret
!= 16)