Skip to content

Instantly share code, notes, and snippets.

@sfionov
Created March 31, 2012 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfionov/2260827 to your computer and use it in GitHub Desktop.
Save sfionov/2260827 to your computer and use it in GitHub Desktop.
iputils ping fallback to numeric addresses while exiting
From 469c214e8909822e320bcd05c6dc741802c37816 Mon Sep 17 00:00:00 2001
From: Sergey Fionov <fionov@gmail.com>
Date: Sat, 31 Mar 2012 16:50:01 +0400
Subject: [PATCH] iputils,ping,ping6: Fallback to numeric addresses while exiting
When user presses Ctrl-C ping waits for pr_addr() to be finished and
then exits. With poor connection it may result in stuck at
gethostbyaddr().
This patch changes handling of exit signals. After setting "exiting"
flag we longjmp() from gethostbyaddr() and print numeric addresses.
---
ping.c | 7 ++++++-
ping6.c | 6 +++++-
ping_common.c | 4 ++++
ping_common.h | 4 ++++
4 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/ping.c b/ping.c
index eacb29d..ddce1fe 100644
--- a/ping.c
+++ b/ping.c
@@ -1160,12 +1160,17 @@ pr_addr(__u32 addr)
struct hostent *hp;
static char buf[4096];
- if ((options & F_NUMERIC) ||
+ in_pr_addr = !setjmp(pr_addr_jmp);
+
+ if (exiting || (options & F_NUMERIC) ||
!(hp = gethostbyaddr((char *)&addr, 4, AF_INET)))
sprintf(buf, "%s", inet_ntoa(*(struct in_addr *)&addr));
else
snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
inet_ntoa(*(struct in_addr *)&addr));
+
+ in_pr_addr = 0;
+
return(buf);
}
diff --git a/ping6.c b/ping6.c
index c5ff881..f5beaf2 100644
--- a/ping6.c
+++ b/ping6.c
@@ -1538,9 +1538,13 @@ char * pr_addr(struct in6_addr *addr)
{
struct hostent *hp = NULL;
- if (!(options&F_NUMERIC))
+ in_pr_addr = !setjmp(pr_addr_jmp);
+
+ if (!(exiting || options&F_NUMERIC))
hp = gethostbyaddr((__u8*)addr, sizeof(struct in6_addr), AF_INET6);
+ in_pr_addr = 0;
+
return hp ? hp->h_name : pr_addr_n(addr);
}
diff --git a/ping_common.c b/ping_common.c
index 82320b1..b45b5c8 100644
--- a/ping_common.c
+++ b/ping_common.c
@@ -30,6 +30,8 @@ struct timeval start_time, cur_time;
volatile int exiting;
volatile int status_snapshot;
int confirm = 0;
+int in_pr_addr = 0; /* pr_addr() is executing */
+jmp_buf pr_addr_jmp;
/* Stupid workarounds for bugs/missing functionality in older linuces.
* confirm_flag fixes refusing service of kernels without MSG_CONFIRM.
@@ -248,6 +250,8 @@ void common_options(int ch)
static void sigexit(int signo)
{
exiting = 1;
+ if (in_pr_addr)
+ longjmp(pr_addr_jmp, 0);
}
static void sigstatus(int signo)
diff --git a/ping_common.h b/ping_common.h
index 730cf9b..2262f91 100644
--- a/ping_common.h
+++ b/ping_common.h
@@ -16,6 +16,7 @@
#include <errno.h>
#include <string.h>
#include <netdb.h>
+#include <setjmp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -206,3 +207,6 @@ extern int gather_statistics(__u8 *ptr, int icmplen,
int csfailed, struct timeval *tv, char *from,
void (*pr_reply)(__u8 *ptr, int cc));
extern void print_timestamp(void);
+
+extern int in_pr_addr;
+extern jmp_buf pr_addr_jmp;
--
1.7.8.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment