home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part04 / m0uc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  5.8 KB  |  232 lines

  1. /*
  2.     m0uc.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25.  
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28.  
  29. #include "l_proto.h"    /* for l_format.c */
  30. #include "c_proto.h"    /* for UDP_SERVERPORT */
  31. #include "copyrght.h"
  32.  
  33. #define VERSION        "0.11"
  34. #define INI_FILE    "cons_ini.m0"
  35.  
  36.  
  37. ushort serverport = UDP_SERVERPORT;
  38. ushort myport = UDP_SERVERPORT+1;
  39. uint host = 0, this_ip_host;
  40. int udp_sock;
  41.  
  42. byte request_queue[8];
  43.  
  44. /* The \xxx..x\ key is used to (a) globally deposit the data field
  45.    (=request) and (b) to restart the queue associated with that key -
  46.    the server already knows how to send back results: */
  47. byte request[]="\\xxxxxxxxxxxxxxxx\\_ctk _1I_dat:0Q";
  48.  
  49. char line[8096];
  50.  
  51. static void usage(FILE *f);
  52.  
  53. main(argc, argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.     while (argc > 1 && argv[1][0] == '-') {
  58.         argc--, argv++;
  59.         switch(argv[0][1]) {
  60.             case 'h': usage(stdout); break;
  61.             case 'm':
  62.             if (argc < 2) usage(stderr);
  63.             host = ntohl(inet_addr(argv[1]));
  64.             argc--, argv++; break;
  65.             case 'p':
  66.             if (argc < 2) usage(stderr);
  67.             serverport = strtol(argv[1], NULL, 0);
  68.             argc--, argv++; break;
  69. /*
  70.             case 'u':
  71.             if (argc < 2) usage(stderr);
  72.             myport = strtol(argv[1], NULL, 0);
  73.             argc--, argv++; break;
  74. */
  75.             case 'v':
  76.             printf("The M0 remote console, version %s (compiled %s at %s)\n",
  77.                     VERSION, __DATE__, __TIME__);
  78.             exit(0);
  79.             default:
  80.             usage(stderr); break;
  81.         }
  82.     }
  83.  
  84.     init();
  85.  
  86.     /* unfortunately we can not do a while loop yet:
  87.        the end of treatement of a requests is not
  88.        specially signalled by the server process
  89.        (one should reprogram cons_ini.m0 ... */
  90.     if (argc > 1) {
  91.         byte *download, *msgr;
  92.         uint msgrlen;
  93.  
  94.         argc--; argv++;
  95.         download = load_m0(".", ".", argv[0]);
  96.         if (!download) {
  97.             fprintf(stderr, "cannot load file %s\n", argv[0]);
  98.             exit(1);
  99.             /* continue; */
  100.         }
  101.         if (strlen((char*)download) > 7000) {
  102.             fprintf(stderr, "file %s to download is too large: skipped\n", argv[0]);
  103.             free(download);
  104.             exit(1);
  105.             /* continue; */
  106.         }
  107.         msgr = make_msgr(request_queue,
  108.             request, strlen((char*)request),
  109.             download, strlen((char*)download),
  110.             &msgrlen);
  111.         if (udp_send(udp_sock, host, UDP_SERVERPORT, msgr, msgrlen) < 0)
  112.             perror("client download");
  113.         free(msgr);
  114.         free(download);
  115.     }
  116.  
  117.     for (;;) {
  118.         int cnt;
  119.         uint msgrlen;
  120.         byteptr msgr;
  121.             fd_set r;
  122.  
  123.             FD_ZERO(&r);
  124.         FD_SET(0, &r);        /* stdin */
  125.         FD_SET(udp_sock, &r);    /* udp port */
  126.         select(FD_SETSIZE, &r, NULL, NULL, NULL);
  127.         if (FD_ISSET(udp_sock, &r)) {
  128.             cnt = recv(udp_sock, line, sizeof(line)-1, 0);
  129.             if (cnt < 0)
  130.                 perror("client recv");
  131.             if (cnt == 0)
  132.                 break;
  133.             line[cnt] = '\0';
  134.             printf("%s", line);
  135.             fflush(stdout);
  136.             continue;
  137.         }
  138.         cnt = read(0, line, sizeof(line));
  139.         if (cnt <= 0)
  140.             break;
  141.         msgr = make_msgr(request_queue,
  142.             request, strlen((char*)request),
  143.             cnt>1 ? (byteptr)line : 0, cnt-1, &msgrlen);
  144.         if (udp_send(udp_sock, host, UDP_SERVERPORT, msgr, (int)msgrlen) < 0)
  145.             perror("client send");
  146.         free(msgr);
  147.     }
  148.     printf("## M0 console terminated\n");
  149. }
  150.  
  151.  
  152. void
  153. usage(FILE *f)
  154. {
  155.     fprintf(f, "The M0 remote UDP console, version %s (compiled %s at %s)\n",
  156.                 VERSION, __DATE__, __TIME__);
  157.     fprintf(f, "%s\n", COPYRIGHT);
  158.     fprintf(f,    "usage: m0uc [options]   [<downloadfile>]\n");
  159.     fprintf(f, "options:\n"
  160.             "\t-help\n"
  161.             "\t-m <M0_ip_address>\n"
  162.             "\t-p <M0_udp_port>\n"
  163. /*
  164.             "\t-u <console_udp_port>\n"
  165. */
  166.             "\t-version\n");
  167.     fprintf(f, "environment variables:\n"
  168.             "\tM0BIN  path for finding the m0strip binary\n"
  169.             "\tM0LIB  path for finding the %s startup file\n", INI_FILE);
  170.     exit(1);
  171. }
  172.  
  173.  
  174. init()
  175. {
  176.     char *bin, *lib;
  177.     int i;
  178.     byte server_queue[8];
  179.     uint msgrlen;
  180.     byteptr server_ini, msgr;
  181.  
  182.     for (i = 10; ;i--) {
  183.         udp_sock = udp_listen(0, myport);
  184.         if (udp_sock > 0)
  185.             break;
  186.         if (i < 1) {
  187.             fprintf(stderr, "Cannot find an empty UDP-port\n");
  188.             exit(1);
  189.         }
  190.         myport++;
  191.     }
  192.  
  193.     printf("## Welcome to the M0 remote console (v %s, compiled %s at %s)\n",
  194.                         VERSION, __DATE__, __TIME__);
  195.     printf("## %s\n\n", COPYRIGHT);
  196.  
  197.     this_ip_host = * get_ip_addresses();
  198.     if (!host)
  199.         host = this_ip_host;
  200.  
  201.     printf( "Local host %s (%u), UDP port is 0x%04x\n",
  202.         iptoa(this_ip_host), this_ip_host, myport);
  203.     printf(    "Contacting the M0 platform at %s (%u), UDP port 0x%04x ...\n\n",
  204.         iptoa(host), host, serverport);
  205.  
  206.     srandom(time(NULL));
  207.     random64(server_queue);
  208.     random64(request_queue);
  209.     for (i = 0; i < 8; i++)
  210.         sprintf((char*)request+1+2*i, "%02x", server_queue[i]);
  211.     request[17] = '\\';
  212.  
  213.     bin = getenv("M0BIN");
  214.     lib = getenv("M0LIB");
  215.     server_ini = load_m0(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH, INI_FILE);
  216.     if (!server_ini) {
  217.         fprintf(stderr, "loading initialisation code from file %s failed\n", INI_FILE);
  218.         exit(1);
  219.     }
  220.     
  221.     sprintf(line, "{[%u %u]0{}_cha'udpipG'_keyG!}", this_ip_host, myport);
  222.     msgr = make_msgr(server_queue,
  223.             server_ini, strlen((char*)server_ini),
  224.             (byteptr)line, strlen(line),
  225.             &msgrlen);
  226.     if (udp_send(udp_sock, host, serverport, msgr, msgrlen) < 0)
  227.         perror("client send");
  228.     free(msgr);
  229.     free(server_ini);
  230. }
  231.  
  232.