1 /* ncmpc (Ncurses MPD Client)
2 (c) 2004-2017 The Music Player Daemon Project
3 Project homepage: http://musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "async_connect.h"
30 #include "../Compiler.h"
32 #include <glib.h>
34 #ifdef WIN32
35 #include <ws2tcpip.h>
36 #endif
38 #include <assert.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
43 struct async_connect {
44 const struct async_connect_handler *handler;
45 void *handler_ctx;
47 socket_t fd;
49 guint source_id;
50 };
52 static gboolean
53 async_connect_source_callback(gcc_unused GIOChannel *source,
54 gcc_unused GIOCondition condition,
55 gpointer data)
56 {
57 struct async_connect *ac = data;
59 const int fd = ac->fd;
60 const struct async_connect_handler *const handler = ac->handler;
61 void *const ctx = ac->handler_ctx;
62 g_free(ac);
64 int s_err = 0;
65 socklen_t s_err_size = sizeof(s_err);
67 if (getsockopt(fd, SOL_SOCKET, SO_ERROR,
68 (char*)&s_err, &s_err_size) < 0)
69 s_err = -last_socket_error();
71 if (s_err == 0) {
72 handler->success(fd, ctx);
73 } else {
74 close_socket(fd);
75 char msg[256];
76 snprintf(msg, sizeof(msg), "Failed to connect socket: %s",
77 strerror(-s_err));
78 handler->error(msg, ctx);
79 }
81 return false;
82 }
84 void
85 async_connect_start(struct async_connect **acp,
86 const struct sockaddr *address, size_t address_size,
87 const struct async_connect_handler *handler, void *ctx)
88 {
89 socket_t fd = create_socket(address->sa_family, SOCK_STREAM, 0);
90 if (fd == INVALID_SOCKET) {
91 char msg[256];
92 snprintf(msg, sizeof(msg), "Failed to create socket: %s",
93 strerror(errno));
94 handler->error(msg, ctx);
95 return;
96 }
98 if (connect(fd, address, address_size) == 0) {
99 handler->success(fd, ctx);
100 return;
101 }
103 const int e = last_socket_error();
104 if (!would_block(e)) {
105 close_socket(fd);
106 char msg[256];
107 snprintf(msg, sizeof(msg), "Failed to connect socket: %s",
108 strerror(e));
109 handler->error(msg, ctx);
110 return;
111 }
113 struct async_connect *ac = g_new(struct async_connect, 1);
114 ac->handler = handler;
115 ac->handler_ctx = ctx;
116 ac->fd = fd;
118 GIOChannel *channel = g_io_channel_unix_new(fd);
119 ac->source_id = g_io_add_watch(channel, G_IO_OUT,
120 async_connect_source_callback, ac);
121 g_io_channel_unref(channel);
123 *acp = ac;
124 }
126 void
127 async_connect_cancel(struct async_connect *ac)
128 {
129 g_source_remove(ac->source_id);
130 close_socket(ac->fd);
131 g_free(ac);
132 }