summaryrefslogtreecommitdiffstats
path: root/btime.c
blob: 15d52f13a556ec4288c2e1dcf0e51c91a49c81b4 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* 
 * Copyright © 2005-2008  Red Hat, Inc. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions of the
 * GNU General Public License v.2.  This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
 * including the implied warranties of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
 * trademarks that are incorporated in the source code or documentation are not
 * subject to the GNU General Public License and may only be used or replicated
 * with the express permission of Red Hat, Inc.
 *
 * Red Hat Author(s): Nathan Straz <nstraz@redhat.com>
 *                    Dean Jansa <djansa@redhat.com>
 */
#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_storage *a, struct sockaddr_storage *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 addrinfo *serv_ail, *serv_aip;
	struct addrinfo *my_ail, *my_aip;
	struct sockaddr_storage resp_addr;
	struct addrinfo hints;
	socklen_t resp_addr_len;
	unsigned int btime = 0;
	ssize_t nbytes;	
	int retry;
	fd_set sdset;
	struct timeval timeout;

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	if (getaddrinfo(host, "5016", &hints, &serv_ail) != 0) {
		fprintf(stderr, "Could not get address info for %s: %s\n",
		        host, strerror(errno));
		return 0;
	}
	
	for (serv_aip = serv_ail; serv_aip != NULL; serv_aip = serv_aip->ai_next) {
		if ((sd = socket(serv_aip->ai_family, serv_aip->ai_socktype, 
		                 serv_aip->ai_protocol)) < 0) {
			continue;
		}

		break;
	}

	if (serv_aip == NULL) {
		fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
		freeaddrinfo(serv_ail);
		return 0;
	}

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_PASSIVE;     /* Fill in my IP for me */
	if (getaddrinfo(NULL, "0", &hints, &my_ail) != 0) {
		fprintf(stderr, "Could not get address info for myself!: %s\n", strerror(errno));
		return 0;
	}

	for (my_aip = my_ail; my_aip != NULL; my_aip = my_aip->ai_next) {
		if (bind(sd, my_aip->ai_addr, my_aip->ai_addrlen) < 0) {
			continue;
		}

		break;
	}

	if (my_aip == NULL) {
		fprintf(stderr, "Could not bind to local address: %s\n", 
	   	     strerror(errno));
		freeaddrinfo(my_ail);
		return 0;
	}

	memset(cookie, 0, BTIME_MSGLEN);
	gen_cookie(cookie);
	for (retry = 0; retry < MAX_RETRY; retry++) {
		if ((nbytes = sendto(sd, &cookie, BTIME_MSGLEN, MSG_DONTWAIT,
	       	 serv_aip->ai_addr, serv_aip->ai_addrlen)) < 0) {
			if (errno == EAGAIN) {
				usleep(retry * 100);
			} else {
				/* Non EAGAIN error... */
				break;
			}
		} else {
			break;
		}
	}
	
	FD_ZERO(&sdset);
	FD_SET(sd, &sdset);
	do {
		retry = 0;

		timeout.tv_sec = 0;
		timeout.tv_usec = 500000;

		if (select(sd + 1, &sdset, NULL, NULL, &timeout) != 1) {
			/* Either an error or a timeout, we don't need to retry */
			break;
		}

		memset(&resp_addr, 0, sizeof resp_addr);
		memset(response, 0, BTIME_MSGLEN);
		resp_addr_len = sizeof resp_addr;
		nbytes = recvfrom(sd, &response, BTIME_MSGLEN, MSG_DONTWAIT, 
				(struct sockaddr *)&resp_addr, &resp_addr_len);
		if (nbytes == 0) {
			/* Nothing received */
			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 {
				/* ignore the invalid btime cookie */
				retry = 1;
				continue;
			}
		} else if (!same_addr((struct sockaddr_storage *)serv_aip->ai_addr, &resp_addr)) {
			/* If no cookie, check peer */
			char rstr[NI_MAXHOST];
			char sstr[NI_MAXHOST];

			inet_ntop(resp_addr.ss_family, &resp_addr, rstr, NI_MAXHOST);
			inet_ntop(serv_aip->ai_family, serv_aip->ai_addr, sstr, NI_MAXHOST);
			fprintf(stderr, "Got response from %s instead of %s\n",
					rstr, sstr);
			retry = 1;
			continue;
		} else {
			/* If peer matches, accept the bare btime */
			btime = strtoul(response, (char **)NULL, 10);
			break;
		}
	} while (retry);

	close(sd);
	freeaddrinfo(serv_ail);
	freeaddrinfo(my_ail);

	return btime;
}

/* same_addr
 *
 * 	Compare two struct sockaddr_storage
 *
 * Returns:
 * 	1 if the addresses match
 * 	0 if the addresses are different
 */
static int
same_addr(struct sockaddr_storage *a, struct sockaddr_storage *b)
{
	return 1;
	if (a->ss_family != b->ss_family) {
		return 0;
	} else if ((a->ss_family == AF_INET) 
	            && (((struct sockaddr_in *)a)->sin_addr.s_addr 
	                != ((struct sockaddr_in *)b)->sin_addr.s_addr)) {
			return 0;
	} else if ((a->ss_family == AF_INET6) 
	            && (((struct sockaddr_in6 *)a)->sin6_addr.s6_addr 
		            != ((struct sockaddr_in6 *)b)->sin6_addr.s6_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++) {
		*(int32_t *)a = (int32_t)random();
		a += sizeof(int32_t);
	}
	*a = '\n';
}