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_rconnect.h"
30 #include "async_connect.h"
31 #include "resolver.h"
32 #include "../Compiler.h"
34 #include <glib.h>
36 #include <assert.h>
37 #include <stdio.h>
38 #include <string.h>
40 struct async_rconnect {
41 const struct async_rconnect_handler *handler;
42 void *handler_ctx;
44 const char *host;
45 struct resolver *resolver;
47 struct async_connect *connect;
49 char *last_error;
50 };
52 static void
53 async_rconnect_next(struct async_rconnect *rc);
55 static void
56 async_rconnect_success(socket_t fd, void *ctx)
57 {
58 struct async_rconnect *rc = ctx;
60 rc->handler->success(fd, rc->handler_ctx);
61 g_free(rc->last_error);
62 resolver_free(rc->resolver);
63 g_free(rc);
64 }
66 static void
67 async_rconnect_error(const char *message, void *ctx)
68 {
69 struct async_rconnect *rc = ctx;
71 g_free(rc->last_error);
72 rc->last_error = g_strdup(message);
74 async_rconnect_next(rc);
75 }
77 static const struct async_connect_handler async_rconnect_connect_handler = {
78 .success = async_rconnect_success,
79 .error = async_rconnect_error,
80 };
82 static void
83 async_rconnect_next(struct async_rconnect *rc)
84 {
85 const struct resolver_address *a = resolver_next(rc->resolver);
86 if (a == NULL) {
87 char msg[256];
89 if (rc->last_error == 0) {
90 snprintf(msg, sizeof(msg),
91 "Host '%s' has no address",
92 rc->host);
93 } else {
94 snprintf(msg, sizeof(msg),
95 "Failed to connect to host '%s': %s",
96 rc->host, rc->last_error);
97 g_free(rc->last_error);
98 }
100 rc->handler->error(msg, rc->handler_ctx);
101 resolver_free(rc->resolver);
102 g_free(rc);
103 return;
104 }
106 async_connect_start(&rc->connect, a->addr, a->addrlen,
107 &async_rconnect_connect_handler, rc);
108 }
110 void
111 async_rconnect_start(struct async_rconnect **rcp,
112 const char *host, unsigned port,
113 const struct async_rconnect_handler *handler, void *ctx)
114 {
115 struct resolver *r = resolver_new(host, port);
116 if (host == NULL)
117 host = "[default]";
119 if (r == NULL) {
120 char msg[256];
121 snprintf(msg, sizeof(msg), "Failed to resolve host '%s'",
122 host);
123 handler->error(msg, ctx);
124 return;
125 }
127 struct async_rconnect *rc = g_new(struct async_rconnect, 1);
128 rc->handler = handler;
129 rc->handler_ctx = ctx;
130 rc->host = host;
131 rc->resolver = r;
132 rc->last_error = NULL;
133 *rcp = rc;
135 async_rconnect_next(rc);
136 }
138 void
139 async_rconnect_cancel(struct async_rconnect *rc)
140 {
141 g_free(rc->last_error);
142 async_connect_cancel(rc->connect);
143 resolver_free(rc->resolver);
144 g_free(rc);
145 }