2 #include "pipe/p_compiler.h"
3 #include "util/u_network.h"
4 #include "util/u_debug.h"
6 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
9 #elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_AROS)
10 # include <sys/socket.h>
11 # include <netinet/in.h>
16 # warning "No socket implementation"
22 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
23 WORD wVersionRequested
;
27 /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
28 wVersionRequested
= MAKEWORD(1, 1);
30 err
= WSAStartup(wVersionRequested
, &wsaData
);
32 debug_printf("WSAStartup failed with error: %d\n", err
);
36 #elif defined(PIPE_HAVE_SOCKETS)
46 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
57 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE)
58 shutdown(s
, SHUT_RDWR
);
60 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
68 int u_socket_accept(int s
)
70 #if defined(PIPE_HAVE_SOCKETS)
71 return accept(s
, NULL
, NULL
);
78 u_socket_send(int s
, void *data
, size_t size
)
80 #if defined(PIPE_HAVE_SOCKETS)
81 return send(s
, data
, size
, 0);
88 u_socket_peek(int s
, void *data
, size_t size
)
90 #if defined(PIPE_HAVE_SOCKETS)
91 return recv(s
, data
, size
, MSG_PEEK
);
98 u_socket_recv(int s
, void *data
, size_t size
)
100 #if defined(PIPE_HAVE_SOCKETS)
101 return recv(s
, data
, size
, 0);
108 u_socket_connect(const char *hostname
, uint16_t port
)
110 #if defined(PIPE_HAVE_SOCKETS)
112 struct sockaddr_in sa
;
113 struct hostent
*host
= NULL
;
115 memset(&sa
, 0, sizeof(struct sockaddr_in
));
116 host
= gethostbyname(hostname
);
120 memcpy((char *)&sa
.sin_addr
,host
->h_addr_list
[0],host
->h_length
);
121 sa
.sin_family
= host
->h_addrtype
;
122 sa
.sin_port
= htons(port
);
124 s
= socket(host
->h_addrtype
, SOCK_STREAM
, IPPROTO_TCP
);
128 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
))) {
141 u_socket_listen_on_port(uint16_t portnum
)
143 #if defined(PIPE_HAVE_SOCKETS)
145 struct sockaddr_in sa
;
146 memset(&sa
, 0, sizeof(struct sockaddr_in
));
148 sa
.sin_family
= AF_INET
;
149 sa
.sin_port
= htons(portnum
);
151 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
155 if (bind(s
, (struct sockaddr
*)&sa
, sizeof(struct sockaddr_in
)) == -1) {
170 u_socket_block(int s
, boolean block
)
172 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE)
173 int old
= fcntl(s
, F_GETFL
, 0);
177 /* TODO obey block */
179 fcntl(s
, F_SETFL
, old
& ~O_NONBLOCK
);
181 fcntl(s
, F_SETFL
, old
| O_NONBLOCK
);
182 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
183 u_long iMode
= block
? 0 : 1;
184 ioctlsocket(s
, FIONBIO
, &iMode
);