aboutsummaryrefslogtreecommitdiff
path: root/eperd/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'eperd/ping.c')
-rw-r--r--eperd/ping.c90
1 files changed, 59 insertions, 31 deletions
diff --git a/eperd/ping.c b/eperd/ping.c
index bf51019..8a6d2a2 100644
--- a/eperd/ping.c
+++ b/eperd/ping.c
@@ -14,12 +14,16 @@
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
+#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include "eperd.h"
#define SAFE_PREFIX ATLAS_DATA_NEW
+/* Don't report psize yet. */
+#define DO_PSIZE 0
+
#define DBQ(str) "\"" #str "\""
#define PING_OPT_STRING ("!46rc:s:A:O:")
@@ -40,7 +44,9 @@ enum
* default data size is calculated as to be like the original
*/
#define IPHDR 20
-#define MAX_DATA_SIZE (4096 - IPHDR - ICMP_MINLEN)
+#define MAX_DATA_SIZE (4096 - IPHDR)
+
+#define ICMP6_HDRSIZE (offsetof(struct icmp6_hdr, icmp6_data16[2]))
/* Error codes */
#define PING_ERR_NONE 0
@@ -109,6 +115,7 @@ struct pingstate
char no_dst;
unsigned char ttl;
unsigned size;
+ unsigned psize;
char *result;
size_t reslen;
@@ -246,6 +253,10 @@ static void report(struct pingstate *state)
fprintf(fh, ", " DBQ(ttl) ":%d", state->ttl);
fprintf(fh, ", " DBQ(size) ":%d", state->size);
+#if DO_PSIZE
+ if (state->psize != -1)
+ fprintf(fh, ", " DBQ(psize) ":%d", state->psize);
+#endif /* DO_PSIZE */
fprintf(fh, ", \"result\": [ %s ] }\n", state->result);
free(state->result);
@@ -257,7 +268,7 @@ static void report(struct pingstate *state)
fclose(fh);
}
-static void ping_cb(int result, int bytes,
+static void ping_cb(int result, int bytes, int psize,
struct sockaddr *sa, socklen_t socklen,
struct sockaddr *loc_sa, socklen_t loc_socklen,
int seq, int ttl,
@@ -265,7 +276,7 @@ static void ping_cb(int result, int bytes,
{
struct pingstate *pingstate;
unsigned long usecs;
- char namebuf[NI_MAXHOST];
+ char namebuf1[NI_MAXHOST], namebuf2[NI_MAXHOST];
char line[256];
(void)socklen; /* Suppress GCC unused parameter warning */
@@ -287,6 +298,7 @@ static void ping_cb(int result, int bytes,
if (pingstate->first)
{
pingstate->size= bytes;
+ pingstate->psize= psize;
pingstate->ttl= ttl;
}
@@ -325,6 +337,15 @@ static void ping_cb(int result, int bytes,
add_str(pingstate, line);
pingstate->size= bytes;
}
+ if (pingstate->psize != psize && psize != -1)
+ {
+#if DO_PSIZE
+ snprintf(line, sizeof(line),
+ ", " DBQ(psize) ":%d", psize);
+ add_str(pingstate, line);
+#endif /* DO_PSIZE */
+ pingstate->psize= psize;
+ }
if (pingstate->ttl != ttl)
{
snprintf(line, sizeof(line),
@@ -332,14 +353,21 @@ static void ping_cb(int result, int bytes,
add_str(pingstate, line);
pingstate->ttl= ttl;
}
- if (memcmp(&pingstate->loc_sin6, loc_sa, loc_socklen) != 0)
+ namebuf1[0]= '\0';
+ getnameinfo(&pingstate->loc_sin6, loc_socklen, namebuf1,
+ sizeof(namebuf1), NULL, 0, NI_NUMERICHOST);
+ namebuf2[0]= '\0';
+ getnameinfo(loc_sa, loc_socklen, namebuf2,
+ sizeof(namebuf2), NULL, 0, NI_NUMERICHOST);
+
+ if (strcmp(namebuf1, namebuf2) != 0)
{
- namebuf[0]= '\0';
- getnameinfo(loc_sa, loc_socklen, namebuf,
- sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
+ printf("loc_sin6: %s\n", namebuf1);
+
+ printf("loc_sa: %s\n", namebuf2);
snprintf(line, sizeof(line),
- ", " DBQ(srcaddr) ":" DBQ(%s), namebuf);
+ ", " DBQ(srcaddr) ":" DBQ(%s), namebuf2);
add_str(pingstate, line);
}
@@ -367,14 +395,14 @@ static void ping_cb(int result, int bytes,
{
if (pingstate->first && pingstate->loc_socklen != 0)
{
- namebuf[0]= '\0';
+ namebuf1[0]= '\0';
getnameinfo((struct sockaddr *)&pingstate->loc_sin6,
pingstate->loc_socklen,
- namebuf, sizeof(namebuf),
+ namebuf1, sizeof(namebuf1),
NULL, 0, NI_NUMERICHOST);
snprintf(line, sizeof(line),
- ", " DBQ(srcaddr) ":" DBQ(%s), namebuf);
+ ", " DBQ(srcaddr) ":" DBQ(%s), namebuf1);
add_str(pingstate, line);
}
add_str(pingstate, " }");
@@ -383,6 +411,7 @@ static void ping_cb(int result, int bytes,
if (result == PING_ERR_DNS)
{
pingstate->size= bytes;
+ pingstate->psize= psize;
snprintf(line, sizeof(line),
"%s{ " DBQ(error) ":" DBQ(dns resolution failed: %s) " }",
pingstate->first ? "" : ", ", (char *)sa);
@@ -450,11 +479,11 @@ static void fmticmp4(u_char *buffer, size_t *sizep, u_int8_t seq,
struct timeval now;
- minlen= ICMP_MINLEN + sizeof(*data);
+ minlen= sizeof(*data);
if (*sizep < minlen)
*sizep= minlen;
- if (*sizep > MAX_DATA_SIZE)
- *sizep= MAX_DATA_SIZE;
+ if (*sizep > MAX_DATA_SIZE - ICMP_MINLEN)
+ *sizep= MAX_DATA_SIZE - ICMP_MINLEN;
if (*sizep > minlen)
memset(buffer+minlen, '\0', *sizep-minlen);
@@ -472,7 +501,7 @@ static void fmticmp4(u_char *buffer, size_t *sizep, u_int8_t seq,
/* Last, compute ICMP checksum */
icmp->icmp_cksum = 0;
- icmp->icmp_cksum = mkcksum((u_short *) icmp, *sizep); /* ones complement checksum of struct */
+ icmp->icmp_cksum = mkcksum((u_short *) icmp, ICMP_MINLEN + *sizep); /* ones complement checksum of struct */
}
@@ -496,15 +525,15 @@ static void fmticmp6(u_char *buffer, size_t *sizep,
{
size_t minlen;
struct icmp6_hdr *icmp = (struct icmp6_hdr *) buffer;
- struct evdata *data = (struct evdata *) (buffer + offsetof(struct icmp6_hdr, icmp6_data16[2]));
+ struct evdata *data = (struct evdata *) (buffer + ICMP6_HDRSIZE);
struct timeval now;
- minlen= offsetof(struct icmp6_hdr, icmp6_data16[2]) + sizeof(*data);
+ minlen= sizeof(*data);
if (*sizep < minlen)
*sizep= minlen;
- if (*sizep > MAX_DATA_SIZE)
- *sizep= MAX_DATA_SIZE;
+ if (*sizep > MAX_DATA_SIZE - ICMP6_HDRSIZE)
+ *sizep= MAX_DATA_SIZE - ICMP6_HDRSIZE;
if (*sizep > minlen)
memset(buffer+minlen, '\0', *sizep-minlen);
@@ -532,10 +561,11 @@ static void ping_xmit(struct pingstate *host)
int nsent, fd4, fd6, t_errno, r;
host->send_error= 0;
+ host->got_reply= 0;
if (host->sentpkts >= host->maxpkts)
{
/* Done. */
- ping_cb(PING_ERR_DONE, host->cursize,
+ ping_cb(PING_ERR_DONE, host->cursize, host->psize,
(struct sockaddr *)&host->sin6, host->socklen,
(struct sockaddr *)&host->loc_sin6, host->loc_socklen,
0, host->rcvd_ttl, NULL,
@@ -572,7 +602,7 @@ static void ping_xmit(struct pingstate *host)
}
}
- nsent = sendto(fd6, base->packet, host->cursize,
+ nsent = sendto(fd6, base->packet, host->cursize+ICMP6_HDRSIZE,
MSG_DONTWAIT, (struct sockaddr *)&host->sin6,
host->socklen);
@@ -601,7 +631,7 @@ static void ping_xmit(struct pingstate *host)
}
- nsent = sendto(fd4, base->packet, host->cursize,
+ nsent = sendto(fd4, base->packet, host->cursize+ICMP_MINLEN,
MSG_DONTWAIT, (struct sockaddr *)&host->sin6,
host->socklen);
@@ -622,7 +652,7 @@ static void ping_xmit(struct pingstate *host)
host->send_error= 1;
/* Report the failure and stop */
- ping_cb(PING_ERR_SENDTO, host->cursize,
+ ping_cb(PING_ERR_SENDTO, host->cursize, -1,
(struct sockaddr *)&host->sin6, host->socklen,
(struct sockaddr *)&host->loc_sin6, host->loc_socklen,
errno, 0, NULL,
@@ -642,7 +672,7 @@ static void noreply_callback(int __attribute((unused)) unused, const short __att
if (!host->got_reply && !host->send_error)
{
- ping_cb(PING_ERR_TIMEOUT, host->cursize,
+ ping_cb(PING_ERR_TIMEOUT, host->cursize, -1,
(struct sockaddr *)&host->sin6, host->socklen,
NULL, 0,
host->seq, -1, &host->base->tv_interval,
@@ -765,7 +795,7 @@ printf("ready_callback4: too short\n");
*/
isDup= (ntohs(icmp->un.echo.sequence) != host->seq);
ping_cb(isDup ? PING_ERR_DUP : PING_ERR_NONE,
- nrecv - IPHDR,
+ nrecv - IPHDR - ICMP_MINLEN, nrecv,
(struct sockaddr *)&host->sin6, host->socklen,
(struct sockaddr *)&loc_sin4, sizeof(loc_sin4),
ntohs(icmp->un.echo.sequence), ip->ip_ttl, &elapsed,
@@ -895,7 +925,7 @@ static void ready_callback6 (int __attribute((unused)) unused,
*/
isDup= (ntohs(icmp->icmp6_seq) != host->seq);
ping_cb(isDup ? PING_ERR_DUP : PING_ERR_NONE,
- nrecv - IPHDR,\
+ nrecv - ICMP6_HDRSIZE, nrecv + sizeof(struct ip6_hdr),
(struct sockaddr *)&host->sin6, host->socklen,
(struct sockaddr *)&loc_sin6, sizeof(loc_sin6),
ntohs(icmp->icmp6_seq), host->rcvd_ttl, &elapsed,
@@ -1159,12 +1189,12 @@ static void dns_cb(int result, struct evutil_addrinfo *res, void *ctx)
if (result != 0)
{
- ping_cb(PING_ERR_DNS, env->maxsize,
+ ping_cb(PING_ERR_DNS, env->maxsize, -1,
(struct sockaddr *)evutil_gai_strerror(result), 0,
(struct sockaddr *)NULL, 0,
0, 0, NULL,
env);
- ping_cb(PING_ERR_DONE, env->maxsize,
+ ping_cb(PING_ERR_DONE, env->maxsize, -1,
(struct sockaddr *)NULL, 0,
(struct sockaddr *)NULL, 0,
0, 0, NULL,
@@ -1200,7 +1230,7 @@ static void dns_cb(int result, struct evutil_addrinfo *res, void *ctx)
evutil_freeaddrinfo(env->dns_res);
env->dns_res= NULL;
env->dns_curr= NULL;
- ping_cb(PING_ERR_DNS_NO_ADDR, env->cursize,
+ ping_cb(PING_ERR_DNS_NO_ADDR, env->cursize, -1,
(struct sockaddr *)NULL, 0,
(struct sockaddr *)NULL, 0,
0, 0, NULL,
@@ -1242,8 +1272,6 @@ static void ping_start(void *state)
memset(&hints, '\0', sizeof(hints));
hints.ai_socktype= SOCK_DGRAM;
hints.ai_family= pingstate->af;
- printf("hostname '%s', family %d\n",
- pingstate->hostname, hints.ai_family);
(void) evdns_getaddrinfo(DnsBase, pingstate->hostname, NULL,
&hints, dns_cb, pingstate);
}