summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNate Straz <nstraz@redhat.com>2006-06-08 22:28:28 +0000
committerNathan Straz <nstraz@redhat.com>2008-09-23 09:37:46 -0400
commit9f0149df7f6da242bfef3505dabbc8a73c97dcba (patch)
tree5af07ef576b8c9c9acfba46c0045d3c6072d28ea
parent935412b4721816438dd8c5ff9e43b470c74dfaa9 (diff)
downloadqarsh-9f0149df7f6da242bfef3505dabbc8a73c97dcba.zip
qarsh-9f0149df7f6da242bfef3505dabbc8a73c97dcba.tar.gz
qarsh-9f0149df7f6da242bfef3505dabbc8a73c97dcba.tar.xz
Add some checking to see if we get responses from someone we shouldn't be.
-rw-r--r--btime.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/btime.c b/btime.c
index f22c08b..bb0857e 100644
--- a/btime.c
+++ b/btime.c
@@ -12,6 +12,7 @@
#include "btime_int.h"
+static int same_addr(struct sockaddr_in *a, struct sockaddr_in *b);
/*
* btime --
*
@@ -29,7 +30,9 @@ btime(const char *host)
char msg[BTIME_MSGLEN];
struct sockaddr_in serv_addr;
struct sockaddr_in my_addr;
+ struct sockaddr_in resp_addr;
int serv_addr_len;
+ int resp_addr_len;
struct hostent *hostent;
in_addr_t inaddr;
unsigned int btime;
@@ -83,17 +86,26 @@ btime(const char *host)
}
+ memset(&resp_addr, 0, sizeof resp_addr);
memset(msg, 0, BTIME_MSGLEN);
for (retry = 0; retry < MAX_RETRY; retry++) {
- serv_addr_len = sizeof serv_addr;
+ resp_addr_len = sizeof resp_addr;
if ((nbytes = recvfrom(sd, &msg, BTIME_MSGLEN, MSG_DONTWAIT,
- (struct sockaddr *)&serv_addr, &serv_addr_len)) < 0) {
+ (struct sockaddr *)&resp_addr, &resp_addr_len)) < 0) {
if (errno == EAGAIN) {
usleep(retry * 100);
} else {
/* Non EAGAIN error... */
break;
}
+ } else if (!same_addr(&serv_addr, &resp_addr)) {
+ char *rstr = strdup(inet_ntoa(resp_addr.sin_addr));
+ char *sstr = strdup(inet_ntoa(serv_addr.sin_addr));
+ fprintf(stderr, "Got response from %s instead of %s\n",
+ rstr, sstr);
+ free(rstr);
+ free(sstr);
+ continue;
} else {
break;
}
@@ -109,3 +121,23 @@ btime(const char *host)
return btime;
}
+
+/* same_addr
+ *
+ * Compare to struct sockaddr_in's
+ *
+ * Returns:
+ * 1 if the addresses match
+ * 0 if the addresses are different
+ */
+static int
+same_addr(struct sockaddr_in *a, struct sockaddr_in *b)
+{
+ if (a->sin_family != b->sin_family) {
+ return 0;
+ } else if (a->sin_addr.s_addr != b->sin_addr.s_addr) {
+ return 0;
+ } else {
+ return 1;
+ }
+}