93 char *uname,
char *upwd,
126 int status, sock,
i,
n, header_length, content_length;
128 struct sockaddr_in bind_addr;
131#if defined( _MSC_VER )
136 if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
146 perror(
"Cannot retrieve host name");
149 phe = gethostbyaddr(phe->h_addr,
sizeof(
int), AF_INET);
151 perror(
"Cannot retrieve host name");
160 if ((sock =
socket(AF_INET, SOCK_STREAM, 0)) == -1) {
161 perror(
"cannot create socket");
166 memset(&bind_addr, 0,
sizeof(bind_addr));
167 bind_addr.sin_family = AF_INET;
168 bind_addr.sin_addr.s_addr = 0;
169 bind_addr.sin_port = htons((
unsigned short) port);
171 phe = gethostbyname(host);
173 perror(
"cannot get host name");
176 memcpy((
char *) &(bind_addr.sin_addr), phe->h_addr, phe->h_length);
179 status = connect(sock, (
struct sockaddr *) &bind_addr,
sizeof(bind_addr));
181 printf(
"Cannot connect to host %s, port %d\n", host, port);
186 printf(
"Successfully connected to host %s, port %d\n", host, port);
189 strcpy(boundary,
"---------------------------7d0bf1a60904bc");
191 strcat(
content,
"\r\nContent-Disposition: form-data; name=\"cmd\"\r\n\r\nSubmit\r\n");
195 "%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n",
201 "%s\r\nContent-Disposition: form-data; name=\"upwd\"\r\n\r\n%s\r\n",
207 "%s\r\nContent-Disposition: form-data; name=\"exp\"\r\n\r\n%s\r\n",
208 boundary, experiment);
210 for (
i = 0;
i < n_attr;
i++)
212 "%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", boundary,
213 attrib_name[
i], attrib[
i]);
216 "%s\r\nContent-Disposition: form-data; name=\"Text\"\r\n\r\n%s\r\n%s\r\n",
217 boundary, text, boundary);
219 content_length = strlen(
content);
223 if (afilename[
i][0]) {
225 "Content-Disposition: form-data; name=\"attfile%d\"; filename=\"%s\"\r\n\r\n",
226 i + 1, afilename[
i]);
228 content_length += strlen(p);
230 memcpy(p, buffer[
i], buffer_size[
i]);
235 content_length += buffer_size[
i] + strlen(p);
242 "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
254 header_length = strlen(
request);
268 printf(
"Request sent to host:\n");
272 send(sock,
request, header_length + content_length, 0);
277 perror(
"Cannot receive response");
293 printf(
"Response received:\n");
299 printf(
"Message successfully transmitted\n");
300 else if (strstr(
response,
"Logbook Selection"))
301 printf(
"No logbook specified\n\n");
302 else if (strstr(
response,
"Enter password"))
303 printf(
"Missing or invalid password\n");
305 printf(
"Missing or invalid user name/password\n");
307 printf(
"Error transmitting message\n");
314int main(
int argc,
char *argv[])
316 char str[1000], text[10000], uname[80], upwd[80];
317 char host_name[256], logbook[32], textfile[256], password[80];
320 INT i,
n, fh, n_att, n_attr, size, port;
323 text[0] = textfile[0] = uname[0] = upwd[0] = 0;
324 host_name[0] = logbook[0] = password[0] = n_att = n_attr = 0;
327 attachment[
i][0] = 0;
333 for (
i = 1;
i < argc;
i++) {
334 if (argv[
i][0] ==
'-' && argv[
i][1] ==
'v')
337 if (argv[
i][0] ==
'-') {
338 if (
i + 1 >= argc || argv[
i + 1][0] ==
'-')
340 if (argv[
i][1] ==
'h')
342 else if (argv[
i][1] ==
'p')
343 port = atoi(argv[++
i]);
344 else if (argv[
i][1] ==
'l')
345 strcpy(logbook, argv[++
i]);
346 else if (argv[
i][1] ==
'w')
347 strcpy(password, argv[++
i]);
348 else if (argv[
i][1] ==
'u') {
349 strcpy(uname, argv[++
i]);
350 strcpy(upwd, argv[++
i]);
351 }
else if (argv[
i][1] ==
'a') {
352 strcpy(
str, argv[++
i]);
353 if (strchr(
str,
'=')) {
354 strcpy(attrib[n_attr], strchr(
str,
'=') + 1);
355 *strchr(
str,
'=') = 0;
356 strcpy(attr_name[n_attr],
str);
360 (
"Error: Attributes must be supplied in the form \"-a <attribute>=<value>\".\n");
363 }
else if (argv[
i][1] ==
'f')
364 strcpy(attachment[n_att++], argv[++
i]);
365 else if (argv[
i][1] ==
'm')
366 strcpy(textfile, argv[++
i]);
369 printf(
"\nusage: elog -h <hostname> [-p port]\n");
370 printf(
" [-l logbook/experiment]\n");
371 printf(
" [-v] for verbose output\n");
373 (
" [-w password] write password defined on server\n");
374 printf(
" [-u username password] user name and password\n");
375 printf(
" [-f <attachment>] (up to %d times)\n",
377 printf(
" -a <attribute>=<value> (up to %d times)\n",
379 printf(
" -m <textfile>] | <text>\n");
380 printf(
"\nArguments with blanks must be enclosed in quotes\n");
381 printf(
"The elog message can either be submitted on the command line\n");
383 (
"or in a file with the -m flag. Multiple attributes and attachments\n");
384 printf(
"can be supplied\n");
388 strcpy(text, argv[
i]);
392 if (
host_name[0] == 0 || n_attr == 0 || (text[0] == 0 && textfile[0] == 0))
398 fh = open(textfile, O_RDONLY |
O_BINARY);
400 printf(
"Message file \"%s\" does not exist.\n", textfile);
404 size = lseek(fh, 0, SEEK_END);
405 lseek(fh, 0, SEEK_SET);
407 if (size > (
int)
sizeof(text) - 1) {
408 printf(
"Message file \"%s\" is too long (%d bytes max).\n", textfile,
413 memset(text, 0,
sizeof(text));
414 i =
read(fh, text, size);
416 printf(
"Cannot fully read message file \"%s\".\n", textfile);
426 if (!attachment[
i][0])
429 fh = open(attachment[
i], O_RDONLY |
O_BINARY);
431 printf(
"Attachment file \"%s\" does not exist.\n", attachment[
i]);
435 att_size[
i] = lseek(fh, 0, SEEK_END);
436 lseek(fh, 0, SEEK_SET);
438 buffer[
i] = (
char*)malloc(att_size[
i] + 1);
439 if (buffer[
i] == NULL || att_size[
i] > 500 * 1024) {
440 printf(
"Attachment file \"%s\" is too long (500k max).\n", attachment[
i]);
444 n =
read(fh, buffer[
i], att_size[
i]);
445 if (
n < att_size[
i]) {
446 printf(
"Cannot fully read attachment file \"%s\".\n", attachment[
i]);
457 attr_name, attrib, n_attr, text, attachment, buffer, att_size);
INT submit_elog(char *host, int port, char *experiment, char *passwd, char *uname, char *upwd, char attrib_name[MAX_N_ATTR][NAME_LENGTH], char attrib[MAX_N_ATTR][NAME_LENGTH], int n_attr, char *text, char afilename[MAX_ATTACHMENTS][256], char *buffer[MAX_ATTACHMENTS], INT buffer_size[MAX_ATTACHMENTS])