annotate th_network.c @ 722:4ca6a3b30fe8

Bump copyright years.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Jan 2021 11:35:54 +0200
parents 9c61834c191b
children 29e44a58bc73
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
722
4ca6a3b30fe8 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 689
diff changeset
4 * (C) Copyright 2013-2021 Tecnic Software productions (TNSP)
102
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
13 static BOOL th_network_inited = FALSE;
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 150
diff changeset
14 static th_llist_t *th_conn_list = NULL;
102
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 {
189
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
diff changeset
19 SOCKS5_AUTH_NONE = 0,
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
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 {
189
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
diff changeset
26 SOCKS5_ADDR_IPV4 = 0x01,
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
diff changeset
27 SOCKS5_ADDR_DOMAIN = 0x03,
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
diff changeset
28 SOCKS5_ADDR_IPV6 = 0x04,
121
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 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
102 int err = th_errno_to_error(h_errno);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
103 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
104 "Could not resolve hostname '%s': %s",
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
105 name, th_error_str(err));
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 else
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 {
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
109 th_conn_msg(conn, THLOG_INFO, "True hostname for %s is %s",
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 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
111 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 return res;
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
117 BOOL th_base_conn_init(th_base_conn_t *base, ssize_t bufsize)
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
118 {
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
119 // Allocate connection data buffer
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
120 base->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
121 if ((base->buf = th_malloc(base->bufsize)) == NULL)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
122 return FALSE;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
123
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
124 return TRUE;
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
125 }
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
126
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
127
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 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
129 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
130 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
131 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
132 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 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
134
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 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
136 return NULL;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
122
452adc41ccf5 Remember to free the connection data buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 121
diff changeset
138 // 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
139 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
140 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
141
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
142 // Do base initialization
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
143 if (!th_base_conn_init(&conn->base, bufsize))
103
f7bec3f7181d Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
144 {
f7bec3f7181d Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
145 th_free(conn);
f7bec3f7181d Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
146 return NULL;
f7bec3f7181d Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
147 }
f7bec3f7181d Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
148
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 return conn;
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
153 static BOOL th_get_addr(struct in_addr *addr, struct hostent *hst)
102
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 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
156 {
147
0a085e2e8bf5 Portability fix.
Matti Hamalainen <ccr@tnsp.org>
parents: 146
diff changeset
157 *addr = *(struct in_addr *) (hst->h_addr_list[0]);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
158 return TRUE;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 else
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 addr->s_addr = 0;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
163 return FALSE;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 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
169 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 if (conn == NULL)
104
ec5a5e573885 Return real error values.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
171 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
172
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 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
174 conn->proxy.auth_type = auth_type;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
175
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
176 conn->proxy.conn.port = port;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
177 th_free(conn->proxy.conn.host);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
178 conn->proxy.conn.host = th_strdup(host);
102
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 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
181 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
182 conn->proxy.conn.hst = th_resolve_host(conn, host);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
183 th_get_addr(&(conn->proxy.conn.addr), conn->proxy.conn.hst);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 else
104
ec5a5e573885 Return real error values.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
186 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
187
104
ec5a5e573885 Return real error values.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
188 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
189 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
192 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
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.mode = mode;
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
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
203 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
204 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
205 if (conn == NULL)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
206 return THERR_NULLPTR;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
207
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
208 conn->proxy.addr_type = atype;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
209
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
210 return THERR_OK;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
211 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
212
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
213
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 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
215 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 if (conn == NULL)
104
ec5a5e573885 Return real error values.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
217 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
218
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 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
220 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
221
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 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
223 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
224
104
ec5a5e573885 Return real error values.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
225 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
226 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
229 static int th_conn_proxy_wait(th_conn_t *conn, const ssize_t want)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 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
232
119
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
233 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
234 {
191
cc5411cb85de Use TH_PLAT_WINDOWS define for #ifdef logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 190
diff changeset
235 #ifdef TH_PLAT_WINDOWS
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
236 Sleep(100);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 #else
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
238 usleep(100000);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 #endif
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 th_conn_reset(conn);
119
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
241 switch (status = th_conn_pull(conn))
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
242 {
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
243 case TH_CONN_ERROR:
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
244 th_conn_err(conn, status, "Proxy negotiation failed at try %d with network error: %s",
119
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
245 tries, th_error_str(conn->err));
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
246 break;
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
247
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
248 case TH_CONN_DATA_AVAIL:
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
249 case TH_CONN_NO_DATA:
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
250 if (conn->base.total_bytes < want)
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
251 status = TH_CONN_NO_DATA;
119
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
252 break;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
253
119
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
254 default:
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
255 return status;
38a39a9e3a1d Fix proxy negotiation.
Matti Hamalainen <ccr@tnsp.org>
parents: 118
diff changeset
256 }
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
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
259 if (status != TH_CONN_DATA_AVAIL)
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
260 th_conn_err(conn, THERR_TIMED_OUT, "Proxy negotiation timed out.");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
262 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
263 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
266 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
267 {
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_reset(conn);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
269 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
270 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 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
274 {
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
275 th_growbuf_t buf;
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
276 uint8_t *ptr;
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
277 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
278
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 (void) host;
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
280 th_growbuf_init(&buf, 128);
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
281 th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 4/a proxy negotiation.");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
283 switch (conn->proxy.mode)
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 case TH_PROXY_CMD_CONNECT: cmd = 1; break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
286 case TH_PROXY_CMD_BIND: cmd = 2; break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
287 default:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
288 err = THERR_NOT_SUPPORTED;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
289 th_conn_err(conn, err, "Invalid SOCKS 4 command/mode, unsupported.");
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
290 goto out;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
291 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
292
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
293 // Create SOCKS 4 handshake
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
294 th_growbuf_put_u8(&buf, 4); // Protocol version
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
295 th_growbuf_put_u8(&buf, cmd); // Command
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
296 th_growbuf_put_u16_be(&buf, port);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
297
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
298 switch (conn->proxy.addr_type)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
299 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
300 case TH_PROXY_ADDR_IPV4:
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
301 th_growbuf_put_str(&buf, (uint8_t *) &(conn->base.addr.s_addr), sizeof(conn->base.addr.s_addr));
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
302 break;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
303
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
304 case TH_PROXY_ADDR_DOMAIN:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
305 if (conn->proxy.type == TH_PROXY_SOCKS4A)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
306 th_growbuf_put_u32_be(&buf, 0x00000032);
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
307 else
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
308 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
309 err = THERR_INIT_FAIL;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
310 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
311 "Invalid proxy settings, SOCKS 4 in use, but domain "
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
312 "address type requested. SOCKS 4a or 5 required for host atype.");
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
313 goto out;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
314 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
315 break;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
316
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
317 default:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
318 err = THERR_INIT_FAIL;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
319 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
320 "Invalid proxy settings for SOCKS 4, unsupported address type requested.");
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
321 goto out;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
322 }
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
324 th_growbuf_puts(&buf, conn->proxy.userid, TRUE);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
325 if (conn->proxy.addr_type == TH_PROXY_ADDR_DOMAIN)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
326 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
327
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 // Send request
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
329 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
330 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
331
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 // Wait for SOCKS server to reply
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
333 if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL)
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
334 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
335
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
336 ptr = (uint8_t*) conn->base.buf;
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
337 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
338 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
339 err = THERR_INIT_FAIL;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
340 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
341 "Invalid SOCKS 4 server reply, does not begin with NUL byte (0x%02x).",
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
342 *ptr);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
343 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
344 }
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
345
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
346 ptr++;
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
347 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
348 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 const char *s = NULL;
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
350 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
351 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 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
353 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
354 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
355 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
356 }
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
357
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
358 err = THERR_INIT_FAIL;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
359 th_conn_err(conn, err, "SOCKS 4 setup failed, 0x%02x: %s", *ptr, s);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
360 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
361 }
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 err = THERR_OK;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
364
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
365 out:
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
366 th_growbuf_free(&buf);
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
367 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
368 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 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
372 {
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
373 th_growbuf_t buf;
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
374 uint8_t *ptr, authlist[16];
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
375 int i, 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
376
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
377 th_growbuf_init(&buf, 256);
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
378 th_conn_msg(conn, THLOG_INFO, "Initializing SOCKS 5 proxy negotiation.");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
380 switch (conn->proxy.mode)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
381 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
382 case TH_PROXY_CMD_CONNECT: cmd = 1; break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
383 case TH_PROXY_CMD_BIND: cmd = 2; break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
384 case TH_PROXY_CMD_ASSOC_UDP: cmd = 3; break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
385 default:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
386 err = THERR_NOT_SUPPORTED;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
387 th_conn_err(conn, err, "Invalid SOCKS 5 command/mode, unsupported.");
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
388 goto out;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
389 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
390
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
391 avail = 0;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 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
393 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 case TH_PROXY_AUTH_USER:
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
395 authlist[avail++] = 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
396 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
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,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
400 "SOCKS 5 user authentication chosen, but no user/pass set.");
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
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
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
404 if (strlen(conn->proxy.userid) > 255 ||
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
405 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
406 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
407 err = THERR_INVALID_DATA;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
408 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
409 "SOCKS 5 proxy userid or password is too long.");
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
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 }
440
d19bd91842c7 Silence a GCC 7+ warning about switch case fallthrough.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
412 // Intentionally fallthrough
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
413
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
414 case TH_PROXY_AUTH_NONE:
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
415 authlist[avail++] = 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
416 break;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 default:
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
419 err = THERR_NOT_SUPPORTED;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
420 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
421 "Unsupported proxy authentication method %d.",
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 conn->proxy.auth_type);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
423 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
424 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 // Form handshake packet
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
427 th_growbuf_clear(&buf);
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
428 th_growbuf_put_u8(&buf, 0x05); // Protocol version
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
429 th_growbuf_put_u8(&buf, avail); // # of available auth methods
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
430 for (i = 0; i < avail; i++)
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
431 th_growbuf_put_u8(&buf, authlist[i]);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 // Send request
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
434 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK)
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
435 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
436
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 // Wait for SOCKS server to reply
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
438 if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL)
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
439 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
440
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
441 ptr = (uint8_t *) conn->base.buf;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 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
443 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
444 err = THERR_INVALID_DATA;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
445 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
446 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).", *ptr);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
447 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
448 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 ptr++;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 auth = *ptr;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 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
453 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
454 err = THERR_NOT_SUPPORTED;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
455 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
456 "No authentication method could be negotiated with the server.");
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
457 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
458 }
114
b44a0308b53f Add missing else statement.
Matti Hamalainen <ccr@tnsp.org>
parents: 113
diff changeset
459 else
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
460 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
461 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 // Attempt user/pass authentication (RFC 1929)
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
463 th_conn_msg(conn, THLOG_INFO, "Attempting SOCKS 5 user/pass authentication.");
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
464 th_growbuf_clear(&buf);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
465
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
466 th_growbuf_put_u8(&buf, 0x01);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
467 th_growbuf_put_u8(&buf, strlen(conn->proxy.userid));
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
468 th_growbuf_puts(&buf, conn->proxy.userid, FALSE);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
469 th_growbuf_put_u8(&buf, strlen(conn->proxy.passwd));
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
470 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
471
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 // Send request
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
473 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK)
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
474 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
475
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 // Wait for SOCKS server to reply
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
477 if (th_conn_proxy_wait(conn, 2) != TH_CONN_DATA_AVAIL)
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
478 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
479
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
480 ptr = (uint8_t *) conn->base.buf;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 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
482 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
483 err = THERR_INVALID_DATA;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
484 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
485 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (0x%02x).", *ptr);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
486 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
487 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 ptr++;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 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
490 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
491 err = THERR_AUTH_FAILED;
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
492 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
493 "SOCKS 5 proxy user/pass authentication failed! Code 0x%02x.", *ptr);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
494 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
495 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 else
130
32dcabfc2644 Add some informative messages to SOCKS 5 authentication steps.
Matti Hamalainen <ccr@tnsp.org>
parents: 126
diff changeset
498 if (auth == SOCKS5_AUTH_NONE)
32dcabfc2644 Add some informative messages to SOCKS 5 authentication steps.
Matti Hamalainen <ccr@tnsp.org>
parents: 126
diff changeset
499 {
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
500 th_conn_msg(conn, THLOG_INFO, "Using no authentication for SOCKS 5.");
130
32dcabfc2644 Add some informative messages to SOCKS 5 authentication steps.
Matti Hamalainen <ccr@tnsp.org>
parents: 126
diff changeset
501 }
32dcabfc2644 Add some informative messages to SOCKS 5 authentication steps.
Matti Hamalainen <ccr@tnsp.org>
parents: 126
diff changeset
502 else
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 {
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
504 err = THERR_NOT_SUPPORTED;
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
505 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
506 "Proxy server chose an unsupported SOCKS 5 authentication method 0x%02x.",
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 auth);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
508 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
509 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
123
c8584cf52c98 Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 122
diff changeset
511
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 // Form client connection request packet
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
513 th_growbuf_clear(&buf);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
514 th_growbuf_put_u8(&buf, 0x05); // Protocol version
123
c8584cf52c98 Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 122
diff changeset
515 th_growbuf_put_u8(&buf, cmd); // Command
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
516 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
517
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
518 switch (conn->proxy.addr_type)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
519 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
520 case TH_PROXY_ADDR_IPV4:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
521 th_growbuf_put_u8(&buf, SOCKS5_ADDR_IPV4);
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
522 th_growbuf_put_str(&buf, (uint8_t *) &(conn->base.addr.s_addr), sizeof(conn->base.addr.s_addr));
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
523 break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
524
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
525 case TH_PROXY_ADDR_IPV6:
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
526 th_growbuf_put_u8(&buf, SOCKS5_ADDR_IPV6);
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
527 //th_growbuf_put_str(&buf, (uint8_t *) &(conn->base.addr.s_addr), sizeof(conn->base.addr.s_addr));
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
528 break;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
529
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
530 case TH_PROXY_ADDR_DOMAIN:
123
c8584cf52c98 Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 122
diff changeset
531 cmd = strlen(host);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
532 if (cmd < 1 || cmd > 255)
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
533 {
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
534 err = THERR_NOT_SUPPORTED;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
535 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
536 "Domain address type requested, but domain name longer than 255 characters (%d).", cmd);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
537 goto out;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
538 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
539
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
540 th_growbuf_put_u8(&buf, SOCKS5_ADDR_DOMAIN);
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
541 th_growbuf_put_u8(&buf, cmd);
123
c8584cf52c98 Various bugfixes in proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 122
diff changeset
542 th_growbuf_put_str(&buf, host, cmd);
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
543 break;
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
544 }
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
545
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
546 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
547
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 // Send request
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
549 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK)
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
550 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
551
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 // Wait for SOCKS server to reply
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
553 if (th_conn_proxy_wait(conn, 3) != TH_CONN_DATA_AVAIL)
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
554 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
555
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
556 ptr = (uint8_t *) conn->base.buf;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 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
558 {
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
559 err = THERR_INVALID_DATA;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
560 th_conn_err(conn, err,
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
561 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).", *ptr);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
562 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
563 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 ptr++;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 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
566 {
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
567 err = THERR_INIT_FAIL;
121
8c39e8e90dbd More work on proxy handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 119
diff changeset
568 if (*ptr < th_socks5_results_msgs_num)
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
569 th_conn_err(conn, err, "SOCKS 5 error: %s.", th_socks5_results_msgs[*ptr]);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
570 else
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
571 th_conn_err(conn, err, "Unknown SOCKS 5 result code 0x%02x.", *ptr);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
572 goto out;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
573 }
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
574 ptr++;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
575 if (*ptr != 0)
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
576 {
131
6624893ad528 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 130
diff changeset
577 err = THERR_INVALID_DATA;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
578 th_conn_err(conn, err, "Invalid reply from SOCKS 5 server: expected 0x00, got 0x%02x.", *ptr);
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
579 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
580 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
582 err = THERR_OK;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
583
112
5403f0d2d692 Use common exit codepath.
Matti Hamalainen <ccr@tnsp.org>
parents: 105
diff changeset
584 out:
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
585 th_growbuf_free(&buf);
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
586 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
587 }
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 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
591 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 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
593 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
594
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 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
596 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
597
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
598 conn->base.port = port;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
599 conn->base.host = th_strdup(host);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
600 conn->base.hst = th_resolve_host(conn, host);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
601
126
8efebf863404 Fix host handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
602 // If name resolving locally fails, force to domain addr type
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
603 if (conn->base.hst == NULL)
126
8efebf863404 Fix host handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 124
diff changeset
604 conn->proxy.addr_type = TH_PROXY_ADDR_DOMAIN;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
606 th_get_addr(&(conn->base.addr), conn->base.hst);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 // 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
609 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
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->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
612 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 // If using a proxy, we connect to the proxy server
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
614 dest.sin_port = htons(conn->proxy.conn.port);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
615 dest.sin_addr = conn->proxy.conn.addr;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
617 th_conn_msg(conn, THLOG_INFO, "Connecting to %s proxy %s:%d ...",
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 th_proxy_types[conn->proxy.type],
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
619 inet_ntoa(conn->proxy.conn.addr), conn->proxy.conn.port);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 else
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
623 dest.sin_port = htons(conn->base.port);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
624 dest.sin_addr = conn->base.addr;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
626 th_conn_msg(conn, THLOG_INFO, "Connecting to %s:%d ...",
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
627 inet_ntoa(conn->base.addr), conn->base.port);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
630 if ((conn->base.socket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 err = th_errno_to_error(th_get_socket_errno());
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
633 th_conn_err(conn, err, "Could not open socket: %s", th_error_str(err));
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 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
637 if (connect(conn->base.socket, (struct sockaddr *) &dest, sizeof(dest)) == -1)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 err = th_errno_to_error(th_get_socket_errno());
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
640 th_conn_err(conn, err, "Could not connect: %s", th_error_str(err));
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 goto error;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
644 FD_ZERO(&(conn->base.sockfds));
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
645 FD_SET(conn->base.socket, &(conn->base.sockfds));
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 // 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
648 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
649 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 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
651 case TH_PROXY_SOCKS4A:
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
652 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
653 goto error;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
654 th_conn_msg(conn, THLOG_INFO, "SOCKS 4 connection established!");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655 break;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
656
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 case TH_PROXY_SOCKS5:
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
658 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
659 goto error;
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
660 th_conn_msg(conn, THLOG_INFO, "SOCKS 5 connection established!");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 break;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662 }
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 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
665 conn->status = TH_CONN_OPEN;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
666
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 // 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
668 conn->node = th_llist_append(&th_conn_list, conn);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
669
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 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
671
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672 error:
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 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
674 return err;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 }
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 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
679 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 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
681 return -1;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
683 if (conn->base.socket >= 0)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 {
191
cc5411cb85de Use TH_PLAT_WINDOWS define for #ifdef logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 190
diff changeset
685 #ifdef TH_PLAT_WINDOWS
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
686 closesocket(conn->base.socket);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 #else
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
688 close(conn->base.socket);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 #endif
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
690 conn->base.socket = -1;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691 }
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 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
694 return 0;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 }
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 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
699 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
700 th_free(conn->base.buf);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 th_conn_close(conn);
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
702 th_free(conn->base.host);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
703 th_free(conn->proxy.conn.host);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 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
705 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706
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 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
709 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 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
711 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 // 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
713 if (conn->node != NULL)
689
9c61834c191b Remove th_llist_delete() and th_llist_delete_node() and rename
Matti Hamalainen <ccr@tnsp.org>
parents: 572
diff changeset
714 th_llist_delete_node(&th_conn_list, conn->node);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
715
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 // 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
717 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
718 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
719 }
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
116
bc540c5bfaf9 More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 114
diff changeset
722 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
723 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724 size_t bufLeft = len;
118
ca33c6c80176 Change type.
Matti Hamalainen <ccr@tnsp.org>
parents: 117
diff changeset
725 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
726
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727 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
728 {
189
2d2a3ed27684 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 181
diff changeset
729 ssize_t bufSent = send(conn->base.socket, bufPtr, bufLeft, 0);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 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
731 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732 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
733 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
734 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
735 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 bufLeft -= bufSent;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 bufPtr += bufSent;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739
105
ea5b1c4b3af5 Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents: 104
diff changeset
740 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
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
117
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
744 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
745 {
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
746 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
747 th_growbuf_clear(buf);
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
748 return ret;
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
749 }
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
750
bfae4758fa6f More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
751
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 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
753 {
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 != NULL)
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
756 conn->base.ptr = conn->base.in_ptr = conn->base.buf;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
757 conn->base.got_bytes = conn->base.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
758 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 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
763 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 int result;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 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
766 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
767
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 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
769 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
770
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 // Shift the input buffer
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
772 if (conn->base.ptr > conn->base.buf)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
774 size_t left = conn->base.in_ptr - conn->base.ptr;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 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
776 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
777 size_t moved = conn->base.ptr - conn->base.buf;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
778 memmove(conn->base.buf, conn->base.ptr, left);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
779 conn->base.ptr = conn->base.buf;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
780 conn->base.in_ptr -= moved;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
781 conn->base.total_bytes -= moved;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 else
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
784 th_conn_reset(conn);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
787 // 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
788 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
789 socktv.tv_usec = TH_DELAY_USEC;
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
790 tmpfds = conn->base.sockfds;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
792 if ((result = select(conn->base.socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1)
102
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 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
795 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
796 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 err = th_errno_to_error(err);
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
798 th_conn_err(conn, err, "Error occured in select(%d, sockfds): %s",
571
423a8ace2598 Fix some format string and argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 564
diff changeset
799 conn->base.socket + 1, th_error_str(err));
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800 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
801 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 }
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
803 else if (FD_ISSET(conn->base.socket, &tmpfds))
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
804 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
805 conn->base.got_bytes = recv(conn->base.socket,
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
806 conn->base.in_ptr, conn->base.bufsize - conn->base.total_bytes, 0);
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
807
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
808 if (conn->base.got_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
809 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810 int err = th_errno_to_error(th_get_socket_errno());
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
811 th_conn_err(conn, err, "Error in recv: %s", th_error_str(err));
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812 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
813 }
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
814 else if (conn->base.got_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
815 {
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
816 th_conn_err(conn, ECONNABORTED, "Server closed connection.");
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
817 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
818 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
819 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
820 else
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
822 conn->base.total_bytes += conn->base.got_bytes;
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
823 conn->base.in_ptr += conn->base.got_bytes;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824 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
825 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
826 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
827
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
828 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
829 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
830
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
831
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
832 BOOL th_conn_check(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
833 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
834 if (conn == NULL)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
835 return FALSE;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
836
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
837 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
838 }
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 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
842 {
191
cc5411cb85de Use TH_PLAT_WINDOWS define for #ifdef logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 190
diff changeset
843 #ifdef TH_PLAT_WINDOWS
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
844 // 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
845 WSADATA wsaData;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
846 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
847 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
848 {
572
6d2d3b512750 Remove linefeeds from the th_network message strings.
Matti Hamalainen <ccr@tnsp.org>
parents: 571
diff changeset
849 THERR("Could not initialize WinSock library (err=%d).", err);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850 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
851 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852 #endif
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
854 th_network_inited = TRUE;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 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
857
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858 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
859 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860
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 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
863 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
864 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
865 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 // Close connections
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 150
diff changeset
867 th_llist_t *curr = th_conn_list;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868 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
869 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 150
diff changeset
870 th_llist_t *next = curr->next;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871 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
872 curr = next;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
874
191
cc5411cb85de Use TH_PLAT_WINDOWS define for #ifdef logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 190
diff changeset
875 #ifdef TH_PLAT_WINDOWS
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 WSACleanup();
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877 #endif
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
880 th_network_inited = FALSE;
102
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
884 BOOL th_conn_buf_check(th_conn_t *conn, size_t n)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
886 return conn && (conn->base.ptr + n <= conn->base.in_ptr);
102
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
889
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
890 BOOL th_conn_buf_skip(th_conn_t *conn, size_t n)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
892 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
893 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
894 conn->base.ptr += n;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
895 return TRUE;
102
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 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
898 return FALSE;
102
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
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 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
903 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 int ret;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905 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
906 return -1;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
908 if ((ret = strncmp(conn->base.ptr, str, n)) == 0)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
910 conn->base.ptr += n;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
911 return 0;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913 else
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
914 return ret;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915 }
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 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
919 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920 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
921 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922
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 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
925 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
926 char *pos;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
927 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
928
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
929 if (th_conn_buf_check(conn, n) && ((pos = strstr(conn->base.ptr, str)) != NULL))
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
930 {
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
931 conn->base.ptr = pos + n;
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
932 return pos;
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 else
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
935 return NULL;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
936 }
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
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
939 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
940 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
941 char *p;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
942 size_t offs, left;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
943
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
944 fprintf(f,
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
945 "\n--------------------------------------------------------------\n"
395
bffd3caf2d2c Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents: 191
diff changeset
946 "err=%d, status=%d, got_bytes=%" PRIu_SIZE_T ", total_bytes=%" PRIu_SIZE_T "\n"
bffd3caf2d2c Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents: 191
diff changeset
947 "buf=0x%p, in_ptr=0x%04" PRIx_SIZE_T ", ptr=0x%04" PRIx_SIZE_T "\n",
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
948 conn->err, conn->status, conn->base.got_bytes, conn->base.total_bytes,
564
a1dac9eb3f06 Typecast to void* to silence a silly clang-9 warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
949 (void *) conn->base.buf,
a1dac9eb3f06 Typecast to void* to silence a silly clang-9 warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 553
diff changeset
950 conn->base.in_ptr - conn->base.buf, conn->base.ptr - conn->base.buf);
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 440
diff changeset
951
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
952 // Dump buffer contents as a hexdump
132
11fa2bf90251 Network API changes due to preparation for fixes in proxy support.
Matti Hamalainen <ccr@tnsp.org>
parents: 131
diff changeset
953 for (offs = 0, left = conn->base.total_bytes, p = conn->base.buf; p < conn->base.in_ptr;)
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
954 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
955 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
956 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
957 left -= amount;
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
958
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 // Dump offs | xx xx xx xx | and fill string
395
bffd3caf2d2c Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents: 191
diff changeset
960 fprintf(f, "%04" PRIx_SIZE_T " | ", offs);
102
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
961 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
962 {
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963 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
964 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
965 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966 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
967
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
968 // Add padding
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
969 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
970 fprintf(f, " ");
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
971
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
972 // Print string
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
973 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
974 }
6ca407bfbeaf New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
975 }