summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Bornecrantz <jakob@vmware.com>2009-03-22 04:22:33 +0100
committerJakob Bornecrantz <wallbraker@gmail.com>2009-06-01 02:18:15 -0700
commitaee1a6f70413235c0c4c2c2adfca97d5128a155e (patch)
tree869b2b32e91fa9dcc4f998bf4bf2923551107350
parentbedf7fa49f549541fde2e19d2fbb6b8fe57ec124 (diff)
util: Add simple network functions
-rw-r--r--src/gallium/auxiliary/util/Makefile1
-rw-r--r--src/gallium/auxiliary/util/SConscript1
-rw-r--r--src/gallium/auxiliary/util/u_network.c188
-rw-r--r--src/gallium/auxiliary/util/u_network.h24
4 files changed, 214 insertions, 0 deletions
diff --git a/src/gallium/auxiliary/util/Makefile b/src/gallium/auxiliary/util/Makefile
index 2995aba1b91..6a8eb73e84a 100644
--- a/src/gallium/auxiliary/util/Makefile
+++ b/src/gallium/auxiliary/util/Makefile
@@ -16,6 +16,7 @@ C_SOURCES = \
16 u_hash.c \ 16 u_hash.c \
17 u_keymap.c \ 17 u_keymap.c \
18 u_linear.c \ 18 u_linear.c \
19 u_network.c \
19 u_math.c \ 20 u_math.c \
20 u_mm.c \ 21 u_mm.c \
21 u_rect.c \ 22 u_rect.c \
diff --git a/src/gallium/auxiliary/util/SConscript b/src/gallium/auxiliary/util/SConscript
index d3ac7f747fd..fb142eebca8 100644
--- a/src/gallium/auxiliary/util/SConscript
+++ b/src/gallium/auxiliary/util/SConscript
@@ -17,6 +17,7 @@ util = env.ConvenienceLibrary(
17 'u_hash.c', 17 'u_hash.c',
18 'u_hash_table.c', 18 'u_hash_table.c',
19 'u_keymap.c', 19 'u_keymap.c',
20 'u_network.c',
20 'u_math.c', 21 'u_math.c',
21 'u_mm.c', 22 'u_mm.c',
22 'u_rect.c', 23 'u_rect.c',
diff --git a/src/gallium/auxiliary/util/u_network.c b/src/gallium/auxiliary/util/u_network.c
new file mode 100644
index 00000000000..465d50255e4
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_network.c
@@ -0,0 +1,188 @@
1
2#include "pipe/p_compiler.h"
3#include "util/u_network.h"
4#include "util/u_debug.h"
5
6#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
7# include <windows.h>
8# include <winsock.h>
9#elif defined(PIPE_OS_LINUX)
10# include <sys/socket.h>
11# include <netinet/in.h>
12# include <unistd.h>
13# include <fcntl.h>
14# include <netdb.h>
15#else
16# warning "No socket implementation"
17#endif
18
19boolean
20u_socket_init()
21{
22#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
23 WORD wVersionRequested;
24 WSADATA wsaData;
25 int err;
26
27 /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
28 wVersionRequested = MAKEWORD(1, 1);
29
30 err = WSAStartup(wVersionRequested, &wsaData);
31 if (err != 0) {
32 debug_printf("WSAStartup failed with error: %d\n", err);
33 return FALSE;
34 }
35 return TRUE;
36#elif defined(PIPE_HAVE_SOCKETS)
37 return TRUE;
38#else
39 return FALSE;
40#endif
41}
42
43void
44u_socket_stop()
45{
46#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
47 WSACleanup();
48#endif
49}
50
51void
52u_socket_close(int s)
53{
54 if (s < 0)
55 return;
56
57#if defined(PIPE_OS_LINUX)
58 shutdown(s, SHUT_RDWR);
59 close(s);
60#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
61 shutdown(s, SD_BOTH);
62 closesocket(s);
63#else
64 assert(0);
65#endif
66}
67
68int u_socket_accept(int s)
69{
70#if defined(PIPE_HAVE_SOCKETS)
71 return accept(s, NULL, NULL);
72#else
73 return -1;
74#endif
75}
76
77int
78u_socket_send(int s, void *data, size_t size)
79{
80#if defined(PIPE_HAVE_SOCKETS)
81 return send(s, data, size, 0);
82#else
83 return -1;
84#endif
85}
86
87int
88u_socket_peek(int s, void *data, size_t size)
89{
90#if defined(PIPE_HAVE_SOCKETS)
91 return recv(s, data, size, MSG_PEEK);
92#else
93 return -1;
94#endif
95}
96
97int
98u_socket_recv(int s, void *data, size_t size)
99{
100#if defined(PIPE_HAVE_SOCKETS)
101 return recv(s, data, size, 0);
102#else
103 return -1;
104#endif
105}
106
107int
108u_socket_connect(const char *hostname, uint16_t port)
109{
110#if defined(PIPE_HAVE_SOCKETS)
111 int s;
112 struct sockaddr_in sa;
113 struct hostent *host = NULL;
114
115 memset(&sa, 0, sizeof(struct sockaddr_in));
116 host = gethostbyname(hostname);
117 if (!host)
118 return -1;
119
120 memcpy((char *)&sa.sin_addr,host->h_addr,host->h_length);
121 sa.sin_family= host->h_addrtype;
122 sa.sin_port = htons(port);
123
124 s = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP);
125 if (s < 0)
126 return -1;
127
128 if (connect(s, (struct sockaddr *)&sa, sizeof(sa))) {
129 u_socket_close(s);
130 return -1;
131 }
132
133 return s;
134#else
135 assert(0);
136 return -1;
137#endif
138}
139
140int
141u_socket_listen_on_port(uint16_t portnum)
142{
143#if defined(PIPE_HAVE_SOCKETS)
144 int s;
145 struct sockaddr_in sa;
146 memset(&sa, 0, sizeof(struct sockaddr_in));
147
148 sa.sin_family = AF_INET;
149 sa.sin_port = htons(portnum);
150
151 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
152 if (s < 0)
153 return -1;
154
155 if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == -1) {
156 u_socket_close(s);
157 return -1;
158 }
159
160 listen(s, 0);
161
162 return s;
163#else
164 assert(0);
165 return -1;
166#endif
167}
168
169void
170u_socket_block(int s, boolean block)
171{
172#if defined(PIPE_OS_LINUX)
173 int old = fcntl(s, F_GETFL, 0);
174 if (old == -1)
175 return;
176
177 /* TODO obey block */
178 if (block)
179 fcntl(s, F_SETFL, old & ~O_NONBLOCK);
180 else
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);
185#else
186 assert(0);
187#endif
188}
diff --git a/src/gallium/auxiliary/util/u_network.h b/src/gallium/auxiliary/util/u_network.h
new file mode 100644
index 00000000000..14d3884427e
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_network.h
@@ -0,0 +1,24 @@
1
2#ifndef _U_NETWORK_H_
3#define _U_NETWORK_H_
4
5#include "pipe/p_compiler.h"
6
7#if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
8# define PIPE_HAVE_SOCKETS
9#elif defined(PIPE_OS_LINUX)
10# define PIPE_HAVE_SOCKETS
11#endif
12
13boolean u_socket_init(void);
14void u_socket_stop(void);
15void u_socket_close(int s);
16int u_socket_listen_on_port(uint16_t portnum);
17int u_socket_accept(int s);
18int u_socket_connect(const char *host, uint16_t port);
19int u_socket_send(int s, void *data, size_t size);
20int u_socket_peek(int s, void *data, size_t size);
21int u_socket_recv(int s, void *data, size_t size);
22void u_socket_block(int s, boolean block);
23
24#endif