proxyclient.c 5.88 KB
Newer Older
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2003-2007 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 * All rights reserved.
 */

/*
 *
 */
#include <stdio.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <paths.h>
#include <errno.h>
18
#include <string.h>
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#include "event.h"
#include "tbdefs.h"
#include "log.h"

static int		debug = 1;
static event_handle_t	localhandle;

/* protos */
static int	RemoteSocket(int *sockp, int *ourport);
static int	ServerConnect(int, int, struct in_addr, int);
static int	GetEvents(event_handle_t handle, int sock);

void
usage(char *progname)
{
    fprintf(stderr, "Usage: %s [-s server] -e pid/eid\n", progname);
    exit(-1);
}

int
main(int argc, char **argv)
{
	char			*progname;
	char			*server = NULL;
	int			serverport = 9843;
	char			*myeid = NULL;
	char			buf[BUFSIZ];
	char			hostname[MAXHOSTNAMELEN];
	struct hostent		*he;
	int			sock, c, ourport;
	struct in_addr		myip, serverip;
	FILE			*fp;

	progname = argv[0];
	
	while ((c = getopt(argc, argv, "ds:p:e:")) != -1) {
		switch (c) {
		case 'd':
			debug++;
			break;
		case 's':
			server = optarg;
			break;
		case 'p':
			serverport = atoi(optarg);
			break;
		case 'e':
			myeid = optarg;
			break;
		default:
			usage(progname);
		}
	}
	argc -= optind;
	argv += optind;

	if (argc)
		usage(progname);

	if (! myeid)
		fatal("Must provide pid/eid");

	if (debug)
		loginit(0, 0);
	else {
		loginit(1, "proxyclient");
		/* See below for daemonization */
	}

	/*
	 * Get our IP address. Thats how we name this host to the
	 * event System. 
	 */
	if (gethostname(hostname, MAXHOSTNAMELEN) == -1) {
		fatal("could not get hostname: %s\n", strerror(errno));
	}
	if (! (he = gethostbyname(hostname))) {
		fatal("could not get IP address from hostname: %s", hostname);
	}
	memcpy((char *)&myip, he->h_addr, he->h_length);

	/*
106
	 * If server is not specified, then it defaults to EVENTSERVER.
107
108
109
110
	 * This allows the client to work on either users.emulab.net
	 * or on a client node. 
	 */
	if (!server)
111
		server = EVENTSERVER;
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

	if (! (he = gethostbyname(server))) {
		fatal("could not get IP address for server: %s", server);
	}
	memcpy((char *)&serverip, he->h_addr, he->h_length);

	/* Register with the event system on the local node */
	localhandle = event_register("elvin://localhost", 0);
	if (localhandle == NULL) {
		fatal("could not register with local event system");
	}
	
	/*
	 * Stash the pid away.
	 */
	sprintf(buf, "%s/evproxy.pid", _PATH_VARRUN);
	fp = fopen(buf, "w");
	if (fp != NULL) {
		fprintf(fp, "%d\n", getpid());
		(void) fclose(fp);
	}

	RemoteSocket(&sock, &ourport);
	ServerConnect(sock, ourport, serverip, serverport);

	/*
	 * Do this now, once we have had a chance to fail on the above
	 * event system calls.
	 */
	if (!debug)
		daemon(0, 0);

	while (1) {
		ServerConnect(sock, ourport, serverip, serverport);
		GetEvents(localhandle, sock);
	}

	/* Unregister with the local event system: */
	if (event_unregister(localhandle) == 0) {
		fatal("could not unregister with local event system");
	}

	return 0;
}


/*
 * Create the remote socket to talk to the server side. This is a UDP socket.
 * The local port is dynamic.
 */
static int
RemoteSocket(int *sockp, int *ourport)
{
	int			sock, length, i;
	struct sockaddr_in	name;
	struct timeval		timeout;

	/* Create socket from which to read. */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		pfatal("opening stream socket");
	}

	i = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
		       (char *)&i, sizeof(i)) < 0)
		pwarning("setsockopt(SO_REUSEADDR)");
	
	/* Create name. */
	name.sin_family = AF_INET;
	name.sin_addr.s_addr = INADDR_ANY;
	name.sin_port = 0;
	if (bind(sock, (struct sockaddr *) &name, sizeof(name))) {
		pfatal("binding dgram socket");
	}

	/* Find assigned port value and print it out. */
	length = sizeof(name);
	if (getsockname(sock, (struct sockaddr *) &name, &length)) {
		pfatal("getsockname");
	}
	info("listening on UDP port %d\n", ntohs(name.sin_port));

	/*
	 * We use a socket level timeout instead of polling for data.
	 */
	timeout.tv_sec  = 10;
	timeout.tv_usec = 0;
	
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
		       &timeout, sizeof(timeout)) < 0)
		pfatal("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");

	*ourport = (int) ntohs(name.sin_port);
	*sockp = sock;
	return 0;
}

/*
 * Tell the server about us. Since the server might crash or whatever,
 * we do this every minute as a heartbeat. There is no need to wait
 * for a reply; if the packet does not get through then we probably do not
 * need to worry about getting events from the server since those packets
 * are also UDP.
 */
static int
ServerConnect(int sock, int ourport, struct in_addr serverip, int serverport)
{
	struct sockaddr_in	request;
	char			buf[BUFSIZ];
	int			cc;
	
	/* Server name. */
	request.sin_family = AF_INET;
	request.sin_addr   = serverip;
	request.sin_port   = htons(serverport);
	strcpy(buf, "Hi!");

	cc = sendto(sock, buf, strlen(buf) + 1, 0,
		    (struct sockaddr *)&request, sizeof(request));

	if (cc <= 0) {
		if (cc < 0) {
			if (errno != EWOULDBLOCK)
				pfatal("Writing to server socket");
				
			errorc("Writing to server socket");
			goto bad;
		}
		error("short write to server\n");
		goto bad;
	}
	return 0;
 bad:
	return -1;
}

static int
GetEvents(event_handle_t handle, int sock)
{
	unsigned char	buf[2*BUFSIZ];

	while (1) {
		struct sockaddr_in	client;
		int			len, cc;
		event_notification_t	newnote;

		len = sizeof(client);
		cc = recvfrom(sock, buf, sizeof(buf), 0,
			      (struct sockaddr *)&client, &len);

		if (cc <= 0) {
			if (cc < 0) {
				if (errno != EWOULDBLOCK)
					pfatal("Reading from socket");
				break;
			}
			error("short read from server\n");
			break;
		}
		event_notification_unpack(handle, &newnote, buf, cc);
		event_notify(handle, newnote);
		event_notification_free(handle, newnote);
	}
	return 0;
}