summaryrefslogtreecommitdiffstats
path: root/btime.c
blob: 5e151be81cf5be60d3edb2a217910bec31d654bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "btime_int.h"

static int same_addr(struct sockaddr_in *a, struct sockaddr_in *b);
static void gen_cookie(char *s);
/*
 * btime --
 * 
 *	Return the boot time of a remote host if that host is running the
 *	btimed deamon.
 *
 * Returns:
 *	btime, or 0 on failure.
 */

unsigned int
btime(const char *host)
{
	int sd;
	char cookie[BTIME_MSGLEN];
	char response[BTIME_MSGLEN];
	struct sockaddr_in serv_addr;
	struct sockaddr_in my_addr;
	struct sockaddr_in resp_addr;
	socklen_t serv_addr_len;
	socklen_t resp_addr_len;
	struct hostent *hostent;
	in_addr_t inaddr;
	unsigned int btime = 0;
	ssize_t nbytes;	
	int retry;

	if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		fprintf(stderr, "Could not create socket: %s\n", 
		        strerror(errno));
		return 0;
	}

	memset(&serv_addr, 0, sizeof serv_addr);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(BTIME_PORT);

	if ((inaddr = inet_addr(host)) != INADDR_NONE) {
		memcpy(&serv_addr.sin_addr, &inaddr, sizeof inaddr);
	} else {
		if ((hostent = gethostbyname(host)) == NULL) {
			return 0;
		}

		memcpy(&serv_addr.sin_addr, hostent->h_addr, hostent->h_length);
	}

	memset(&my_addr, 0, sizeof my_addr);
	my_addr.sin_family = AF_INET;
	my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	my_addr.sin_port = htons(0);
	if (bind(sd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
		fprintf(stderr, "Could not bind to local address: %s\n", 
		        strerror(errno));
		return 0;
	}

	memset(cookie, 0, BTIME_MSGLEN);
	gen_cookie(cookie);
	for (retry = 0; retry < MAX_RETRY; retry++) {
		serv_addr_len = sizeof serv_addr;
		if ((nbytes = sendto(sd, &cookie, BTIME_MSGLEN, MSG_DONTWAIT,
	       	 (struct sockaddr *)&serv_addr, serv_addr_len)) < 0) {
			if (errno == EAGAIN) {
				usleep(retry * 100);
			} else {
				/* Non EAGAIN error... */
				break;
			}
		} else {
			break;
		}
	}
	

	memset(&resp_addr, 0, sizeof resp_addr);
	memset(response, 0, BTIME_MSGLEN);
	for (retry = 0; retry < MAX_RETRY; retry++) {
		resp_addr_len = sizeof resp_addr;
		if ((nbytes = recvfrom(sd, &response, BTIME_MSGLEN, MSG_DONTWAIT, 
	         (struct sockaddr *)&resp_addr, &resp_addr_len)) < 0) {
			if (errno == EAGAIN) {
				usleep(retry * 100);
				continue;
			} else {
				/* Non EAGAIN error... */
				break;
			}
		}

		if (nbytes == 0) {
			/* Nothing received, try again */
			continue;
		} else if (response[0] == 'B' && response[1] == 'T') {
			/* Check for new style cookie */
			if (memcmp(cookie, response, COOKIE_LEN) == 0) {
				btime = strtoul(response+COOKIE_LEN, NULL, 10);
				break;
			} else {
				fprintf(stderr, "Ignoring invalid cookie\n");
				continue;
			}
		} else if (!same_addr(&serv_addr, &resp_addr)) {
			/* If no cookie, check peer */
			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 {
			/* If peer matches, accept the bare btime */
			btime = strtoul(response, (char **)NULL, 10);
			break;
		}
	}

	close(sd);

	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;
	}
}
/* gen_cookie
 *
 * 	Generate a random string that btimed will send back to us.
 */
static void gen_cookie(char *s)
{
	char *a = s;
	int i;

	*a++ = 'B';
	*a++ = 'T';
	for (i = 0; i < COOKIE_RANDOM_PARTS; i++) {
		*(int *)a = random();
		a += sizeof(int);
	}
	*a = '\n';
}