home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / fsp / part02 / server_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  5.4 KB  |  193 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "server_def.h"
  11.  
  12. #define ERR(S) { send_error(from,ub,S); return; }
  13.  
  14. int inetd_mode =            0;
  15. int dbug       =            0;
  16. int   udp_port = DEF_FSP_PORT;
  17. char *home_dir = DEF_FSP_HOME;
  18. char *run_uid  =            0;
  19.  
  20. /****************************************************************************
  21. *  This is the dispatch loop for message that has been accepted.
  22. *    bytes: size of the message received.
  23. *       ub: pointer to the message buffer.
  24. *      old: true if this message contains old sequence number (retransmit).
  25. *       hp: pointer to the entry for the client host who sent this message.
  26. *     from: pointer to the socket address structure of the client host.
  27. ****************************************************************************/
  28.  
  29. server_get_packet(bytes,ub,old,hp,from)
  30.     int   bytes,old;
  31.     UBUF *ub;
  32.     HTAB *hp;
  33.     struct sockaddr_in *from;
  34. {
  35.     unsigned long inet_num;
  36.     unsigned l1, l2;
  37.     char *s1, *s2, *pe;
  38.     FILE *fp;
  39.  
  40.     l1 = ub->len;
  41.     l2 = bytes - l1 - UBUF_HSIZE;
  42.     s1 = ub->buf;
  43.     s2 = ub->buf + l1;
  44.  
  45.     if(dbug) fprintf(stderr,"rcv (%c,%d,%d,%lu) <--- %d.%d.%d.%d\n",
  46.         ub->cmd, l1, l2, ub->pos,
  47.         ((unsigned char *)(&hp->inet_num))[0],
  48.         ((unsigned char *)(&hp->inet_num))[1],
  49.         ((unsigned char *)(&hp->inet_num))[2],
  50.         ((unsigned char *)(&hp->inet_num))[3]);
  51.  
  52.     if(!old) { hp->last_key = hp->next_key;
  53.            hp->next_key = get_next_key() + ((hp->last_key+1) & 0x00ff); }
  54.  
  55.     ub->key  = hp->next_key;
  56.     inet_num = hp->inet_num;
  57.  
  58.     switch(ub->cmd)
  59.     {
  60.       case CC_VERSION : { ERR(VERSION_STR); }
  61.  
  62.       case CC_BYE     : { if(!old) hp->active = 0;
  63.               server_reply(from,ub,0,0);
  64.               return; }
  65.             
  66.       case CC_GET_DIR : { if((pe = check_path(s1,l1)) ||
  67.                  (pe = server_get_dir(s1,&fp))) ERR(pe);
  68.               send_file(from,ub,fp);
  69.               fclose(fp);
  70.               return; }
  71.  
  72.       case CC_GET_FILE: { if((pe = check_path(s1,l1)) ||
  73.                      (pe = server_get_file(s1,&fp))) ERR(pe);
  74.               send_file(from,ub,fp);
  75.               fclose(fp);
  76.               return; }
  77.  
  78.       case CC_DEL_FILE: { if(!old)
  79.                 if((pe = check_path(s1,l1)) ||
  80.                    (pe = server_del_file(s1,inet_num))) ERR(pe);
  81.               server_reply(from,ub,0,0);
  82.               return; }
  83.  
  84.       case CC_DEL_DIR : { if(!old)
  85.                 if((pe = check_path(s1,l1)) ||
  86.                    (pe = server_del_dir(s1,inet_num))) ERR(pe);
  87.               server_reply(from,ub,0,0);
  88.               return; }
  89.  
  90.       case CC_UP_LOAD : { if(!old)
  91.                 if(pe = server_up_load(s1,l1,ub->pos,inet_num))
  92.                                       ERR(pe);
  93.               server_reply(from,ub,0,0);
  94.               return; }
  95.  
  96.       case CC_INSTALL : { if(!old)
  97.                 if((pe = check_path(s1,l1)) ||
  98.                    (pe = server_install(s1,inet_num))) ERR(pe);
  99.               server_reply(from,ub,0,0);
  100.               return; }
  101.  
  102.       case CC_MAKE_DIR: { if(!old)
  103.                 if((pe = check_path(s1,l1)) ||
  104.                    (pe = server_make_dir(s1,inet_num))) ERR(pe);
  105.               if(pe = server_get_pro(s1,inet_num)) ERR(pe);
  106.               server_reply(from,ub,strlen(ub->buf)+1,0);
  107.               return; }
  108.  
  109.       case CC_GET_PRO : { if((pe = check_path(s1,l1)) ||
  110.                  (pe = server_get_pro(s1,inet_num))) ERR(pe);
  111.               server_reply(from,ub,strlen(ub->buf)+1,0);
  112.               return; }
  113.  
  114.       case CC_SET_PRO : { if(!old)
  115.                 if((pe = check_path(s1,l1)) ||
  116.                    (pe = server_set_pro(s1,s2,inet_num))) ERR(pe);
  117.               if(pe = server_get_pro(s1,inet_num)) ERR(pe);
  118.               server_reply(from,ub,strlen(ub->buf)+1,0);
  119.               return; }
  120.     }
  121. }
  122.  
  123. arg_err()
  124. {
  125.     fputs("arg: -h absolute_path    set home directory.\n",stderr);
  126.     fputs("     -p udp_port_number  set port number.\n",stderr);
  127.     fputs("     -u uid_number       assume this uid after startup.\n",stderr);
  128.     fputs("     -d                  turn on debug mode.\n",stderr);
  129.     exit(1);
  130. }
  131.  
  132. main(argc,argv)
  133.     int argc;
  134.     char **argv;
  135. {
  136.     int t;
  137.  
  138.     inetd_mode = !strcmp(argv[0],"in.fspd");
  139.  
  140.     if(inetd_mode)
  141.     {
  142.     init_inetd();
  143.     freopen("/dev/null","r",stdin);
  144.     freopen("/dev/null","w",stdout);
  145.     freopen("/dev/null","w",stderr);
  146.     }
  147.  
  148.     for(t = 1; t < argc; t++)
  149.     {
  150.     if(argv[t][0] == '-') switch(argv[t][1])
  151.     {
  152.         case 'd':    dbug++; break;
  153.  
  154.         case 'h':    if(argv[t][2]) home_dir = argv[t]+2; else
  155.             if(argv[t+1] ) home_dir = argv[++t]; else
  156.                                 arg_err();
  157.             break;
  158.  
  159.         case 'u':    if(argv[t][2]) run_uid = argv[t]+2; else
  160.             if(argv[t+1] ) run_uid = argv[++t]; else
  161.                                 arg_err();
  162.             break;
  163.  
  164.         case 'p':    if(argv[t][2]) udp_port = atoi(argv[t]+2); else
  165.             if(argv[t+1] ) udp_port = atoi(argv[++t]); else
  166.                                 arg_err();
  167.             break;
  168.  
  169.         default : arg_err();
  170.  
  171.     } else arg_err();
  172.     }
  173.  
  174.     if(!inetd_mode)
  175.     {
  176.     init_network(udp_port);
  177.     if(!dbug) { freopen("/dev/null","r",stdin);
  178.             freopen("/dev/null","w",stdout);
  179.             freopen("/dev/null","w",stderr); }
  180.     }
  181.  
  182.     if(run_uid) if(setuid(atoi(run_uid)) != 0) exit(1);
  183.     init_home_dir();
  184.     init_htab();
  185.  
  186.     srandom(getpid());
  187.  
  188.     if(inetd_mode) {          server_loop(120*1000L); }  /* 2 minutes */
  189.           else { while(1) server_loop(      -1L); }
  190.  
  191.     exit(0);
  192. }
  193.