-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriverlib.c
More file actions
408 lines (357 loc) · 11.1 KB
/
driverlib.c
File metadata and controls
408 lines (357 loc) · 11.1 KB
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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/***********************************************************************
*
* driverlib.c - A package of helper functions for C Autolab drivers
*
* Copyright (c) 2004, D. O'Hallaron, All rights reserved. May not be
* used, modified, or copied without permission.
*
# $Id: driverlib.c,v 1.6 2006/11/21 03:44:23 autolab Exp $
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include "driverhdrs.h"
#include "driverlib.h"
/**************************
* Private helper functions
**************************/
/*
* sigalrm_handler - handles SIGALRM timeout signals
*/
void sigalrm_handler(int sig) {
fprintf(stderr, "Program timed out after %d seconds\n", AUTOGRADE_TIMEOUT);
exit(1);
}
/*
* rio_readinitb - Associate a descriptor with a read buffer and reset buffer
*/
typedef struct sockaddr SA;
static void rio_readinitb(rio_t *rp, int fd)
{
rp->rio_fd = fd;
rp->rio_cnt = 0;
rp->rio_bufptr = rp->rio_buf;
}
/*
* rio_read - This is a wrapper for the Unix read() function that
* transfers min(n, rio_cnt) bytes from an internal buffer to a user
* buffer, where n is the number of bytes requested by the user and
* rio_cnt is the number of unread bytes in the internal buffer. On
* entry, rio_read() refills the internal buffer via a call to
* read() if the internal buffer is empty.
*/
static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n)
{
int cnt;
while (rp->rio_cnt <= 0) { /* refill if buf is empty */
rp->rio_cnt = read(rp->rio_fd, rp->rio_buf,
sizeof(rp->rio_buf));
if (rp->rio_cnt < 0) {
if (errno != EINTR) /* interrupted by sig handler return */
return -1;
}
else if (rp->rio_cnt == 0) /* EOF */
return 0;
else
rp->rio_bufptr = rp->rio_buf; /* reset buffer ptr */
}
/* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */
cnt = n;
if (rp->rio_cnt < n)
cnt = rp->rio_cnt;
memcpy(usrbuf, rp->rio_bufptr, cnt);
rp->rio_bufptr += cnt;
rp->rio_cnt -= cnt;
return cnt;
}
/*
* rio_readlineb - robustly read a text line (buffered)
*/
static ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen)
{
int n, rc;
char c, *bufp = usrbuf;
for (n = 1; n < maxlen; n++) {
if ((rc = rio_read(rp, &c, 1)) == 1) {
*bufp++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
if (n == 1)
return 0; /* EOF, no data read */
else
break; /* EOF, some data was read */
} else
return -1; /* error */
}
*bufp = 0;
return n;
}
/*
* rio_writen - Robustly write n bytes (unbuffered)
*/
static ssize_t rio_writen(int fd, void *usrbuf, size_t n)
{
size_t nleft = n;
ssize_t nwritten;
char *bufp = usrbuf;
while (nleft > 0) {
if ((nwritten = write(fd, bufp, nleft)) <= 0) {
if (errno == EINTR) /* interrupted by sig handler return */
nwritten = 0; /* and call write() again */
else
return -1; /* errorno set by write() */
}
nleft -= nwritten;
bufp += nwritten;
}
return n;
}
/*
* urlencode - URL-encodes the src input string into dst
*/
static int urlencode(unsigned char *src, unsigned char *dst)
{
int len = strlen((char *)src);
char buf[8];
for ( ; len--; src++) {
if ((*src == '*') || (*src == '-') ||
(*src== '.') || (*src == '_') ||
(('0' <= *src) && (*src <= '9')) ||
(('A' <= *src) && (*src <= 'Z')) ||
(('a' <= *src) && (*src <= 'z'))) {
*dst++ = *src;
}
else if (*src == ' ') {
*dst++ = '+';
}
/*
* Allow only the printable ASCII characters Note: for a
* general purpose URL-encoding routine, we would also allow
* newline and form feeds, but since result submissions are
* constrained to be single text lines, we disallow them in this
* context. However, tabs are allowd.
*/
else if (((*src > 31) && (*src < 128)) ||
(*src == '\t')) {
sprintf(buf, "%%%02X", *src);
*dst++ = buf[0];
*dst++ = buf[1];
*dst++ = buf[2];
}
else {
return -1;
}
}
return 0;
}
/*
* submitr - Submit a client result string to the autolab server.
*/
int submitr(char *hostname, /* Server domain name */
int port, /* Server port */
char *course, /* Course name */
char *userid, /* Userid */
char *lab, /* Lab name */
char *result, /* Result string to submit */
char *status_msg) /* Status message returned to caller */
{
int clientfd; /* socket descriptor */
struct hostent *hp; /* DNS host entry */
struct sockaddr_in serveraddr; /* Server's socket address */
size_t result_size; /* Input result size in bytes */
size_t req_size; /* HTTP request size in bytes */
rio_t rio; /* Handle for buffered RIO functions */
char buf[SUBMITR_MAXBUF]; /* Buffer for HTTP requests/responses */
char enc_result[SUBMITR_MAXBUF]; /* Buffer for URL-encoded result string */
char version[SUBMITR_MAXBUF]; /* Fields from first response line */
int errcode=0;
char errmsg[SUBMITR_MAXBUF];
/* Create the initial socket descriptor */
if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
strcpy(status_msg, "Error: Client unable to create socket");
return -1;
}
/* Fill in the server's IP address and port */
if ((hp = gethostbyname(hostname)) == NULL) {
strcpy(status_msg, "Error: DNS is unable to resolve Autolab server address");
close(clientfd);
return -1;
}
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)hp->h_addr,
(char *)&serveraddr.sin_addr.s_addr, hp->h_length);
serveraddr.sin_port = htons(port);
/* Establish a connection with the server */
if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) {
strcpy(status_msg, "Error: Unable to connect to the Autolab server");
close(clientfd);
return -1;
}
/*
* Make sure the data will fit in the buffer. Make the
* conservative assumption that each character in the result
* string will be translated into its corresponding 3 character
* '%XX' hex url encoding. Include a conservative pad of 128
* bytes for separators in the HTTP URI.
*/
result_size = strlen(result);
req_size = strlen(course) + strlen(userid) +
strlen(lab) + 3*result_size + 128;
if (req_size > SUBMITR_MAXBUF) {
strcpy(status_msg, "Error: Result string too large. Increase SUBMITR_MAXBUF");
close(clientfd);
return -1;
}
/* URL-encode the result string */
bzero((char *)enc_result, SUBMITR_MAXBUF);
if (urlencode((unsigned char *)result, (unsigned char *)enc_result) < 0) {
strcpy(status_msg, "Error: Result string contains an illegal or unprintable character.");
close(clientfd);
return -1;
}
/* Construct the HTTP request */
sprintf(buf, "GET /%s/submitr.pl/?userid=%s&lab=%s&result=%s&submit=submit HTTP/1.0\r\n\r\n", course, userid, lab, enc_result);
/* Send the request to the server */
if (rio_writen(clientfd, buf, strlen(buf)) < 0) {
strcpy(status_msg, "Error: Client unable to write to the Autolab server");
close(clientfd);
return -1;
}
/* Read first HTTP response header line from the server */
rio_readinitb(&rio, clientfd);
if (rio_readlineb(&rio, buf, SUBMITR_MAXBUF) <= 0) {
strcpy(status_msg, "Error: Client unable to read first header from Autolab server");
close(clientfd);
return -1;
}
sscanf(buf, "%s %d %[a-zA-z ]", version, &errcode, errmsg);
if (errcode != 200) {
sprintf(status_msg, "Error: HTTP request failed with error %d: %s",
errcode, errmsg);
close(clientfd);
return -1;
}
/* Read the remaining HTTP response header lines */
while (strcmp(buf, "\r\n")) {
if (rio_readlineb(&rio, buf, SUBMITR_MAXBUF) <= 0) {
strcpy(status_msg, "Error: Client unable to read headers from Autolab server");
close(clientfd);
return -1;
}
}
/* Read and echo the response from the server */
if (rio_readlineb(&rio, buf, SUBMITR_MAXBUF) <= 0) {
strcpy(status_msg, "Error: Client unable to read status message from Autolab server");
close(clientfd);
return -1;
}
/* Set the return status message, clean up, and exit */
strcpy(status_msg, buf);
close(clientfd);
if (!strcmp(status_msg, "OK"))
return 0;
else
return -1;
}
/******************
* Public functions
******************/
/*
* init_timeout - Time out the driver if student code hangs.
* The argument is in seconds; -1 means to use the AUTOGRADE_TIMEOUT, 0
* means never timeout.
*/
void init_timeout(int timeout) {
if (timeout == 0) {
return;
}
if (timeout < 0) {
timeout = AUTOGRADE_TIMEOUT;
}
signal(SIGALRM, sigalrm_handler);
alarm(timeout);
}
/*
* init_driver - Initialize the driverlib package
*/
int init_driver(char *status_msg)
{
int clientfd; /* Socket descriptor */
struct hostent *hp; /* DNS host entry */
struct sockaddr_in serveraddr; /* Server's socket address */
char *hostname = SERVER_NAME;
int port = SERVER_PORT;
/* Ignore any terminating SIGPIPE signals */
signal(SIGPIPE, SIG_IGN);
signal(SIGPOLL, SIG_IGN);
signal(SIGPOLL, SIG_IGN);
/*
* Make sure that we can talk to the server
*/
/* Create the initial socket descriptor */
if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
strcpy(status_msg, "Error: Client unable to create socket");
return -1;
}
/* Fill in the server's IP address and port */
if ((hp = gethostbyname(hostname)) == NULL) {
strcpy(status_msg, "Error: DNS is unable to resolve server address");
close(clientfd);
return -1;
}
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)hp->h_addr,
(char *)&serveraddr.sin_addr.s_addr, hp->h_length);
serveraddr.sin_port = htons(port);
/* Establish a connection with the server */
if (connect(clientfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) {
strcpy(status_msg, "Error: Unable to connect to server");
close(clientfd);
return -1;
}
/* Close the connection with the server */
close(clientfd);
strcpy(status_msg, "OK");
return 0;
}
/*
* driver_post - This is the routine that the driver calls when
* it needs to transmit an autoresult string to Autolab
*/
int driver_post(char *userid, char *result, int autograded, char *status_msg)
{
int status;
/* Echo autoresult string to stdout if driver called by an autograder */
if (autograded) {
printf("\nAUTORESULT_STRING=%s\n", result);
strcpy(status_msg, "OK");
return 0;
}
/*
* If the driver was called with a specific user, then submit
* the autoresult string to the Autolab server
*/
if (userid && strcmp(userid, "")) {
status = submitr(SERVER_NAME,
SERVER_PORT,
COURSE_NAME,
userid,
LAB,
result,
status_msg);
return status;
}
/* Did nothing, simply return success */
strcpy(status_msg, "OK");
return 0;
}