Exemplo n.º 1
0
int main(int argc, char *argv[]) {
	pthread_t t_cleanup; // thread for cleaning up dead sessions.
	pthread_t t_healthagent; // thread for health agent.
	pthread_t t_cli; // thread for cli.
	pthread_t t_counters; // thread for performance counters.
	pthread_t t_memorymanager; // thread for the memory manager.
	struct ifaddrs *ifaddr;//, *ifa;
	__u32 tempIP;
//	int s;
	int i;
	char message[LOGSZ];
	char strIP[20];
//	char host[NI_MAXHOST];
	char path[100] = "/etc/opennop/opennop.conf";
	int tmp;
	__u32 packet_size = PACKET_SIZE, packet_number = PACKET_NUMBER, thr_num = DEF_THR_NUM;
        __u32 fpPerPkt = DEF_FP_PER_PKT, fpsFactor = DEF_FPS_FACTOR;

#if defined(DEBUG)

	int daemonize = false;
#else

	int daemonize = true;
#endif

	/* Setup signal handling */
	signal(SIGHUP, signal_handler);
	signal(SIGTERM, signal_handler);
	signal(SIGINT, signal_handler);
	signal(SIGQUIT, signal_handler);
	signal(SIGPIPE,SIG_IGN);

	int c;
	while ((c = getopt(argc, argv, "nh|help")) != -1) {
		switch (c) {
		case 'h':
			PrintUsage(argc, argv);
			exit(0);
			break;
		case 'n':
			daemonize = 0;
			isdaemon = false;
			break;
		default:
			PrintUsage(argc, argv);
			break;
		}
	}


	sprintf(message, "Initialization: %s daemon starting up.\n", DAEMON_NAME);
	logger(LOG_INFO, message);

	/*
	 * Configuration: read the configuration file
	 */
	sprintf(message, "Configuring: reading file %s.\n", path);
	logger(LOG_INFO, message);

	tmp = configure(path, &localID, &packet_number, &packet_size, &thr_num, &fpPerPkt, &fpsFactor);

	if(tmp == 1){
		sprintf(message, "Configuring error. EXIT\n");
		logger(LOG_INFO, message);
		exit(EXIT_FAILURE);
	}

	sprintf(message, "Initialization: Version %s.\n", VERSION);
	logger(LOG_INFO, message);

	/*
	 * Get the numerically highest local IP address.
	 * This will be used as the acceleratorID.
	 */
	if (getifaddrs(&ifaddr) == -1) {
		sprintf(message, "Initialization: Error opening interfaces.\n");
		logger(LOG_INFO, message);
		exit(EXIT_FAILURE);
	}

//	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { // loop through all interfaces.
//
//		if (ifa->ifa_addr != NULL) {
//
//			if (ifa->ifa_addr->sa_family == AF_INET) { // get all IPv4 addresses.
//				s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
//						host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
//
//				if (s != 0) {
//					exit(EXIT_FAILURE);
//				}
//
//				inet_pton(AF_INET, (char *) &host, &tempIP); // convert string to decimal.
//
//				/*
//				 * Lets fine the largest local IP, and use that as accelleratorID
//				 * Lets also exclude 127.0.0.1 as a valid ID.
//				 */
//				if ((tempIP > localID) && (tempIP != LOOPBACKIP)) {
//					localID = tempIP;
//				}
//			} // end get all IPv4 addresses.
//		} // end ifa->ifa_addr NULL test.
//	} // end loop through all interfaces.

	if (localID == 0) { // fail if no usable IP found.
		inet_ntop(AF_INET, &tempIP, strIP, INET_ADDRSTRLEN);
		sprintf(message, "Initialization: No usable IP Address. %s\n", strIP);
		logger(LOG_INFO, message);
		exit(EXIT_FAILURE);
	}

#if defined(DEBUG)
	setlogmask(LOG_UPTO(LOG_DEBUG));
	openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else

	setlogmask(LOG_UPTO(LOG_INFO));
	openlog(DAEMON_NAME, LOG_CONS, LOG_USER);
#endif

	/* Our process ID and Session ID */
	pid_t pid, sid;
	if (daemonize) {
		sprintf(message, "Initialization: Daemonizing the %s process.\n",
				DAEMON_NAME);
		logger(LOG_INFO, message);

		/* Fork off the parent process */
		pid = fork();
		if (pid < 0) {
			exit(EXIT_FAILURE);
		}

		/* If we got a good PID, then
		 we can exit the parent process. */
		if (pid > 0) {
			exit(EXIT_SUCCESS);
		}

		/* Change the file mode mask */
		umask(0);

		/* Create a new SID for the child process */
		sid = setsid();
		if (sid < 0) {
			/* Log the failure */
			exit(EXIT_FAILURE);
		}

		/* Change the current working directory */
		if ((chdir("/")) < 0) {
			/* Log the failure */
			exit(EXIT_FAILURE);
		}

		/* Close out the standard file descriptors */
		close(STDIN_FILENO);
		close(STDOUT_FILENO);
		close(STDERR_FILENO);
	}

	/*
	 * Starting up the daemon.
	 */

	initialize_sessiontable();

	if (get_workers() == 0) {
		set_workers(thr_num);
		//set_workers(sysconf(_SC_NPROCESSORS_ONLN) * 2);
	}

#ifdef ROLLING
	init_common(packet_number,packet_size, fpPerPkt, fpsFactor, shareddict);
	init_debugd() ;
#endif

 	for (i = 0; i < get_workers(); i++) {
		create_worker(i);
	}

#ifdef BASIC
	create_hashmap(&ht); // Create hash table
#endif

	// Only one type of optimization can be active
//	assert(deduplication != compression);

	/*
	 * Create the fetcher thread that retrieves
	 * IP packets from the Netfilter Queue.
	 */
	create_fetcher();
	pthread_create(&t_cleanup, NULL, cleanup_function, (void *) NULL);
	pthread_create(&t_healthagent, NULL, healthagent_function, (void *) NULL);
	pthread_create(&t_cli, NULL, cli_manager_init, (void *) NULL);
	pthread_create(&t_counters, NULL, counters_function, (void *) NULL);
	pthread_create(&t_memorymanager, NULL, memorymanager_function, (void *) NULL);

	sprintf(message, "[OpenNOP]: Started all threads.\n");
	logger(LOG_INFO, message);

	/*
	 * All the threads have been created now commands should be registered.
	 */
	register_command("show traces mask", cli_show_traces_mask, false, false);
	register_command("traces mask orr", cli_traces_mask_orr, true, false);
	register_command("traces mask and", cli_traces_mask_and, true, false);
	register_command("traces mask nand", cli_traces_mask_nand, true, false);
	register_command("reset stats in_dedup", cli_reset_stats_in_dedup, false, false);
	register_command("reset stats in_dedup_thread", cli_reset_stats_in_dedup, false, false);
	register_command("show stats in_dedup", cli_show_stats_in_dedup, false, false);
	register_command("show stats in_dedup_thread", cli_show_stats_in_dedup_thread, false, false);
	register_command("reset stats out_dedup", cli_reset_stats_out_dedup, false, false);
	register_command("reset stats out_dedup_thread", cli_reset_stats_out_dedup, false, false);
	register_command("show stats out_dedup", cli_show_stats_out_dedup, false, false);
	register_command("show stats out_dedup_thread", cli_show_stats_out_dedup_thread, false, false);

	register_command("traces enable", cli_traces_enable, true, false);

	register_command("traces disable", cli_traces_disable, true, false);

	register_command("show version", cli_show_version, false, false);
	register_command("show compression", cli_show_compression, false, false);
	register_command("show workers", cli_show_workers, false, false);
	register_command("show fetcher", cli_show_fetcher, false, false);
	register_command("show sessions", cli_show_sessionss, false, false);
	register_command("show deduplication", cli_show_deduplication, false, false);
	register_command("compression enable", cli_compression_enable, false, false);
	register_command("compression disable", cli_compression_disable, false, false);
	register_command("deduplication enable", cli_deduplication_enable, false, false);
	register_command("deduplication disable", cli_deduplication_disable, false, false);

	/*
	 * Rejoin all threads before we exit!
	 */
	rejoin_fetcher();
	sprintf(message, "[OpenNOP] Fetcher joined\n");
	logger(LOG_INFO, message);
	pthread_join(t_cleanup, NULL);
	sprintf(message, "joined cleanup\n");
	logger(LOG_INFO, message);
	pthread_join(t_healthagent, NULL);
	sprintf(message, "joined health\n");
	logger(LOG_INFO, message);
	pthread_join(t_cli, NULL);
	sprintf(message, "joined cli\n");
	logger(LOG_INFO, message);
	pthread_join(t_counters, NULL);
	sprintf(message, "joined counters\n");
	logger(LOG_INFO, message);
	pthread_join(t_memorymanager, NULL);

	for (i = 0; i < get_workers(); i++) {
		rejoin_worker(i);
	}
#ifdef BASIC
	remove_hashmap(ht);
#endif
	clear_sessiontable();

	sprintf(message, "Exiting: %s daemon exiting", DAEMON_NAME);
	logger(LOG_INFO, message);

	exit(EXIT_SUCCESS);
}
Exemplo n.º 2
0
int
main(int argc, char **argv)
{
    struct server *s = NULL;
    pthread_t pt, ctl;
    int c, is_forward = 0;
    const char *config = SR_CONFIG_FILE;
    int daemon = 0;
    while ((c = getopt(argc,argv,"c:vhfd")) != -1)
    {
        switch(c)
        {
            case 'c':
                config = optarg;
                break;
            case 'h':
                help(argv[0]);
                exit(0);
                break;
            case 'f':
                is_forward = 1;
                break;
            case 'd':
                daemon = 1;
                break;
            case '?':
                printf("Try -h please\n");
                exit(0);
                break;
            case 'v':
                printf("dnspod-sr 0.01\n");
                exit(0);
                break;
            default:
                exit(0);
                break;
        }
    }
    sanity_test(0);
    drop_privilege("./");
    daemonrize(daemon);
    trig_signals(1);
    global_now = time(NULL);    //for read root.z
    g_nameservers[0] = g_nameservers[1] = NULL;
    init_globe();
    init_mempool();
    s = server_init();
    s->is_forward = is_forward;
    read_config(config, (char *)s->logpath, s->forward, g_nameservers);
    // add default dns server 8.8.8.8, 114.114.114.114
    if (g_nameservers[0] == NULL) {
        assert(g_nameservers[1] == NULL);
        g_nameservers[0] = strdup("119.29.29.29");
        g_nameservers[1] = strdup("8.8.4.4");
    }
    if (g_nameservers[1] == NULL) {
        if (strcmp(g_nameservers[0], "119.29.29.29") == 0) {
            g_nameservers[1] = strdup("8.8.4.4");
        } else {
            g_nameservers[1] = strdup("119.29.29.29");
        }
    }
    //
    if (create_fetcher(s, s->nfetcher) < 0)
        dns_error(0, "create worker");
    if (create_author(s, s->nquizzer) < 0)
        dns_error(0, "create author");
    if (pthread_create(&pt, NULL, (void *) time_cron, s) != 0)
        dns_error(0, "time cron error");
    if (pthread_create(&ctl, NULL, (void *)recv_update, s) != 0) {
        dns_error(0, "recv update thread error");
    }
    read_root(s->datasets, s->ttlexp);
    print_basic_debug();
    global_serv = s;
    run_sentinel(s);
    return 0;
}