aboutsummaryrefslogtreecommitdiffstats
path: root/test/run_tcp_connect.c
blob: aa8fb05f59b1bc9be6a663cdddea886de6b70e0e (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
/*
 * Copyright (C) 2003-2011 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "resolver.h"
#include "io_thread.h"
#include "tcp_connect.h"
#include "fd_util.h"

#include <assert.h>
#include <stdlib.h>

#ifdef WIN32
#define WINVER 0x0501
#include <ws2tcpip.h>
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif

static struct tcp_connect *handle;
static GMutex *mutex;
static GCond *cond;
static bool done, success;

static void
my_tcp_connect_success(int fd, G_GNUC_UNUSED void *ctx)
{
	assert(!done);
	assert(!success);

	close_socket(fd);
	g_print("success\n");

	g_mutex_lock(mutex);
	done = success = true;
	g_cond_signal(cond);
	g_mutex_unlock(mutex);
}

static void
my_tcp_connect_error(GError *error, G_GNUC_UNUSED void *ctx)
{
	assert(!done);
	assert(!success);

	g_printerr("error: %s\n", error->message);
	g_error_free(error);

	g_mutex_lock(mutex);
	done = true;
	g_cond_signal(cond);
	g_mutex_unlock(mutex);
}

static void
my_tcp_connect_timeout(G_GNUC_UNUSED void *ctx)
{
	assert(!done);
	assert(!success);

	g_printerr("timeout\n");

	g_mutex_lock(mutex);
	done = true;
	g_cond_signal(cond);
	g_mutex_unlock(mutex);
}

static void
my_tcp_connect_canceled(G_GNUC_UNUSED void *ctx)
{
	assert(!done);
	assert(!success);

	g_printerr("canceled\n");

	g_mutex_lock(mutex);
	done = true;
	g_cond_signal(cond);
	g_mutex_unlock(mutex);
}

static const struct tcp_connect_handler my_tcp_connect_handler = {
	.success = my_tcp_connect_success,
	.error = my_tcp_connect_error,
	.timeout = my_tcp_connect_timeout,
	.canceled = my_tcp_connect_canceled,
};

int main(int argc, char **argv)
{
	if (argc != 2) {
		g_printerr("Usage: run_tcp_connect IP:PORT\n");
		return 1;
	}

	GError *error = NULL;
	struct addrinfo *ai = resolve_host_port(argv[1], 80, 0, SOCK_STREAM,
						&error);
	if (ai == NULL) {
		g_printerr("%s\n", error->message);
		g_error_free(error);
		return EXIT_FAILURE;
	}

	/* initialize GLib */

	g_thread_init(NULL);

	/* initialize MPD */

	io_thread_init();
	if (!io_thread_start(&error)) {
		freeaddrinfo(ai);
		g_printerr("%s", error->message);
		g_error_free(error);
		return EXIT_FAILURE;
	}

	/* open the connection */

	mutex = g_mutex_new();
	cond = g_cond_new();

	tcp_connect_address(ai->ai_addr, ai->ai_addrlen, 5000,
			    &my_tcp_connect_handler, NULL,
			    &handle);
	freeaddrinfo(ai);

	if (handle != NULL) {
		g_mutex_lock(mutex);
		while (!done)
			g_cond_wait(cond, mutex);
		g_mutex_unlock(mutex);

		tcp_connect_free(handle);
	}

	g_cond_free(cond);
	g_mutex_free(mutex);

	/* deinitialize everything */

	io_thread_deinit();

	return EXIT_SUCCESS;
}