summaryrefslogtreecommitdiffstats
path: root/jnlib/w32-afunix.c
blob: 57962149132d99ce8b329c104c07181035324ac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* w32-afunix.c - AF_UNIX emulation for Windows (Client only).
 * Copyright (C) 2004, 2006 g10 Code GmbH
 *
 * This file is part of JNLIB.
 *
 * JNLIB is free software; you can redistribute it and/or modify it
 * under the terms of 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.
 *
 * JNLIB is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/* Use of this code is preprecated - you better use the sockt wrappers
   from libassuan. */

#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <errno.h>

#include "w32-afunix.h"



/* 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)
    {
#warning remove this file
      jnlib_set_errno (EIO);
      return -1;
    }
  buffer[nread] = 0;
  aval = atoi (buffer);
  if (aval < 1 || aval > 65535)
    {
      jnlib_set_errno (EINVAL);
      return -1;
    }
  *port = (unsigned int)aval;
  for (p=buffer; nread && *p != '\n'; p++, nread--)
    ;
  if (*p != '\n' || nread != 17)
    {
      jnlib_set_errno (EINVAL);
      return -1;
    }
  p++; nread--;
  memcpy (nonce, p, 16);
  return 0;
}



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
_w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
{
  struct sockaddr_in myaddr;
  struct sockaddr_un *unaddr;
  unsigned short port;
  char nonce[16];
  int ret;

  (void)addrlen;
      
  unaddr = (struct sockaddr_un *)addr;
  if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
    return -1;
      
  myaddr.sin_family = AF_INET;
  myaddr.sin_port = htons (port); 
  myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  
  /* Set return values.  */
  unaddr->sun_family = myaddr.sin_family;
  unaddr->sun_port = myaddr.sin_port;
  unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
  
  ret = connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
  if (!ret)
    {
      /* Send the nonce. */
      ret = send (sockfd, nonce, 16, 0);
      if (ret >= 0 && ret != 16)
        {
          jnlib_set_errno (EIO);
          ret = -1;
        }
    }
  return ret;
}


#endif /*_WIN32*/