Exemplo n.º 1
0
static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
{
	struct sockaddr_in sa_in;
	char *dup_address = strdup(wp->config->listen_address);
	char *port_str = strchr(dup_address, ':');
	char *addr = NULL;
	int port = 0;

	if (port_str) { /* this is host:port pair */
		*port_str++ = '\0';
		port = atoi(port_str);
		addr = dup_address;
	}
	else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
		port = atoi(dup_address);
		port_str = dup_address;
	}

	if (port == 0) {
		zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
		return -1;
	}

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

	if (addr) {

		sa_in.sin_addr.s_addr = inet_addr(addr);

		if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
			if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
				return -1;
			}
			zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
		}
	}
	else {

		sa_in.sin_addr.s_addr = htonl(INADDR_ANY);

	}

	sa_in.sin_family = AF_INET;
	sa_in.sin_port = htons(port);

	free(dup_address);

	return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
}
Exemplo n.º 2
0
static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op) /* {{{ */
{
	if (key == NULL) {
		switch (type) {
			case FPM_AF_INET : {
				struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
				key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
				sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
				break;
			}

			case FPM_AF_UNIX : {
				struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
				key = alloca(strlen(sa_un->sun_path) + 1);
				strcpy(key, sa_un->sun_path);
				break;
			}

			default :
				return -1;
		}
	}

	switch (op) {

		case FPM_GET_USE_SOCKET :
		{
			unsigned i;
			struct listening_socket_s *ls = sockets_list.data;

			for (i = 0; i < sockets_list.used; i++, ls++) {
				if (!strcmp(ls->key, key)) {
					++ls->refcount;
					return ls->sock;
				}
			}
			break;
		}

		case FPM_STORE_SOCKET :			/* inherited socket */
		case FPM_STORE_USE_SOCKET :		/* just created */
		{
			struct listening_socket_s *ls;

			ls = fpm_array_push(&sockets_list);
			if (!ls) {
				break;
			}

			if (op == FPM_STORE_SOCKET) {
				ls->refcount = 0;
			} else {
				ls->refcount = 1;
			}
			ls->type = type;
			ls->sock = sock;
			ls->key = strdup(key);

			return 0;
		}
	}
	return -1;
}