2007-10-01 14:48:39 +00:00
|
|
|
/* w32-afunix.c - AF_UNIX emulation for Windows (Client only).
|
2006-10-02 11:54:35 +00:00
|
|
|
* Copyright (C) 2004, 2006 g10 Code GmbH
|
2004-11-30 09:48:37 +00:00
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
* This file is part of GnuPG.
|
2004-11-30 09:48:37 +00:00
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify it
|
2011-09-30 12:52:11 +02:00
|
|
|
* under the terms of either
|
|
|
|
*
|
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
2004-11-30 09:48:37 +00:00
|
|
|
*
|
2015-04-24 16:42:28 +02:00
|
|
|
* GnuPG is distributed in the hope that it will be useful, but
|
2006-10-02 11:54:35 +00:00
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2011-09-30 12:52:11 +02:00
|
|
|
* General Public License for more details.
|
2004-11-30 09:48:37 +00:00
|
|
|
*
|
2011-09-30 12:52:11 +02:00
|
|
|
* You should have received a copies of the GNU General Public License
|
|
|
|
* and the GNU Lesser General Public License along with this program;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
2004-11-30 09:48:37 +00:00
|
|
|
*/
|
2006-10-02 11:54:35 +00:00
|
|
|
|
2010-03-15 13:08:51 +00:00
|
|
|
/* Use of this code is deprecated - you better use the socket wrappers
|
2007-10-01 14:48:39 +00:00
|
|
|
from libassuan. */
|
|
|
|
|
2004-11-30 09:48:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <stdio.h>
|
2007-10-01 14:48:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2004-11-30 09:48:37 +00:00
|
|
|
#include <windows.h>
|
2007-06-19 09:11:11 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2004-11-30 09:48:37 +00:00
|
|
|
#include <io.h>
|
2006-10-02 11:54:35 +00:00
|
|
|
#include <errno.h>
|
2004-11-30 09:48:37 +00:00
|
|
|
|
|
|
|
#include "w32-afunix.h"
|
|
|
|
|
2007-10-01 14:48:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* The buffer for NONCE needs to be at least 16 bytes. Returns 0 on
|
|
|
|
success. */
|
|
|
|
static int
|
|
|
|
read_port_and_nonce (const char *fname, unsigned short *port, char *nonce)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buffer[50], *p;
|
|
|
|
size_t nread;
|
|
|
|
int aval;
|
|
|
|
|
|
|
|
fp = fopen (fname, "rb");
|
|
|
|
if (!fp)
|
|
|
|
return -1;
|
|
|
|
nread = fread (buffer, 1, sizeof buffer - 1, fp);
|
|
|
|
fclose (fp);
|
|
|
|
if (!nread)
|
|
|
|
{
|
2010-03-15 13:08:51 +00:00
|
|
|
gpg_err_set_errno (EIO);
|
2007-10-01 14:48:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buffer[nread] = 0;
|
|
|
|
aval = atoi (buffer);
|
|
|
|
if (aval < 1 || aval > 65535)
|
|
|
|
{
|
2010-03-15 13:08:51 +00:00
|
|
|
gpg_err_set_errno (EINVAL);
|
2007-10-01 14:48:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*port = (unsigned int)aval;
|
|
|
|
for (p=buffer; nread && *p != '\n'; p++, nread--)
|
|
|
|
;
|
|
|
|
if (*p != '\n' || nread != 17)
|
|
|
|
{
|
2010-03-15 13:08:51 +00:00
|
|
|
gpg_err_set_errno (EINVAL);
|
2007-10-01 14:48:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p++; nread--;
|
|
|
|
memcpy (nonce, p, 16);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-19 09:11:11 +00:00
|
|
|
|
|
|
|
|
2004-11-30 09:48:37 +00:00
|
|
|
int
|
|
|
|
_w32_close (int fd)
|
|
|
|
{
|
|
|
|
int rc = closesocket (fd);
|
|
|
|
if (rc && WSAGetLastError () == WSAENOTSOCK)
|
|
|
|
rc = close (fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_w32_sock_new (int domain, int type, int proto)
|
|
|
|
{
|
|
|
|
if (domain == AF_UNIX || domain == AF_LOCAL)
|
|
|
|
domain = AF_INET;
|
|
|
|
return socket (domain, type, proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2007-10-01 14:48:39 +00:00
|
|
|
_w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
|
2004-11-30 09:48:37 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in myaddr;
|
2007-10-01 14:48:39 +00:00
|
|
|
struct sockaddr_un *unaddr;
|
|
|
|
unsigned short port;
|
|
|
|
char nonce[16];
|
|
|
|
int ret;
|
2008-10-20 13:53:23 +00:00
|
|
|
|
|
|
|
(void)addrlen;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-11-30 09:48:37 +00:00
|
|
|
unaddr = (struct sockaddr_un *)addr;
|
2007-10-01 14:48:39 +00:00
|
|
|
if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
|
2004-12-21 19:05:15 +00:00
|
|
|
return -1;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-11-30 09:48:37 +00:00
|
|
|
myaddr.sin_family = AF_INET;
|
2011-02-04 12:57:53 +01:00
|
|
|
myaddr.sin_port = htons (port);
|
2004-11-30 09:48:37 +00:00
|
|
|
myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-10-01 14:48:39 +00:00
|
|
|
/* Set return values. */
|
2004-11-30 09:48:37 +00:00
|
|
|
unaddr->sun_family = myaddr.sin_family;
|
|
|
|
unaddr->sun_port = myaddr.sin_port;
|
|
|
|
unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-10-01 14:48:39 +00:00
|
|
|
ret = connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
|
|
|
|
if (!ret)
|
2004-11-30 09:48:37 +00:00
|
|
|
{
|
2007-10-01 14:48:39 +00:00
|
|
|
/* Send the nonce. */
|
|
|
|
ret = send (sockfd, nonce, 16, 0);
|
|
|
|
if (ret >= 0 && ret != 16)
|
2007-06-19 09:11:11 +00:00
|
|
|
{
|
2010-03-15 13:08:51 +00:00
|
|
|
gpg_err_set_errno (EIO);
|
2007-10-01 14:48:39 +00:00
|
|
|
ret = -1;
|
2007-06-19 09:11:11 +00:00
|
|
|
}
|
2004-11-30 09:48:37 +00:00
|
|
|
}
|
2007-10-01 14:48:39 +00:00
|
|
|
return ret;
|
2004-11-30 09:48:37 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 14:48:39 +00:00
|
|
|
|
2004-11-30 09:48:37 +00:00
|
|
|
#endif /*_WIN32*/
|