MIDAS
Loading...
Searching...
No Matches
melog.cxx File Reference
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
Include dependency graph for melog.cxx:

Go to the source code of this file.

Macros

#define closesocket(s)   close(s)
 
#define O_BINARY   0
 
#define MAX_ATTACHMENTS   10
 
#define NAME_LENGTH   100
 
#define MAX_N_ATTR   20
 

Typedefs

typedef int INT
 

Functions

void base64_encode (char *s, char *d)
 
void sgets (char *string, int 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])
 
int main (int argc, char *argv[])
 

Variables

int verbose
 
const charmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 
char request [600000]
 
char response [10000]
 
char content [600000]
 

Macro Definition Documentation

◆ closesocket

#define closesocket (   s)    close(s)

Definition at line 29 of file melog.cxx.

◆ MAX_ATTACHMENTS

#define MAX_ATTACHMENTS   10

Definition at line 37 of file melog.cxx.

◆ MAX_N_ATTR

#define MAX_N_ATTR   20

Definition at line 39 of file melog.cxx.

◆ NAME_LENGTH

#define NAME_LENGTH   100

Definition at line 38 of file melog.cxx.

◆ O_BINARY

#define O_BINARY   0

Definition at line 31 of file melog.cxx.

Typedef Documentation

◆ INT

Definition at line 35 of file melog.cxx.

Function Documentation

◆ base64_encode()

void base64_encode ( char s,
char d 
)

Definition at line 47 of file melog.cxx.

48{
49 unsigned int t, pad;
50
51 pad = 3 - strlen(s) % 3;
52 while (*s) {
53 t = (*s++) << 16;
54 if (*s)
55 t |= (*s++) << 8;
56 if (*s)
57 t |= (*s++) << 0;
58
59 *(d + 3) = map[t & 63];
60 t >>= 6;
61 *(d + 2) = map[t & 63];
62 t >>= 6;
63 *(d + 1) = map[t & 63];
64 t >>= 6;
65 *(d + 0) = map[t & 63];
66
67 d += 4;
68 }
69 *d = 0;
70 while (pad--)
71 *(--d) = '=';
72}
const char * map
Definition melog.cxx:45
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
double d
Definition system.cxx:1311
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 314 of file melog.cxx.

315{
316 char str[1000], text[10000], uname[80], upwd[80];
317 char host_name[256], logbook[32], textfile[256], password[80];
318 char *buffer[MAX_ATTACHMENTS], attachment[MAX_ATTACHMENTS][256];
320 INT i, n, fh, n_att, n_attr, size, port;
322
323 text[0] = textfile[0] = uname[0] = upwd[0] = 0;
324 host_name[0] = logbook[0] = password[0] = n_att = n_attr = 0;
325 port = 80;
326 for (i = 0; i < MAX_ATTACHMENTS; i++) {
327 attachment[i][0] = 0;
328 buffer[i] = NULL;
329 att_size[i] = 0;
330 }
331
332 /* parse command line parameters */
333 for (i = 1; i < argc; i++) {
334 if (argv[i][0] == '-' && argv[i][1] == 'v')
335 verbose = 1;
336 else {
337 if (argv[i][0] == '-') {
338 if (i + 1 >= argc || argv[i + 1][0] == '-')
339 goto usage;
340 if (argv[i][1] == 'h')
341 strcpy(host_name, argv[++i]);
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);
357 n_attr++;
358 } else {
359 printf
360 ("Error: Attributes must be supplied in the form \"-a <attribute>=<value>\".\n");
361 return 0;
362 }
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]);
367 else {
368 usage:
369 printf("\nusage: elog -h <hostname> [-p port]\n");
370 printf(" [-l logbook/experiment]\n");
371 printf(" [-v] for verbose output\n");
372 printf
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",
378 MAX_N_ATTR);
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");
382 printf
383 ("or in a file with the -m flag. Multiple attributes and attachments\n");
384 printf("can be supplied\n");
385 return 0;
386 }
387 } else
388 strcpy(text, argv[i]);
389 }
390 }
391
392 if (host_name[0] == 0 || n_attr == 0 || (text[0] == 0 && textfile[0] == 0))
393 goto usage;
394
395 /*---- open text file ----*/
396
397 if (textfile[0]) {
398 fh = open(textfile, O_RDONLY | O_BINARY);
399 if (fh < 0) {
400 printf("Message file \"%s\" does not exist.\n", textfile);
401 return 0;
402 }
403
404 size = lseek(fh, 0, SEEK_END);
405 lseek(fh, 0, SEEK_SET);
406
407 if (size > (int)sizeof(text) - 1) {
408 printf("Message file \"%s\" is too long (%d bytes max).\n", textfile,
409 (int)sizeof(text));
410 return 0;
411 }
412
413 memset(text, 0, sizeof(text));
414 i = read(fh, text, size);
415 if (i < size) {
416 printf("Cannot fully read message file \"%s\".\n", textfile);
417 return 0;
418 }
419
420 close(fh);
421 }
422
423 /*---- open attachment file ----*/
424
425 for (i = 0; i < MAX_ATTACHMENTS; i++) {
426 if (!attachment[i][0])
427 break;
428
429 fh = open(attachment[i], O_RDONLY | O_BINARY);
430 if (fh < 0) {
431 printf("Attachment file \"%s\" does not exist.\n", attachment[i]);
432 return 0;
433 }
434
435 att_size[i] = lseek(fh, 0, SEEK_END);
436 lseek(fh, 0, SEEK_SET);
437
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]);
441 return 0;
442 }
443
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]);
447 return 0;
448 }
449 buffer[i][n] = 0;
450
451 close(fh);
452 }
453
454 /* now submit message */
455 submit_elog(host_name, port, logbook, password,
456 uname, upwd,
457 attr_name, attrib, n_attr, text, attachment, buffer, att_size);
458
459 for (i = 0; i < MAX_ATTACHMENTS; i++)
460 if (buffer[i])
461 free(buffer[i]);
462
463 return 1;
464}
static void usage()
DWORD n[4]
Definition mana.cxx:247
char host_name[HOST_NAME_LENGTH]
Definition mana.cxx:242
INT i
Definition mdump.cxx:32
int verbose
Definition melog.cxx:41
#define O_BINARY
Definition melog.cxx:31
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])
Definition melog.cxx:92
#define MAX_ATTACHMENTS
Definition melog.cxx:37
#define MAX_N_ATTR
Definition melog.cxx:39
#define NAME_LENGTH
Definition melog.cxx:38
int INT
Definition midas.h:129
#define read(n, a, f)
char str[256]
Definition odbhist.cxx:33
Here is the call graph for this function:

◆ sgets()

void sgets ( char string,
int  size 
)

Definition at line 76 of file melog.cxx.

77{
78 char *p;
79
80 do {
81 p = fgets(string, size, stdin);
82 } while (p == NULL);
83
84 if (strlen(p) > 0 && p[strlen(p) - 1] == '\n')
85 p[strlen(p) - 1] = 0;
86}
Here is the call graph for this function:

◆ submit_elog()

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] 
)

Definition at line 92 of file melog.cxx.

125{
126 int status, sock, i, n, header_length, content_length;
127 struct hostent *phe;
128 struct sockaddr_in bind_addr;
129 char host_name[256], boundary[80], str[80], *p;
130
131#if defined( _MSC_VER )
132 {
134
135 /* Start windows sockets */
136 if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
137 return -1;
138 }
139#endif
140
141 /* get local host name */
143
145 if (phe == NULL) {
146 perror("Cannot retrieve host name");
147 return -1;
148 }
149 phe = gethostbyaddr(phe->h_addr, sizeof(int), AF_INET);
150 if (phe == NULL) {
151 perror("Cannot retrieve host name");
152 return -1;
153 }
154
155 /* if domain name is not in host name, hope to get it from phe */
156 if (strchr(host_name, '.') == NULL)
157 strcpy(host_name, phe->h_name);
158
159 /* create socket */
160 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
161 perror("cannot create socket");
162 return -1;
163 }
164
165 /* compose remote address */
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);
170
171 phe = gethostbyname(host);
172 if (phe == NULL) {
173 perror("cannot get host name");
174 return -1;
175 }
176 memcpy((char *) &(bind_addr.sin_addr), phe->h_addr, phe->h_length);
177
178 /* connect to server */
179 status = connect(sock, (struct sockaddr *) &bind_addr, sizeof(bind_addr));
180 if (status != 0) {
181 printf("Cannot connect to host %s, port %d\n", host, port);
182 return -1;
183 }
184
185 if (verbose)
186 printf("Successfully connected to host %s, port %d\n", host, port);
187
188 /* compose content */
189 strcpy(boundary, "---------------------------7d0bf1a60904bc");
190 strcpy(content, boundary);
191 strcat(content, "\r\nContent-Disposition: form-data; name=\"cmd\"\r\n\r\nSubmit\r\n");
192
193 if (uname[0])
195 "%s\r\nContent-Disposition: form-data; name=\"unm\"\r\n\r\n%s\r\n",
196 boundary, uname);
197
198 if (upwd[0]) {
201 "%s\r\nContent-Disposition: form-data; name=\"upwd\"\r\n\r\n%s\r\n",
202 boundary, str);
203 }
204
205 if (experiment[0])
207 "%s\r\nContent-Disposition: form-data; name=\"exp\"\r\n\r\n%s\r\n",
208 boundary, experiment);
209
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]);
214
216 "%s\r\nContent-Disposition: form-data; name=\"Text\"\r\n\r\n%s\r\n%s\r\n",
217 boundary, text, boundary);
218
221
222 for (i = 0; i < MAX_ATTACHMENTS; i++)
223 if (afilename[i][0]) {
224 snprintf(p, sizeof(content) - strlen(content),
225 "Content-Disposition: form-data; name=\"attfile%d\"; filename=\"%s\"\r\n\r\n",
226 i + 1, afilename[i]);
227
229 p += strlen(p);
230 memcpy(p, buffer[i], buffer_size[i]);
231 p += buffer_size[i];
232 strcpy(p, boundary);
233 strcat(p, "\r\n");
234
235 content_length += buffer_size[i] + strlen(p);
236 p += strlen(p);
237 }
238
239 /* compose request */
240 snprintf(request, sizeof(request), "POST /EL/ HTTP/1.0\r\n");
242 "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
243 snprintf(request + strlen(request), sizeof(request) - strlen(request), "Host: %s\r\n", host_name);
244 snprintf(request + strlen(request), sizeof(request) - strlen(request), "User-Agent: ELOG\r\n");
245 snprintf(request + strlen(request), sizeof(request) - strlen(request), "Content-Length: %d\r\n", content_length);
246
247 if (passwd[0]) {
249 snprintf(request + strlen(request), sizeof(request) - strlen(request), "Cookie: elog_wpwd=%s\r\n", str);
250 }
251
252 strcat(request, "\r\n");
253
256
257 /*
258 {
259 FILE *f;
260 f = fopen("elog.log", "w");
261 fwrite(request, header_length+content_length, 1, f);
262 fclose(f);
263 }
264 */
265
266 /* send request */
267 if (verbose) {
268 printf("Request sent to host:\n");
269 puts(request);
270 }
271
273
274 /* receive response */
275 i = recv(sock, response, 10000, 0);
276 if (i < 0) {
277 perror("Cannot receive response");
278 return -1;
279 }
280
281 /* discard remainder of response */
282 n = i;
283 while (i > 0) {
284 i = recv(sock, response + n, 10000, 0);
285 if (i > 0)
286 n += i;
287 }
288 response[n] = 0;
289
290 closesocket(sock);
291
292 if (verbose) {
293 printf("Response received:\n");
294 puts(response);
295 }
296
297 /* check response status */
298 if (strstr(response, "302 Found"))
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");
304 else if (strstr(response, "login"))
305 printf("Missing or invalid user name/password\n");
306 else
307 printf("Error transmitting message\n");
308
309 return 1;
310}
char content[600000]
Definition melog.cxx:90
char response[10000]
Definition melog.cxx:90
#define closesocket(s)
Definition melog.cxx:29
char request[600000]
Definition melog.cxx:90
void base64_encode(char *s, char *d)
Definition melog.cxx:47
DWORD status
Definition odbhist.cxx:39
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ content

char content[600000]

Definition at line 90 of file melog.cxx.

◆ map

const char* map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 45 of file melog.cxx.

◆ request

char request[600000]

Definition at line 90 of file melog.cxx.

◆ response

char response[10000]

Definition at line 90 of file melog.cxx.

◆ verbose

int verbose

Definition at line 41 of file melog.cxx.