Mercurial > hg > octave-kai > gnulib-hg
annotate lib/sockets.c @ 16201:8250f2777afc
maint: update all copyright year number ranges
Run "make update-copyright".
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Sun, 01 Jan 2012 10:04:58 +0100 |
parents | b86e9061a6d0 |
children | e542fd46ad6f |
rev | line source |
---|---|
9642 | 1 /* sockets.c --- wrappers for Windows socket functions |
2 | |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
15752
diff
changeset
|
3 Copyright (C) 2008-2012 Free Software Foundation, Inc. |
9642 | 4 |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | |
18 /* Written by Simon Josefsson */ | |
19 | |
20 #include <config.h> | |
21 | |
11416
1b59f3d3ede6
Make sockets.h self-contained.
Bruno Haible <bruno@clisp.org>
parents:
9642
diff
changeset
|
22 /* Specification. */ |
1b59f3d3ede6
Make sockets.h self-contained.
Bruno Haible <bruno@clisp.org>
parents:
9642
diff
changeset
|
23 #include "sockets.h" |
1b59f3d3ede6
Make sockets.h self-contained.
Bruno Haible <bruno@clisp.org>
parents:
9642
diff
changeset
|
24 |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
25 #if WINDOWS_SOCKETS |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
26 |
9642 | 27 /* This includes winsock2.h on MinGW. */ |
12209
31e5813d01e1
sockets: avoid compiler warning
Eric Blake <ebb9@byu.net>
parents:
11764
diff
changeset
|
28 # include <sys/socket.h> |
9642 | 29 |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
30 # include "fd-hook.h" |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
14726
diff
changeset
|
31 # include "msvc-nothrow.h" |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
32 |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
33 /* Get set_winsock_errno, FD_TO_SOCKET etc. */ |
12209
31e5813d01e1
sockets: avoid compiler warning
Eric Blake <ebb9@byu.net>
parents:
11764
diff
changeset
|
34 # include "w32sock.h" |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
35 |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
36 static int |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
37 close_fd_maybe_socket (const struct fd_hook *remaining_list, |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
38 gl_close_fn primary, |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
39 int fd) |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
40 { |
14726
3e7ea0014c67
fclose: Fix possible link error.
Bruno Haible <bruno@clisp.org>
parents:
14612
diff
changeset
|
41 /* Note about multithread-safety: There is a race condition where, between |
3e7ea0014c67
fclose: Fix possible link error.
Bruno Haible <bruno@clisp.org>
parents:
14612
diff
changeset
|
42 our calls to closesocket() and the primary close(), some other thread |
3e7ea0014c67
fclose: Fix possible link error.
Bruno Haible <bruno@clisp.org>
parents:
14612
diff
changeset
|
43 could make system calls that allocate precisely the same HANDLE value |
3e7ea0014c67
fclose: Fix possible link error.
Bruno Haible <bruno@clisp.org>
parents:
14612
diff
changeset
|
44 as sock; then the primary close() would call CloseHandle() on it. */ |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
45 SOCKET sock; |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
46 WSANETWORKEVENTS ev; |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
47 |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
48 /* Test whether fd refers to a socket. */ |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
49 sock = FD_TO_SOCKET (fd); |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
50 ev.lNetworkEvents = 0xDEADBEEF; |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
51 WSAEnumNetworkEvents (sock, NULL, &ev); |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
52 if (ev.lNetworkEvents != 0xDEADBEEF) |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
53 { |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
54 /* fd refers to a socket. */ |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
55 /* FIXME: other applications, like squid, use an undocumented |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
56 _free_osfhnd free function. But this is not enough: The 'osfile' |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
57 flags for fd also needs to be cleared, but it is hard to access it. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
58 Instead, here we just close twice the file descriptor. */ |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
59 if (closesocket (sock)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
60 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
61 set_winsock_errno (); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
62 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
63 } |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
64 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
65 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
66 /* This call frees the file descriptor and does a |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
67 CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
68 _close (fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
69 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
70 } |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
71 } |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
72 else |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
73 /* Some other type of file descriptor. */ |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
74 return execute_close_hooks (remaining_list, primary, fd); |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
75 } |
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
76 |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
77 static int |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
78 ioctl_fd_maybe_socket (const struct fd_hook *remaining_list, |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
79 gl_ioctl_fn primary, |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
80 int fd, int request, void *arg) |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
81 { |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
82 SOCKET sock; |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
83 WSANETWORKEVENTS ev; |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
84 |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
85 /* Test whether fd refers to a socket. */ |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
86 sock = FD_TO_SOCKET (fd); |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
87 ev.lNetworkEvents = 0xDEADBEEF; |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
88 WSAEnumNetworkEvents (sock, NULL, &ev); |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
89 if (ev.lNetworkEvents != 0xDEADBEEF) |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
90 { |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
91 /* fd refers to a socket. */ |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
92 if (ioctlsocket (sock, request, arg) < 0) |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
93 { |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
94 set_winsock_errno (); |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
95 return -1; |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
96 } |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
97 else |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
98 return 0; |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
99 } |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
100 else |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
101 /* Some other type of file descriptor. */ |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
102 return execute_ioctl_hooks (remaining_list, primary, fd, request, arg); |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
103 } |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
104 |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
105 static struct fd_hook fd_sockets_hook; |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
106 |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
107 static int initialized_sockets_version /* = 0 */; |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
108 |
12209
31e5813d01e1
sockets: avoid compiler warning
Eric Blake <ebb9@byu.net>
parents:
11764
diff
changeset
|
109 #endif /* WINDOWS_SOCKETS */ |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
110 |
14612
6ef4f1f39105
Revert "use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE"
Jim Meyering <meyering@redhat.com>
parents:
14610
diff
changeset
|
111 int |
12482
11bc92a9e2bc
gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents:
12421
diff
changeset
|
112 gl_sockets_startup (int version _GL_UNUSED) |
9642 | 113 { |
114 #if WINDOWS_SOCKETS | |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
115 if (version > initialized_sockets_version) |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
116 { |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
117 WSADATA data; |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
118 int err; |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
119 |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
120 err = WSAStartup (version, &data); |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
121 if (err != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
122 return 1; |
9642 | 123 |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
124 if (data.wVersion < version) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12209
diff
changeset
|
125 return 2; |
9642 | 126 |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
127 if (initialized_sockets_version == 0) |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
128 register_fd_hook (close_fd_maybe_socket, ioctl_fd_maybe_socket, |
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
129 &fd_sockets_hook); |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
130 |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
131 initialized_sockets_version = version; |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
132 } |
9642 | 133 #endif |
134 | |
135 return 0; | |
136 } | |
137 | |
138 int | |
139 gl_sockets_cleanup (void) | |
140 { | |
141 #if WINDOWS_SOCKETS | |
142 int err; | |
143 | |
11764
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
144 initialized_sockets_version = 0; |
57245c9661b9
Allow multiple calls to gl_sockets_startup.
Bruno Haible <bruno@clisp.org>
parents:
11426
diff
changeset
|
145 |
14595
32d72ffe29dd
ioctl: Remove link dependency on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
146 unregister_fd_hook (&fd_sockets_hook); |
11426
d40b890c5200
Remove dependency from 'close' module to -lws2_32 on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
11416
diff
changeset
|
147 |
9642 | 148 err = WSACleanup (); |
149 if (err != 0) | |
150 return 1; | |
151 #endif | |
152 | |
153 return 0; | |
154 } |