c375ea38e237a9d3376219b6067de436b324f494
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 #include <assert.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
39 struct async_connect {
40 const struct async_connect_handler *handler;
41 void *handler_ctx;
43 socket_t fd;
45 guint source_id;
46 };
48 static gboolean
49 async_connect_source_callback(gcc_unused GIOChannel *source,
50 gcc_unused GIOCondition condition,
51 gpointer data)
52 {
53 struct async_connect *ac = data;
55 const int fd = ac->fd;
56 const struct async_connect_handler *const handler = ac->handler;
57 void *const ctx = ac->handler_ctx;
58 g_free(ac);
60 int s_err = 0;
61 socklen_t s_err_size = sizeof(s_err);
63 if (getsockopt(fd, SOL_SOCKET, SO_ERROR,
64 (char*)&s_err, &s_err_size) < 0)
65 s_err = -last_socket_error();
67 if (s_err == 0) {
68 handler->success(fd, ctx);
69 } else {
70 close_socket(fd);
71 char msg[256];
72 snprintf(msg, sizeof(msg), "Failed to connect socket: %s",
73 strerror(-s_err));
74 handler->error(msg, ctx);
75 }
77 return false;
78 }
80 void
81 async_connect_start(struct async_connect **acp,
82 const struct sockaddr *address, size_t address_size,
83 const struct async_connect_handler *handler, void *ctx)
84 {
85 socket_t fd = create_socket(address->sa_family, SOCK_STREAM, 0);
86 if (fd == INVALID_SOCKET) {
87 char msg[256];
88 snprintf(msg, sizeof(msg), "Failed to create socket: %s",
89 strerror(errno));
90 handler->error(msg, ctx);
91 return;
92 }
94 if (connect(fd, address, address_size) == 0) {
95 handler->success(fd, ctx);
96 return;
97 }
99 const int e = last_socket_error();
100 if (!would_block(e)) {
101 close_socket(fd);
102 char msg[256];
103 snprintf(msg, sizeof(msg), "Failed to connect socket: %s",
104 strerror(e));
105 handler->error(msg, ctx);
106 return;
107 }
109 struct async_connect *ac = g_new(struct async_connect, 1);
110 ac->handler = handler;
111 ac->handler_ctx = ctx;
112 ac->fd = fd;
114 GIOChannel *channel = g_io_channel_unix_new(fd);
115 ac->source_id = g_io_add_watch(channel, G_IO_OUT,
116 async_connect_source_callback, ac);
117 g_io_channel_unref(channel);
119 *acp = ac;
120 }
122 void
123 async_connect_cancel(struct async_connect *ac)
124 {
125 g_source_remove(ac->source_id);
126 close_socket(ac->fd);
127 g_free(ac);
128 }