home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / OS2OMMM.SRC / ROUTEMSC.C < prev   
Text File  |  1989-06-01  |  14KB  |  594 lines

  1. /***************************************************************************/
  2. /***                                                                     ***/
  3. /***               oMMM - The Outbound Matrix Message Masher             ***/
  4. /***                      Copyright 1989 BS Software                     ***/
  5. /***                                                                     ***/
  6. /***                         FILENAME: ROUTEMSC.C                         ***/
  7. /***                                                                     ***/
  8. /***                     Miscellaneous Router Functions                  ***/
  9. /***                                                                     ***/
  10. /***                 Based on the original oMMM, a portion of             ***/
  11. /***               the Opus Computer-Based Conversation System             ***/
  12. /***                     Copyright 1986, Wynn Wagner III                 ***/
  13. /***                                                                     ***/
  14. /***************************************************************************/
  15. /***                                                                     ***/
  16. /***                    Tabs set at every 4th column                     ***/
  17. /***                                                                     ***/
  18. /***************************************************************************/
  19.  
  20. /*
  21.     Polytron Version Control System Comments:
  22.  
  23.     The revision of this file is *** $Revision:   1.40    $ ***
  24.  
  25.     History of changes from 1.30 release version
  26.  
  27.     $Log:    C:/OMMM/PROJFILE/ROUTEMSC.C_V  $
  28.  *
  29.  *      Rev 1.40BP 2 June 1989 20:22:00    Bill Andrus
  30.  * Public Release Version 1.40BP (OS/2 Protected and Bound)
  31.  *
  32.  *      Rev 1.40     12 Feb 1989  4:56:52    Marshall Presnell
  33.  * Public Release Version 1.40
  34.  *
  35.  *      Rev 1.31     31 Jan 1989  0:59:00    Marshall Presnell
  36.  * oMMM 1.35 Beta Release Version
  37.  *
  38.  *      Rev 1.30     23 Jan 1989 17:54:04    Marshall Presnell
  39.  * Public Source Code Release - Version 1.30
  40.  
  41. */
  42.  
  43. /*--------------------------------------------------------------------------*/
  44. /* Include files                                                            */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47. #include    "ommm.h"
  48.  
  49. #ifdef    MSC
  50. #include <sys\types.h>
  51. #endif
  52.  
  53. #include    <sys\stat.h>
  54. #include    <ctype.h>
  55. #include    <errno.h>
  56. #include    <stdlib.h>
  57. #include    <string.h>
  58. #include    <io.h>
  59. #include    <fcntl.h>
  60. #include    <time.h>
  61.  
  62. #ifdef ZTC
  63. #    include <sys\stat.h>
  64. #    include <dos.h>
  65. #endif
  66.  
  67. /*--------------------------------------------------------------------------*/
  68. /* Static function declarations                                             */
  69. /*--------------------------------------------------------------------------*/
  70.  
  71. static void append_flo(char *oldname, char *newname);
  72.  
  73. /*--------------------------------------------------------------------------*/
  74. /* Static variable definitions                                                */
  75. /*--------------------------------------------------------------------------*/
  76.  
  77. static char         modpktpath[100];
  78. static unsigned int last_dig = 0;
  79. static char *        name_storage[256];
  80. static char         t1[100];
  81.  
  82. /*--------------------------------------------------------------------------*/
  83. /* External variable declarations                                            */
  84. /*--------------------------------------------------------------------------*/
  85.  
  86. extern NETADDRESS    ctlnet[];
  87.  
  88. /*--------------------------------------------------------------------------*/
  89. /* External function declarations                                            */
  90. /*--------------------------------------------------------------------------*/
  91.  
  92. #ifdef ZTC
  93.     int access(char *fn, int mode);
  94. #endif
  95.  
  96. /*--------------------------------------------------------------------------*/
  97. /* Local Definitions                                                        */
  98. /*--------------------------------------------------------------------------*/
  99.  
  100. #ifdef ZTC
  101. #    define    O_BINARY    0
  102. #    define    ENOTBLK     15
  103. #endif
  104.  
  105. /****************************************************************************/
  106.  
  107. /*--------------------------------------------------------------------------*/
  108. /* PULL ADDRESS                                                             */
  109. /*--------------------------------------------------------------------------*/
  110.  
  111.  char *
  112. pull_address(char * s)
  113. {
  114.     static char *    others    = "others";
  115.     static char *    all     = "all";
  116.     static char *    world    = "world";
  117.     static char *    ournet    = "ournet";
  118.     static char *    netword = "net";
  119.     unsigned int    u;
  120.     char *            t;
  121.  
  122.  
  123.     s = stpblk(s);
  124.     t = s;
  125.     cur.zone = our_zone;
  126.     if (isdigit(*t)) {
  127.         while (isdigit(*t))
  128.             ++t;
  129.         if (*t == ':') {
  130.             u = atoi(s);
  131.             while (isdigit(s[0]))
  132.                 s++;
  133.             cur.zone = u;
  134.             ++s;
  135.         }
  136.     }
  137.  
  138.     if (isdigit(s[0])) {
  139.  
  140.         u = atoi(s);
  141.         while (isdigit(s[0]))
  142.             s++;
  143.  
  144.         if (s[0] == '/') {
  145.  
  146.             cur.net = u;
  147.             s++;
  148.             if (isdigit(s[0])) {
  149.                 cur.node = atoi(s);
  150.                 while (isdigit(s[0]))
  151.                     s++;
  152.             } else if (!strncmp(s, others, 6)) {
  153.                 cur.node = DO_OTHERS;
  154.                 s += 6;
  155.             } else if (!strncmp(s, all, 3)) {
  156.                 cur.node = DO_ALL;
  157.                 s += 3;
  158.             } else if (!strncmp(s, world, 5)) {
  159.                 cur.node = DO_ALL;
  160.                 s += 5;
  161.             }
  162.         } else {
  163.             cur.net = OURNET;
  164.             cur.node = u;
  165.         }
  166.     } else if (!strncmp(s, others, 6)) {
  167.         cur.net = DO_OTHERS;
  168.         s += 6;
  169.     } else if (!strncmp(s, ournet, 6)) {
  170.         cur.net = DO_OURNET;
  171.         s += 6;
  172.     } else if (!strncmp(s, all, 3)) {
  173.         cur.net = DO_ALL;
  174.         cur.node = DO_ALL;
  175.         s += 3;
  176.     } else if (!strncmp(s, world, 5)) {
  177.         cur.net = DO_ALL;
  178.         cur.node = DO_ALL;
  179.         s += 5;
  180.     } else if (!strncmp(s, netword, 3)) {
  181.         cur.net = atoi(&s[3]);
  182.         cur.node = DO_ALL;
  183.         s += 3;
  184.         while (isdigit(s[0]))
  185.             ++s;
  186.     } else {
  187.         while (s[0] && (!isalnum(s[0])))
  188.             ++s;
  189.         cur.net = DO_ILLEGAL;
  190.         cur.node = DO_ILLEGAL;
  191.     }
  192.     return (s);
  193. }
  194.  
  195.  
  196. /*--------------------------------------------------------------------------*/
  197. /* ADD PARM                                                                 */
  198. /*--------------------------------------------------------------------------*/
  199.  
  200.  void
  201. add_parm(char * parm)
  202. {
  203.     struct _dta     dta;
  204.     char            temp[80];
  205.     char            new_fname[80];
  206.     char            ournet_str[10];
  207.     int             i;
  208.     unsigned int    curr_net;
  209.     unsigned int    net1;
  210.     unsigned int    node1;
  211. #    ifdef IBMC
  212.     char            tnet[5];
  213.     char            tnode[5];
  214. #    endif
  215.  
  216.     char *            format;
  217.  
  218.     if (cur.node == DO_OTHERS)
  219.         cur.net = DO_OTHERS;
  220.  
  221.     if (cur.net == DO_ALL)
  222.         cur.node = DO_ALL;
  223.  
  224.     if (cur.net == DO_OURNET)
  225.         cur.node = DO_ALL;
  226.  
  227.     if (cur.node == DO_ALL)
  228.         format = "%s%04x*.OUT";
  229.     else
  230.         format = "%s%04x%04x.OUT";
  231.  
  232.     curr_net = cur.net;
  233.  
  234.     if ((cur.net == DO_OTHERS) || (cur.net == DO_ALL)) {
  235.         format = "%s*.OUT";
  236.     } else if (cur.net == DO_OURNET) {
  237.         curr_net = OURNET;
  238.     }
  239.  
  240.     sprintf(temp, format, adjust_packet_path(cur.zone), curr_net, cur.node);
  241.     sprintf(ournet_str, "%04X", ctlnet[0].net);
  242.  
  243.     if (!dir_findfirst(temp, NORMAL, &dta)) {
  244.         do {
  245.             if (cur.net == DO_OTHERS) {
  246.                 if (!strncmp(ournet_str, dta.name, 4)) {
  247.                     continue;
  248.                 }
  249.             }
  250.  
  251.             sprintf(temp, "%s%s", adjust_packet_path(cur.zone), dta.name);
  252. #            ifdef IBMC
  253.             sscanf(dta.name, "%4s%4s", tnet, tnode);
  254.             sscanf(tnet, "%04x", &net1);
  255.             sscanf(tnode, "%04x", &node1);
  256. #            else
  257.             sscanf(dta.name, "%04x%04x", &net1, &node1);
  258. #            endif
  259.             i = strlen(temp) - 1;
  260.             temp[i--] = 'O';
  261.             temp[i--] = 'L';
  262.             temp[i] = 'F';
  263.             if ((!access(temp, 0)) && (dest.net != cur.net) && (cur.node != dest.node)) {
  264.                 continue;
  265.             }
  266.             temp[i++] = 'O';
  267.             temp[i++] = 'U';
  268.             temp[i] = 'T';
  269. #            ifdef RANDALL
  270.                 dostime(&hr, &mn, &se);
  271.                 dosdate(&mo, &md, &yr, &i);
  272. #            endif
  273.  
  274.             do {
  275.                 errno = 0;
  276.                 rename(temp, strcpy(new_fname,get_packet_name(our_zone,net1,node1)));
  277.                 ++last_dig;
  278.             } while (errno == ENOTBLK);
  279.  
  280.             if (errno) {
  281.                 perror("Can't rename");
  282.                 continue;
  283.             }
  284.  
  285.             if ((strlen(parm) + strlen(new_fname) + strlen(adjust_packet_path(cur.zone)) + 30) > 100) {
  286.                 archive(parm);
  287.                 parm[0] = 0;
  288.             }
  289.  
  290.             strcat(parm, new_fname);
  291.             strcat(parm," ");
  292.  
  293.         } while (!dir_findnext(&dta));
  294.     }
  295. }
  296.  
  297. /*--------------------------------------------------------------------------*/
  298. /* MAKE NORMAL                                                                */
  299. /*--------------------------------------------------------------------------*/
  300.  
  301.  void
  302. make_normal(int zone, char * template1, char newtype, int verbose)
  303. {
  304.     register char * p;
  305.     register char * q;
  306.     char            template[80];
  307.     char            oldname[80];
  308.     char            newname[80];
  309. #    ifdef IBMC
  310.     char            tnet[5];
  311.     char            tnode[5];
  312. #    endif
  313.     int             nnet;
  314.     int             nnode;
  315.     int             i;
  316.     int             j;
  317.  
  318.     if (verbose)
  319.         printf("\nNormalizing ...\n");
  320.     sprintf(template, "%s%s", adjust_packet_path(zone), template1);
  321.  
  322. big_loop:
  323.  
  324.     if (dir_findfirst(template, NORMAL, &dta)) {
  325.         if (verbose)
  326.             printf("\rNormalizing completed\n");
  327.         return;
  328.     }
  329.  
  330.     i = 0;
  331.     do {
  332.         name_storage[i] = malloc(80);
  333.         if (!name_storage[i]) {
  334.             printf("MEM\n");
  335.             exit(1);
  336.         }
  337.         sprintf(name_storage[i++], "%s", dta.name);
  338.     } while ((!dir_findnext(&dta)) && (i < 256));
  339.  
  340.     for (j = 0; j < i; j++) {
  341.         sprintf(oldname, "%s%s", adjust_packet_path(zone), name_storage[j]);
  342.         strcpy(newname, adjust_packet_path(zone));
  343.         q = &newname[strlen(newname)];
  344.         for (p = name_storage[j]; *p != '.';)
  345.             *q++ = *p++;
  346.         *q++ = '.';
  347.         if (newtype == 'O') {
  348.             p += 2;
  349.             *q++ = *p++;
  350.             switch (*p) {
  351.             case 'T':
  352.                 *q++ = 'U';
  353.                 break;
  354.             case 'O':
  355.                 *q++ = 'L';
  356.                 break;
  357.             case 'Q':
  358.                 *q++ = 'E';
  359.                 break;
  360.             case 'S':
  361.             default:
  362.                 free(name_storage[j]);
  363.                 continue;
  364.             }
  365.             *q++ = *p;
  366. #            ifdef IBMC
  367.             sscanf(name_storage[j], "%4s%4s", tnet, tnode);
  368.             sscanf(tnet, "%04x", &nnet);
  369.             sscanf(tnode, "%04x", &nnode);
  370. #            else
  371.             sscanf(name_storage[j], "%04x%04x", &nnet, &nnode);
  372. #            endif
  373.             if (!verbose)
  374.                 printf("\rSEND TO %d:%d/%d", zone, nnet, nnode);
  375.         } else {
  376.             p += 2;
  377.             switch (*p) {
  378.             case 'U':
  379.                 if (newtype == 'o')
  380.                     *q++ = 'O';
  381.                 else
  382.                     *q++ = newtype;
  383.                 break;
  384.             case 'L':
  385.                 if (newtype == 'o')
  386.                     *q++ = 'F';
  387.                 else
  388.                     *q++ = newtype;
  389.                 break;
  390.             case 'E':
  391.             case 'M':
  392.             default:
  393.                 free(name_storage[j]);
  394.                 continue;
  395.             }
  396.             *q++ = *p++;
  397.             *q++ = *p++;
  398. #            ifdef IBMC
  399.             sscanf(name_storage[j], "%4s%4s", tnet, tnode);
  400.             sscanf(tnet, "%04x", &nnet);
  401.             sscanf(tnode, "%04x", &nnode);
  402. #            else
  403.             sscanf(name_storage[j], "%04x%04x", &nnet, &nnode);
  404. #            endif
  405.             if (!verbose)
  406.                 printf("\r%s %d:%d/%d", ((newtype != 'C') ? ((newtype != 'H') ? ((newtype != 'D') ? "UNDO   " : "DIRECT ") :
  407.                       "HOLD   ") : "CM     "),zone,nnet, nnode);
  408.         }
  409.         *q = 0;
  410.         q--;
  411.         free(name_storage[j]);
  412.         if (!access(newname, 0)) {
  413.             if (*q == 'O') {
  414.                 /* Handle the case of a FLO file collision */
  415.                 append_flo(oldname, newname);
  416.             } else {
  417.                 /* Handle the case of a packet collision */
  418.                 append_out(oldname, newname);
  419.             }
  420.         } else {
  421.             errno = 0;
  422.  
  423.             /*
  424.              * This is not at all right.  I believe the kludge
  425.              * could result in
  426.              */
  427.             /* multiple flow files existing in the outbound. */
  428.  
  429. #            ifdef ADD_STUFF
  430.                 if (!AddFlag)
  431.                     rename(oldname, newname);
  432. #            else
  433.                 rename(oldname, newname);
  434. #            endif
  435.             if (errno) {
  436.                 printf("\nCan't rename '%s' to '%s'\n", oldname, newname);
  437.             }
  438.         }
  439.     }
  440.     if (i == 256)
  441.         goto big_loop;
  442.     if (verbose)
  443.         printf("\rNormalizing completed\n");
  444. }
  445.  
  446. /*--------------------------------------------------------------------------*/
  447. /* APPEND FLO                                             LOCAL FUNCTION     */
  448. /*--------------------------------------------------------------------------*/
  449.  
  450.  static void
  451. append_flo(char * oldname, char * newname)
  452. {
  453.     FILE *    in1;
  454.     FILE *    out1;
  455.  
  456.     printf("\nappend_flo: %s %s\n",
  457.            oldname,
  458.            newname);
  459.  
  460. #    ifdef ADD_STUFF
  461.         if (AddFlag) {
  462.             printf("Add mode - skipping flow file alteration\n");
  463.             return;
  464.         }
  465. #    endif
  466.  
  467.     in1 = fopen(oldname, "rt");
  468.     if (in1 == NULL) {
  469.         printf("\rName collision cannot be avoided '%s' -=> '%s'\n", oldname, newname);
  470.         return;
  471.     }
  472.     out1 = fopen(newname, "at");
  473.     if (out1 == NULL) {
  474.         printf("\rName collision cannot be avoided '%s' -=> '%s'\n", oldname, newname);
  475.         fclose(in1);
  476.         in1 = NULL;
  477.         return;
  478.     }
  479.  
  480.     printf("adding %s to %s\n", oldname, newname);
  481.     while (fgets(t1, 100, in1)) {
  482.         fputs(t1, out1);
  483.         putc('\n', out1);
  484.     }
  485.     fclose(in1);
  486.     fclose(out1);
  487.     in1 = out1 = NULL;
  488.     unlink(oldname);
  489. }
  490.  
  491. /*--------------------------------------------------------------------------*/
  492. /* APPEND OUT                                                                */
  493. /*--------------------------------------------------------------------------*/
  494.  
  495.  void
  496. append_out(char * oldname, char * newname)
  497. {
  498.     FILE *    in1;
  499.     FILE *    out1;
  500.  
  501.     in1 = fopen(oldname, "rb");
  502.     out1 = fopen(newname,"r+b");
  503.  
  504.     fseek(in1, (long) sizeof(struct _pkthdr), SEEK_SET);
  505.     fseek(out1, -2L, SEEK_END);
  506.  
  507.     buffer = malloc(BUFFER_SIZE);
  508.     if (!buffer) {
  509.         printf("MEM\n");
  510.         return;
  511.     }
  512.  
  513.     copy_out(in1, out1, buffer, BUFFER_SIZE);
  514.     fclose(in1);
  515.     fclose(out1);
  516.     in1 = out1 = NULL;
  517.     free(buffer);
  518.     unlink(oldname);
  519. }
  520.  
  521. /*--------------------------------------------------------------------------*/
  522. /* ADJUST PACKET PATH                                                        */
  523. /*--------------------------------------------------------------------------*/
  524.  
  525.  char *
  526. adjust_packet_path(int n)
  527. {
  528.     char   *p;
  529.  
  530.     if ((n <= 0) || (our_zone == 0) || (n == our_zone))
  531.         return (holding_path);
  532.     strcpy(modpktpath, holding_path);
  533.     p = modpktpath + strlen(modpktpath) - 1;
  534.     sprintf(p, ".%03x\\", n);
  535.     return (modpktpath);
  536. }
  537.  
  538. /*--------------------------------------------------------------------------*/
  539. /* ACCESS                                                      FOR ZORTECH    */
  540. /*--------------------------------------------------------------------------*/
  541.  
  542. #ifdef ZTC
  543.  
  544.  int
  545. access(char * fn, int mode)
  546. {
  547.     struct stat buf;
  548.  
  549.     if (stat(fn, &buf) != 0)
  550.         return (-1);
  551.  
  552.     if (mode == 0) {
  553.         errno = 0;
  554.         return (0);
  555.     }
  556.  
  557.     if ((mode & 6) == 2)
  558.         if ((buf.st_mode & S_IWRITE) == 0) {
  559.             errno = EACCES;
  560.             return (-1);
  561.         } else {
  562.             errno = 0;
  563.             return (0);
  564.         }
  565.  
  566.     if ((mode & 6) == 4)
  567.         if ((buf.st_mode & S_IREAD) == 0) {
  568.             errno = EACCES;
  569.             return (-1);
  570.         } else {
  571.             errno = 0;
  572.             return (0);
  573.         }
  574.  
  575.     if ((mode & 6) == 6)
  576.         if (((buf.st_mode & S_IREAD) == 0) &&
  577.             ((buf.st_mode & S_IWRITE) == 0)) {
  578.             errno = EACCES;
  579.             return (-1);
  580.         } else {
  581.             return (0);
  582.             errno = 0;
  583.         }
  584.  
  585.     errno = EACCES;
  586.     return (-1);
  587. }
  588.  
  589. #endif
  590.  
  591. /*--------------------------------------------------------------------------*/
  592. /*                                  END OF FILE                                */
  593. /*--------------------------------------------------------------------------*/
  594.