Skip to content

Commit 2ee82d1

Browse files
ammarfaizi2axboe
authored andcommitted
io_uring-udp: Fix the wrong IPv6 binary to string conversion
Another io_uring-udp fix. The verbose output shows the wrong address when using IPv6. When the address family is AF_INET6, the pointer should be cast to 'struct sockaddr_in6', not 'struct sockaddr_in'. While in there, also add a square bracket around the IP address to easily read the port number, especially for IPv6. Before this patch: port bound to 49567 received 4 bytes 28 from ::2400:6180:0:d1:0:0:47048 received 4 bytes 28 from ::2400:6180:0:d1:0:0:54755 received 4 bytes 28 from ::2400:6180:0:d1:0:0:57968 (the IPv6 address is wrong) After this patch: port bound to 48033 received 4 bytes 28 from [2400:6180:0:d1::6a4:a00f]:40456 received 4 bytes 28 from [2400:6180:0:d1::6a4:a00f]:50306 received 4 bytes 28 from [2400:6180:0:d1::6a4:a00f]:52291 Link: axboe#814 (comment) Fixes: axboe#814 Fixes: 61d472b ("add an example for a UDP server") Signed-off-by: Ammar Faizi <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 4fed795 commit 2ee82d1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

examples/io_uring-udp.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,22 @@ static int process_cqe_recv(struct ctx *ctx, struct io_uring_cqe *cqe,
271271
}
272272

273273
if (ctx->verbose) {
274+
struct sockaddr_in *addr = io_uring_recvmsg_name(o);
275+
struct sockaddr_in6 *addr6 = (void *)addr;
274276
char buff[INET6_ADDRSTRLEN + 1];
275277
const char *name;
276-
struct sockaddr_in *addr = io_uring_recvmsg_name(o);
278+
void *paddr;
277279

278-
name = inet_ntop(ctx->af, &addr->sin_addr, buff, sizeof(buff));
280+
if (ctx->af == AF_INET6)
281+
paddr = &addr6->sin6_addr;
282+
else
283+
paddr = &addr->sin_addr;
284+
285+
name = inet_ntop(ctx->af, paddr, buff, sizeof(buff));
279286
if (!name)
280287
name = "<INVALID>";
281-
fprintf(stderr, "received %u bytes %d from %s:%d\n",
288+
289+
fprintf(stderr, "received %u bytes %d from [%s]:%d\n",
282290
io_uring_recvmsg_payload_length(o, cqe->res, &ctx->msg),
283291
o->namelen, name, (int)ntohs(addr->sin_port));
284292
}

0 commit comments

Comments
 (0)