Mercurial > hg > th-libs
annotate th_network.c @ 116:bc540c5bfaf9
More work on network module.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 21 Jun 2014 23:19:57 +0300 |
parents | b44a0308b53f |
children | bfae4758fa6f |
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 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
|
25 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 "none", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 "SOCKS 4", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 "SOCKS 4a", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 "SOCKS 5", |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 NULL |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 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
|
35 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 return errno; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 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
|
41 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 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
|
43 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 char *msg; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 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
|
49 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 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
|
51 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
|
52 } |
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 |
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 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
|
57 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 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
|
59 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 char *msg; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 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
|
67 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
|
68 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 } |
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 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
|
73 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 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
|
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 (res == 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 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
|
79 "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
|
80 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
|
81 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 else |
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 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
|
85 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
|
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 return res; |
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 |
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 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
|
93 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
|
94 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
|
95 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
|
96 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 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
|
98 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 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
|
100 return NULL; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 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
|
103 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
|
104 |
103
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
105 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
|
106 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
|
107 { |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
108 th_free(conn); |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
109 return NULL; |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
110 } |
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
111 |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 return conn; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 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
|
117 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 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
|
119 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 *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
|
121 return TRUE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 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
|
126 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 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
|
132 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 if (conn == NULL) |
104 | 134 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
|
135 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 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
|
141 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
|
142 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 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
|
144 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 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
|
146 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
|
147 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 else |
104 | 149 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
|
150 |
104 | 151 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
|
152 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 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
|
156 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 if (conn == NULL) |
104 | 158 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
|
159 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 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
|
161 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
|
162 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 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
|
164 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
|
165 |
104 | 166 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
|
167 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
170 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
|
171 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 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
|
173 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 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
|
175 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 Sleep(50); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 #else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 usleep(50000); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 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
|
182 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
|
183 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
185 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
|
186 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 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
|
188 tries, status); |
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 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 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
|
192 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
193 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
|
194 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
197 static int th_conn_proxy_send(th_conn_t *conn, th_growbuf_t *buf) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
199 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
|
200 th_conn_reset(conn); |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
201 ret = th_conn_send_buf(conn, (void *) buf->data, buf->len); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 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
|
203 return ret; |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 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
|
208 { |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
209 th_growbuf_t buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
210 uint8_t *ptr; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 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
|
212 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 (void) host; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 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
|
216 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
217 // Create SOCKS 4 handshake |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
218 th_growbuf_init(&buf, 128); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
219 th_growbuf_put_u8(&buf, 4); // Protocol version |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
220 th_growbuf_put_u8(&buf, TH_PROXY_CMD_CONNECT); // Command |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
221 th_growbuf_put_u16_be(&buf, port); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 if (conn->proxy.type == TH_PROXY_SOCKS4A) |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
223 th_growbuf_put_u32_be(&buf, 0x00000032); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
224 else |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
225 th_growbuf_put_str(&buf, (uint8_t *) &(conn->addr.s_addr), sizeof(conn->addr.s_addr)); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
227 th_growbuf_puts(&buf, conn->proxy.userid, TRUE); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 if (conn->proxy.type == TH_PROXY_SOCKS4A) |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
229 th_growbuf_puts(&buf, conn->host, TRUE); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
232 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
233 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
|
234 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
236 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
|
237 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
|
238 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
239 ptr = (uint8_t*) conn->buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
240 if (*ptr != 0) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
242 err = THERR_INIT_FAIL; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
243 th_conn_err(conn, err, |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
244 "Invalid SOCKS 4 server reply, does not begin with NUL byte (%d).\n", |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
245 *ptr); |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
246 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
|
247 } |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
248 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
249 ptr++; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
250 if (*ptr != 0x5a) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 const char *s = NULL; |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
253 switch (*ptr) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 } |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
260 |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
261 err = THERR_INIT_FAIL; |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
262 th_conn_err(conn, err, "SOCKS 4 setup failed, 0x%02x: %s.\n", *ptr, s); |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
263 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
|
264 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
266 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
|
267 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 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
|
271 { |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
272 size_t userid_len = 0, passwd_len = 0; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
273 th_growbuf_t buf; |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
274 uint8_t *ptr; |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
275 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
|
276 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 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
|
278 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 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
|
280 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
281 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
|
282 avail = 1; |
113 | 283 auth = TH_SOCKS5_AUTH_NONE; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 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
|
287 avail = 2; |
113 | 288 auth = TH_SOCKS5_AUTH_USER; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 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
|
290 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
291 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
292 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
|
293 "SOCKS 5 user authentication chosen, but no user/pass set.\n"); |
112 | 294 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 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
|
298 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
|
299 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
|
300 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
301 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
302 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
|
303 "SOCKS 5 proxy userid or password is too long.\n"); |
112 | 304 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 break; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 default: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
309 err = THERR_NOT_SUPPORTED; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
310 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
|
311 "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
|
312 conn->proxy.auth_type); |
112 | 313 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
317 // Form handshake packet |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
318 th_growbuf_init(&buf, 0); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
319 th_growbuf_put_u8(&buf, 0x05); // Protocol version |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
320 th_growbuf_put_u8(&buf, avail); // # of available auth methods |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
321 th_growbuf_put_u8(&buf, auth); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
324 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 325 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
328 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
|
329 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
|
330 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 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
|
332 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
|
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 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); |
112 | 337 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 auth = *ptr; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 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
|
343 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
344 err = THERR_NOT_SUPPORTED; |
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 "No authentication method could be negotiated with the server.\n"); |
112 | 347 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 } |
114
b44a0308b53f
Add missing else statement.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
349 else |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
350 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
|
351 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
352 // Attempt user/pass authentication (RFC 1929) |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
353 th_growbuf_init(&buf, 256); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
354 |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
355 th_growbuf_put_u8(&buf, 0x01); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
356 th_growbuf_put_u8(&buf, userid_len); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
357 th_growbuf_puts(&buf, conn->proxy.userid, FALSE); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
358 th_growbuf_put_u8(&buf, passwd_len); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
359 th_growbuf_puts(&buf, conn->proxy.passwd, FALSE); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
360 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
361 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
362 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 363 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
366 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
|
367 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
|
368 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
369 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
|
370 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
|
371 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
372 err = THERR_INVALID_DATA; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
373 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
|
374 "Invalid SOCKS 5 server reply, does not begin with protocol version byte (%d).\n", *ptr); |
112 | 375 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
376 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
377 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
378 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
|
379 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
380 err = THERR_AUTH_FAILED; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
381 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
|
382 "SOCKS 5 proxy user/pass authentication failed! Code %d.\n", *ptr); |
112 | 383 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
384 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
386 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 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
|
388 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
389 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
|
390 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
|
391 "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
|
392 auth); |
112 | 393 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
395 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
396 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 #if 0 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
398 // Form client connection request packet |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
399 th_growbuf_init(&buf, 128); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
400 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
401 th_growbuf_put_u8(&buf, 0x05); // Protocol version |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
402 th_growbuf_put_u8(&buf, cmd); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
403 th_growbuf_put_u8(&buf, 0x00); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
404 th_growbuf_put_u8(&buf, SOCKS5_ADDR_IPV4); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
405 th_growbuf_put_str(&buf, conn->addr.s_addr, sizeof(conn->addr.s_addr)); |
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
406 th_growbuf_put_u16_be(&buf, port); |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 // Send request |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
409 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
112 | 410 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 // Wait for SOCKS server to reply |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
413 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
|
414 return FALSE; |
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 = (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
|
417 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
|
418 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 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
|
420 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
422 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 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
|
424 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
425 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
|
426 if (*ptr >= 0 && *ptr < sizeof()) |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
427 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
|
428 else |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
429 th_conn_err(conn, err ".Unknown\n"); |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
430 |
112 | 431 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 ptr++; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 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
|
435 { |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
436 int err = asdf; |
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
437 th_conn_err(conn, err, ".\n"); |
112 | 438 goto out; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
439 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
440 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
441 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
442 |
112 | 443 out: |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
444 th_growbuf_free(&buf); |
112 | 445 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
446 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
|
447 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
449 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
450 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
|
451 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 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
|
453 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
|
454 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
455 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
|
456 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
|
457 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
458 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
|
459 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
|
460 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
461 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
|
462 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
|
463 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
464 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
465 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
|
466 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 // 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
|
468 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
|
469 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
470 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
|
471 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
472 // 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
|
473 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
|
474 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
|
475 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
476 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
|
477 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
|
478 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
|
479 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
480 else |
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 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
|
483 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
|
484 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
485 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
|
486 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
|
487 } |
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 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
|
490 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
491 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
|
492 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
|
493 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
494 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
495 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
496 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
|
497 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
498 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
|
499 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
|
500 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
501 } |
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 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
|
504 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
|
505 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 // 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
|
507 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
|
508 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
509 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
|
510 case TH_PROXY_SOCKS4A: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
511 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
|
512 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
513 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
|
514 break; |
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 case TH_PROXY_SOCKS5: |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
517 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
|
518 goto error; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
519 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
|
520 break; |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
523 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
|
524 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
|
525 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 // 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
|
527 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
|
528 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 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
|
530 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
531 error: |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
532 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
|
533 return err; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
535 |
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 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
|
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 (conn == NULL) |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 return -1; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
541 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
542 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
|
543 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
544 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 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
|
546 #else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 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
|
548 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
549 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
|
550 } |
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 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
|
553 return 0; |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
557 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
|
558 { |
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_close(conn); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
560 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
|
561 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
|
562 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
|
563 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
566 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
|
567 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
568 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
|
569 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
570 // 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
|
571 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
|
572 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
|
573 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
574 // 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
|
575 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
|
576 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
577 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
578 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
579 |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
580 int th_conn_send_buf(th_conn_t *conn, const void *buf, const size_t len) |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
581 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
582 size_t bufLeft = len; |
116
bc540c5bfaf9
More work on network module.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
583 const uint8_t *bufPtr = (uint8_t *) buf; |
102
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
584 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
585 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
|
586 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
587 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
|
588 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
|
589 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
|
590 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
591 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
|
592 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
|
593 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
|
594 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
595 bufLeft -= bufSent; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
596 bufPtr += bufSent; |
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 |
105
ea5b1c4b3af5
Cleanups and a breaking API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
599 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
|
600 } |
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 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
|
604 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
605 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
|
606 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
607 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
|
608 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
|
609 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
610 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
611 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
612 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
613 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
|
614 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
615 int result; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
616 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
|
617 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
|
618 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
619 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
|
620 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
|
621 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
622 // 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
|
623 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
|
624 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
625 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
|
626 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
|
627 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
628 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
|
629 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
|
630 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
|
631 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
|
632 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
|
633 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
634 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
635 th_conn_reset(conn); |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
638 // 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
|
639 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
|
640 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
|
641 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
|
642 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
643 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
|
644 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
645 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
|
646 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
|
647 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
648 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
|
649 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
|
650 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
|
651 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
|
652 } |
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 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
|
655 { |
103
f7bec3f7181d
Change connection creation API to specify incoming buffer size.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
656 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
|
657 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
|
658 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
659 int 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
|
660 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
|
661 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
|
662 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
663 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
|
664 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
665 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
|
666 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
|
667 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
|
668 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
669 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
670 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
671 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
|
672 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
|
673 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
|
674 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
675 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
676 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
677 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
|
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
681 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
|
682 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
683 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
|
684 return FALSE; |
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 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
|
687 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
688 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
689 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
690 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
|
691 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
692 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
693 // 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
|
694 WSADATA wsaData; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
695 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
|
696 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
|
697 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
698 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
|
699 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
|
700 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
701 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
702 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
703 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
|
704 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
705 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
|
706 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
707 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
|
708 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
711 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
|
712 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
713 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
|
714 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
715 // Close connections |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
716 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
|
717 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
|
718 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
719 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
|
720 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
|
721 curr = next; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
722 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
723 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
724 #ifdef __WIN32 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
725 WSACleanup(); |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
726 #endif |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
727 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
728 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
729 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
|
730 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
733 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
|
734 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
735 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
|
736 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
739 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
|
740 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
741 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
|
742 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
743 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
|
744 return TRUE; |
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 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
747 return FALSE; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
748 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
751 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
|
752 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
753 int ret; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
754 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
|
755 return -1; |
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 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
|
758 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
759 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
|
760 return 0; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
761 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
762 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
763 return ret; |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
766 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
767 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
|
768 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
769 return th_conn_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
|
770 } |
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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
773 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
|
774 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
775 char *pos; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
776 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
|
777 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
778 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
|
779 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
780 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
|
781 return pos; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
782 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
783 else |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
784 return 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 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
787 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
788 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
|
789 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
790 char *p; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
791 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
|
792 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
793 fprintf(f, |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
794 "\n--------------------------------------------------------------\n" |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
795 "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
|
796 "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
|
797 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
|
798 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
|
799 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
800 // 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
|
801 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
|
802 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
803 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
|
804 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
|
805 left -= amount; |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
806 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
807 // 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
|
808 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
|
809 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
|
810 { |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
811 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
|
812 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
|
813 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
814 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
|
815 |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
816 // Add padding |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
817 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
|
818 fprintf(f, " "); |
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 // Print string |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
821 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
|
822 } |
6ca407bfbeaf
New TCP network module, rather simple and only useful for client applications now.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
823 } |