Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: experimental/visual_studio_plugin/src/debug_conn/debug_socket_impl.c

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can
4 * be found in the LICENSE file.
5 */
6
7
8 #ifdef WIN32
9 # include <winsock2.h>
10 # include <stdio.h>
11 # define snprintf _snprintf
12 typedef int socklen_t;
13 #else
14 # include <arpa/inet.h>
15 # include <sys/socket.h>
16 # include <errno.h>
17 # include <unistd.h>
18 #endif
19
20 #include "debug_conn/debug_socket_impl.h"
21 #include "debug_conn/debug_util.h"
22 #include "native_client/src/include/portability.h"
23
24 #define CHECK_ERROR() DebugSocketLogError(__FILE__, __LINE__, 1)
25 #define PROCESS_ERROR() DebugSocketLogError(__FILE__, __LINE__, 0)
26
27
28 DSError DebugSocketLogError(const char *file, int line, int block_ok) {
29 int err = DebugSocketGetError(block_ok);
30
31 // If this is a REAL error, then log it
32 if (err) {
33 debug_log_error("%s(%d) : Socket error %d.\n",
34 file, line, err);
35 return DSE_ERROR;
36 }
37
38 // Otherwise return what is effectively "WOULDBLOCK"
39 return DSE_TIMEOUT;
40 }
41
42
43 DSError DebugSocketAccept(DSHandle in,
44 DSHandle *out,
45 char *addr,
46 uint32_t max) {
47 struct sockaddr_in saddr;
48 socklen_t addrlen = (socklen_t)(sizeof(saddr));
49
50 SOCKET s;
51 s = accept((SOCKET) in, (struct sockaddr *) &saddr, &addrlen);
52
53 // Check if we WOULDBLOCK
54 if (-1 == s)
55 return CHECK_ERROR();
56
57 *out = (DSHandle) s;
58
59 DebugSocketAddrToStr(&saddr, addrlen, addr, max);
60 return DSE_OK;
61 }
62
63 DSError DebugSocketBind(DSHandle handle, const char *addr) {
64 struct sockaddr_in saddr;
65 socklen_t addrlen = (socklen_t) sizeof(saddr);
66
67 if (DebugSocketStrToAddr(addr, &saddr, addrlen) != DSE_OK)
68 return DSE_ERROR;
69
70 if (bind((SOCKET) handle, (struct sockaddr *) &saddr, addrlen))
71 return PROCESS_ERROR();
72
73 return DSE_OK;
74 }
75
76 DSError DebugSocketConnect(DSHandle handle, const char *addr) {
77 struct sockaddr_in saddr;
78 socklen_t addrlen = (socklen_t) sizeof(saddr);
79
80 if (DebugSocketStrToAddr(addr, &saddr, addrlen) != DSE_OK)
81 return DSE_ERROR;
82
83 DebugSocketStrToAddr(addr, &saddr, addrlen);
84
85 // Check if we WOULDBLOCK
86 if (connect((SOCKET) handle,
87 (struct sockaddr *) &saddr,
88 sizeof(saddr)))
89 return CHECK_ERROR();
90
91 return DSE_OK;
92 }
93
94 DSError DebugSocketListen(DSHandle handle, uint32_t cnt) {
95 if (listen((SOCKET) handle, cnt) == -1)
96 return PROCESS_ERROR();
97
98 return DSE_OK;
99 }
100
101 DSError DebugSocketSend(DSHandle handle, void *data, int32_t max, int32_t *len) {
102 int res = (int) send((SOCKET) handle, (char *)(data), max, 0);
103
104 // If we have nothing, then the socket must have closed
105 if (0 == res) {
106 *len = 0;
107 return DSE_TIMEOUT;
108 }
109
110 if (-1 == res) {
111 // If error (or would block), transfer size must be zero
112
113 *len = 0;
114 return CHECK_ERROR();
115 }
116
117 *len = res;
118 return DSE_OK;
119 }
120
121 DSError DebugSocketRecv(DSHandle handle, void *data, int32_t max, int32_t *len) {
122 int res = (int) recv((SOCKET) handle, (char *)(data), max, 0);
123
124 // If we have nothing, then the socket must have closed
125 if (0 == res) {
126 *len = 0;
127 return DSE_TIMEOUT;
128 }
129
130 if (-1 == res) {
131 // If error (or would block), transfer size must be zero
132 *len = 0;
133 return CHECK_ERROR();
134 }
135
136 *len = res;
137 return DSE_OK;
138 }
139
140 DSError DebugSocketRecvAvail(DSHandle handle, uint32_t ms_usec) {
141 struct timeval timeout;
142 fd_set fds;
143 int cnt;
144
145 FD_ZERO(&fds);
146 FD_SET((SOCKET) handle, &fds);
147
148 // We want a "non-blocking" check
149 timeout.tv_sec = 0;
150 timeout.tv_usec= ms_usec * 1000;
151
152 // Check if this file handle can select on read
153 cnt = select(0, &fds, 0, &fds, &timeout);
154 if (-1 == cnt)
155 return PROCESS_ERROR();
156
157 if (cnt > 0)
158 return DSE_OK;
159
160 return DSE_TIMEOUT;
161 }
162
163 DSError DebugSocketSendAvail(DSHandle handle, uint32_t ms_usec) {
164 fd_set fds;
165 struct timeval timeout;
166 int cnt;
167
168 FD_ZERO(&fds);
169 FD_SET((SOCKET) handle, &fds);
170
171 // We want a "non-blocking" check
172 timeout.tv_sec = 0;
173 timeout.tv_usec= ms_usec * 1000;
174
175 // Check if this file handle can select on write
176 cnt = select(0, 0, &fds, 0, &timeout);
177 if (-1 == cnt)
178 return PROCESS_ERROR();
179
180 if (cnt > 0)
181 return DSE_OK;
182
183 return DSE_TIMEOUT;
184 }
185
186
187 DSError DebugSocketStrToAddr(const char *saddr, void *daddr, uint32_t len) {
188 struct sockaddr_in *saddr_in = (struct sockaddr_in *) daddr;
189 char *ip_port[2];
190 int ip_port_cnt = 0;
191
192 char *octets[4];
193 int octets_cnt = 0;
194
195 unsigned host = 0;
196 unsigned port = 0;
197
198 if (len != sizeof(struct sockaddr_in))
199 return DSE_ERROR;
200
201 ip_port_cnt = debug_get_tokens(saddr, ':', ip_port, 2);
202 if (ip_port_cnt > 0) {
203 octets_cnt = debug_get_tokens(ip_port[0], '.', octets, 4);
204 if (4 == octets_cnt) {
205 int a;
206 for (a = 0; a < 4; a++) {
207 host <<= 8;
208 host |= (atoi(octets[a]) & 0xFF);
209 }
210 }
211 debug_free_tokens(octets, octets_cnt);
212 }
213 if (ip_port_cnt > 1) {
214 port = atoi(ip_port[1]);
215 }
216 debug_free_tokens(ip_port, ip_port_cnt);
217
218 saddr_in->sin_family = AF_INET;
219 saddr_in->sin_addr.s_addr = htonl(host);
220 saddr_in->sin_port = htons(port);
221
222 return DSE_OK;
223 }
224
225
226 DSError DebugSocketAddrToStr(void *saddr, uint32_t len, char *daddr, uint32_t ma x) {
227 char tmp[MAX_ADDR_LEN];
228 struct sockaddr_in *saddr_in = (struct sockaddr_in *)(saddr);
229
230 unsigned int host;
231 unsigned int port;
232
233 if (len != sizeof(struct sockaddr_in))
234 return DSE_ERROR;
235
236 host = htonl(saddr_in->sin_addr.s_addr);
237 port = htons(saddr_in->sin_port);
238
239 if (snprintf(tmp,
240 sizeof(tmp),
241 "%d.%d.%d.%d:%d",
242 (host >> 24) & 0xFF,
243 (host >> 16) & 0xFF,
244 (host >> 8) & 0xFF,
245 (host >> 0) & 0xFF,
246 port) > (int) max)
247 return DSE_ERROR;
248
249 strcpy(daddr, tmp);
250 return DSE_OK;
251 }
252
253 DSError DebugSocketAddrSize(uint32_t *len) {
254 if (NULL == len)
255 return DSE_ERROR;
256
257 *len = (uint32_t) sizeof(struct sockaddr_in);
258 return DSE_OK;
259 }
260
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698