Beispiel #1
0
int main(int argc, const char *argv[])
{
    int peerfd = socket(PF_INET, SOCK_STREAM, 0);
    if(peerfd == -1)
        ERR_EXIT("socket");

    struct sockaddr_in peeraddr;
    peeraddr.sin_family = AF_INET;
    peeraddr.sin_port = htons(8080);
    peeraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if((connect(peerfd, (struct sockaddr*)&peeraddr, sizeof peeraddr)) < 0)
        ERR_EXIT("connect");

    do_service(peerfd);

    close(peerfd);
    return 0;
}
Beispiel #2
0
int main(int argc, const char *argv[])
{
    int peerfd = socket(PF_INET, SOCK_STREAM, 0);
    if(peerfd == -1)
        ERR_EXIT("socket");

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof addr);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("192.168.64.129"); //localhost
    addr.sin_port = htons(8899);
    socklen_t len = sizeof addr;
    if(connect(peerfd, (struct sockaddr*)&addr, len) == -1)
        ERR_EXIT("Connect");

    do_service(peerfd);


    return 0;
}
Beispiel #3
0
int main(int argc, const char *argv[])
{
    //创建socket
    int listenfd = socket(PF_INET, SOCK_STREAM, 0);
    if(listenfd == -1)
        ERR_EXIT("socket");


    //设置端口复用
    int on = 1;
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
        ERR_EXIT("setsockopt");

    struct sockaddr_in servaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8989);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    //bind端口
    if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof servaddr) < 0)
        ERR_EXIT("bind"); 

    //listen端口
    if(listen(listenfd, SOMAXCONN) < 0)
        ERR_EXIT("listen");


    int peerfd;
    //accept请求
    if((peerfd = accept(listenfd, NULL, NULL)) < 0)
        ERR_EXIT("accept");

    do_service(peerfd);


    close(peerfd);
    close(listenfd);

    return 0;
}
Beispiel #4
0
int main(int argc, const char *argv[])
{
    int peerfd = socket(PF_INET, SOCK_STREAM, 0);
    if(peerfd == -1)
        ERR_EXIT("socket");

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof addr);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("192.168.175.133"); //localhost
    addr.sin_port = htons(9999);
    socklen_t len = sizeof addr;
    if(connect(peerfd, (struct sockaddr*)&addr, len) == -1)
        ERR_EXIT("Connect");

	char buf[1024] = { 0 };
	int ret = read(peerfd, buf, sizeof(buf));
	printf("%s\n", buf);

    do_service(peerfd);


    return 0;
}
Beispiel #5
0
static void do_class(config_t *cpe, config_t *config, struct rs_config *rs_config)
{
	config_t *cp, *cp1;

	if (class_recurs > MAX_CLASS_RECURS)
	{
		fatal(
		"do_class: nesting level too high for class '%s' at %s:%d",
			cpe->word, cpe->file, cpe->line);
	}
	class_recurs++;

	/* Process classes */
	for (; cpe; cpe= cpe->next)
	{
		if (cpe->flags & CFG_SUBLIST)
		{
			fatal("do_class: unexpected sublist at %s:%d",
				cpe->file, cpe->line);
		}
		if (cpe->flags & CFG_STRING)
		{
			fatal("do_uid: unexpected string at %s:%d",
				cpe->file, cpe->line);
		}

		/* Find entry for the class */
		for (cp= config; cp; cp= cp->next)
		{
			if (!(cp->flags & CFG_SUBLIST))
			{
				fatal("do_class: expected list at %s:%d",
					cp->file, cp->line);
			}
			cp1= cp->list;
			if ((cp1->flags & CFG_STRING) ||
				(cp1->flags & CFG_SUBLIST))
			{
				fatal("do_class: expected word at %s:%d",
					cp1->file, cp1->line);
			}

			/* At this place we expect the word KW_SERVICE */
			if (strcmp(cp1->word, KW_SERVICE) != 0)
				fatal("do_class: exected word '%S' at %s:%d",
					KW_SERVICE, cp1->file, cp1->line);

			cp1= cp1->next;
			if ((cp1->flags & CFG_STRING) ||
				(cp1->flags & CFG_SUBLIST))
			{
				fatal("do_class: expected word at %s:%d",
					cp1->file, cp1->line);
			}

			/* At this place we expect the name of the service */
			if (strcmp(cp1->word, cpe->word) == 0)
				break;
		}
		if (cp == NULL)
		{
			fatal(
			"do_class: no entry found for class '%s' at %s:%d",
				cpe->word, cpe->file, cpe->line);
		}
		do_service(cp1->next, config, rs_config);
	}

	class_recurs--;
}
Beispiel #6
0
static const char *do_config(const char *label, char *filename, struct rs_config *rs_config)
{
	config_t *config, *cp, *cpe;
	struct passwd *pw;
	struct rs_start *rs_start = &rs_config->rs_start;

	if(!(config= config_read(filename, 0, NULL)))
		return NULL; /* config file read failed. */

	/* Set clean rs_start defaults. */
	memset(rs_config, 0, sizeof(*rs_config));
	if(!(pw= getpwnam(SERVICE_LOGIN)))
		fatal("no passwd file entry for '%s'", SERVICE_LOGIN);
	rs_start->rss_uid= pw->pw_uid;
	rs_start->rss_sigmgr= DSRV_SM;
	rs_start->rss_scheduler= DSRV_SCH;
	rs_start->rss_priority= DSRV_Q;
	rs_start->rss_quantum= DSRV_QT;
	rs_start->rss_cpu = DSRV_CPU;
	rs_start->rss_flags = RSS_VM_BASIC_CALLS | RSS_SYS_BASIC_CALLS;

	/* Find an entry for our service */
	for (cp= config; cp; cp= cp->next)
	{
		if (!(cp->flags & CFG_SUBLIST))
		{
			fatal("do_config: expected list at %s:%d",
				cp->file, cp->line);
		}
		cpe= cp->list;
		if ((cpe->flags & CFG_STRING) || (cpe->flags & CFG_SUBLIST))
		{
			fatal("do_config: expected word at %s:%d",
				cpe->file, cpe->line);
		}

		/* At this place we expect the word KW_SERVICE */
		if (strcmp(cpe->word, KW_SERVICE) != 0)
			fatal("do_config: exected word '%S' at %s:%d",
				KW_SERVICE, cpe->file, cpe->line);

		cpe= cpe->next;
		if ((cpe->flags & CFG_STRING) || (cpe->flags & CFG_SUBLIST))
		{
			fatal("do_config: expected word at %s:%d",
				cpe->file, cpe->line);
		}

		/* At this place we expect the name of the service. */
		if (!label || strcmp(cpe->word, label) == 0) {
			label = cpe->word;
			break;
		}
	}
	if (cp == NULL)
	{
		fprintf(stderr, "service: service '%s' not found in '%s'\n",
			label, filename);
		exit(1);
	}

	cpe= cpe->next;

	do_service(cpe, config, rs_config);

	{
		char *default_ipc = RSS_IPC_ALL_SYS;
		if(!rs_start->rss_ipc) {
		      rs_start->rss_ipc= default_ipc;
		      rs_start->rss_ipclen= strlen(default_ipc);
		}
	}

	/* config file read ok. */
	return label;
}
Beispiel #7
0
int main(int argc, char* argv[])
{
	int sockfd;
	struct sockaddr_in servaddr;
	if ( (sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
	{
		perror("socket fail!");
		exit(EXIT_FAILURE);
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(8888);
	if(argv[1] == NULL)
	{
		argv[1] = "0.0.0.0";
	}
	if (inet_aton(argv[1], &servaddr.sin_addr) == 0)
	{
		perror("inet_aton failure!");
		printf("address:%u\n", servaddr.sin_addr.s_addr);
		exit(EXIT_FAILURE);
	}

	int on = 1;
	if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
	{
		perror("setsockopt:");
		exit(EXIT_FAILURE);
	}

	if (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
	{
		perror("bind error!");
		exit(EXIT_FAILURE);
	}

	listen(sockfd, SOMAXCONN);
    if (signal(SIGCHLD, sig_chld) == SIG_ERR)  // 忽略 在linux下起作用,但不可移植
        perror("signal:");


	struct sockaddr_in cliaddr;
	int connfd;
	pid_t pid;

	while(1)
	{
		socklen_t clilen = sizeof(cliaddr);
		memset(&cliaddr, 0, sizeof(cliaddr));
		if ((connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &clilen)) < 0)
		{
            // 未必能保证所有的系统在处理信号方面都支持 SA_RESTART
            // 在信号支持SA_RESTART的情况下,这个判断没必要
            if (errno == EINTR)
            {
                fprintf(stderr, "errno == EINTR");
                continue;
            }
            else
            {
                perror("accept error");
                exit(EXIT_FAILURE);
            }
        }
        fprintf(stderr, "the client addr:port,%s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);

        pid = fork();
        if (pid == 0)  //子进程
        {
            close(sockfd);
            do_service(connfd);
            exit(0);
        }
        else if (pid < 0)
        {
            perror("fork error");
            exit(EXIT_FAILURE);
        }
        close(connfd);

    }
    fputs("will close connfd\n", stderr);
    close(connfd);
    return 0;
}
int main(int argc, char *argv[])
{
	int i, rc;

	if (argc < 3) {
		usage();
		rc = 1;
		goto out;
	}

	rc = setup_signal_handling();
	if (rc != 0) {
		fprintf(stderr, "FAIL: Couldn't set up signal handler\n");
		rc = 1;
		goto out;
	}

	for (i = 1; i < argc && interface == NULL; i++) {
		char *arg = argv[i];

		if (strcmp(arg, "--system") == 0) {
			type = DBUS_BUS_SYSTEM;
			session_or_system = TRUE;
		} else if (strcmp(arg, "--session") == 0) {
			type = DBUS_BUS_SESSION;
			session_or_system = TRUE;
		} else if (strstr(arg, "--address") == arg) {
			address = strchr(arg, '=');

			if (address == NULL) {
				fprintf(stderr,
					"FAIL: \"--address=\" requires an ADDRESS\n");
				usage();
				rc = 1;
				goto out;
			} else {
				address = address + 1;
			}
		} else if (strstr(arg, "--name=") == arg)
			name = strchr(arg, '=') + 1;
		else if (strstr(arg, "--log=") == arg) {
			char *path = strchr(arg, '=') + 1;

			log_fd = open(path, O_CREAT | O_TRUNC | O_WRONLY,
				      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
				      S_IROTH | S_IWOTH);
			if (log_fd < 0) {
				fprintf(stderr,
					"FAIL: Couldn't open log file \"%s\"\n",
					path);
				exit(1);
			}
		} else if (strstr(arg, "--lock-fd=") == arg) {
			char *fd = strchr(arg, '=') + 1;

			lock_fd = atoi(fd);
		} else if (!strcmp(arg, "--help")) {
			usage();
			rc = 0;
			goto out;
		} else if (arg[0] == '-') {
			usage();
			rc = 1;
			goto out;
		} else if (path == NULL)
			path = arg;
		else	/* interface == NULL guaranteed by the 'while' loop */
			interface = arg;
	}

	if (name == NULL || path == NULL || interface == NULL || i < argc) {
		usage();
		rc = 1;
		goto out;
	}

	if (session_or_system && (address != NULL)) {
		fprintf(stderr,
			"FAIL: \"--address\" may not be used with \"--system\" or \"--session\"\n");
		usage();
		rc = 1;
		goto out;
	}

	dbus_error_init(&error);

	if (address != NULL)
		connection = dbus_connection_open(address, &error);
	else
		connection = dbus_bus_get(type, &error);

	if (connection == NULL) {
		fprintf(stderr,
			"FAIL: Failed to open connection to \"%s\" message bus: %s\n",
			address ? address :
				  ((type == DBUS_BUS_SYSTEM) ? "system" : "session"),
			error.message);
		dbus_error_free(&error);
		rc = 1;
		goto out;
	} else if (address != NULL)
		dbus_bus_register(connection, &error);

	rc = do_service();

out:
	if (connection)
		dbus_connection_unref(connection);

	unlock_fd();

	if (rc == 0)
		printf("PASS\n");

	exit(rc);
}
Beispiel #9
0
/** Main routine for petal */
int main(int argc, char* argv[])
{
	int c;
	int port = 443;
	char* addr = "127.0.0.1", *key = "petal.key", *cert = "petal.pem";
#ifdef USE_WINSOCK
	WSADATA wsa_data;
	if((c=WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
	{	printf("WSAStartup failed\n"); exit(1); }
	atexit((void (*)(void))WSACleanup);
#endif

	/* parse the options */
	while( (c=getopt(argc, argv, "a:c:k:hp:v")) != -1) {
		switch(c) {
		case 'a':
			addr = optarg;
			break;
		case 'c':
			cert = optarg;
			break;
		case 'k':
			key = optarg;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'v':
			verb++;
			break;
		case '?':
		case 'h':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if(argc != 0)
		usage();

#ifdef SIGPIPE
	(void)signal(SIGPIPE, SIG_IGN);
#endif
#ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
	ERR_load_crypto_strings();
#endif
	ERR_load_SSL_strings();
#if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
	OpenSSL_add_all_algorithms();
#else
	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
		| OPENSSL_INIT_ADD_ALL_DIGESTS
		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
	(void)SSL_library_init();
#else
	(void)OPENSSL_init_ssl(0, NULL);
#endif

	do_service(addr, port, key, cert);

#ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
	CRYPTO_cleanup_all_ex_data();
#endif
#ifdef HAVE_ERR_FREE_STRINGS
	ERR_free_strings();
#endif
	return 0;
}
Beispiel #10
0
void invoke_new_session(struct xnbd_info *xnbd, int csockfd)
{
	struct xnbd_session *ses = g_malloc0(sizeof(struct xnbd_session));
	ses->clientfd = csockfd;
	ses->xnbd = xnbd;

	/* used for sending msg to the session process */
	get_event_connecter(&ses->event_notifier_fd, &ses->event_listener_fd);
	xnbd_session_initialize_connections(xnbd, ses);

	info("negotiations done");

	pid_t pid = fork(); 
	if (pid == -1)
		err("fork failed");

	if (pid == 0) {
		/* child */
		close(ses->event_notifier_fd);

		for (GList *list = g_list_first(xnbd->sessions); list != NULL; list = g_list_next(list)) {
			struct xnbd_session *s = (struct xnbd_session *) list->data;
			info("cleanup pid %d", s->pid);
			if (xnbd->proxymode)
				close(s->remotefd);
			info(" clientfd %d", s->clientfd);
			close(s->clientfd);

			/* this must be commented out, closed alreay in the parent */
			//close(s->event_listener_fd);
			//info(" s->event_listener_fd %d", s->event_listener_fd);
			close(s->event_notifier_fd);
			info(" s->event_notifier_fd %d", s->event_notifier_fd);
		}


		for (nfds_t j = 0; j < nlistened; j++) 
			close(eventfds[j].fd);



		dbg("new connection");
		info("do service %d", getpid());
		//{
		//	pid_t mypid = getpid();
		//	char buf[100];
		//	sprintf(buf, "lsof -p %d", mypid);
		//	system(buf);
		//	sleep(10);
		//}

		do_service(ses);

		close(csockfd);

		info("process %d exit", getpid());
		exit(EXIT_SUCCESS);
	}


	close(ses->event_listener_fd);

	ses->pid = pid;
	xnbd->sessions = g_list_append(xnbd->sessions, ses);

	/* parent */
	//close(csockfd);
}
Beispiel #11
0
int main(int argc, const char *argv[])
{
    if(argc<=2)
    {
        printf("Usage:%s ip_addr, ip_port\n", argv[0]);
        return -1;
    }

    signal(SIGCHLD, handle);

    const char *ip = argv[1];
    int port = atoi(argv[2]);

    int sockfd;
    if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
    {
        ERR_EXIT("socket");
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    inet_pton(AF_INET, ip, &addr.sin_addr);

    int on=1;
    if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))<0)
    {
        ERR_EXIT("setsockopt");
    }

    if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr))<0)
    {
        ERR_EXIT("bind");
    }

    if(listen(sockfd, SOMAXCONN)<0)
    {
        ERR_EXIT("listen");
    }

    struct sockaddr_in peeraddr;
    socklen_t peerlen = sizeof(peeraddr);
    pid_t pid;
    int conn;
    while(1)
    {
        if((conn = accept(sockfd, (struct sockaddr *)&peeraddr, &peerlen))<0)
        {
            ERR_EXIT("accept");
        }

        printf("ip:%s, port=%d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));

        pid = fork();
        if(pid==-1)
        {
            ERR_EXIT("fork");
        }
        if(pid == 0)
        {
            close(sockfd);
            do_service(conn);
            exit(EXIT_SUCCESS);
        }
        else
        {
            close(conn);
        }
    }
    return 0;
}