summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--idle.c10
-rw-r--r--utils.c39
-rw-r--r--utils.h4
3 files changed, 40 insertions, 13 deletions
diff --git a/idle.c b/idle.c
index e1a73ea..0607a61 100644
--- a/idle.c
+++ b/idle.c
@@ -287,7 +287,8 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected
char buf[1024];
char *pos;
char *cmdbuf = idle->tbuf.buf;
- uint16_t port = 25565;
+ uint16_t port;
+ char hostname[INET6_ADDRSTRLEN];
if (!connected) {
fprintf(stderr, "%s: idle check connection to remote server failed\n",
@@ -298,11 +299,14 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected
fprintf(stderr, "%s: connected to remote %s\n", __func__, idle->conn.remotestr);
+ port = sockaddr_port(&conn->remote);
+ sockaddr_addr(&conn->remote, hostname, sizeof(hostname));
+
pos = buf;
write_byte(&pos, MC_HELO);
write_varint(&pos, -1); /* Protocol version, -1 = undefined */
- write_varint(&pos, strlen("hostname"));
- write_str(&pos, "hostname");
+ write_varint(&pos, strlen(hostname));
+ write_str(&pos, hostname);
write_byte(&pos, (port >> 8) & 0xff);
write_byte(&pos, (port >> 0) & 0xff);
write_byte(&pos, MC_NEXT_STATE_STATUS);
diff --git a/utils.c b/utils.c
index 1fa8621..56b75a5 100644
--- a/utils.c
+++ b/utils.c
@@ -286,7 +286,8 @@ connect_any(struct cfg *cfg, struct uring_task *task,
connect_next(cfg, task, conn);
}
-uint16_t sockaddr_port(struct sockaddr_in46 *addr)
+uint16_t
+sockaddr_port(struct sockaddr_in46 *addr)
{
switch (addr->storage.ss_family) {
case AF_INET:
@@ -299,23 +300,43 @@ uint16_t sockaddr_port(struct sockaddr_in46 *addr)
}
char *
-sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen)
+sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len)
+{
+ switch (addr->storage.ss_family) {
+ case AF_INET:
+ if (inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, buf, len))
+ return buf;
+ break;
+ case AF_INET6:
+ if (inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, buf, len))
+ return buf;
+ break;
+ default:
+ break;
+ }
+
+ snprintf(buf, len, "<unknown>");
+ return buf;
+}
+
+char *
+sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t len)
{
char abuf[ADDRSTRLEN];
switch (addr->storage.ss_family) {
case AF_INET:
- snprintf(buf, buflen, "AF_INET4 %s %u",
- inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, abuf, sizeof(abuf)),
- (unsigned)ntohs(addr->in4.sin_port));
+ snprintf(buf, len, "AF_INET4 %s %" PRIu16,
+ sockaddr_addr(addr, abuf, sizeof(abuf)),
+ sockaddr_port(addr));
break;
case AF_INET6:
- snprintf(buf, buflen, "AF_INET6 %s %u",
- inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, abuf, sizeof(abuf)),
- (unsigned)ntohs(addr->in6.sin6_port));
+ snprintf(buf, len, "AF_INET6 %s %" PRIu16,
+ sockaddr_addr(addr, abuf, sizeof(abuf)),
+ sockaddr_port(addr));
break;
default:
- snprintf(buf, buflen, "AF_UNKNOWN");
+ snprintf(buf, len, "AF_UNKNOWN");
break;
}
diff --git a/utils.h b/utils.h
index adedc37..8317e22 100644
--- a/utils.h
+++ b/utils.h
@@ -61,9 +61,11 @@ void connect_any(struct cfg *cfg, struct uring_task *task,
struct list_head *addrs, struct connection *conn,
void (*callback)(struct cfg *, struct connection *, bool));
+char *sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len);
+
uint16_t sockaddr_port(struct sockaddr_in46 *addr);
-char *sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen);
+char *sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t len);
int strtou16_strict(const char *str, uint16_t *result);