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