Revert last. It is still wrong.
[gnupg.git] / util / assuan-socket.c
blob4d79792ce98934a0b64cfa4f6dc81ac75226cf84
1 /* assuan-socket.c
2 * Copyright (C) 2004 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 /* Please note that this is a stripped down and modified version of
23 the orginal Assuan code from libassuan. */
25 #include <config.h>
26 #include <stdio.h>
27 #ifdef HAVE_W32_SYSTEM
28 #include <windows.h>
29 #include <io.h>
30 #else
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #endif
34 #include "assuan-defs.h"
36 int
37 _assuan_close (int fd)
39 #ifndef HAVE_W32_SYSTEM
40 return close (fd);
41 #else
42 int rc = closesocket (fd);
43 if (rc && WSAGetLastError () == WSAENOTSOCK)
44 rc = close (fd);
45 return rc;
46 #endif
50 int
51 _assuan_sock_new (int domain, int type, int proto)
53 #ifndef HAVE_W32_SYSTEM
54 return socket (domain, type, proto);
55 #else
56 if (domain == AF_UNIX || domain == AF_LOCAL)
57 domain = AF_INET;
58 return socket (domain, type, proto);
59 #endif
63 int
64 _assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen)
66 #ifndef HAVE_W32_SYSTEM
67 return connect (sockfd, addr, addrlen);
68 #else
69 struct sockaddr_in myaddr;
70 struct sockaddr_un * unaddr;
71 FILE * fp;
72 int port = 0;
74 unaddr = (struct sockaddr_un *)addr;
75 fp = fopen (unaddr->sun_path, "rb");
76 if (!fp)
77 return -1;
78 fscanf (fp, "%d", &port);
79 fclose (fp);
80 /* XXX: set errno in this case */
81 if (port < 0 || port > 65535)
82 return -1;
84 myaddr.sin_family = AF_INET;
85 myaddr.sin_port = port;
86 myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
88 /* we need this later. */
89 unaddr->sun_family = myaddr.sin_family;
90 unaddr->sun_port = myaddr.sin_port;
91 unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
93 return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
94 #endif