Skip to content

Commit 3e8e3bb

Browse files
lumagsuperna9999
authored andcommitted
conmux: switch to getaddrinfo
On ARM the conmux.c around gethostbyname() results in a compiler warning/error: conmux.c:269:27: error: cast increases required alignment of target type [-Werror=cast-align] 269 | saddr.sin_addr = *(struct in_addr *)hent->h_addr_list[0]; Instead of trying to fix the (deprecated) gethostbyname() call, switch conmux to use getaddrinfo(), which is the proper way to resolve addresses. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent c403782 commit 3e8e3bb

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

conmux.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct conmux {
5151

5252
struct conmux_lookup {
5353
char *host;
54-
int port;
54+
char *port;
5555
};
5656

5757
struct conmux_response {
@@ -193,7 +193,7 @@ static int registry_lookup(const char *service, struct conmux_lookup *result)
193193
*p++ = '\0';
194194

195195
result->host = strdup(resp.result);
196-
result->port = strtol(p, NULL, 10);
196+
result->port = strdup(p);
197197

198198
ret = strcmp(resp.status, "OK") ? -1 : 0;
199199

@@ -226,17 +226,16 @@ static int conmux_data(int fd, void *data)
226226

227227
void *conmux_open(struct device *dev)
228228
{
229+
struct addrinfo hints = {0}, *addrs, *addr;
229230
struct conmux_response resp = {};
230231
struct conmux_lookup lookup;
231-
struct sockaddr_in saddr;
232232
struct conmux *conmux;
233-
struct hostent *hent;
234233
const char *service = dev->control_dev;
235234
const char *user;
236235
ssize_t n;
237236
char req[256];
238237
int ret;
239-
int fd;
238+
int fd = -1;
240239

241240
user = getenv("USER");
242241
if (!user)
@@ -246,27 +245,31 @@ void *conmux_open(struct device *dev)
246245
if (ret)
247246
exit(1);
248247

249-
fprintf(stderr, "conmux device at %s:%d\n", lookup.host, lookup.port);
248+
fprintf(stderr, "conmux device at %s:%s\n", lookup.host, lookup.port);
250249

251-
fd = socket(AF_INET, SOCK_STREAM, 0);
252-
if (fd < 0)
253-
err(1, "failed to create registry socket");
250+
hints.ai_family = AF_UNSPEC;
251+
hints.ai_socktype = SOCK_STREAM;
252+
hints.ai_protocol = IPPROTO_TCP;
254253

255-
memset(&saddr, 0, sizeof(saddr));
256-
saddr.sin_family = AF_INET;
257-
saddr.sin_port = htons(lookup.port);
254+
ret = getaddrinfo(lookup.host, lookup.port, &hints, &addrs);
255+
if (ret != 0)
256+
errx(1, "failed resolve \"%s\": %s", lookup.host, gai_strerror(ret));
258257

259-
hent = gethostbyname(lookup.host);
260-
if (!hent) {
261-
errno = h_errno;
262-
err(1, "failed resolve \"%s\"", lookup.host);
263-
}
258+
for (addr = addrs; addr; addr = addr->ai_next) {
259+
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
260+
if (fd < 0)
261+
err(1, "failed to create registry socket");
264262

265-
saddr.sin_addr = *(struct in_addr *)hent->h_addr_list[0];
263+
ret = connect(fd, addr->ai_addr, addr->ai_addrlen);
264+
if (ret < 0) {
265+
warn("failed to connect to conmux instance");
266+
close(fd);
267+
fd = -1;
268+
}
269+
}
266270

267-
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
268-
if (ret < 0)
269-
err(1, "failed to connect to conmux instance");
271+
if (fd == -1)
272+
errx(1, "failed to connect to conmux instance");
270273

271274
ret = snprintf(req, sizeof(req), "CONNECT id=cdba:%s to=console\n", user);
272275
if (ret >= (int)sizeof(req))

0 commit comments

Comments
 (0)