home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 3.ddi / OS2LAN / CTSMSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  10.4 KB  |  447 lines

  1. /*
  2.  *   System Dependent Server Message Handling
  3.  *
  4.  *   OS/2 Lan Manager using named pipes
  5.  *
  6.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  7.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  8.  *    transfer of this program is strictly prohibited.
  9.  *
  10.  *      Copyright (c) 1987, 1988, 1989 FairCom Corporation
  11.  *    (Subject to limited distribution and
  12.  *     restricted disclosure only.)
  13.  *    *** ALL RIGHTS RESERVED ***
  14.  *
  15.  *    4006 West Broadway
  16.  *    Columbia, MO 65203
  17.  *
  18.  *
  19.  *    c-tree(R)    Version 4.3
  20.  *            Release C
  21.  *            February 7, 1989 17:30
  22.  *
  23.  */
  24.  
  25. #include <memory.h>
  26. #include "ctstdr.h"
  27. #include "ctoptn.h"
  28. #include "ctstrc.h"
  29. #include "cterrc.h"
  30. #include "ctcomm.h"
  31.  
  32. #define INCL_SERVERPIPE
  33. #include "ctpipes.h"
  34.  
  35. #define NOT_A_SESN 0xff      /* illegal value for session number */
  36.  
  37. #ifdef DEBUG
  38.  #ifdef LOCAL
  39.   #undef LOCAL
  40.   #define LOCAL /* */
  41.  #endif
  42. #endif
  43.  
  44. /*
  45.  * PROTOTYPES
  46.  */
  47. COUNT stops(COUNT);
  48. COUNT display_users(COUNT);
  49.  
  50. unsigned  pipe_handle;
  51. static unsigned  pipe_open = 0;
  52. int log_time = 0;
  53.  
  54. LOCAL COUNT namenum = 0;   /* name's number */
  55. LOCAL UTEXT *usr_to_ses;   /* xlate ctree user number to netbios ses */
  56. LOCAL TEXT *usr_to_name;   /* xlate to netbios name */
  57. /*LOCAL UTEXT act_ses[256];  * 1 if session number of active session */
  58. LOCAL UCOUNT msgsiz;       /* max message size */
  59. LOCAL UCOUNT usrmax;       /* max number of users */
  60.  
  61. LOCAL UCOUNT lastsess;        /* session number rcvd on TST_MSGSIZ message */
  62. LOCAL TEXT lastname[USR_NAME_LEN+1];   /* name  rcvd on TST_MSGSIZ message */
  63.  
  64. extern COUNT uerr_cod;
  65. extern TEXT  ct_buf[];
  66. extern COUNT cts_mxfil;
  67. extern int fl_backgr_proc;
  68.  
  69.  
  70. TEXT *mballc();
  71.  
  72.  
  73. /*
  74.  * hook up to the network.
  75.  */
  76. TEXT *getmid(sizmsg, maxusr)
  77. UCOUNT        sizmsg;
  78. COUNT         maxusr;
  79. {
  80.    TEXT *retval;
  81.    UCOUNT e, i;
  82.    UCOUNT ctxsize;
  83.  
  84.    uerr_cod = 0;      /* in case no error */
  85.  
  86.    usrmax = maxusr;   /* save (used when cvrting sesn to usrn) */
  87.  
  88.    strcpy(pipename, ISAM_PIPENAME);
  89.    if (e = makepipe(&pipe_handle, pipename)) {
  90.       uerr_cod = SSID_ERR;
  91.       return (NULL);
  92.    }
  93.    pipe_open = 1;
  94.  
  95.    msgsiz = sizmsg;   /* local max msg size for start_recv's */
  96.  
  97.    /* get current dir */
  98.    if (get_default_dir())
  99.       return (NULL);
  100.  
  101.    /*
  102.     * allocate
  103.     *  retval - return to ctsrvr for message buffer
  104.     *  usr_to_ses - logical user to netbios session xlate table
  105.     *  usr_to_name - logical user to netbios name table
  106.     */
  107.    if ( (usr_to_ses = (UTEXT *) mballc(maxusr + 1, sizeof(UTEXT))) == NULL
  108.        || (usr_to_name = (TEXT *) mballc(maxusr, USR_NAME_LEN + 1)) == NULL
  109.        || (retval = (UTEXT *) mballc(1, sizmsg + sizeof(UCOUNT))) == NULL) {
  110.        uerr_cod = SSPC_ERR;
  111.        return (NULL);
  112.     }
  113.  
  114.    /*
  115.     * init the usernum/sesnum xlate table
  116.     */
  117.  
  118.    memset(usr_to_ses, NOT_A_SESN, maxusr+1);
  119.  
  120.    /* first word of ctsrvr's buffer is hidden and used here to
  121.     * remember which session for last recv
  122.     */
  123.    retval += sizeof(UCOUNT);
  124.  
  125.    set_max_file_handles(cts_mxfil);
  126.  
  127.    display_menu();
  128.  
  129.    return (retval);      
  130. }
  131.  
  132. /*
  133.  * undo the comm link
  134.  */
  135. COUNT ridmid()
  136. {
  137. VOID far disconnect();
  138.  
  139.    disconnect();
  140.    return (0);
  141. }
  142.  
  143.  
  144. /*
  145.  * getumsg associates logical user number with session number
  146.  * of last recv'd message.  ctsrvr use's 0 to max-1 for logged in
  147.  * users.  uses the max-th entry for temporarily remembering a user
  148.  * that, for examp., may not 'log in'.
  149.  */
  150. LONG getumsg(pmsg, usrn, msgptr)
  151. MESSAGE     *pmsg;
  152. COUNT         usrn;
  153. TEXT         *msgptr;
  154. {
  155.    if (usrn >= 0)
  156.       usr_to_ses[usrn] = lastsess;
  157.    if (usrn < usrmax)
  158.       cpybuf(&usr_to_name[usrn*(USR_NAME_LEN+1)],
  159.              lastname, (USR_NAME_LEN+1));
  160.    return ((LONG) usr_to_ses[usrn]);
  161. }
  162.  
  163. /*
  164.  * if disconnect, call stop user
  165.  */
  166. stpses(COUNT user_num)
  167. {
  168.    if (usr_to_ses[user_num] != NOT_A_SESN) {
  169.       if (user_num < usrmax) {
  170.          usr_to_ses[user_num] = NOT_A_SESN;    /* user logged out */
  171.          stpusr(user_num);
  172.       }
  173.    }
  174. }
  175.  
  176. /*
  177.  * when new user, stop previous login of same
  178.  */
  179. stpname(TEXT *name, COUNT session)
  180. {
  181. FAST COUNT usrn;
  182. int byte_offs;
  183. char *this_name;
  184.  
  185.    for (usrn = 0; usrn < usrmax; usrn++) {
  186.       byte_offs = (usrn * USR_NAME_LEN) + 1;
  187.       this_name = &usr_to_name[byte_offs];
  188.       if (NOT_A_SESN != usr_to_ses[usrn] &&
  189.           !strcmp(name, this_name)       &&
  190.           usr_to_ses[usrn] == session) {
  191.          usr_to_ses[usrn] = NOT_A_SESN;
  192.          stpusr(usrn);
  193.          break;
  194.       }
  195.    }
  196. }
  197.  
  198.  
  199. COUNT dedusr(msgid)
  200. LONG        msgid;
  201. {
  202.    return(-1);
  203. }
  204.  
  205. /*
  206.  * wait for request from user
  207.  */
  208. COUNT ctrqst(PFAST TEXT **msgadr, MESSAGE *pmsg)
  209. /* msgadr = ptr to message area */
  210. /* pmsg   = ptr to message header */
  211. {
  212. FILEPARM   *fpm;
  213. unsigned bytes, namlen;
  214. char *tp;
  215.  
  216.    set_exec_priority(0);
  217.    for (;;) {
  218.  
  219.       if (!(bytes = readpipe(pipe_handle, (char far*)*msgadr))) {
  220.          return(uerr_cod = SRQS_ERR);
  221.       }
  222.  
  223. /*
  224.  * an improvment of system is to implement readpipe
  225.  * to use non-blocking mode
  226.  */
  227.  
  228.       if (bytes == sizeof(long)) {   /* check for handshake */
  229.          if (*(long*)*msgadr == PIPE_TEST) {
  230.             if (sendlogonreply(pipe_handle))
  231.                   return(uerr_cod = SRQS_ERR);
  232.             else
  233.                continue; /* go, wait for next message */
  234.          }
  235.          else
  236.          if (*(long*)*msgadr == PIPE_TERM_SRV) {
  237.             sendlogonreply(pipe_handle);
  238.             stops(0);
  239.             disconnect();
  240.               srvexit("Ctree server terminated normally",0,0);
  241.          }
  242.       }
  243.  
  244.       break;
  245.  
  246.    } /* end for forever */
  247.  
  248.  
  249.    set_exec_priority(1);
  250.    /* a receive has completed */
  251.  
  252.    /* save the session number in hidden (to ctsrvr) part of
  253.     * buffer in case ctsrvr wants to associate this session
  254.     * with a user id
  255.     */
  256.    *( ((UCOUNT *) (*msgadr)) - 1 ) = usr_to_ses[pmsg->musrn];
  257.  
  258.    /* copy to ctsrvr's msg header buffer */
  259.    cpybuf(pmsg, *msgadr, sizeof(MESSAGE));
  260.  
  261.    /* got a message.  if its a
  262.     * login (TST_MSGSIZ), then the user's machine name has
  263.     * been appended on the end by ctamsg.  If so, use it to
  264.     * search for duplicate user.
  265.     */
  266.    if (pmsg->mfunc == TST_MSGSIZ ||
  267.         pmsg->mfunc == TST_SPCLOG)    {
  268.       namlen = USR_NAME_LEN + sizeof(int) + 1;
  269.       if (pmsg->mdlen >= namlen) {
  270.         pmsg->mdlen -= namlen;
  271.         ((MESSAGE *)(*msgadr))->mdlen -= namlen;
  272.  
  273.         /* save session number for later association with usrn in getumsg */
  274.         cpybuf((char*)&lastsess,
  275.                *msgadr + sizeof(MESSAGE) + pmsg->mdlen,
  276.                sizeof(int));
  277.         *( ((UCOUNT *) (*msgadr)) - 1 ) = lastsess;
  278.         /* save name for later association with usrn in getumsg */
  279.         memset(lastname, '\0', USR_NAME_LEN);
  280.         cpybuf(lastname,
  281.                *msgadr + sizeof(MESSAGE) + pmsg->mdlen + sizeof(int),
  282.                USR_NAME_LEN);
  283.         stpname(lastname, lastsess);   /* kill any with same name an session */
  284.  
  285.       }
  286.    }
  287.  
  288.  
  289.    /* check for file name conversion to absolute */
  290.  
  291.    if (pmsg->mfunc == FN_OPNFIL || pmsg->mfunc == FN_CREDAT ||
  292.        pmsg->mfunc == FN_CREIDX || pmsg->mfunc == FN_CREMEM) {
  293.       fpm = (FILEPARM *) (((TEXT *) *msgadr) + sizeof(MESSAGE));
  294.       strcpy(ct_buf,fpm->fpnam);
  295.       fulnam(fpm->fpnam,ct_buf,MAX_NAME);
  296.       tp = konvert_drive_to_local(fpm->fpnam);
  297.       if (tp)
  298.          strcpy(fpm->fpnam,tp);
  299.    }
  300.  
  301.    return (NO_ERROR);
  302. }
  303.  
  304. /*
  305.  * send data to user
  306.  */
  307. COUNT ctrspn(msgadr, pmsg, sizmsg, usrn)
  308. PFAST TEXT  *msgadr;
  309. MESSAGE     *pmsg;
  310. UCOUNT            sizmsg;
  311. COUNT               usrn;
  312. {
  313. UCOUNT e = 0;
  314.  
  315.     /* copy message header */
  316.    cpybuf(msgadr, pmsg, sizeof(MESSAGE));
  317.  
  318.     if (usrn < 0) {         /* signals a call from TST_STPUSR */
  319.         usrn = - (usrn + 1);
  320.         if (usrn < usrmax)
  321.             usr_to_ses[usrn] = NOT_A_SESN;    /* user logged out */
  322.     }
  323.  
  324.    set_exec_priority(0);
  325.  
  326.     /* send response */
  327.    if (writepipe(pipe_handle, (char far*)msgadr, sizmsg) != sizmsg) {
  328.       /* only display of a message */
  329.       stpses(pmsg->musrn);
  330.    }
  331.  
  332.    if (pmsg->mfunc == TST_MSGSIZ ||
  333.        pmsg->mfunc == TST_STPUSR ||
  334.        pmsg->mfunc == TST_STPSRV ||
  335.        pmsg->mfunc == TST_DEDUSR ||
  336.        pmsg->mfunc == TST_EXISTS ||
  337.        pmsg->mfunc == TST_SPCLOG)
  338.       display_users(0);
  339.  
  340.    return (e ? (uerr_cod = SRSP_ERR) : NO_ERROR);
  341. }
  342.  
  343. /*
  344.  * check status of user - return YES if session lost
  345.  */
  346. COUNT norspn(usrn)
  347. COUNT        usrn;
  348. {
  349.    return NO;   /* session lost always caught elsewhere */
  350. }
  351.  
  352.  
  353. /*
  354.  * close all sessions, cancel async commands and remove name from netbios
  355.  */
  356. VOID far disconnect()
  357. {
  358. FAST COUNT usrn;
  359. UCOUNT i;
  360. COUNT had = 0;
  361.  
  362.    set_exec_priority(0);
  363.  
  364.    if (pipe_open) {
  365.       i = disconnect_pipe(pipe_handle);
  366.       if (i)
  367.          srvmesg("COULD NOT CLOSE PIPE",i,0);
  368.       pipe_open = 0;
  369.    }
  370.  
  371.    /* close all active sessions */
  372.    for (usrn = 0; usrn < usrmax; usrn++) {
  373.       if (NOT_A_SESN != usr_to_ses[usrn])
  374.          stpses(usrn);
  375.    }
  376.  
  377. }
  378.  
  379. chkusrtim(usrtim,usrmap,usrtrn)
  380. LONG    *usrtim;
  381. COUNT      *usrmap;
  382. LONG             *usrtrn;
  383. {
  384.    /* called from login */
  385.    /* note, sessions lost & duplicate sessions caught elsewhere */
  386.    return(NO_ERROR);
  387. }
  388.  
  389. /*
  390.  * dedicated server console routines
  391.  */
  392.  
  393. display_menu()
  394. {
  395.    if (fl_backgr_proc)
  396.       return;
  397.     printf("\n\n\n\n\n\n\n\n\n\n");
  398.     printf("Dedicated server active\n");
  399. }
  400.  
  401.  
  402. COUNT stops(mode)      /* returns != 0 if answer pending */
  403. COUNT mode;      /* either 0 or answer to y/n */
  404. {
  405.    FAST COUNT usrn;
  406.    COUNT had = 0;
  407.  
  408.    if (!mode) {
  409.       for (usrn = 0; usrn < usrmax; usrn++)
  410.          if (NOT_A_SESN != usr_to_ses[usrn])
  411.             had ++;
  412.    }
  413.  
  414.    had = stpsrv(0L,1,-1);
  415.       ridmid();
  416.    return (0);
  417. }
  418.  
  419. COUNT display_users(COUNT mode)     /* mode not used */
  420. {
  421. FAST COUNT usrn;
  422. COUNT had = 0;
  423.  
  424.    if (fl_backgr_proc)
  425.       return;
  426.  
  427.    printf("\n");
  428.    for (usrn = 0; usrn < usrmax; usrn++)
  429.       if (NOT_A_SESN != usr_to_ses[usrn]) {
  430.          had ++;
  431.          if (had == 1) {
  432.             printf("Username             session\n");
  433.             printf("-------------------- -------\n");
  434.          }
  435.          printf("%-20.20s   %5d\n", &usr_to_name[usrn*(USR_NAME_LEN+1)],
  436.                                   usr_to_ses[usrn]);
  437.       }
  438.    if (!had)
  439.       printf("No");
  440.    else
  441.       printf("%d", had);
  442.    printf(" active users\n");
  443.    return (0);
  444. }
  445.  
  446. /* end of ctsmsg.c */
  447.