home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser-CD: Millenium / STCMILLENIUM.iso / vollvers / draconis / drtomint / main.c < prev    next >
Text File  |  1999-08-23  |  4KB  |  198 lines

  1. "/*
  2.  * Filename:     
  3.  * Version:      
  4.  * Author:       Frank Naumann
  5.  * Started:      1999-05
  6.  * Last Updated: 1999-08-22
  7.  * Target O/S:   MiNT
  8.  * Description:  
  9.  * 
  10.  * Note:         Please send suggestions, patches or bug reports to me
  11.  *               or the MiNT mailing list <mint@fishpool.com>.
  12.  * 
  13.  * Copying:      Copyright 1999 Frank Naumann <fnaumann@cs.uni-magdeburg.de>
  14.  * 
  15.  * This program is free software; you can redistribute it and/or modify
  16.  * it under the terms of the GNU General Public License as published by
  17.  * the Free Software Foundation; either version 2, or (at your option)
  18.  * any later version.
  19.  * 
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU General Public License for more details.
  24.  * 
  25.  * You should have received a copy of the GNU General Public License
  26.  * along with this program; if not, write to the Free Software
  27.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  * 
  29.  */
  30.  
  31. # include <signal.h>
  32. # include <osbind.h>
  33. # include <mintbind.h>
  34. # include <mint/ssystem.h>
  35.  
  36. # include "global.h"
  37. # include "mgw.h"
  38.  
  39. /*
  40.  * version
  41.  */
  42.  
  43. # define VER_MAJOR    0
  44. # define VER_MINOR    21
  45. # define VER_STATUS    b
  46.  
  47.  
  48. /*
  49.  * messages
  50.  */
  51.  
  52. # define MSG_VERSION    str (VER_MAJOR) "." str (VER_MINOR) str (VER_STATUS) 
  53. # define MSG_BUILDDATE    __DATE__
  54.  
  55. # define MSG_BOOT    \
  56.     "\033p Draconis to MiNT-Net gateway " MSG_VERSION " \033q\r\n" \
  57.     "Redirect Daemon\r\n"
  58.  
  59. # define MSG_GREET    \
  60.     "╜ 1999 Jens Heitmann.\r\n" \
  61.     "╜ " MSG_BUILDDATE " Frank Naumann.\r\n\r\n"
  62.  
  63. # define MSG_MINT    \
  64.     "\7This program requires FreeMiNT!\r\n"
  65.  
  66. # define MSG_ALREADY    \
  67.     "\7There is a active Draconis TCP/IP Cookie!\r\n"
  68.  
  69. # define MSG_FAILURE    \
  70.     "\7Sorry, driver NOT installed - initialization failed!\r\n\r\n"
  71.  
  72.  
  73. /* ------------------
  74.    | Install cookie |
  75.    ------------------ */
  76. static int
  77. install_cookie (void)
  78. {
  79.     long dummy;
  80.     
  81.     if (Ssystem (-1, 0, 0) == -32)
  82.     {
  83.         Cconws (MSG_MINT);
  84.         return 1;
  85.     }
  86.     
  87.     if (Ssystem (S_GETCOOKIE, TCPCOOKIE, &dummy) == 0)
  88.     {
  89.         Cconws (MSG_ALREADY);
  90.         return 1;
  91.     }
  92.     
  93.     if (Ssystem (S_SETCOOKIE, TCPCOOKIE, tcp_cookie) != 0)
  94.     {
  95.         return 1;
  96.     }
  97.     
  98.     return 0;
  99. }
  100.  
  101. /* ------------------
  102.    | Remove cookie |
  103.    ------------------ */
  104. static void
  105. uninstall_cookie (void)
  106. {
  107.     long old_stk = Super (0L);
  108.     long *jar;
  109.     
  110.     jar = *(long **) JAR;
  111.     if (jar)
  112.     {
  113.         while (*jar)
  114.         {
  115.             if (*jar == TCPCOOKIE)
  116.             {
  117.                 *jar++ = FREECOOKIE;
  118.                 *jar++ = 0L;
  119.             }
  120.             
  121.             jar += 2;
  122.         }
  123.     }
  124.     
  125.     Super ((void *) old_stk);
  126. }
  127.  
  128.  
  129. static void
  130. nothing (long sig)
  131. {
  132.     ;
  133. }
  134.  
  135. static void
  136. end (long sig)
  137. {
  138.     Psigreturn ();
  139.     uninstall_cookie ();
  140.     exit (0);
  141. }
  142.  
  143. int
  144. main (void)
  145. {
  146.     (void) Dsetdrv ('U'-'A');
  147.     
  148.     if (fork () == 0)
  149.     {
  150.         Cconws (MSG_BOOT);
  151.         Cconws (MSG_GREET);
  152.         
  153.         if (install_cookie ())
  154.         {
  155.             Cconws (MSG_FAILURE);
  156.             exit (1);
  157.         }
  158.         
  159.         Psignal (SIGALRM, nothing);
  160.         Psignal (SIGTERM, end);
  161.         Psignal (SIGQUIT, end);
  162.         Psignal (SIGHUP,  nothing);
  163.         Psignal (SIGTSTP, nothing);
  164.         Psignal (SIGINT,  nothing);
  165.         Psignal (SIGABRT, nothing);
  166.         Psignal (SIGUSR1, nothing);
  167.         Psignal (SIGUSR2, nothing);
  168.         
  169.         for (;;)
  170.         {
  171.             PMSG pmsg;
  172.             long r;
  173.             
  174.             r = Pmsg (0, MGW_GETHOSTBYNAME, &pmsg);
  175.             if (r)
  176.             {
  177.                 /* printf ("Pmsg wait fail!\n"); */
  178.                 break;
  179.             }
  180.             
  181.             if ((pmsg.msg2 != 0) && (pmsg.msg1 == 0))
  182.                 break;
  183.             
  184.             pmsg.msg2 = (long) gethostbyname ((char *) pmsg.msg1);
  185.             
  186.             r = Pmsg (1, 0xffff0000L | pmsg.pid, &pmsg);
  187.             if (r)
  188.             {
  189.                 /* printf ("Pmsg back fail!\n"); */
  190.             }
  191.         }
  192.         
  193.         uninstall_cookie ();
  194.     }
  195.     
  196.     return 0;
  197. }
  198.