Skip to content

Commit 2205ff6

Browse files
committed
logging: add square brackets when print raw ipv6 addresses
otherwise it'll print malformed ip in log like "Connection to fe80:3221::1:32514 closed"
1 parent 3e8d466 commit 2205ff6

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/cli-session.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ static void cli_finished() {
376376
TRACE(("cli_finished()"))
377377

378378
session_cleanup();
379+
/* IPv6 need to be captuled by [] */
379380
fprintf(stderr, "Connection to %s@%s:%s closed.\n", cli_opts.username,
380-
cli_opts.remotehost, cli_opts.remoteport);
381+
cli_opts.remotehost, cli_opts.remoteport);
381382
exit(cli_ses.retval);
382383
}
383384

@@ -422,10 +423,17 @@ void cli_dropbear_exit(int exitcode, const char* format, va_list param) {
422423
if (!ses.init_done) {
423424
snprintf(fullmsg, sizeof(fullmsg), "Exited: %s", exitmsg);
424425
} else {
425-
snprintf(fullmsg, sizeof(fullmsg),
426+
if (strchr(cli_opts.remotehost, ':') != NULL) {
427+
snprintf(fullmsg, sizeof(fullmsg),
428+
"Connection to %s@[%s]:%s exited: %s",
429+
cli_opts.username, cli_opts.remotehost,
430+
cli_opts.remoteport, exitmsg);
431+
} else {
432+
snprintf(fullmsg, sizeof(fullmsg),
426433
"Connection to %s@%s:%s exited: %s",
427434
cli_opts.username, cli_opts.remotehost,
428435
cli_opts.remoteport, exitmsg);
436+
}
429437
}
430438

431439
/* Do the cleanup first, since then the terminal will be reset */

src/svr-main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ static void main_inetd() {
105105
if (svr_opts.reexec_childpipe < 0) {
106106
/* In case our inetd was lax in logging source addresses */
107107
get_socket_address(0, NULL, NULL, &host, &port, 0);
108-
dropbear_log(LOG_INFO, "Child connection from %s:%s", host, port);
108+
if (strchr(host, ':') != NULL) {
109+
dropbear_log(LOG_INFO, "Child connection from [%s]:%s", host, port);
110+
} else {
111+
dropbear_log(LOG_INFO, "Child connection from %s:%s", host, port);
112+
}
113+
109114
m_free(host);
110115
m_free(port);
111116

@@ -326,7 +331,12 @@ static void main_noinetd(int argc, char ** argv, const char* multipath) {
326331

327332
/* child */
328333
getaddrstring(&remoteaddr, NULL, &remote_port, 0);
329-
dropbear_log(LOG_INFO, "Child connection from %s:%s", remote_host, remote_port);
334+
if (strchr(remote_host, ':') != NULL) {
335+
dropbear_log(LOG_INFO, "Child connection from [%s]:%s", remote_host, remote_port);
336+
} else {
337+
dropbear_log(LOG_INFO, "Child connection from %s:%s", remote_host, remote_port);
338+
}
339+
330340
m_free(remote_host);
331341
m_free(remote_port);
332342

src/svr-session.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ void svr_session(int sock, int childpipe) {
121121
get_socket_address(ses.sock_in, NULL, NULL, &host, &port, 0);
122122
len = strlen(host) + strlen(port) + 2;
123123
svr_ses.addrstring = m_malloc(len);
124+
/* bracket is needed if it's IPv6 address */
125+
if (strchr(host, ':') != NULL) {
126+
snprintf(svr_ses.addrstring, len, "[%s]:%s", host, port);
127+
} else {
124128
snprintf(svr_ses.addrstring, len, "%s:%s", host, port);
129+
}
125130
m_free(host);
126131
m_free(port);
127132

0 commit comments

Comments
 (0)