2 * Copyright (C) 2004 g10 Code GmbH
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,
26 #include "w32-afunix.h"
31 int rc
= closesocket (fd
);
32 if (rc
&& WSAGetLastError () == WSAENOTSOCK
)
39 _w32_sock_new (int domain
, int type
, int proto
)
41 if (domain
== AF_UNIX
|| domain
== AF_LOCAL
)
43 return socket (domain
, type
, proto
);
48 _w32_sock_connect (int sockfd
, struct sockaddr
* addr
, int addrlen
)
50 struct sockaddr_in myaddr
;
51 struct sockaddr_un
* unaddr
;
55 unaddr
= (struct sockaddr_un
*)addr
;
56 fp
= fopen (unaddr
->sun_path
, "rb");
59 fscanf (fp
, "%d", &port
);
62 /* XXX: set errno in this case */
63 if (port
< 0 || port
> 65535)
66 myaddr
.sin_family
= AF_INET
;
67 myaddr
.sin_port
= port
;
68 myaddr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
70 /* we need this later. */
71 unaddr
->sun_family
= myaddr
.sin_family
;
72 unaddr
->sun_port
= myaddr
.sin_port
;
73 unaddr
->sun_addr
.s_addr
= myaddr
.sin_addr
.s_addr
;
75 return connect (sockfd
, (struct sockaddr
*)&myaddr
, sizeof myaddr
);
80 _w32_sock_bind (int sockfd
, struct sockaddr
* addr
, int addrlen
)
82 if (addr
->sa_family
== AF_LOCAL
|| addr
->sa_family
== AF_UNIX
)
84 struct sockaddr_in myaddr
;
85 struct sockaddr_un
* unaddr
;
87 int len
= sizeof myaddr
;
91 myaddr
.sin_family
= AF_INET
;
92 myaddr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
94 rc
= bind (sockfd
, (struct sockaddr
*)&myaddr
, len
);
97 rc
= getsockname (sockfd
, (struct sockaddr
*)&myaddr
, &len
);
100 unaddr
= (struct sockaddr_un
*)addr
;
101 fp
= fopen (unaddr
->sun_path
, "wb");
104 fprintf (fp
, "%d", myaddr
.sin_port
);
107 /* we need this later. */
108 unaddr
->sun_family
= myaddr
.sin_family
;
109 unaddr
->sun_port
= myaddr
.sin_port
;
110 unaddr
->sun_addr
.s_addr
= myaddr
.sin_addr
.s_addr
;
114 return bind (sockfd
, addr
, addrlen
);