summaryrefslogtreecommitdiffstats
path: root/PICA_nodewait.c
blob: b554b9437c5c35ad7601a15625a8ad348d8ca580 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
	(c) Copyright  2012 - 2018 Anton Sviridenko
	https://picapica.im

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "PICA_nodewait.h"
#include "PICA_log.h"
#include "PICA_common.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef WIN32

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>

#else

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#endif

struct nodewait *nodewait_list;

struct nodewait *nodewait_list_addnew()
{
	struct nodewait *nw;

	nw = (struct nodewait*)calloc(1, sizeof(struct nodewait));

	if (!nw)
		return 0;

	nw->next = nodewait_list;
	nodewait_list = nw;

	nw->state = PICA_NODEWAIT_NEW;
	return nw;
}

void nodewait_list_delete(struct nodewait *nw)
{
	struct nodewait **pp = &nodewait_list;
	struct nodewait *p = nodewait_list;

	PICA_debug3("nodewait_list_delete(%p)", nw);

	while(p)
	{
		if (p == nw)
		{
			*pp = nw->next;
			//
			//free resources
			//
			if (nw->ai)
				freeaddrinfo(nw->ai);

			free(nw);

			return;
		}

		pp = &p->next;
		p = p->next;
	}
}

#ifdef WIN32
static DWORD WINAPI nodewait_resolve_thread (void *arg)
#else
static void  *nodewait_resolve_thread (void *arg)
#endif
{
	/*
	* DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
	*/
	struct nodewait *nw = (struct nodewait *)arg;
	struct addrinfo h;
	char portbuf[8];


	memset(&h, 0, sizeof(struct addrinfo));

#ifdef AI_ADDRCONFIG
#ifdef AI_IDN
#define H_AI_FLAGS (AI_ADDRCONFIG | AI_IDN)
#else
#define H_AI_FLAGS AI_ADDRCONFIG
#endif
#else
#define H_AI_FLAGS 0
#endif

	h.ai_flags = H_AI_FLAGS;
	h.ai_family = AF_INET; //CONF - IPv4, IPv6, IPv4 then IPv6, IPv6 then IPv4
	h.ai_socktype = SOCK_STREAM;
	h.ai_protocol = IPPROTO_TCP;

	sprintf(portbuf, "%u", nw->addr.port);

	nw->ai_errorcode = getaddrinfo(nw->addr.addr, portbuf, &h, &nw->ai);

	if (nw->ai_errorcode)
		nw->state = PICA_NODEWAIT_RESOLVING_FAILED;
	else
		nw->state = PICA_NODEWAIT_RESOLVED;

	return 0;
}


#ifdef WIN32
static DWORD WINAPI nodewait_connect_thread (void *arg)
#else
static void  *nodewait_connect_thread (void *arg)
#endif
{
	/*
	* DO NOT CALL LOGGING FUNCTIONS HERE, THEY ARE NOT THREAD-SAFE
	*/
	struct nodewait *nw = (struct nodewait *)arg;
	struct  addrinfo *ap;


	ap = nw->ai;

	while(ap)
	{
		nw->nc.sck = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
		if 	(nw->nc.sck == SOCKET_ERROR)
		{
			ap = ap->ai_next;
			continue;
		}

		if (0 != connect(nw->nc.sck, ap->ai_addr, ap->ai_addrlen))
		{
			ap = ap->ai_next;

			CLOSE(nw->nc.sck);
			continue;
		}
		else
		{
			nw->nc.anonssl = SSL_new(anon_ctx);

			if (!nw->nc.anonssl)
			{
				CLOSE(nw->nc.sck);
				return (void  *)0;
			}

			if (!SSL_set_fd(nw->nc.anonssl, nw->nc.sck) || 1 != SSL_connect(nw->nc.anonssl))
			{
				SSL_free(nw->nc.anonssl);
				nw->nc.anonssl = NULL;
				CLOSE(nw->nc.sck);
				ap = ap->ai_next;
				continue;
			}

			break;
		}
	}

	if (ap)
	{
		nw->nc.addr = *((struct sockaddr_in*)(ap->ai_addr));

		nw->state = PICA_NODEWAIT_CONNECTED;
		return (void  *)1;
	}

	nw->state = PICA_NODEWAIT_CONNECT_FAILED;
	return (void  *)0;
}

void nodewait_start_resolve(struct PICA_nodeaddr *a)
{
	struct nodewait *nw;
#ifndef WIN32
	pthread_t thr;
	pthread_attr_t attr;
#else
	DWORD thread_id;
	HANDLE thread_h;
#endif

	nw = nodewait_list_addnew();

	if (!nw)
		return;

	nw ->addr = *a;

	PICA_debug1("starting to resolve  node address %.255s %u ...", nw->addr.addr, nw->addr.port);

	nw->state = PICA_NODEWAIT_RESOLVING;

#ifndef WIN32
	if (0 != pthread_attr_init(&attr))
		PICA_error("pthread_attr_init() call failed.");

	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_attr_setstacksize(&attr, 65536);

	if (0 != pthread_create(&thr, &attr, nodewait_resolve_thread, (void*)nw))
		PICA_error("unable to create thread: %s", strerror(errno));

	pthread_attr_destroy(&attr);
#else
	thread_h = CreateThread(NULL, 4096, nodewait_resolve_thread, (void*)nw, 0, &thread_id);

	if (NULL != thread_h)
		CloseHandle(thread_h);
	else
		PICA_error("unable to create thread: %u", GetLastError());
#endif
}

void nodewait_start_connect(struct nodewait *nw)
{
#ifndef WIN32
	pthread_t thr;
	pthread_attr_t attr;
#else
	DWORD thread_id;
	HANDLE thread_h;
#endif

	PICA_debug1("connecting to  node  %.255s %u ...", nw->addr.addr, nw->addr.port);

	nw->state = PICA_NODEWAIT_CONNECTING;
#ifndef WIN32
	if (0 != pthread_attr_init(&attr))
		PICA_error("pthread_attr_init() call failed.");

	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

	if (0 != pthread_create(&thr, &attr, nodewait_connect_thread, (void*)nw))
		PICA_error("unable to create thread: %s", strerror(errno));

	pthread_attr_destroy(&attr);
#else
	thread_h = CreateThread(NULL, 4096, nodewait_connect_thread, (void*)nw, 0, &thread_id);

	if (NULL != thread_h)
		CloseHandle(thread_h);
	else
		PICA_error("unable to create thread: %u", GetLastError());
#endif

}