home *** CD-ROM | disk | FTP | other *** search
- /*
- m0uc.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
-
- #include <stdlib.h>
- #include <sys/types.h>
-
- #include "l_proto.h" /* for l_format.c */
- #include "c_proto.h" /* for UDP_SERVERPORT */
- #include "copyrght.h"
-
- #define VERSION "0.11"
- #define INI_FILE "cons_ini.m0"
-
-
- ushort serverport = UDP_SERVERPORT;
- ushort myport = UDP_SERVERPORT+1;
- uint host = 0, this_ip_host;
- int udp_sock;
-
- byte request_queue[8];
-
- /* The \xxx..x\ key is used to (a) globally deposit the data field
- (=request) and (b) to restart the queue associated with that key -
- the server already knows how to send back results: */
- byte request[]="\\xxxxxxxxxxxxxxxx\\_ctk _1I_dat:0Q";
-
- char line[8096];
-
- static void usage(FILE *f);
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- while (argc > 1 && argv[1][0] == '-') {
- argc--, argv++;
- switch(argv[0][1]) {
- case 'h': usage(stdout); break;
- case 'm':
- if (argc < 2) usage(stderr);
- host = ntohl(inet_addr(argv[1]));
- argc--, argv++; break;
- case 'p':
- if (argc < 2) usage(stderr);
- serverport = strtol(argv[1], NULL, 0);
- argc--, argv++; break;
- /*
- case 'u':
- if (argc < 2) usage(stderr);
- myport = strtol(argv[1], NULL, 0);
- argc--, argv++; break;
- */
- case 'v':
- printf("The M0 remote console, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- exit(0);
- default:
- usage(stderr); break;
- }
- }
-
- init();
-
- /* unfortunately we can not do a while loop yet:
- the end of treatement of a requests is not
- specially signalled by the server process
- (one should reprogram cons_ini.m0 ... */
- if (argc > 1) {
- byte *download, *msgr;
- uint msgrlen;
-
- argc--; argv++;
- download = load_m0(".", ".", argv[0]);
- if (!download) {
- fprintf(stderr, "cannot load file %s\n", argv[0]);
- exit(1);
- /* continue; */
- }
- if (strlen((char*)download) > 7000) {
- fprintf(stderr, "file %s to download is too large: skipped\n", argv[0]);
- free(download);
- exit(1);
- /* continue; */
- }
- msgr = make_msgr(request_queue,
- request, strlen((char*)request),
- download, strlen((char*)download),
- &msgrlen);
- if (udp_send(udp_sock, host, UDP_SERVERPORT, msgr, msgrlen) < 0)
- perror("client download");
- free(msgr);
- free(download);
- }
-
- for (;;) {
- int cnt;
- uint msgrlen;
- byteptr msgr;
- fd_set r;
-
- FD_ZERO(&r);
- FD_SET(0, &r); /* stdin */
- FD_SET(udp_sock, &r); /* udp port */
- select(FD_SETSIZE, &r, NULL, NULL, NULL);
- if (FD_ISSET(udp_sock, &r)) {
- cnt = recv(udp_sock, line, sizeof(line)-1, 0);
- if (cnt < 0)
- perror("client recv");
- if (cnt == 0)
- break;
- line[cnt] = '\0';
- printf("%s", line);
- fflush(stdout);
- continue;
- }
- cnt = read(0, line, sizeof(line));
- if (cnt <= 0)
- break;
- msgr = make_msgr(request_queue,
- request, strlen((char*)request),
- cnt>1 ? (byteptr)line : 0, cnt-1, &msgrlen);
- if (udp_send(udp_sock, host, UDP_SERVERPORT, msgr, (int)msgrlen) < 0)
- perror("client send");
- free(msgr);
- }
- printf("## M0 console terminated\n");
- }
-
-
- void
- usage(FILE *f)
- {
- fprintf(f, "The M0 remote UDP console, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- fprintf(f, "%s\n", COPYRIGHT);
- fprintf(f, "usage: m0uc [options] [<downloadfile>]\n");
- fprintf(f, "options:\n"
- "\t-help\n"
- "\t-m <M0_ip_address>\n"
- "\t-p <M0_udp_port>\n"
- /*
- "\t-u <console_udp_port>\n"
- */
- "\t-version\n");
- fprintf(f, "environment variables:\n"
- "\tM0BIN path for finding the m0strip binary\n"
- "\tM0LIB path for finding the %s startup file\n", INI_FILE);
- exit(1);
- }
-
-
- init()
- {
- char *bin, *lib;
- int i;
- byte server_queue[8];
- uint msgrlen;
- byteptr server_ini, msgr;
-
- for (i = 10; ;i--) {
- udp_sock = udp_listen(0, myport);
- if (udp_sock > 0)
- break;
- if (i < 1) {
- fprintf(stderr, "Cannot find an empty UDP-port\n");
- exit(1);
- }
- myport++;
- }
-
- printf("## Welcome to the M0 remote console (v %s, compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- printf("## %s\n\n", COPYRIGHT);
-
- this_ip_host = * get_ip_addresses();
- if (!host)
- host = this_ip_host;
-
- printf( "Local host %s (%u), UDP port is 0x%04x\n",
- iptoa(this_ip_host), this_ip_host, myport);
- printf( "Contacting the M0 platform at %s (%u), UDP port 0x%04x ...\n\n",
- iptoa(host), host, serverport);
-
- srandom(time(NULL));
- random64(server_queue);
- random64(request_queue);
- for (i = 0; i < 8; i++)
- sprintf((char*)request+1+2*i, "%02x", server_queue[i]);
- request[17] = '\\';
-
- bin = getenv("M0BIN");
- lib = getenv("M0LIB");
- server_ini = load_m0(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH, INI_FILE);
- if (!server_ini) {
- fprintf(stderr, "loading initialisation code from file %s failed\n", INI_FILE);
- exit(1);
- }
-
- sprintf(line, "{[%u %u]0{}_cha'udpipG'_keyG!}", this_ip_host, myport);
- msgr = make_msgr(server_queue,
- server_ini, strlen((char*)server_ini),
- (byteptr)line, strlen(line),
- &msgrlen);
- if (udp_send(udp_sock, host, serverport, msgr, msgrlen) < 0)
- perror("client send");
- free(msgr);
- free(server_ini);
- }
-
-