Fixed EOF detection for encrypted packets.
[gnupg.git] / jnlib / w32-afunix.c
blob63653941e6c3fb1ccf585aafb8964965c6181803
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
21 from libassuan. */
23 #ifdef _WIN32
24 #include <stdio.h>
25 #include <stdlib.h>
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <io.h>
31 #include <errno.h>
33 #include "w32-afunix.h"
37 /* The buffer for NONCE needs to be at least 16 bytes. Returns 0 on
38 success. */
39 static int
40 read_port_and_nonce (const char *fname, unsigned short *port, char *nonce)
42 FILE *fp;
43 char buffer[50], *p;
44 size_t nread;
45 int aval;
47 fp = fopen (fname, "rb");
48 if (!fp)
49 return -1;
50 nread = fread (buffer, 1, sizeof buffer - 1, fp);
51 fclose (fp);
52 if (!nread)
54 errno = ENOFILE;
55 return -1;
57 buffer[nread] = 0;
58 aval = atoi (buffer);
59 if (aval < 1 || aval > 65535)
61 errno = EINVAL;
62 return -1;
64 *port = (unsigned int)aval;
65 for (p=buffer; nread && *p != '\n'; p++, nread--)
67 if (*p != '\n' || nread != 17)
69 errno = EINVAL;
70 return -1;
72 p++; nread--;
73 memcpy (nonce, p, 16);
74 return 0;
79 int
80 _w32_close (int fd)
82 int rc = closesocket (fd);
83 if (rc && WSAGetLastError () == WSAENOTSOCK)
84 rc = close (fd);
85 return rc;
89 int
90 _w32_sock_new (int domain, int type, int proto)
92 if (domain == AF_UNIX || domain == AF_LOCAL)
93 domain = AF_INET;
94 return socket (domain, type, proto);
98 int
99 _w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
101 struct sockaddr_in myaddr;
102 struct sockaddr_un *unaddr;
103 unsigned short port;
104 char nonce[16];
105 int ret;
107 (void)addrlen;
109 unaddr = (struct sockaddr_un *)addr;
110 if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
111 return -1;
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);
123 if (!ret)
125 /* Send the nonce. */
126 ret = send (sockfd, nonce, 16, 0);
127 if (ret >= 0 && ret != 16)
129 errno = EIO;
130 ret = -1;
133 return ret;
137 #endif /*_WIN32*/