Esempio n. 1
0
struct server *server_open_unix(const char *path, uint16_t id)
{
	struct server *server;

	server = malloc(sizeof(*server));
	if (!server)
		return NULL;

	memset(server, 0, sizeof(*server));
	server->id = id;

	server->fd = open_server(path);
	if (server->fd < 0) {
		free(server);
		return NULL;
	}

	if (mainloop_add_fd(server->fd, EPOLLIN, server_accept_callback,
						server, server_destroy) < 0) {
		close(server->fd);
		free(server);
		return NULL;
	}

	return server;
}
Esempio n. 2
0
/* Parse a servers part of config */
void
parse_servers_line(char *buf)
{
	char *p, *n;
	int inum;

	p = buf;
	strsep(&p, " \t");
	if (!open_server(buf)) {
		logd(LOG_WARNING, "Can't open server %s. Ignored.", buf);
		return;
	}
	inum = 0;
	while ((n = strsep(&p, " \t")) != NULL) {
		if (*n != '\0') {
			if (open_interface(n))
				inum++;
			else {
				logd(LOG_WARNING, "Interface %s does not exist. Ignored.", n);
			}
		}
	}
	/* We found no interfaces for listening on this computer */
	if (inum == 0) {
		srv_num--;
		free(servers[srv_num]);
	}
}
Esempio n. 3
0
static void listen_sensor_app(short port)
{
  SOCKET guest_sock;
  SOCKET sock;
  char *data;

  printf("opening server on port 22470\n");
  while ((guest_sock = open_server("22470")) == INVALID_SOCKET) 
    {
      printf("unable to listen on port 22470... retyring in few seconds\n");
      our_sleep(5);
    }

  while (1)
    {
      printf("Waiting for connection from VM\n");
      SOCKET client_sock = connect_client(guest_sock);
      if (client_sock == INVALID_SOCKET)
	{
	  perror("accept()");
	  continue;
	}
      printf("Got sensors connection from VM\n");
      while (1) 
	{
	  printf("Connecting to hardware device on port %d...\n", port);
	  sock = open_socket("127.0.0.1", port);
	  if (sock != INVALID_SOCKET)
	    {
	      printf("Connected to hardware device, socket=%d :-)\n", sock);
	      while ((data = read_socket(sock, PACKET_LEN)))
		{
		  if (write_socket(client_sock, data, PACKET_LEN)<0)
		    {
		      printf("Connection closed with VM - closing connection with hardware device\n");
		      free(data);
	            }
	        }
	      printf("Closing connection to hardware device\n");
	      closesocket(sock);
	      our_sleep(2);
	    }
	  else
	    {
	      printf("Unable to connect to hardware device\n");
	      closesocket(client_sock);
	      closesocket(guest_sock);
	      return;
	    }
	    
	}
    }
  closesocket(guest_sock);
}
Esempio n. 4
0
int main (int argc, char **argv)
{
  if (argc < 2)
    usage ("Invalid number of arguments");

  {    
    const char *const action = argv[1];    
    Success (*function) (Stream, Connection_Id);
    Connection_Id conn_id;
    char *host;
    int port;

    if (! strcmp (action, "--version"))
      {
        printf ("spdsend %s\n", VERSION);
        exit (EXIT_OK);
      }
    
    if (! strcmp (action, "--open"))
      {
        if (argc != 4)
          usage ("Invalid number of arguments");
        host = argv[2];
        port = string_to_number (argv[3], 0, 65535);
      }
    else
      {
        if (argc != 3)
          usage ("Invalid number of arguments");
        conn_id = string_to_number (argv[2], CONNECTION_ID_MIN,
                                    CONNECTION_ID_MAX);
        if (! strcmp (action, "--close"))
          function = close_connection;
        else if (! strcmp (action, "--send"))
          function = send_command;
        else
          usage ("Invalid option");
      }
    
    {
      Stream server = open_server ();
      if (server == NONE)
        return EXIT_ERROR;
      
      {
        int result = (! strcmp (action, "--open")
                      ? open_connection (server, host, port)
                      : function (server, conn_id));
        return (result == OK ? EXIT_OK : EXIT_ERROR);
      }
    }
  }
}
Esempio n. 5
0
File: main.c Progetto: SeHoonYang/SE
int main()
{
  /* Get arguments / Make GUI. It depends */

  /* Initializing */
  srand(time(NULL));

  printf("Initializing data structures\n");
  init_user_data();

  printf("Initializing monster data\n");
  init_mob();

  printf("Loading map data to the memory\n");
  load_maps();

  /* Release monster data */
  clear_mob_data();

  /* Add thread to properly stop the server */
  init_socket(sizeof(struct packet));
  init_server(&server_socket, 2033);
  open_server(server_socket,20);
  printf("Server has been opened\n");

  /* Mob manager */
  _beginthread(mob_manager, 0, NULL);

  /* Networking thread */
  _beginthread(start_server, 0, server_socket);

  while(getch() != 27);

  /* Stop monster manager */
  stop_mob_manager();

  /* Write behind; Write user data to file system */
  save_users_data();

  /* Release user data */
  clear_user_data();

  /* Release map data */
  clear_map_data();

  /* Terminate the server */
  close_socket(server_socket);

  end_socket();

  return 0;
}
Esempio n. 6
0
int
main(int argc, char *argv[])
{
	int c, i, j, configured = 0;
	pid_t opid;
	char prgname[80], filename[256], *p;
	struct servent *servent;
	pthread_t tid;

	/* Default plugin_base */
	strlcpy(plugin_base, PLUGIN_PATH, sizeof(plugin_base));

	if ((servent = getservbyname("bootps", 0)) == NULL)
		errx(EX_UNAVAILABLE, "getservbyname(bootps)");
	bootps_port = servent->s_port;
	if ((servent = getservbyname("bootpc", 0)) == NULL)
		errx(EX_UNAVAILABLE, "getservbyname(bootpc)");
	bootpc_port = servent->s_port;

	openlog("dhcprelya", LOG_NDELAY | LOG_PID, LOG_DAEMON);

	strlcpy(prgname, argv[0], sizeof(prgname));
	filename[0] = '\0';
	while ((c = getopt(argc, argv, "A:c:df:hi:p:")) != -1) {
		switch (c) {
		case 'A':
			if (configured == 2)
				errx(1, "Either config file or command line options allowed. Not both.");
			max_packet_size = strtol(optarg, NULL, 10);
			if (max_packet_size < 300 || max_packet_size > DHCP_MTU_MAX)
				errx(1, "Wrong packet size");
			break;
		case 'c':
			if (configured == 2)
				errx(1, "Either config file or command line options allowed. Not both.");
			max_hops = strtol(optarg, NULL, 10);
			if (max_hops < 1 || max_hops > 16)
				errx(1, "Wrong hops number");
			break;
		case 'd':
			debug++;
			break;
		case 'f':
			if (configured == 1)
				errx(1, "Either config file or command line options allowed. Not both.");
			if (configured == 2)
				errx(1, "only one config file allowed");
			configured = 2;
			read_config(optarg);
			break;
		case 'i':
			if (configured == 2)
				errx(1, "Either config file or command line options allowed. Not both.");
			configured = 1;
			if (!open_interface(optarg))
				logd(LOG_DEBUG, "Interface %s does not exist. Ignored.", optarg);
			break;
		case 'p':
			strlcpy(filename, optarg, sizeof(filename));
			break;
		case 'h':
		default:
			usage(prgname);
		}
	}

	argc -= optind;
	argv += optind;

	if (optind == 0)
		argc--;

	if ((configured == 1 && argc < 1) || (configured == 2 && argc >= 1))
		usage(prgname);

	/* Initialize polugins */
	for (i = 0; i < plugins_number; i++) {
		if (plugins[i]->init)
			if ((plugins[i]->init) (options_heads[i]) == 0)
				errx(1, "Can't initialize a plugin %s\n", plugins[i]->name);
	}

	for (i = 0; i < argc; i++) {
		open_server(argv[i]);
		for (j = 0; j < if_num; j++) {
			if (i > ifs[j]->srv_num - 1) {
				ifs[j]->srv_num++;
				ifs[j]->srvrs = realloc(ifs[j]->srvrs, ifs[j]->srv_num * sizeof(int));
			}
			ifs[j]->srvrs[ifs[j]->srv_num - 1] = i;
		}
	}

	if (if_num == 0)
		errx(1, "No interfaces found to listen. Exiting.");

	logd(LOG_WARNING, "Total interfaces: %d", if_num);

	/* Make a PID filename */
	if (filename[0] == '\0') {
		strlcpy(filename, "/var/run/", sizeof(filename));
		p = strrchr(prgname, '/');
		if (p == NULL)
			p = prgname;
		else
			p++;
		strlcat(filename, p, sizeof(filename));
		strlcat(filename, ".pid", sizeof(filename));
	}
	/* Create a PID file and daemonize if no debug flag */
	if (!debug && (pfh = pidfile_open(filename, 0644, &opid)) == NULL) {
		if (errno == EEXIST)
			errx(1, "Already run with PID %lu. Exiting.", (unsigned long)opid);
		errx(1, "Can't create PID file");
	}
	signal(SIGHUP, SIG_IGN);

	if (!debug) {
		if (daemon(0, 0) == -1)
			process_error(1, "Can't daemonize. Exiting.");
		else
			logd(LOG_DEBUG, "Runned as %d", getpid());
	}
	if (pfh)
		pidfile_write(pfh);

	STAILQ_INIT(&q_head);

	pthread_mutex_init(&queue_lock, NULL);

	/* Create listeners for every interface */
	for (i = 0; i < if_num; i++) {
		pthread_create(&tid, NULL, listener, ifs[i]);
		pthread_detach(tid);
	}

	/* Main loop */
	while (1) {
		if (queue_size > 0)
			process_queue();

		/* Process one packet from server(s) */
		process_server_answer();
	}

	/* Destroy polugins */
	for (i = 0; i < plugins_number; i++) {
		if (plugins[i]->destroy)
			(plugins[i]->destroy) ();
	}
	pthread_mutex_destroy(&queue_lock);
}
Esempio n. 7
0
/**
 * Send mail messages via SMTP
 * @param mail A Mail object
 * @return FALSE if failed, TRUE if succeeded
 */
int sendmail(Mail_T mail) {

  int i;
  int rv;
  Mail_T m;
  SendMail_T S;
  char *b64 = NULL;
  char now[STRLEN];
  
  ASSERT(mail);
  
  S.socket = NULL;
  if(sigsetjmp(S.error, TRUE)) {
    rv = FALSE;
    goto exit;
  } else {
    rv = TRUE;
  }
  
  open_server(&S);
  
  Time_gmtstring(Time_now(), now);
  
  snprintf(S.localhost, sizeof(S.localhost), "%s", Run.mail_hostname ? Run.mail_hostname : Run.localhostname);
  
  do_status(&S);

  /* Use EHLO if TLS or Authentication is requested */
  if((S.ssl.use_ssl && S.ssl.version == SSL_VERSION_TLS) || S.username) {
    do_send(&S, "EHLO %s\r\n", S.localhost);
  } else {
    do_send(&S, "HELO %s\r\n", S.localhost);
  }
  do_status(&S);

  /* Switch to TLS now if configured */
  if(S.ssl.use_ssl && S.ssl.version == SSL_VERSION_TLS) {
    do_send(&S, "STARTTLS\r\n"); 
    do_status(&S);
    if(!socket_switch2ssl(S.socket, S.ssl)) {
      rv = FALSE;
      goto exit;
    }
    /* After starttls, send ehlo again: RFC 3207: 4.2 Result of the STARTTLS Command */
    do_send(&S, "EHLO %s\r\n", S.localhost);
    do_status(&S);
  }

  /* Authenticate if possible */
  if(S.username) {
    unsigned char buffer[STRLEN];
    int len;

    len = snprintf((char *)buffer, STRLEN, "%c%s%c%s", '\0', S.username, '\0', S.password?S.password:"");
    b64 = encode_base64(len, buffer);
    do_send(&S, "AUTH PLAIN %s\r\n", b64); 
    do_status(&S);
  }
  
  for(i = 0, m= mail; m; m= m->next, i++) { 
    do_send(&S, "MAIL FROM: <%s>\r\n", m->from);
    do_status(&S);
    do_send(&S, "RCPT TO: <%s>\r\n", m->to);
    do_status(&S);
    do_send(&S, "DATA\r\n");
    do_status(&S);
    do_send(&S, "From: %s\r\n", m->from);
    if (m->replyto)
      do_send(&S, "Reply-To: %s\r\n", m->replyto);
    do_send(&S, "To: %s\r\n", m->to);
    do_send(&S, "Subject: %s\r\n", m->subject);
    do_send(&S, "Date: %s\r\n", now);
    do_send(&S, "X-Mailer: %s %s\r\n", prog, VERSION);
    do_send(&S, "Mime-Version: 1.0\r\n");
    do_send(&S, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
    do_send(&S, "Content-Transfer-Encoding: 8bit\r\n");
    do_send(&S, "Message-id: <%ld.%lu@%s>\r\n", time(NULL), random(), S.localhost);
    do_send(&S, "\r\n");
    do_send(&S, "%s\r\n", m->message);
    do_send(&S, ".\r\n");
    do_status(&S);
  }
  do_send(&S, "QUIT\r\n");
  do_status(&S);
  
exit:
  if(S.socket)
    socket_free(&S.socket);
  
  FREE(b64);

  return rv;
}
Esempio n. 8
0
static void
make_server_connections(void)
{
    int i, wait_time;

    /*
     * Try to open the menuserver socket, if I can not, then set a flag
     */

    if (open_server(MenuServerName) == -2) {
        fprintf(stderr, "(HyperDoc) Warning: Not connected to FriCAS Server!\n");
        MenuServerOpened = 0;
    } else {
        atexit(&clean_socket);
        MenuServerOpened = 1;
    }


    /*
     * If I have opened the MenuServer socket, then I should also try to open
     * the SpadServer socket, so I can send stuff right to SPAD.
     */

    if (MenuServerOpened) {

        /*
         * If I am a ht server, then I should not continue on unless I
         * establish some sort of connection
         */

        /*
         * Modified on 11/20 so that it prints an error message every ten for
         * ten tries at opeing the socket. If it fails all ten times, it
         * gives up and exits.
         */

        if (!is_fricas_server)
            wait_time = 2;
        else
            wait_time = 1000;

        for (i = 0, spad_socket = NULL; i < 2 && spad_socket == NULL; i++) {
            spad_socket = connect_to_local_server(SpadServer,
                                                  MenuServer, wait_time);
            if (is_fricas_server && spad_socket == NULL)
                fprintf(stderr, "(HyperDoc) Error opening FriCAS server. Retrying ...\n");
            else
                i = 11;
        }
        if (! spad_socket) {
            fprintf(stderr, "(HyperDoc) Couldn't connect to FriCAS server!\n");
            if (!is_fricas_server)
                MenuServerOpened = 0;
            else {
                fprintf(stderr, "(HyperDoc) Couldn't connect to FriCAS server!\n");
                exit(-1);
            }
        }
        else {

            /*
             * Do the same thing for the SessionServer
             */

            for (i = 0, session_server = NULL; i < 2 && session_server == NULL
                 ; i++) {
                session_server =
                    connect_to_local_server(SessionServer, MenuServer,
                                            wait_time);
                if (is_fricas_server && session_server == NULL) {
                    fprintf(stderr,
                            "(HyperDoc) Error opening SessionServer, Retrying ...\n");
                }
                else
                    i = 11;
            }
            if (session_server == NULL) {
                fprintf(stderr, "(HyperDoc) Connection attempt to session manager timed out.\n");
                if (is_fricas_server) {
                    fprintf(stderr,
                            "(HyperDoc) Server unable to connect to session server\n");
                    exit(-1);
                }
                else {
                    MenuServerOpened = 0;
                }
            }
        }
    }
}