Mercurial > hg > th-libs
annotate th_network.c @ 124:61d882a9758b
Oops, remove a lingering #endif.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 22 Jun 2014 02:49:17 +0300 |
parents | c8584cf52c98 |
children | 8efebf863404 |
rev | line source |
---|---|
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Simple TCP network connection handling |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2013-2014 Tecnic Software productions (TNSP) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_network.h" |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include <errno.h> |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 static BOOL th_network_inited = FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 static qlist_t *th_conn_list = NULL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 enum |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 { |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
19 SOCKS5_AUTH_NONE = 0, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
20 SOCKS5_AUTH_USER = 2, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 }; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
24 enum |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
25 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
26 SOCKS5_ADDR_IPV4 = 0x01, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
27 SOCKS5_ADDR_DOMAIN = 0x03, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
28 SOCKS5_ADDR_IPV6 = 0x04, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
29 }; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
30 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 static const char *th_proxy_types[] = |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 "none", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 "SOCKS 4", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 "SOCKS 4a", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 "SOCKS 5", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 NULL |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 }; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
41 static const char *th_socks5_results_msgs[] = |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
42 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
43 "Succeeded", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
44 "General SOCKS server failure", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
45 "Connection not allowed by ruleset", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
46 "Network unreachable", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
47 "Host unreachable", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
48 "Connection refused", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
49 "TTL expired", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
50 "Command not supported", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
51 "Address type not supported", |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
52 }; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
53 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
54 static const int th_socks5_results_msgs_num = sizeof(th_socks5_results_msgs) / sizeof(th_socks5_results_msgs[0]); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
55 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
56 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
57 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 static int th_get_socket_errno(void) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 return errno; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 void th_conn_err(th_conn_t *conn, int err, const char *fmt, ...) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 if (conn->errfunc != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 char *msg; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 va_list ap; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 va_start(ap, fmt); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 msg = th_strdup_vprintf(fmt, ap); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 va_end(ap); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 conn->errfunc(conn, err, msg); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 th_free(msg); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 void th_conn_msg(th_conn_t *conn, int loglevel, const char *fmt, ...) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 if (conn->msgfunc != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 char *msg; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 va_list ap; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 va_start(ap, fmt); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 msg = th_strdup_vprintf(fmt, ap); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 va_end(ap); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 conn->msgfunc(conn, loglevel, msg); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 th_free(msg); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 struct hostent *th_resolve_host(th_conn_t *conn, const char *name) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 struct hostent *res = gethostbyname(name); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 if (res == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 th_conn_err(conn, h_errno, |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 "Could not resolve hostname '%s': %s\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 name, th_error_str(h_errno)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 th_conn_msg(conn, THLOG_INFO, "True hostname for %s is %s\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 name, res->h_name); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 return res; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 th_conn_t * th_conn_new( |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 void (*errfunc)(th_conn_t *conn, int err, const char *msg), |
103
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
118 void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg), |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
119 ssize_t bufsize) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 th_conn_t *conn = th_malloc0(sizeof(th_conn_t)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 if (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 return NULL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
122
452adc41ccf5
Remember to free the connection data buffer.
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
126 // Set function pointers |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 conn->errfunc = errfunc; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 conn->msgfunc = msgfunc; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
122
452adc41ccf5
Remember to free the connection data buffer.
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
130 // Allocate connection data buffer |
103
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
131 conn->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize; |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
132 if ((conn->buf = th_malloc(conn->bufsize)) == NULL) |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
133 { |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
134 th_free(conn); |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
135 return NULL; |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
136 } |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
137 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 return conn; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 static BOOL th_get_addr(struct in_addr *addr, struct hostent *hst) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 if (hst != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 *addr = *(struct in_addr *) (hst->h_addr); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 return TRUE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 addr->s_addr = 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 int th_conn_set_proxy(th_conn_t *conn, int type, int port, const char *host, int auth_type) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 if (conn == NULL) |
104 | 160 return THERR_NULLPTR; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 conn->proxy.type = type; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 conn->proxy.port = port; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 conn->proxy.auth_type = auth_type; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 th_free(conn->proxy.host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 conn->proxy.host = th_strdup(host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 if (host != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 conn->proxy.hst = th_resolve_host(conn, host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 th_get_addr(&(conn->proxy.addr), conn->proxy.hst); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 else |
104 | 175 return THERR_INVALID_DATA; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 |
104 | 177 return THERR_OK; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
181 int th_conn_set_proxy_mode(th_conn_t *conn, const int mode) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
182 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
183 if (conn == NULL) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
184 return THERR_NULLPTR; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
185 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
186 conn->proxy.mode = mode; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
187 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
188 return THERR_OK; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
189 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
190 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
191 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
192 int th_conn_set_proxy_addr_type(th_conn_t *conn, const int atype) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
193 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
194 if (conn == NULL) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
195 return THERR_NULLPTR; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
196 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
197 conn->proxy.addr_type = atype; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
198 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
199 return THERR_OK; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
200 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
201 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
202 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 int th_conn_set_proxy_auth_user(th_conn_t *conn, const char *userid, const char *passwd) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 if (conn == NULL) |
104 | 206 return THERR_NULLPTR; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 th_free(conn->proxy.userid); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 conn->proxy.userid = th_strdup(userid); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 th_free(conn->proxy.passwd); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 conn->proxy.passwd = th_strdup(passwd); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 |
104 | 214 return THERR_OK; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
217 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
218 static int th_conn_proxy_wait(th_conn_t *conn) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 int status, tries; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 |
119 | 222 for (status = TH_CONN_NO_DATA, tries = 1; tries <= 20 && status != TH_CONN_DATA_AVAIL; tries++) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 Sleep(50); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 #else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 usleep(50000); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 th_conn_reset(conn); |
119 | 230 switch (status = th_conn_pull(conn)) |
231 { | |
232 case TH_CONN_ERROR: | |
233 th_conn_err(conn, status, "Proxy negotiation failed at try %d with network error: %s.\n", | |
234 tries, th_error_str(conn->err)); | |
235 break; | |
236 | |
237 case TH_CONN_DATA_AVAIL: | |
238 case TH_CONN_NO_DATA: | |
239 break; | |
240 | |
241 default: | |
242 return status; | |
243 } | |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
246 if (status != TH_CONN_DATA_AVAIL) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 th_conn_err(conn, THERR_TIMED_OUT, "Proxy negotiation timed out.\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
249 return status; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
253 static int th_conn_proxy_send(th_conn_t *conn, th_growbuf_t *buf) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 th_conn_reset(conn); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
256 return th_conn_send_growbuf(conn, buf); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 static int th_conn_socks4_negotiate(th_conn_t *conn, const int port, const char *host) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 { |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
262 th_growbuf_t buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
263 uint8_t *ptr; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
264 int cmd, err = THERR_INIT_FAIL; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 (void) host; |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
267 th_growbuf_init(&buf, 128); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 4/a proxy negotiation.\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
270 switch (conn->proxy.mode) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
271 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
272 case TH_PROXY_CMD_CONNECT: cmd = 1; break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
273 case TH_PROXY_CMD_BIND: cmd = 2; break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
274 default: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
275 err = THERR_NOT_SUPPORTED; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
276 th_conn_err(conn, err, "Invalid SOCKS 4 command/mode, unsupported.\n"); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
277 goto out; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
278 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
279 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
280 // Create SOCKS 4 handshake |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
281 th_growbuf_put_u8(&buf, 4); // Protocol version |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
282 th_growbuf_put_u8(&buf, cmd); // Command |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
283 th_growbuf_put_u16_be(&buf, port); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
284 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
285 switch (conn->proxy.addr_type) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
286 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
287 case TH_PROXY_ADDR_IPV4: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
288 th_growbuf_put_str(&buf, (uint8_t *) &(conn->addr.s_addr), sizeof(conn->addr.s_addr)); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
289 break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
290 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
291 case TH_PROXY_ADDR_DOMAIN: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
292 if (conn->proxy.type == TH_PROXY_SOCKS4A) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
293 th_growbuf_put_u32_be(&buf, 0x00000032); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
294 else |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
295 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
296 err = THERR_INIT_FAIL; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
297 th_conn_err(conn, err, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
298 "Invalid proxy settings, SOCKS 4 in use, but domain address type requested. SOCKS 4a or 5 required for host atype.\n"); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
299 goto out; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
300 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
301 break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
302 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
303 default: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
304 err = THERR_INIT_FAIL; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
305 th_conn_err(conn, err, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
306 "Invalid proxy settings for SOCKS 4, unsupported address type requrested.\n"); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
307 goto out; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
308 } |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
310 th_growbuf_puts(&buf, conn->proxy.userid, TRUE); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
311 if (conn->proxy.addr_type == TH_PROXY_ADDR_DOMAIN) |
123
c8584cf52c98
Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
122
diff
changeset
|
312 th_growbuf_puts(&buf, host, TRUE); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
313 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
315 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
316 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
317 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
319 if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
320 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
322 ptr = (uint8_t*) conn->buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
323 if (*ptr != 0) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
325 err = THERR_INIT_FAIL; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
326 th_conn_err(conn, err, |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
327 "Invalid SOCKS 4 server reply, does not begin with NUL byte (%d).\n", |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
328 *ptr); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
329 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 } |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
331 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
332 ptr++; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
333 if (*ptr != 0x5a) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 const char *s = NULL; |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
336 switch (*ptr) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 case 0x5b: s = "Request rejected or failed"; break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 case 0x5c: s = "Request failed because client is not running identd (or not reachable from the server)"; break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 default: s = "Unknown SOCKS 4 error response"; break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 } |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
343 |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
344 err = THERR_INIT_FAIL; |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
345 th_conn_err(conn, err, "SOCKS 4 setup failed, 0x%02x: %s.\n", *ptr, s); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
346 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
347 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
349 err = THERR_OK; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
350 |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
351 out: |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
352 th_growbuf_free(&buf); |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
353 return err; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
354 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
355 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
356 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
357 static int th_conn_socks5_negotiate(th_conn_t *conn, const int port, const char *host) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
358 { |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
359 th_growbuf_t buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
360 uint8_t *ptr; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
361 int cmd, avail, auth, err = THERR_INIT_FAIL; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
363 th_growbuf_init(&buf, 256); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 5 proxy negotiation.\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
366 switch (conn->proxy.mode) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
367 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
368 case TH_PROXY_CMD_CONNECT: cmd = 1; break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
369 case TH_PROXY_CMD_BIND: cmd = 2; break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
370 case TH_PROXY_CMD_ASSOC_UDP: cmd = 3; break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
371 default: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
372 err = THERR_NOT_SUPPORTED; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
373 th_conn_err(conn, err, "Invalid SOCKS 5 command/mode, unsupported.\n"); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
374 goto out; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
375 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
376 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
377 switch (conn->proxy.auth_type) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
378 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
379 case TH_PROXY_AUTH_NONE: |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
380 avail = 1; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
381 auth = SOCKS5_AUTH_NONE; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
384 case TH_PROXY_AUTH_USER: |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 avail = 2; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
386 auth = SOCKS5_AUTH_USER; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 if (conn->proxy.userid == NULL || conn->proxy.passwd == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
388 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
389 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
390 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
391 "SOCKS 5 user authentication chosen, but no user/pass set.\n"); |
112 | 392 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
393 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
395 if (strlen(conn->proxy.userid) > 255 || |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
396 strlen(conn->proxy.passwd) > 255) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
398 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
399 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
400 "SOCKS 5 proxy userid or password is too long.\n"); |
112 | 401 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
402 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
403 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
404 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
405 default: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
406 err = THERR_NOT_SUPPORTED; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
407 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 "Unsupported proxy authentication method %d.\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
409 conn->proxy.auth_type); |
112 | 410 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
413 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
414 // Form handshake packet |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
415 th_growbuf_clear(&buf); |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
416 th_growbuf_put_u8(&buf, 0x05); // Protocol version |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
417 th_growbuf_put_u8(&buf, avail); // # of available auth methods |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
418 th_growbuf_put_u8(&buf, auth); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
420 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
421 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 422 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
425 if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
426 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
427 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
428 ptr = (uint8_t *) conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
429 if (*ptr != 0x05) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
431 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
432 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); |
112 | 434 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
435 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
436 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
437 auth = *ptr; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
439 if (auth == 0xff) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
440 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
441 err = THERR_NOT_SUPPORTED; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
442 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 "No authentication method could be negotiated with the server.\n"); |
112 | 444 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
445 } |
114
b44a0308b53f
Add missing else statement.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
446 else |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
447 if (auth == SOCKS5_AUTH_USER) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
449 // Attempt user/pass authentication (RFC 1929) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
450 th_growbuf_clear(&buf); |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
451 |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
452 th_growbuf_put_u8(&buf, 0x01); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
453 th_growbuf_put_u8(&buf, strlen(conn->proxy.userid)); |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
454 th_growbuf_puts(&buf, conn->proxy.userid, FALSE); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
455 th_growbuf_put_u8(&buf, strlen(conn->proxy.passwd)); |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
456 th_growbuf_puts(&buf, conn->proxy.passwd, FALSE); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
457 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
458 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
459 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 460 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
461 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
462 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
463 if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
464 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
465 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
466 ptr = (uint8_t *) conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 if (*ptr != 0x01) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
468 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
469 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
470 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
471 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); |
112 | 472 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
473 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
474 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
475 if (*ptr != 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
476 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
477 err = THERR_AUTH_FAILED; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
478 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
479 "SOCKS 5 proxy user/pass authentication failed! Code %d.\n", *ptr); |
112 | 480 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
481 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
482 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
483 else |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
484 if (auth != SOCKS5_AUTH_NONE) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
485 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
486 err = THERR_NOT_SUPPORTED; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
487 th_conn_err(conn, err, |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
488 "Proxy server chose an unsupported SOCKS 5 authentication method %d.\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
489 auth); |
112 | 490 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
491 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
492 |
123
c8584cf52c98
Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
122
diff
changeset
|
493 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
494 // Form client connection request packet |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
495 th_growbuf_clear(&buf); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
496 th_growbuf_put_u8(&buf, 0x05); // Protocol version |
123
c8584cf52c98
Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
122
diff
changeset
|
497 th_growbuf_put_u8(&buf, cmd); // Command |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
498 th_growbuf_put_u8(&buf, 0x00); // Reserved |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
500 switch (conn->proxy.addr_type) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
501 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
502 case TH_PROXY_ADDR_IPV4: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
503 th_growbuf_put_u8(&buf, SOCKS5_ADDR_IPV4); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
504 th_growbuf_put_str(&buf, (uint8_t *) &(conn->addr.s_addr), sizeof(conn->addr.s_addr)); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
505 break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
506 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
507 case TH_PROXY_ADDR_IPV6: |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
508 th_growbuf_put_u8(&buf, SOCKS5_ADDR_IPV6); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
509 //th_growbuf_put_str(&buf, (uint8_t *) &(conn->addr.s_addr), sizeof(conn->addr.s_addr)); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
510 break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
511 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
512 case TH_PROXY_ADDR_DOMAIN: |
123
c8584cf52c98
Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
122
diff
changeset
|
513 cmd = strlen(host); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
514 if (cmd < 1 || cmd > 255) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
515 { |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
516 err = THERR_NOT_SUPPORTED; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
517 th_conn_err(conn, err, |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
518 "Domain address type requested, but domain name longer than 255 characters (%d).\n", cmd); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
519 goto out; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
520 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
521 |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
522 th_growbuf_put_u8(&buf, SOCKS5_ADDR_DOMAIN); |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
523 th_growbuf_put_u8(&buf, cmd); |
123
c8584cf52c98
Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
122
diff
changeset
|
524 th_growbuf_put_str(&buf, host, cmd); |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
525 break; |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
526 } |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
527 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
528 th_growbuf_put_u16_be(&buf, port); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
530 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
531 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 532 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
533 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
535 if (th_conn_proxy_wait(conn) != TH_CONN_DATA_AVAIL) |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
536 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
537 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
538 ptr = (uint8_t *) conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
539 if (*ptr != 0x05) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 { |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
541 err = THERR_INVALID_DATA; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
542 th_conn_err(conn, err, |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
543 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); |
112 | 544 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
546 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 if (*ptr != 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
548 { |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
549 err = THERR_INIT_FAIL; |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
550 if (*ptr < th_socks5_results_msgs_num) |
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
551 th_conn_err(conn, err, "SOCKS 5 error: %s.\n", th_socks5_results_msgs[*ptr]); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
552 else |
121
8c39e8e90dbd
More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
119
diff
changeset
|
553 th_conn_err(conn, err, "Unknown SOCKS 5 result code 0x02x.\n", *ptr); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
554 goto out; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
555 } |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
556 ptr++; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
557 if (*ptr != 0) |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
558 { |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
559 err = THERR_INIT_FAIL; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
560 th_conn_err(conn, err, "wot.\n"); |
112 | 561 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
562 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
563 |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
564 err = THERR_OK; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
565 |
112 | 566 out: |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
567 th_growbuf_free(&buf); |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
568 return err; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
569 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
570 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
571 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
572 int th_conn_open(th_conn_t *conn, const int port, const char *host) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
573 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
574 struct sockaddr_in dest; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
575 int err = THERR_INIT_FAIL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
576 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
577 if (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
578 return THERR_NULLPTR; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
579 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
580 conn->port = port; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
581 if (host != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
582 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
583 conn->host = th_strdup(host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
584 conn->hst = th_resolve_host(conn, host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
585 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
586 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
587 th_get_addr(&(conn->addr), conn->hst); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
588 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
589 // Prepare for connection |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
590 dest.sin_family = AF_INET; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
591 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
592 if (conn->proxy.type > TH_PROXY_NONE && conn->proxy.type < TH_PROXY_LAST) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
593 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
594 // If using a proxy, we connect to the proxy server |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
595 dest.sin_port = htons(conn->proxy.port); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
596 dest.sin_addr = conn->proxy.addr; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
597 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
598 th_conn_msg(conn, THLOG_INFO, "Connecting to %s proxy %s:%d ...\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
599 th_proxy_types[conn->proxy.type], |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
600 inet_ntoa(conn->proxy.addr), conn->proxy.port); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
601 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
602 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
603 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
604 dest.sin_port = htons(conn->port); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
605 dest.sin_addr = conn->addr; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
606 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
607 th_conn_msg(conn, THLOG_INFO, "Connecting to %s:%d ...\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
608 inet_ntoa(conn->addr), conn->port); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
609 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
610 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
611 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
612 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
613 err = th_errno_to_error(th_get_socket_errno()); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
614 th_conn_err(conn, err, "Could not open socket: %s\n", th_error_str(err)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
615 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
616 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
617 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
618 if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
619 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
620 err = th_errno_to_error(th_get_socket_errno()); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
621 th_conn_err(conn, err, "Could not connect: %s\n", th_error_str(err)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
622 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
623 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
624 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
625 FD_ZERO(&(conn->sockfds)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
626 FD_SET(conn->socket, &(conn->sockfds)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
627 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
628 // Proxy-specific setup |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
629 switch (conn->proxy.type) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
630 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
631 case TH_PROXY_SOCKS4: |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
632 case TH_PROXY_SOCKS4A: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
633 if ((err = th_conn_socks4_negotiate(conn, port, host)) != THERR_OK) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
634 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
635 th_conn_msg(conn, THLOG_INFO, "SOCKS 4 connection established!\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
636 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
637 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
638 case TH_PROXY_SOCKS5: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
639 if ((err = th_conn_socks5_negotiate(conn, port, host)) != THERR_OK) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
640 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
641 th_conn_msg(conn, THLOG_INFO, "SOCKS 5 connection established!\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
642 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
643 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
644 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
645 th_conn_reset(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
646 conn->status = TH_CONN_OPEN; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
647 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
648 // Insert to connection list |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
649 conn->node = th_llist_append(&th_conn_list, conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
650 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
651 return THERR_OK; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
652 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
653 error: |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
654 th_conn_close(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
655 return err; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
656 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
657 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
658 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
659 int th_conn_close(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
660 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
661 if (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
662 return -1; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
663 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
664 if (conn->socket >= 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
665 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
666 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
667 closesocket(conn->socket); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
668 #else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
669 close(conn->socket); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
670 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
671 conn->socket = -1; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
672 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
673 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
674 conn->status = TH_CONN_CLOSED; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
675 return 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
676 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
677 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
678 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
679 static void th_conn_free_nodelete(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
680 { |
122
452adc41ccf5
Remember to free the connection data buffer.
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
681 th_free(conn->buf); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
682 th_conn_close(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
683 th_free(conn->host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
684 th_free(conn->proxy.host); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
685 th_free(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
686 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
687 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
688 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
689 void th_conn_free(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
690 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
691 if (conn != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
692 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
693 // Remove from linked list |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
694 if (conn->node != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
695 th_llist_delete_node_fast(&th_conn_list, conn->node); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
696 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
697 // Free connection data |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
698 th_conn_free_nodelete(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
699 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
700 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
701 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
702 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
703 int th_conn_send_buf(th_conn_t *conn, const void *buf, const size_t len) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
704 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
705 size_t bufLeft = len; |
118 | 706 const char *bufPtr = (char *) buf; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
707 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
708 while (bufLeft > 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
709 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
710 ssize_t bufSent; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
711 bufSent = send(conn->socket, bufPtr, bufLeft, 0); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
712 if (bufSent < 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
713 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
714 int err = th_errno_to_error(th_get_socket_errno()); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
715 th_conn_err(conn, err, "th_conn_send_buf() failed: %s", th_error_str(err)); |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
716 return err; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
717 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
718 bufLeft -= bufSent; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
719 bufPtr += bufSent; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
720 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
721 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
722 return THERR_OK; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
723 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
724 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
725 |
117
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
726 int th_conn_send_growbuf(th_conn_t *conn, th_growbuf_t *buf) |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
727 { |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
728 int ret = th_conn_send_buf(conn, buf->data, buf->len); |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
729 th_growbuf_clear(buf); |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
730 return ret; |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
731 } |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
732 |
bfae4758fa6f
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
733 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
734 void th_conn_reset(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
735 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
736 if (conn != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
737 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
738 conn->ptr = conn->in_ptr = conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
739 conn->got_bytes = conn->total_bytes = 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
740 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
741 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
742 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
743 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
744 int th_conn_pull(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
745 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
746 int result; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
747 struct timeval socktv; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
748 fd_set tmpfds; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
749 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
750 if (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
751 return TH_CONN_ERROR; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
752 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
753 // Shift the input buffer |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
754 if (conn->ptr > conn->buf) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
755 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
756 size_t left = conn->in_ptr - conn->ptr; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
757 if (left > 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
758 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
759 size_t moved = conn->ptr - conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
760 memmove(conn->buf, conn->ptr, left); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
761 conn->ptr = conn->buf; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
762 conn->in_ptr -= moved; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
763 conn->total_bytes -= moved; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
764 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
765 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
766 th_conn_reset(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
767 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
768 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
769 // Check for incoming data |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
770 socktv.tv_sec = 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
771 socktv.tv_usec = TH_DELAY_USEC; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
772 tmpfds = conn->sockfds; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
773 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
774 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
775 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
776 int err = th_get_socket_errno(); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
777 if (err != EINTR) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
778 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
779 err = th_errno_to_error(err); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
780 th_conn_err(conn, err, "Error occured in select(%d, sockfds): %s\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
781 socket, th_error_str(err)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
782 return TH_CONN_ERROR; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
783 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
784 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
785 else if (FD_ISSET(conn->socket, &tmpfds)) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
786 { |
103
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
787 conn->got_bytes = recv(conn->socket, conn->in_ptr, conn->bufsize - conn->total_bytes, 0); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
788 if (conn->got_bytes < 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
789 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
790 int err = th_errno_to_error(th_get_socket_errno()); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
791 th_conn_err(conn, err, "Error in recv: %s\n", th_error_str(err)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
792 return TH_CONN_ERROR; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
793 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
794 else if (conn->got_bytes == 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
795 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
796 th_conn_err(conn, ECONNABORTED, "Server closed connection.\n"); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
797 conn->status = TH_CONN_CLOSED; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
798 return TH_CONN_CLOSED; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
799 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
800 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
801 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
802 conn->total_bytes += conn->got_bytes; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
803 conn->in_ptr += conn->got_bytes; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
804 return TH_CONN_DATA_AVAIL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
805 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
806 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
807 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
808 return TH_CONN_NO_DATA; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
809 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
810 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
811 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
812 BOOL th_conn_check(th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
813 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
814 if (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
815 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
816 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
817 return conn->err == 0 && conn->status == TH_CONN_OPEN; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
818 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
819 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
820 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
821 int th_network_init(void) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
822 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
823 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
824 // Initialize WinSock, if needed |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
825 WSADATA wsaData; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
826 int err = WSAStartup(0x0101, &wsaData); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
827 if (err != 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
828 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
829 THERR("Could not initialize WinSock library (err=%d).\n", err); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
830 return THERR_INIT_FAIL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
831 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
832 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
833 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
834 th_network_inited = TRUE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
835 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
836 th_conn_list = NULL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
837 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
838 return THERR_OK; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
839 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
840 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
841 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
842 void th_network_close(void) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
843 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
844 if (th_network_inited) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
845 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
846 // Close connections |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
847 qlist_t *curr = th_conn_list; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
848 while (curr != NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
849 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
850 qlist_t *next = curr->next; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
851 th_conn_free_nodelete(curr->data); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
852 curr = next; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
853 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
854 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
855 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
856 WSACleanup(); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
857 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
858 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
859 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
860 th_network_inited = FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
861 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
862 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
863 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
864 BOOL th_conn_buf_check(th_conn_t *conn, size_t n) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
865 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
866 return conn && (conn->ptr + n <= conn->in_ptr); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
867 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
868 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
869 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
870 BOOL th_conn_buf_skip(th_conn_t *conn, size_t n) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
871 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
872 if (th_conn_buf_check(conn, n)) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
873 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
874 conn->ptr += n; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
875 return TRUE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
876 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
877 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
878 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
879 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
880 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
881 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
882 int th_conn_buf_strncmp(th_conn_t *conn, const char *str, const size_t n) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
883 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
884 int ret; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
885 if (!th_conn_buf_check(conn, n)) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
886 return -1; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
887 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
888 if ((ret = strncmp(conn->ptr, str, n)) == 0) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
889 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
890 conn->ptr += n; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
891 return 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
892 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
893 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
894 return ret; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
895 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
896 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
897 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
898 int th_conn_buf_strcmp(th_conn_t *conn, const char *str) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
899 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
900 return th_conn_buf_strncmp(conn, str, strlen(str)); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
901 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
902 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
903 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
904 char *th_conn_buf_strstr(th_conn_t *conn, const char *str) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
905 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
906 char *pos; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
907 size_t n = strlen(str); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
908 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
909 if (th_conn_buf_check(conn, n) && ((pos = strstr(conn->ptr, str)) != NULL)) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
910 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
911 conn->ptr = pos + n; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
912 return pos; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
913 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
914 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
915 return NULL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
916 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
917 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
918 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
919 void th_conn_dump_buffer(FILE *f, th_conn_t *conn) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
920 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
921 char *p; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
922 size_t offs, left; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
923 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
924 fprintf(f, |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
925 "\n--------------------------------------------------------------\n" |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
926 "err=%d, status=%d, got_bytes=%d, total_bytes=%d\n" |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
927 "buf=0x%p, in_ptr=0x%04x, ptr=0x%04x\n", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
928 conn->err, conn->status, conn->got_bytes, conn->total_bytes, |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
929 conn->buf, conn->in_ptr - conn->buf, conn->ptr - conn->buf); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
930 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
931 // Dump buffer contents as a hexdump |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
932 for (offs = 0, left = conn->total_bytes, p = conn->buf; p < conn->in_ptr;) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
933 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
934 char buf[TH_DUMP_BYTES + 1]; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
935 size_t bufoffs, amount = left < TH_DUMP_BYTES ? left : TH_DUMP_BYTES; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
936 left -= amount; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
937 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
938 // Dump offs | xx xx xx xx | and fill string |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
939 fprintf(f, "%04x | ", offs); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
940 for (bufoffs = 0; bufoffs < amount; offs++, bufoffs++, p++) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
941 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
942 fprintf(f, "%02x ", *p); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
943 buf[bufoffs] = th_isprint(*p) ? *p : '.'; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
944 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
945 buf[bufoffs] = 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
946 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
947 // Add padding |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
948 for (; bufoffs < TH_DUMP_BYTES; bufoffs++) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
949 fprintf(f, " "); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
950 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
951 // Print string |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
952 fprintf(f, "| %s\n", buf); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
953 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
954 } |