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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #ifdef HAVE_W32_SYSTEM
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include "assuan-defs.h"
32 _assuan_close (int fd)
34 #ifndef HAVE_W32_SYSTEM
37 int rc = closesocket (fd);
38 if (rc && WSAGetLastError () == WSAENOTSOCK)
46 _assuan_sock_new (int domain, int type, int proto)
48 #ifndef HAVE_W32_SYSTEM
49 return socket (domain, type, proto);
51 if (domain == AF_UNIX || domain == AF_LOCAL)
53 return socket (domain, type, proto);
59 _assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen)
61 #ifndef HAVE_W32_SYSTEM
62 return connect (sockfd, addr, addrlen);
64 struct sockaddr_in myaddr;
65 struct sockaddr_un * unaddr;
69 unaddr = (struct sockaddr_un *)addr;
70 fp = fopen (unaddr->sun_path, "rb");
73 fscanf (fp, "%d", &port);
75 /* XXX: set errno in this case */
76 if (port < 0 || port > 65535)
79 myaddr.sin_family = AF_INET;
80 myaddr.sin_port = port;
81 myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
83 /* we need this later. */
84 unaddr->sun_family = myaddr.sin_family;
85 unaddr->sun_port = myaddr.sin_port;
86 unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
88 return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
94 _assuan_sock_bind (int sockfd, struct sockaddr * addr, int addrlen)
96 #ifndef HAVE_W32_SYSTEM
97 return bind (sockfd, addr, addrlen);
99 if (addr->sa_family == AF_LOCAL || addr->sa_family == AF_UNIX)
101 struct sockaddr_in myaddr;
102 struct sockaddr_un * unaddr;
104 int len = sizeof myaddr;
108 myaddr.sin_family = AF_INET;
109 myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
111 rc = bind (sockfd, (struct sockaddr *)&myaddr, len);
114 rc = getsockname (sockfd, (struct sockaddr *)&myaddr, &len);
117 unaddr = (struct sockaddr_un *)addr;
118 fp = fopen (unaddr->sun_path, "wb");
121 fprintf (fp, "%d", myaddr.sin_port);
124 /* we need this later. */
125 unaddr->sun_family = myaddr.sin_family;
126 unaddr->sun_port = myaddr.sin_port;
127 unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
131 return bind (sockfd, addr, addrlen);