2
* Copyright (C) 2004 g10 Code GmbH
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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
22
27
#include <windows.h>
25
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);
28
80
_w32_close (int fd)
47
_w32_sock_connect (int sockfd, struct sockaddr * addr, int addrlen)
99
_w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
49
101
struct sockaddr_in myaddr;
50
struct sockaddr_un * unaddr;
102
struct sockaddr_un *unaddr;
54
107
unaddr = (struct sockaddr_un *)addr;
55
fp = fopen (unaddr->sun_path, "rb");
58
fscanf (fp, "%d", &port);
61
/* XXX: set errno in this case */
62
if (port < 0 || port > 65535)
108
if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
65
111
myaddr.sin_family = AF_INET;
66
myaddr.sin_port = port;
112
myaddr.sin_port = htons (port);
67
113
myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
69
/* we need this later. */
115
/* Set return values. */
70
116
unaddr->sun_family = myaddr.sin_family;
71
117
unaddr->sun_port = myaddr.sin_port;
72
118
unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
74
return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
79
_w32_sock_bind (int sockfd, struct sockaddr * addr, int addrlen)
81
if (addr->sa_family == AF_LOCAL || addr->sa_family == AF_UNIX)
120
ret = connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
83
struct sockaddr_in myaddr;
84
struct sockaddr_un * unaddr;
86
int len = sizeof myaddr;
90
myaddr.sin_family = AF_INET;
91
myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
93
rc = bind (sockfd, (struct sockaddr *)&myaddr, len);
96
rc = getsockname (sockfd, (struct sockaddr *)&myaddr, &len);
99
unaddr = (struct sockaddr_un *)addr;
100
fp = fopen (unaddr->sun_path, "wb");
103
fprintf (fp, "%d", myaddr.sin_port);
106
/* we need this later. */
107
unaddr->sun_family = myaddr.sin_family;
108
unaddr->sun_port = myaddr.sin_port;
109
unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
123
/* Send the nonce. */
124
ret = send (sockfd, nonce, 16, 0);
125
if (ret >= 0 && ret != 16)
113
return bind (sockfd, addr, addrlen);
116
135
#endif /*_WIN32*/