⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 网络端口的服务程序
💻 C
字号:
/*
    Portserver - Use RTERM protocol to serve serial ports over a network
    Copyright (C) 1998 Kenn Humborg

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <syslog.h>
#include <unistd.h>

#include "config.h"
#include "connection.h"
#include "port.h"
#include "portserver.h"
#include "options.h"

void Initialize(int argc, char **argv)
{
   struct Port *port;
   char hostname[256];
   struct hostent *host;
   struct sockaddr_in addr;
   STATUS status;
   int retval;
   int flags;
   int reuse;

   /* Initialize the port and connection lists */
   port_list.next = port_list.prev = &port_list;
   connection_list.next = connection_list.prev = &connection_list;

   status = CreatePort(TEMPLATE_PORT_NAME, &port);
   if (IS_FAILURE(status)) {
      syslog(LOG_ERR, "Cannot create template port: %s\n", ErrorMsg(status));
      exit(1);
   }

   port->flags |= PORT_TEMPLATE;
   AddPort(port);

   status = ParseCommandLine(argc, argv);
   if (IS_FAILURE(status)) {
      syslog(LOG_ERR, "Command line options processing failed: %s\n", 
                ErrorMsg(status));
      exit(1);
   }

   retval = ParseConfigFile(cmd_options.config_file);
   if (retval != 0) {
      exit(1);
   }

   gethostname(hostname, sizeof(hostname)-1);
   host = gethostbyname(hostname);
   if (host == NULL) {
      syslog(LOG_ERR, "Cannot get local hostname: %m");
      exit(1);
   }

   /* Clear out the socket address structure */
   memset(&addr, 0, sizeof(addr));

   addr.sin_family = host->h_addrtype;
   addr.sin_port = htons(PORT_NUMBER);

   main_socket = socket(AF_INET, SOCK_STREAM, 0);
   if (main_socket == -1) {
      syslog(LOG_ERR, "Cannot create control socket: %m");
      exit(1);
   }

   reuse = TRUE;
   retval = setsockopt(main_socket, SOL_SOCKET, SO_REUSEADDR,
                          &reuse, sizeof(reuse));
   if (retval == -1) {
      syslog(LOG_ERR, "Cannot set socket options to reuse addr: %m");
      exit(1);
   }

   retval = bind(main_socket, &addr, sizeof(addr));
   if (retval == -1) {
      syslog(LOG_ERR, "Cannot bind control socket: %m");
      exit(1);
   }


   retval = listen(main_socket, 5);
   if (retval == -1) {
      syslog(LOG_ERR, "listen() failed for control socket: %m");
      exit(1);
   }

   flags = fcntl(main_socket, F_GETFL);
   flags |= O_NONBLOCK;
   fcntl(main_socket, flags);

   GetMaxFD();
   FD_ZERO(&read_fds);
   FD_ZERO(&write_fds);
   FD_ZERO(&excep_fds);

   FD_SET(main_socket, &read_fds);
   FD_SET(main_socket, &excep_fds);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -