annotate network.c @ 599:eeea75b8b6f3

Implement proxy user id setting.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 May 2014 16:14:09 +0300
parents 97a49a6cc959
children 4bae14092b78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
581
97a49a6cc959 Bump copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 564
diff changeset
4 * (C) Copyright 2008-2013 Tecnic Software productions (TNSP)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
6 #include "network.h"
415
8263cb88556a Some #include cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
7 #include <errno.h>
8263cb88556a Some #include cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
8
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
9 static BOOL nn_network_inited = FALSE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
414
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
11 static const char *nn_proxy_types[] =
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
12 {
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
13 "none",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
14 "SOCKS 4",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
15 "SOCKS 4a",
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
16 NULL
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
17 };
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 #ifdef __WIN32
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 const char *hstrerror(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 static char buf[64];
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 snprintf(buf, sizeof(buf), "Error #%d", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 return buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
28
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 int nn_get_socket_errno(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 return WSAGetLastError();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
34
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 const char *nn_get_socket_errstr(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 static char buf[64];
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 switch (err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 case WSAEADDRINUSE: return "Address already in use";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 case WSAECONNABORTED: return "Software caused connection abort";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 case WSAECONNREFUSED: return "Connection refused";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 case WSAECONNRESET: return "Connection reset by peer";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 case WSAEHOSTUNREACH: return "No route to host";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 case WSAENETDOWN: return "Network is down";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 case WSAETIMEDOUT: return "Connection timed out";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 case WSAHOST_NOT_FOUND: return "Host not found";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 case WSAVERNOTSUPPORTED: return "Wrong WinSock DLL version";
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 default:
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 snprintf(buf, sizeof(buf), "Error #%d", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 return buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 #else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 int nn_get_socket_errno(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 return errno;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
413
14b685cdbd2c Rename files.
Matti Hamalainen <ccr@tnsp.org>
parents: 412
diff changeset
61
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 const char *nn_get_socket_errstr(int err)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 return strerror(err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 #endif
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 void nn_conn_err(nn_conn_t *conn, const char *fmt, ...)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 if (conn->errfunc != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
73 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 va_list ap;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 va_start(ap, fmt);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
76 msg = th_strdup_vprintf(fmt, ap);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 va_end(ap);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
78
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
79 conn->errfunc(conn, msg);
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
80 th_free(msg);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 static void nn_conn_msg(nn_conn_t *conn, const char *fmt, ...)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 if (conn->msgfunc != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 {
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
89 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 va_list ap;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 va_start(ap, fmt);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
92 msg = th_strdup_vprintf(fmt, ap);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 va_end(ap);
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
94
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
95 conn->msgfunc(conn, msg);
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
96 th_free(msg);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
414
ac4862a94cd1 Move some structures around.
Matti Hamalainen <ccr@tnsp.org>
parents: 413
diff changeset
100
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 struct hostent *nn_resolve_host(nn_conn_t *conn, const char *name)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 struct hostent *res = gethostbyname(name);
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
104
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 if (res == NULL)
456
bb65460c7aa2 Clean up some initialization messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
106 nn_conn_err(conn, "Could not resolve hostname '%s': %s\n", name, strerror(h_errno));
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 nn_conn_msg(conn, "True hostname for %s is %s\n", name, res->h_name);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 return res;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 nn_conn_t * nn_conn_new(
564
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
115 void (*errfunc)(nn_conn_t *conn, const char *msg),
6e5789cbb4d4 Change network layer error/info message passing API to pass direct strings
Matti Hamalainen <ccr@tnsp.org>
parents: 490
diff changeset
116 void (*msgfunc)(nn_conn_t *conn, const char *msg))
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 nn_conn_t *conn = th_calloc(1, sizeof(nn_conn_t));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 return NULL;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 conn->errfunc = errfunc;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 conn->msgfunc = msgfunc;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 return conn;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
129
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 static BOOL nn_get_addr(struct in_addr *addr, struct hostent *hst)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 if (hst != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 *addr = *(struct in_addr *) (hst->h_addr);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 addr->s_addr = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
144
599
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
145 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host, const char *userid)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 conn->proxy.type = type;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 conn->proxy.port = port;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 conn->proxy.host = th_strdup(host);
599
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
153 conn->proxy.userid = th_strdup(userid);
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
154 if (conn->proxy.userid == NULL)
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
155 conn->proxy.userid = th_strdup("James Bond");
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 if (host != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 conn->proxy.hst = nn_resolve_host(conn, host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 nn_get_addr(&(conn->proxy.addr), conn->proxy.hst);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
458
6eef0dc83f0a Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 456
diff changeset
168
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 int nn_conn_open(nn_conn_t *conn, const int port, const char *host)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 struct sockaddr_in dest;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 conn->port = port;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 if (host != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 conn->host = th_strdup(host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 conn->hst = nn_resolve_host(conn, host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 nn_get_addr(&(conn->addr), conn->hst);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
185 // Prepare for connection
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 dest.sin_family = AF_INET;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 dest.sin_port = htons(conn->proxy.port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 dest.sin_addr = conn->proxy.addr;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 nn_proxy_types[conn->proxy.type],
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 inet_ntoa(conn->proxy.addr), conn->proxy.port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 dest.sin_port = htons(conn->port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 dest.sin_addr = conn->addr;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 nn_conn_msg(conn, "Connecting to %s:%d ...\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 inet_ntoa(conn->addr), conn->port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 nn_conn_err(conn, "Could not connect: %s\n", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 FD_ZERO(&(conn->sockfds));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 FD_SET(conn->socket, &(conn->sockfds));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
223 // Proxy-specific setup
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 struct nn_socks_t *socksh;
599
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
227 size_t bufsiz = sizeof(struct nn_socks_t) + strlen(conn->proxy.userid) + 1;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 char *ptr, *buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 int tries, status = -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230
488
bbe05594f29d Clean up some initialization messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 482
diff changeset
231 nn_conn_msg(conn, "Initializing proxy negotiation.\n");
bbe05594f29d Clean up some initialization messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 482
diff changeset
232
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 if (conn->proxy.type == NN_PROXY_SOCKS4A)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 bufsiz += strlen(conn->host) + 1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 ptr = buf = th_malloc(bufsiz);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 if (buf == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 conn->err = -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
244 // Create SOCKS 4/4A request
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 socksh = (struct nn_socks_t *) buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 socksh->version = 4;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 socksh->command = SOCKS_CMD_CONNECT;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 socksh->port = htons(port);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 if (conn->proxy.type == NN_PROXY_SOCKS4A)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 socksh->addr = htonl(0x00000032);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 socksh->addr = conn->addr.s_addr;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 ptr += sizeof(struct nn_socks_t);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254
599
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
255 strcpy(ptr, conn->proxy.userid);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (conn->proxy.type == NN_PROXY_SOCKS4A)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 {
599
eeea75b8b6f3 Implement proxy user id setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 581
diff changeset
259 ptr += strlen(conn->proxy.userid) + 1;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 strcpy(ptr, conn->host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
263 // Send request
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 nn_conn_reset(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 if (!nn_conn_send_buf(conn, buf, bufsiz))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 th_free(buf);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 nn_conn_err(conn, "Error sending SOCKS proxy request.\n");
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 th_free(buf);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
273 // Wait for SOCKS server to reply
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 for (status = tries = 1; tries <= 20 && status > 0; tries++)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 #ifdef __WIN32
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 Sleep(50);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 #else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 usleep(50000);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 #endif
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 nn_conn_reset(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 status = nn_conn_pull(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
285 // Check results
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 if (status == 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 if (res->nb != 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 if (res->result != 0x5a)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 char *s = NULL;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 switch (res->result)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 case 0x5b: s = "Request rejected or failed"; break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 case 0x5c: s = "Request failed because client is not running identd (or not reachable from the server)"; break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 default: s = "Unknown SOCKS error response"; break;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 nn_conn_err(conn, "SOCKS setup failed, 0x%02x: %s.\n", res->result, s);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 nn_conn_msg(conn, "SOCKS connection established!\n");
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 else if (status < 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 nn_conn_err(conn, "Proxy negotiation failed at try %d with network error: %d\n", tries, status);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 nn_conn_err(conn, "Proxy negotiation timed out.\n");
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 goto error;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 nn_conn_reset(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 conn->status = NN_CONN_OPEN;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 error:
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 conn->status = NN_CONN_CLOSED;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 void nn_conn_close(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 return;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 if (conn->socket >= 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 #ifdef __WIN32
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 closesocket(conn->socket);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 #else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 close(conn->socket);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 #endif
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 conn->socket = -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 th_free(conn->host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 th_free(conn->proxy.host);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 conn->status = NN_CONN_CLOSED;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 th_free(conn);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 BOOL nn_conn_send_buf(nn_conn_t *conn, const char *buf, const size_t len)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 size_t bufLeft = len;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 const char *bufPtr = buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 while (bufLeft > 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 ssize_t bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 bufSent = send(conn->socket, bufPtr, bufLeft, 0);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 if (bufSent < 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 nn_conn_err(conn, "nn_conn_send_buf() failed: %s", nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 bufLeft -= bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 bufPtr += bufSent;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 void nn_conn_reset(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 if (conn != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 conn->ptr = conn->in_ptr = conn->buf;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 conn->got_bytes = conn->total_bytes = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 int nn_conn_pull(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 int result;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 struct timeval socktv;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 fd_set tmpfds;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 return -10;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395
438
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
396 // Shift the input buffer
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
397 if (conn->ptr > conn->buf)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 {
438
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
399 size_t left = conn->in_ptr - conn->ptr;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
400 if (left > 0)
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
401 {
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
402 size_t moved = conn->ptr - conn->buf;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
403 memmove(conn->buf, conn->ptr, left);
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
404 conn->ptr = conn->buf;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
405 conn->in_ptr -= moved;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
406 conn->total_bytes -= moved;
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
407 }
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
408 else
3be600124da8 Fix packet shifting.
Matti Hamalainen <ccr@tnsp.org>
parents: 434
diff changeset
409 nn_conn_reset(conn);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
412 // Check for incoming data
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 socktv.tv_sec = 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 socktv.tv_usec = NN_DELAY_USEC;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 tmpfds = conn->sockfds;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 {
490
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
419 int err = nn_get_socket_errno();
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
420 if (err != EINTR)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 {
490
9141f13be70c Don't set conn->err if we are handling EINTR and it isn't supposed to get to
Matti Hamalainen <ccr@tnsp.org>
parents: 489
diff changeset
422 conn->err = err;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 nn_conn_err(conn, "Error occured in select(%d, sockfds): %d, %s\n",
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 socket, conn->err, nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 else if (FD_ISSET(conn->socket, &tmpfds))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 conn->got_bytes = recv(conn->socket, conn->in_ptr, NN_CONNBUF_SIZE - conn->total_bytes, 0);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 if (conn->got_bytes < 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 conn->err = nn_get_socket_errno();
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 return -2;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 else if (conn->got_bytes == 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 nn_conn_err(conn, "Server closed connection.\n");
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 conn->status = NN_CONN_CLOSED;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 return -3;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 conn->total_bytes += conn->got_bytes;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 conn->in_ptr += conn->got_bytes;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 return 1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 BOOL nn_conn_check(nn_conn_t *conn)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 if (conn == NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 return conn->err == 0 && conn->status == NN_CONN_OPEN;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 BOOL nn_network_init(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 #ifdef __WIN32
419
d015ecbd231d Use C99 style comments, too.
Matti Hamalainen <ccr@tnsp.org>
parents: 415
diff changeset
467 // Initialize WinSock, if needed
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 WSADATA wsaData;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 int err = WSAStartup(0x0101, &wsaData);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 if (err != 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 THERR("Could not initialize WinSock library (err=%d).\n", err);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 #endif
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
476
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
477 nn_network_inited = TRUE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 void nn_network_close(void)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 {
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
484 if (nn_network_inited)
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
485 {
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 #ifdef __WIN32
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
487 WSACleanup();
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 #endif
489
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
489 }
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
490
ba48840b8525 Move network initialization/shutdown flag checking to network module.
Matti Hamalainen <ccr@tnsp.org>
parents: 488
diff changeset
491 nn_network_inited = FALSE;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 BOOL nn_conn_buf_check(nn_conn_t *conn, size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 {
428
162011bbec48 Remove somewhat useless pointer checks.
Matti Hamalainen <ccr@tnsp.org>
parents: 423
diff changeset
497 return conn && (conn->ptr + n <= conn->in_ptr);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 BOOL nn_conn_buf_skip(nn_conn_t *conn, size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 if (nn_conn_buf_check(conn, n))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 conn->ptr += n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 return TRUE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 int nn_conn_buf_strncmp(nn_conn_t *conn, const char *str, const size_t n)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 int ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 if (!nn_conn_buf_check(conn, n))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 return -1;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 if ((ret = strncmp(conn->ptr, str, n)) == 0)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 conn->ptr += n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 return 0;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 return ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 int nn_conn_buf_strcmp(nn_conn_t *conn, const char *str)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 return nn_conn_buf_strncmp(conn, str, strlen(str));
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 char *nn_conn_buf_strstr(nn_conn_t *conn, const char *str)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 char *pos;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 size_t n = strlen(str);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 if (nn_conn_buf_check(conn, n) && ((pos = strstr(conn->ptr, str)) != NULL))
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 conn->ptr = pos + n;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 return pos;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 return NULL;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
550 BOOL nn_conn_send_msg(nn_conn_t *conn, const char *user, const char *str)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 {
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
552 char *msg;
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
554 if (str == NULL)
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 return FALSE;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
556
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
557 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str);
412
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 if (msg != NULL)
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 {
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 th_free(msg);
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 return ret;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 }
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 else
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 return FALSE;
3e64acb433e8 Split libnnchat into libnnet and libnnutil
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 }
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
568
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
569
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
570 BOOL nn_conn_send_msg_v(nn_conn_t *conn, const char *user, const char *fmt, ...)
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
571 {
482
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
572 BOOL res;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
573 char *tmp;
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
574 va_list ap;
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
575
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
576 va_start(ap, fmt);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
577 tmp = th_strdup_vprintf(fmt, ap);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
578 va_end(ap);
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
579
482
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
580 res = nn_conn_send_msg(conn, user, tmp);
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
581 th_free(tmp);
8bd6bad09fb8 Fix a memory leak in nn_conn_send_msg_v().
Matti Hamalainen <ccr@tnsp.org>
parents: 458
diff changeset
582 return res;
423
6727fec3c326 Rename nn_conn_send_msg() to nn_conn_send_msg_v() and add new nn_conn_send_msg().
Matti Hamalainen <ccr@tnsp.org>
parents: 419
diff changeset
583 }
434
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
584
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
585
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
586 void nn_conn_dump_buffer(FILE *f, nn_conn_t *conn)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
587 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
588 char *p;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
589 size_t offs, left;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
590
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
591 fprintf(f,
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
592 "\n--------------------------------------------------------------\n"
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
593 "err=%d, status=%d, got_bytes=%d, total_bytes=%d\n"
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
594 "buf=0x%p, in_ptr=0x%04x, ptr=0x%04x\n",
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
595 conn->err, conn->status, conn->got_bytes, conn->total_bytes,
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
596 conn->buf, conn->in_ptr - conn->buf, conn->ptr - conn->buf);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
597
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
598 // Dump buffer contents as a hexdump
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
599 for (offs = 0, left = conn->total_bytes, p = conn->buf; p < conn->in_ptr;)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
600 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
601 char buf[NN_DUMP_BYTES + 1];
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
602 size_t bufoffs, amount = left < NN_DUMP_BYTES ? left : NN_DUMP_BYTES;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
603 left -= amount;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
604
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
605 // Dump offs | xx xx xx xx | and fill string
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
606 fprintf(f, "%04x | ", offs);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
607 for (bufoffs = 0; bufoffs < amount; offs++, bufoffs++, p++)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
608 {
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
609 fprintf(f, "%02x ", *p);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
610 buf[bufoffs] = th_isprint(*p) ? *p : '.';
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
611 }
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
612 buf[bufoffs] = 0;
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
613
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
614 // Add padding
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
615 for (; bufoffs < NN_DUMP_BYTES; bufoffs++)
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
616 fprintf(f, " ");
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
617
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
618 // Print string
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
619 fprintf(f, "| %s\n", buf);
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
620 }
691400f1c9bb Add function for dumping current network buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 433
diff changeset
621 }