コード例 #1
0
ファイル: main.c プロジェクト: ftnapps/smapinntpd
int main(int argc, char **argv) 
{ 
   SOCKET sock;
   int error,res; 
   struct sockaddr_in local;
   fd_set fds;
   struct timeval tv;
   FILE *fp;
   struct _minf m;
   
   if(argc == 2 && (stricmp(argv[1],"?")==0 || stricmp(argv[1],"-h")==0 || stricmp(argv[1],"--help")==0))
   {

           /*          1         2         3         4         5         6         7         8 */
           /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */     
      printf("\n"
             "Usage: smapinntpd [<options>]\n"
             "\n"
             " General options:\n"
             "\n"
             " -p[ort] <port>         Port number for SmapiNNTPd (default: 5000)\n"
             " -m[ax] <maxconn>       Maximum number of simultaneous connections (default: 5)\n"
             " -g[roups] <groupsfile> Read this file instead of " CFG_GROUPSFILE "\n"
             " -a[llow] <allowfile>   Read this file instead of " CFG_ALLOWFILE "\n"
             " -u[sers] <usersfile>   Read this file instead of " CFG_USERSFILE "\n"
             " -x[lat] <xlatfile>     Read this file instead of " CFG_XLATFILE "\n"
             " -l[ogfile] <logfile>   Log to this file instead of " CFG_LOGFILE "\n"
             " -noecholog             Do not write log messages to console\n" 
             " -debug                 Write all network communication to console\n"
             "\n"
             " Options for displaying messages:\n"
             "\n"
             " -readorigin           Get addresses from the origin line instead of JAM header\n"
             " -noencode             Do not MIME encode headers with 8-bit characters\n"
             " -strictnetmail        Use strict article counters in netmail areas\n"
             " -def_flowed on/off    Default setting for format=flowed (RFC 2646)\n"
             " -def_showto on/off    Default setting for the display of recipient on from line\n"
             "\n"
             " Options for posting messages:\n"
             "\n"
             " -nostripre            Do not remove \"Re:\" from subject line\n"
             " -notearline           Do not put X-Newsreader/User-Agent string on tearline\n"
             " -noreplyaddr          Do not create REPLYADDR kludges\n"
             " -notzutc              Do not create TZUTC kludges\n"
             " -nocancel             Do not allow cancelling of messages\n"
             " -smartquote           Reformat quoted text to fidonet style\n"
             " -origin <origin>      Origin to use instead of contents of Organization line\n"
             " -guestsuffix <suffix> Suffix added to from name of unauthenticated users\n"
             " -echomailjam <file>   Create echomail.jam file for CrashMail and other tossers\n"
             "\n"
             " Options for configuration files:\n"
             "\n"
             " -config <file>        Read options from this file\n"
             " -create <file>        Create a configuration file with the default options\n"
             "\n"
             "If no options are specified on the commandline, SmapiNNTPd will attempt to read\n"
             "options from " CONFIGFILE " (if it exists).\n"
             "\n");
      
      return(FALSE);
   }

   if(argc == 1)
   {
      if((fp=fopen(CONFIGFILE,"r")))
      {
         fclose(fp);
     
         if(!readargs(CONFIGFILE))
         {
            freeargs();
            exit(0);
         }
      }
   }
   else
   {
      if(!parseargs(argc-1,&argv[1],NULL,0))
      { 
         freeargs();
         exit(0);
      }
   }

   if(!os_init())
   {
       freeargs();
       exit(10);
   }
	
   m.req_version = 0;
   m.def_zone = 0;

   if(MsgOpenApi(&m) != 0)
   {
      os_showerror("Failed to initialize SMAPI");
      freeargs();
      os_free();
      exit(10);
   }

   sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);

   if(sock == INVALID_SOCKET)
   {
      uchar err[200];

      os_showerror("Failed to create socket: %s",os_strerr(os_errno(),err,200));
      MsgCloseApi();
      os_free();
      freeargs();
      exit(10);
   }

   memset(&local, 0, sizeof(local) );

   local.sin_family = AF_INET;
   local.sin_addr.s_addr = INADDR_ANY;
   local.sin_port = htons(cfg_port);

   error = bind(sock,(struct sockaddr *)&local,sizeof(local));

   if(error == SOCKET_ERROR)
   {
      uchar err[200];

      os_showerror("Could not bind to port (server already running?): %s",os_strerr(os_errno(),err,200));
      close(sock);
      MsgCloseApi();
      os_free();
      freeargs();
      exit(10);
   }

   error = listen(sock,5);

   if(error == SOCKET_ERROR)
   {
      uchar err[200];

      os_showerror("Could not listen to socket: %s",os_strerr(os_errno(),err,200));
      close(sock);
      MsgCloseApi();
      os_free();
      freeargs();
      exit(10);
   }

   os_logwrite(SERVER_NAME " " SERVER_VERSION " is running on port %d",cfg_port);

   if(cfg_debug)
      os_logwrite("Compiled " __DATE__ " " __TIME__);

   while(!get_server_quit())
   {
      FD_ZERO(&fds);
      FD_SET(sock,&fds);
	
      tv.tv_sec=1;
      tv.tv_usec=0;
	
      res=select(sock+1,&fds,NULL,NULL,&tv);

      if(res != 0 && res != -1)
      {
          SOCKET active_sock; 
          struct sockaddr_in from; 
          int fromlen = sizeof(from);               

          active_sock = accept(sock,(struct sockaddr *)&from,&fromlen);

          if(active_sock == SOCKET_ERROR)
          {
              uchar err[200];
              os_showerror("Failed to accept incoming connection: %s",os_strerr(os_errno(),err,200));
              break;
          }

          os_startserver(server,active_sock);
      }
   }

   if(get_server_openconnections())
   {
      os_logwrite("Exiting. %ld connection(s) are active, waiting for them to quit",
			get_server_openconnections());

      while(get_server_openconnections())
         os_sleep(1);
   }

   close(sock);

   os_logwrite(SERVER_NAME " exited");
   MsgCloseApi();
   os_free();
   freeargs();
   exit(0);
}
コード例 #2
0
ファイル: squish.c プロジェクト: klamonte/maximus
static void near CleanupConfig(void)
{
  (void)MsgCloseApi();
  Cleanup();
}