comparison network.c @ 419:d015ecbd231d

Use C99 style comments, too.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 07:37:36 +0300
parents 8263cb88556a
children 6727fec3c326
comparison
equal deleted inserted replaced
418:8ca09a6cca09 419:d015ecbd231d
165 conn->hst = nn_resolve_host(conn, host); 165 conn->hst = nn_resolve_host(conn, host);
166 } 166 }
167 167
168 nn_get_addr(&(conn->addr), conn->hst); 168 nn_get_addr(&(conn->addr), conn->hst);
169 169
170 /* Prepare for connection */ 170 // Prepare for connection
171 dest.sin_family = AF_INET; 171 dest.sin_family = AF_INET;
172 172
173 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) 173 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST)
174 { 174 {
175 dest.sin_port = htons(conn->proxy.port); 175 dest.sin_port = htons(conn->proxy.port);
205 } 205 }
206 206
207 FD_ZERO(&(conn->sockfds)); 207 FD_ZERO(&(conn->sockfds));
208 FD_SET(conn->socket, &(conn->sockfds)); 208 FD_SET(conn->socket, &(conn->sockfds));
209 209
210 /* Proxy-specific setup */ 210 // Proxy-specific setup
211 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) 211 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A)
212 { 212 {
213 struct nn_socks_t *socksh; 213 struct nn_socks_t *socksh;
214 size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1; 214 size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1;
215 char *ptr, *buf; 215 char *ptr, *buf;
224 conn->err = -1; 224 conn->err = -1;
225 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz); 225 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz);
226 goto error; 226 goto error;
227 } 227 }
228 228
229 /* Create SOCKS 4/4A request */ 229 // Create SOCKS 4/4A request
230 nn_conn_msg(conn, "Initializing proxy negotiation.\n"); 230 nn_conn_msg(conn, "Initializing proxy negotiation.\n");
231 socksh = (struct nn_socks_t *) buf; 231 socksh = (struct nn_socks_t *) buf;
232 socksh->version = 4; 232 socksh->version = 4;
233 socksh->command = SOCKS_CMD_CONNECT; 233 socksh->command = SOCKS_CMD_CONNECT;
234 socksh->port = htons(port); 234 socksh->port = htons(port);
244 { 244 {
245 ptr += strlen(userid) + 1; 245 ptr += strlen(userid) + 1;
246 strcpy(ptr, conn->host); 246 strcpy(ptr, conn->host);
247 } 247 }
248 248
249 /* Send request */ 249 // Send request
250 nn_conn_reset(conn); 250 nn_conn_reset(conn);
251 if (!nn_conn_send_buf(conn, buf, bufsiz)) 251 if (!nn_conn_send_buf(conn, buf, bufsiz))
252 { 252 {
253 th_free(buf); 253 th_free(buf);
254 nn_conn_err(conn, "Error sending SOCKS proxy request.\n"); 254 nn_conn_err(conn, "Error sending SOCKS proxy request.\n");
255 goto error; 255 goto error;
256 } 256 }
257 th_free(buf); 257 th_free(buf);
258 258
259 /* Wait for SOCKS server to reply */ 259 // Wait for SOCKS server to reply
260 for (status = tries = 1; tries <= 20 && status > 0; tries++) 260 for (status = tries = 1; tries <= 20 && status > 0; tries++)
261 { 261 {
262 #ifdef __WIN32 262 #ifdef __WIN32
263 Sleep(50); 263 Sleep(50);
264 #else 264 #else
266 #endif 266 #endif
267 nn_conn_reset(conn); 267 nn_conn_reset(conn);
268 status = nn_conn_pull(conn); 268 status = nn_conn_pull(conn);
269 } 269 }
270 270
271 /* Check results */ 271 // Check results
272 if (status == 0) 272 if (status == 0)
273 { 273 {
274 struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf); 274 struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf);
275 if (res->nb != 0) 275 if (res->nb != 0)
276 { 276 {
377 fd_set tmpfds; 377 fd_set tmpfds;
378 378
379 if (conn == NULL) 379 if (conn == NULL)
380 return -10; 380 return -10;
381 381
382 /* Prod the input buffer */ 382 // Prod the input buffer
383 if (conn->in_ptr > conn->buf && conn->in_ptr - conn->ptr > 0) 383 if (conn->in_ptr > conn->buf && conn->in_ptr - conn->ptr > 0)
384 { 384 {
385 size_t delta = conn->in_ptr - conn->ptr; 385 size_t delta = conn->in_ptr - conn->ptr;
386 memmove(conn->buf, conn->in_ptr, delta); 386 memmove(conn->buf, conn->in_ptr, delta);
387 conn->ptr = conn->buf; 387 conn->ptr = conn->buf;
388 conn->in_ptr -= delta; 388 conn->in_ptr -= delta;
389 conn->total_bytes -= delta; 389 conn->total_bytes -= delta;
390 } 390 }
391 391
392 /* Check for incoming data */ 392 // Check for incoming data
393 socktv.tv_sec = 0; 393 socktv.tv_sec = 0;
394 socktv.tv_usec = NN_DELAY_USEC; 394 socktv.tv_usec = NN_DELAY_USEC;
395 tmpfds = conn->sockfds; 395 tmpfds = conn->sockfds;
396 396
397 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) 397 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1)
441 441
442 442
443 BOOL nn_network_init(void) 443 BOOL nn_network_init(void)
444 { 444 {
445 #ifdef __WIN32 445 #ifdef __WIN32
446 /* Initialize WinSock, if needed */ 446 // Initialize WinSock, if needed
447 WSADATA wsaData; 447 WSADATA wsaData;
448 int err = WSAStartup(0x0101, &wsaData); 448 int err = WSAStartup(0x0101, &wsaData);
449 if (err != 0) 449 if (err != 0)
450 { 450 {
451 THERR("Could not initialize WinSock library (err=%d).\n", err); 451 THERR("Could not initialize WinSock library (err=%d).\n", err);