annotate network.c @ 618:7d1aac6b27af

Disable SOCKS 5 specific code that is not supported yet.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 21 May 2014 22:54:26 +0300
parents 399ce077b2a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
605
ad00f2bbb615 Bump copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 604
diff changeset
4 * (C) Copyright 2008-2014 Tecnic Software productions (TNSP)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
6 #include "network.h"
415
8263cb88556a Some #include cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
7 #include <errno.h>
8263cb88556a Some #include cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
8
614
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
9 enum
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
10 {
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
11 NN_SOCKS5_AUTH_NONE = 0,
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
12 NN_SOCKS5_AUTH_USER = 2,
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
13 };
1fc22e8efd26 Some cleanup work, preparation for SOCKS 5.
Matti Hamalainen <ccr@tnsp.org>
parents: 613
diff changeset
14
604
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
15 struct nn_socks4_t
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
16 {
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
17 uint8_t version;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
18 uint8_t command;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
19 in_port_t port;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
20 in_addr_t addr;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
21 } __attribute__((__packed__));
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
22
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
23 struct nn_socks4_res_t
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
24 {
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
25 uint8_t nb;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
26 uint8_t result;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
27 in_port_t port;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
28 in_addr_t addr;
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
29 } __attribute__((__packed__));
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
30
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
31
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
32 static BOOL nn_network_inited = FALSE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
604
37ab4725e4f9 In preparation for SOCKS 5 support, move some structs etc. into network.c
Matti Hamalainen <ccr@tnsp.org>
parents: 602
diff changeset
34
414
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
35 static const char *nn_proxy_types[] =
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
36 {
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
37 "none",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
38 "SOCKS 4",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
39 "SOCKS 4a",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
40 NULL
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
41 };
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 #ifdef __WIN32
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 const char *hstrerror(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 static char buf[64];
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 snprintf(buf, sizeof(buf), "Error #%d", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 return buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
52
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 int nn_get_socket_errno(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 return WSAGetLastError();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
58
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 const char *nn_get_socket_errstr(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 static char buf[64];
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 switch (err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 case WSAEADDRINUSE: return "Address already in use";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 case WSAECONNABORTED: return "Software caused connection abort";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 case WSAECONNREFUSED: return "Connection refused";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 case WSAECONNRESET: return "Connection reset by peer";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 case WSAEHOSTUNREACH: return "No route to host";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 case WSAENETDOWN: return "Network is down";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 case WSAETIMEDOUT: return "Connection timed out";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 case WSAHOST_NOT_FOUND: return "Host not found";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 case WSAVERNOTSUPPORTED: return "Wrong WinSock DLL version";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 default:
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 snprintf(buf, sizeof(buf), "Error #%d", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 return buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 #else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 int nn_get_socket_errno(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 return errno;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
85
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 const char *nn_get_socket_errstr(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 return strerror(err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 #endif
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 void nn_conn_err(nn_conn_t *conn, const char *fmt, ...)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 if (conn->errfunc != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
97 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 va_list ap;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 va_start(ap, fmt);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
100 msg = th_strdup_vprintf(fmt, ap);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 va_end(ap);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
102
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
103 conn->errfunc(conn, msg);
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
104 th_free(msg);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 static void nn_conn_msg(nn_conn_t *conn, const char *fmt, ...)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 if (conn->msgfunc != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 {
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
113 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 va_list ap;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 va_start(ap, fmt);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
116 msg = th_strdup_vprintf(fmt, ap);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 va_end(ap);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
118
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
119 conn->msgfunc(conn, msg);
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
120 th_free(msg);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
414
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
124
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 struct hostent *nn_resolve_host(nn_conn_t *conn, const char *name)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 struct hostent *res = gethostbyname(name);
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
128
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 if (res == NULL)
456
bb65460c7aa2 Clean up some initialization messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
130 nn_conn_err(conn, "Could not resolve hostname '%s': %s\n", name, strerror(h_errno));
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 nn_conn_msg(conn, "True hostname for %s is %s\n", name, res->h_name);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return res;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 nn_conn_t * nn_conn_new(
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
139 void (*errfunc)(nn_conn_t *conn, const char *msg),
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
140 void (*msgfunc)(nn_conn_t *conn, const char *msg))
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 {
606
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
142 nn_conn_t *conn = th_malloc0(sizeof(nn_conn_t));
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 return NULL;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 conn->errfunc = errfunc;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 conn->msgfunc = msgfunc;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 return conn;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
153
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 static BOOL nn_get_addr(struct in_addr *addr, struct hostent *hst)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 if (hst != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 *addr = *(struct in_addr *) (hst->h_addr);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 addr->s_addr = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
168
606
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
169 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host, int auth_type)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 conn->proxy.type = type;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 conn->proxy.port = port;
606
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
176 conn->proxy.auth_type = auth_type;
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
177
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
178 th_free(conn->proxy.host);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 conn->proxy.host = th_strdup(host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 if (host != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 conn->proxy.hst = nn_resolve_host(conn, host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 nn_get_addr(&(conn->proxy.addr), conn->proxy.hst);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
192
606
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
193 int nn_conn_set_proxy_auth_user(nn_conn_t *conn, const char *userid, const char *passwd)
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
194 {
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
195 if (conn == NULL)
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
196 return -1;
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
197
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
198 th_free(conn->proxy.userid);
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
199 conn->proxy.userid = th_strdup(userid);
611
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
200
606
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
201 th_free(conn->proxy.passwd);
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
202 conn->proxy.passwd = th_strdup(passwd);
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
203
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
204 return 0;
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
205 }
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
206
2cc5434a8ce0 Change proxy authentication handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 605
diff changeset
207
611
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
208 static int nn_conn_proxy_wait(nn_conn_t *conn)
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
209 {
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
210 int status, tries;
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
211
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
212 for (status = tries = 1; tries <= 20 && status > 0; tries++)
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
213 {
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
214 #ifdef __WIN32
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
215 Sleep(50);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
216 #else
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
217 usleep(50000);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
218 #endif
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
219 nn_conn_reset(conn);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
220 status = nn_conn_pull(conn);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
221 }
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
222
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
223 if (status < 0)
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
224 nn_conn_err(conn, "Proxy negotiation failed at try %d with network error: %d\n", tries, status);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
225 else
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
226 nn_conn_err(conn, "Proxy negotiation timed out.\n");
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
227
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
228 return status;
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
229 }
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
230
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
231
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
232 static BOOL nn_conn_proxy_send(nn_conn_t *conn, void *buf, size_t bufsiz)
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
233 {
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
234 BOOL ret;
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
235 nn_conn_reset(conn);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
236 if ((ret = nn_conn_send_buf(conn, buf, bufsiz)) == FALSE)
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
237 nn_conn_err(conn, "Error sending SOCKS proxy request.\n");
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
238 th_free(buf);
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
239 return ret;
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
240 }
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
241
019a54b07f60 Clean up and modularize proxy connection code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 606
diff changeset
242
615
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
243 static int nn_conn_socks4_negotiate(nn_conn_t *conn, const int port, const char *host)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
244 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
245 struct nn_socks4_res_t *sockres;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
246 struct nn_socks4_t *socksh;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
247 size_t bufsiz;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
248 uint8_t *ptr, *buf;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
249
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
250 (void) host;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
251
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
252 nn_conn_msg(conn, "Initializing SOCKS 4/a proxy negotiation.\n");
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
253
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
254 bufsiz = sizeof(struct nn_socks4_t) + strlen(conn->proxy.userid) + 1;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
255 if (conn->proxy.type == NN_PROXY_SOCKS4A)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
256 bufsiz += strlen(conn->host) + 1;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
257
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
258 if ((ptr = buf = th_malloc(bufsiz)) == NULL)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
259 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
260 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
261 return FALSE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
262 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
263
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
264 // Create SOCKS 4 handshake
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
265 socksh = (struct nn_socks4_t *) buf;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
266 socksh->version = 4;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
267 socksh->command = NN_PROXY_CMD_CONNECT;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
268 socksh->port = htons(port);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
269 socksh->addr = (conn->proxy.type == NN_PROXY_SOCKS4A) ? htonl(0x00000032) : conn->addr.s_addr;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
270 ptr += sizeof(struct nn_socks4_t);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
271
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
272 strcpy((char *) ptr, conn->proxy.userid);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
273
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
274 if (conn->proxy.type == NN_PROXY_SOCKS4A)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
275 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
276 ptr += strlen(conn->proxy.userid) + 1;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
277 strcpy((char *)ptr, conn->host);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
278 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
279
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
280 // Send request
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
281 if (!nn_conn_proxy_send(conn, buf, bufsiz))
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
282 return FALSE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
283
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
284 // Wait for SOCKS server to reply
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
285 if (nn_conn_proxy_wait(conn) != 0)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
286 return FALSE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
287
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
288 sockres = (struct nn_socks4_res_t *) &(conn->buf);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
289 if (sockres->nb != 0)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
290 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
291 nn_conn_err(conn, "Invalid SOCKS 4 server reply, does not begin with NUL byte (%d).\n", sockres->nb);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
292 return FALSE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
293 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
294 if (sockres->result != 0x5a)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
295 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
296 char *s = NULL;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
297 switch (sockres->result)
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
298 {
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
299 case 0x5b: s = "Request rejected or failed"; break;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
300 case 0x5c: s = "Request failed because client is not running identd (or not reachable from the server)"; break;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
301 case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
302 default: s = "Unknown SOCKS 4 error response"; break;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
303 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
304 nn_conn_err(conn, "SOCKS 4 setup failed, 0x%02x: %s.\n", sockres->result, s);
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
305 return FALSE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
306 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
307
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
308 return TRUE;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
309 }
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
310
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
311
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 int nn_conn_open(nn_conn_t *conn, const int port, const char *host)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 struct sockaddr_in dest;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 conn->port = port;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 if (host != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 conn->host = th_strdup(host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 conn->hst = nn_resolve_host(conn, host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 nn_get_addr(&(conn->addr), conn->hst);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
328 // Prepare for connection
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 dest.sin_family = AF_INET;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 {
615
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
333 // If using a proxy, we connect to the proxy server
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 dest.sin_port = htons(conn->proxy.port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 dest.sin_addr = conn->proxy.addr;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 nn_proxy_types[conn->proxy.type],
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 inet_ntoa(conn->proxy.addr), conn->proxy.port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 dest.sin_port = htons(conn->port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 dest.sin_addr = conn->addr;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 nn_conn_msg(conn, "Connecting to %s:%d ...\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 inet_ntoa(conn->addr), conn->port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 nn_conn_err(conn, "Could not connect: %s\n", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 FD_ZERO(&(conn->sockfds));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 FD_SET(conn->socket, &(conn->sockfds));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
367 // Proxy-specific setup
615
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
368 switch (conn->proxy.type)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 {
615
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
370 case NN_PROXY_SOCKS4:
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
371 case NN_PROXY_SOCKS4A:
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
372 if (!nn_conn_socks4_negotiate(conn, port, host))
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
373 goto error;
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
374 nn_conn_msg(conn, "SOCKS 4 connection established!\n");
399ce077b2a0 Factorize more proxy initialization stuff into separate functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
375 break;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 nn_conn_reset(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 conn->status = NN_CONN_OPEN;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 error:
613
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
383 nn_conn_close(conn);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
613
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
388 int nn_conn_close(nn_conn_t *conn)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 if (conn == NULL)
613
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
391 return -1;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 if (conn->socket >= 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 #ifdef __WIN32
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 closesocket(conn->socket);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 #else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 close(conn->socket);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 #endif
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 conn->socket = -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
613
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
403 conn->status = NN_CONN_CLOSED;
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
404 return 0;
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
405 }
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
406
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407
613
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
408 void nn_conn_free(nn_conn_t *conn)
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
409 {
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
410 if (conn != NULL)
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
411 {
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
412 nn_conn_close(conn);
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
413 th_free(conn->host);
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
414 th_free(conn->proxy.host);
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
415 th_free(conn);
2cd71b7c1e8e Repurpose nn_conn_close() to only close the socket, but not free the
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
416 }
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 BOOL nn_conn_send_buf(nn_conn_t *conn, const char *buf, const size_t len)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 size_t bufLeft = len;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 const char *bufPtr = buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 while (bufLeft > 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 ssize_t bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 bufSent = send(conn->socket, bufPtr, bufLeft, 0);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 if (bufSent < 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 nn_conn_err(conn, "nn_conn_send_buf() failed: %s", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 bufLeft -= bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 bufPtr += bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 void nn_conn_reset(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 if (conn != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 conn->ptr = conn->in_ptr = conn->buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 conn->got_bytes = conn->total_bytes = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 int nn_conn_pull(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 int result;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 struct timeval socktv;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 fd_set tmpfds;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 return -10;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460
438
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
461 // Shift the input buffer
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
462 if (conn->ptr > conn->buf)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 {
438
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
464 size_t left = conn->in_ptr - conn->ptr;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
465 if (left > 0)
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
466 {
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
467 size_t moved = conn->ptr - conn->buf;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
468 memmove(conn->buf, conn->ptr, left);
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
469 conn->ptr = conn->buf;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
470 conn->in_ptr -= moved;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
471 conn->total_bytes -= moved;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
472 }
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
473 else
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
474 nn_conn_reset(conn);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
477 // Check for incoming data
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 socktv.tv_sec = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 socktv.tv_usec = NN_DELAY_USEC;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 tmpfds = conn->sockfds;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 {
490
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
484 int err = nn_get_socket_errno();
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
485 if (err != EINTR)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 {
490
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
487 conn->err = err;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 nn_conn_err(conn, "Error occured in select(%d, sockfds): %d, %s\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 socket, conn->err, nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 else if (FD_ISSET(conn->socket, &tmpfds))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 conn->got_bytes = recv(conn->socket, conn->in_ptr, NN_CONNBUF_SIZE - conn->total_bytes, 0);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 if (conn->got_bytes < 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 else if (conn->got_bytes == 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 nn_conn_err(conn, "Server closed connection.\n");
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 conn->status = NN_CONN_CLOSED;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 return -3;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 conn->total_bytes += conn->got_bytes;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 conn->in_ptr += conn->got_bytes;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 return 1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 BOOL nn_conn_check(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 return conn->err == 0 && conn->status == NN_CONN_OPEN;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 BOOL nn_network_init(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 #ifdef __WIN32
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
532 // Initialize WinSock, if needed
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 WSADATA wsaData;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 int err = WSAStartup(0x0101, &wsaData);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 if (err != 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 THERR("Could not initialize WinSock library (err=%d).\n", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 #endif
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
541
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
542 nn_network_inited = TRUE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 void nn_network_close(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 {
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
549 if (nn_network_inited)
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
550 {
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 #ifdef __WIN32
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
552 WSACleanup();
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 #endif
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
554 }
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
555
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
556 nn_network_inited = FALSE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 BOOL nn_conn_buf_check(nn_conn_t *conn, size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 {
428
162011bbec48 Remove somewhat useless pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents: 423
diff changeset
562 return conn && (conn->ptr + n <= conn->in_ptr);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 BOOL nn_conn_buf_skip(nn_conn_t *conn, size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 if (nn_conn_buf_check(conn, n))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 conn->ptr += n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 int nn_conn_buf_strncmp(nn_conn_t *conn, const char *str, const size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 int ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 if (!nn_conn_buf_check(conn, n))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 if ((ret = strncmp(conn->ptr, str, n)) == 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 conn->ptr += n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 return ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 int nn_conn_buf_strcmp(nn_conn_t *conn, const char *str)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 return nn_conn_buf_strncmp(conn, str, strlen(str));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 char *nn_conn_buf_strstr(nn_conn_t *conn, const char *str)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 char *pos;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 size_t n = strlen(str);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 if (nn_conn_buf_check(conn, n) && ((pos = strstr(conn->ptr, str)) != NULL))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 conn->ptr = pos + n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 return pos;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611 return NULL;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
615 BOOL nn_conn_send_msg(nn_conn_t *conn, const char *user, const char *str)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 {
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
617 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
619 if (str == NULL)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 return FALSE;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
621
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
622 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 if (msg != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 th_free(msg);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 return ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 }
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
633
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
634
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
635 BOOL nn_conn_send_msg_v(nn_conn_t *conn, const char *user, const char *fmt, ...)
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
636 {
482
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
637 BOOL res;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
638 char *tmp;
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
639 va_list ap;
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
640
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
641 va_start(ap, fmt);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
642 tmp = th_strdup_vprintf(fmt, ap);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
643 va_end(ap);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
644
482
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
645 res = nn_conn_send_msg(conn, user, tmp);
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
646 th_free(tmp);
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
647 return res;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
648 }
434
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
649
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
650
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
651 void nn_conn_dump_buffer(FILE *f, nn_conn_t *conn)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
652 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
653 char *p;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
654 size_t offs, left;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
655
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
656 fprintf(f,
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
657 "\n--------------------------------------------------------------\n"
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
658 "err=%d, status=%d, got_bytes=%d, total_bytes=%d\n"
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
659 "buf=0x%p, in_ptr=0x%04x, ptr=0x%04x\n",
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
660 conn->err, conn->status, conn->got_bytes, conn->total_bytes,
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
661 conn->buf, conn->in_ptr - conn->buf, conn->ptr - conn->buf);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
662
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
663 // Dump buffer contents as a hexdump
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
664 for (offs = 0, left = conn->total_bytes, p = conn->buf; p < conn->in_ptr;)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
665 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
666 char buf[NN_DUMP_BYTES + 1];
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
667 size_t bufoffs, amount = left < NN_DUMP_BYTES ? left : NN_DUMP_BYTES;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
668 left -= amount;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
669
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
670 // Dump offs | xx xx xx xx | and fill string
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
671 fprintf(f, "%04x | ", offs);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
672 for (bufoffs = 0; bufoffs < amount; offs++, bufoffs++, p++)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
673 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
674 fprintf(f, "%02x ", *p);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
675 buf[bufoffs] = th_isprint(*p) ? *p : '.';
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
676 }
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
677 buf[bufoffs] = 0;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
678
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
679 // Add padding
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
680 for (; bufoffs < NN_DUMP_BYTES; bufoffs++)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
681 fprintf(f, " ");
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
682
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
683 // Print string
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
684 fprintf(f, "| %s\n", buf);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
685 }
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
686 }