Mercurial > hg > th-libs
comparison th_network.c @ 735:31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 12:14:39 +0200 |
parents | 29e44a58bc73 |
children |
comparison
equal
deleted
inserted
replaced
734:2ae1045f6c18 | 735:31bc1ed07cf5 |
---|---|
8 #include "th_network.h" | 8 #include "th_network.h" |
9 #include "th_string.h" | 9 #include "th_string.h" |
10 #include <errno.h> | 10 #include <errno.h> |
11 | 11 |
12 | 12 |
13 static BOOL th_network_inited = FALSE; | 13 static bool th_network_inited = false; |
14 static th_llist_t *th_conn_list = NULL; | 14 static th_llist_t *th_conn_list = NULL; |
15 | 15 |
16 | 16 |
17 enum | 17 enum |
18 { | 18 { |
112 | 112 |
113 return res; | 113 return res; |
114 } | 114 } |
115 | 115 |
116 | 116 |
117 BOOL th_base_conn_init(th_base_conn_t *base, ssize_t bufsize) | 117 bool th_base_conn_init(th_base_conn_t *base, ssize_t bufsize) |
118 { | 118 { |
119 // Allocate connection data buffer | 119 // Allocate connection data buffer |
120 base->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize; | 120 base->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize; |
121 if ((base->buf = th_malloc(base->bufsize)) == NULL) | 121 if ((base->buf = th_malloc(base->bufsize)) == NULL) |
122 return FALSE; | 122 return false; |
123 | 123 |
124 return TRUE; | 124 return true; |
125 } | 125 } |
126 | 126 |
127 | 127 |
128 th_conn_t * th_conn_new( | 128 th_conn_t * th_conn_new( |
129 void (*errfunc)(th_conn_t *conn, int err, const char *msg), | 129 void (*errfunc)(th_conn_t *conn, int err, const char *msg), |
148 | 148 |
149 return conn; | 149 return conn; |
150 } | 150 } |
151 | 151 |
152 | 152 |
153 static BOOL th_get_addr(struct in_addr *addr, struct hostent *hst) | 153 static bool th_get_addr(struct in_addr *addr, struct hostent *hst) |
154 { | 154 { |
155 if (hst != NULL) | 155 if (hst != NULL) |
156 { | 156 { |
157 *addr = *(struct in_addr *) (hst->h_addr_list[0]); | 157 *addr = *(struct in_addr *) (hst->h_addr_list[0]); |
158 return TRUE; | 158 return true; |
159 } | 159 } |
160 else | 160 else |
161 { | 161 { |
162 addr->s_addr = 0; | 162 addr->s_addr = 0; |
163 return FALSE; | 163 return false; |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 | 167 |
168 int th_conn_set_proxy(th_conn_t *conn, int type, int port, const char *host, int auth_type) | 168 int th_conn_set_proxy(th_conn_t *conn, int type, int port, const char *host, int auth_type) |
319 th_conn_err(conn, err, | 319 th_conn_err(conn, err, |
320 "Invalid proxy settings for SOCKS 4, unsupported address type requested."); | 320 "Invalid proxy settings for SOCKS 4, unsupported address type requested."); |
321 goto out; | 321 goto out; |
322 } | 322 } |
323 | 323 |
324 th_growbuf_puts(&buf, conn->proxy.userid, TRUE); | 324 th_growbuf_puts(&buf, conn->proxy.userid, true); |
325 if (conn->proxy.addr_type == TH_PROXY_ADDR_DOMAIN) | 325 if (conn->proxy.addr_type == TH_PROXY_ADDR_DOMAIN) |
326 th_growbuf_puts(&buf, host, TRUE); | 326 th_growbuf_puts(&buf, host, true); |
327 | 327 |
328 // Send request | 328 // Send request |
329 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) | 329 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
330 goto out; | 330 goto out; |
331 | 331 |
463 th_conn_msg(conn, THLOG_INFO, "Attempting SOCKS 5 user/pass authentication."); | 463 th_conn_msg(conn, THLOG_INFO, "Attempting SOCKS 5 user/pass authentication."); |
464 th_growbuf_clear(&buf); | 464 th_growbuf_clear(&buf); |
465 | 465 |
466 th_growbuf_put_u8(&buf, 0x01); | 466 th_growbuf_put_u8(&buf, 0x01); |
467 th_growbuf_put_u8(&buf, strlen(conn->proxy.userid)); | 467 th_growbuf_put_u8(&buf, strlen(conn->proxy.userid)); |
468 th_growbuf_puts(&buf, conn->proxy.userid, FALSE); | 468 th_growbuf_puts(&buf, conn->proxy.userid, false); |
469 th_growbuf_put_u8(&buf, strlen(conn->proxy.passwd)); | 469 th_growbuf_put_u8(&buf, strlen(conn->proxy.passwd)); |
470 th_growbuf_puts(&buf, conn->proxy.passwd, FALSE); | 470 th_growbuf_puts(&buf, conn->proxy.passwd, false); |
471 | 471 |
472 // Send request | 472 // Send request |
473 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) | 473 if ((err = th_conn_proxy_send(conn, &buf)) != THERR_OK) |
474 goto out; | 474 goto out; |
475 | 475 |
827 | 827 |
828 return TH_CONN_NO_DATA; | 828 return TH_CONN_NO_DATA; |
829 } | 829 } |
830 | 830 |
831 | 831 |
832 BOOL th_conn_check(th_conn_t *conn) | 832 bool th_conn_check(th_conn_t *conn) |
833 { | 833 { |
834 if (conn == NULL) | 834 if (conn == NULL) |
835 return FALSE; | 835 return false; |
836 | 836 |
837 return conn->err == 0 && conn->status == TH_CONN_OPEN; | 837 return conn->err == 0 && conn->status == TH_CONN_OPEN; |
838 } | 838 } |
839 | 839 |
840 | 840 |
849 THERR("Could not initialize WinSock library (err=%d).", err); | 849 THERR("Could not initialize WinSock library (err=%d).", err); |
850 return THERR_INIT_FAIL; | 850 return THERR_INIT_FAIL; |
851 } | 851 } |
852 #endif | 852 #endif |
853 | 853 |
854 th_network_inited = TRUE; | 854 th_network_inited = true; |
855 | 855 |
856 th_conn_list = NULL; | 856 th_conn_list = NULL; |
857 | 857 |
858 return THERR_OK; | 858 return THERR_OK; |
859 } | 859 } |
875 #ifdef TH_PLAT_WINDOWS | 875 #ifdef TH_PLAT_WINDOWS |
876 WSACleanup(); | 876 WSACleanup(); |
877 #endif | 877 #endif |
878 } | 878 } |
879 | 879 |
880 th_network_inited = FALSE; | 880 th_network_inited = false; |
881 } | 881 } |
882 | 882 |
883 | 883 |
884 BOOL th_conn_buf_check(th_conn_t *conn, size_t n) | 884 bool th_conn_buf_check(th_conn_t *conn, size_t n) |
885 { | 885 { |
886 return conn && (conn->base.ptr + n <= conn->base.in_ptr); | 886 return conn && (conn->base.ptr + n <= conn->base.in_ptr); |
887 } | 887 } |
888 | 888 |
889 | 889 |
890 BOOL th_conn_buf_skip(th_conn_t *conn, size_t n) | 890 bool th_conn_buf_skip(th_conn_t *conn, size_t n) |
891 { | 891 { |
892 if (th_conn_buf_check(conn, n)) | 892 if (th_conn_buf_check(conn, n)) |
893 { | 893 { |
894 conn->base.ptr += n; | 894 conn->base.ptr += n; |
895 return TRUE; | 895 return true; |
896 } | 896 } |
897 else | 897 else |
898 return FALSE; | 898 return false; |
899 } | 899 } |
900 | 900 |
901 | 901 |
902 int th_conn_buf_strncmp(th_conn_t *conn, const char *str, const size_t n) | 902 int th_conn_buf_strncmp(th_conn_t *conn, const char *str, const size_t n) |
903 { | 903 { |