コード例 #1
0
ファイル: usock.c プロジェクト: BackupGGCode/wl500g
static int usock_inet(int type, const char *host, const char *service, int socktype, bool server)
{
	struct addrinfo *result, *rp;
	struct addrinfo hints = {
		.ai_family = (type & USOCK_IPV6ONLY) ? AF_INET6 :
			(type & USOCK_IPV4ONLY) ? AF_INET : AF_UNSPEC,
		.ai_socktype = socktype,
		.ai_flags = AI_ADDRCONFIG
			| ((type & USOCK_SERVER) ? AI_PASSIVE : 0)
			| ((type & USOCK_NUMERIC) ? AI_NUMERICHOST : 0),
	};
	int sock = -1;

	if (getaddrinfo(host, service, &hints, &result))
		return -1;

	for (rp = result; rp != NULL; rp = rp->ai_next) {
		sock = usock_connect(rp->ai_addr, rp->ai_addrlen, rp->ai_family, socktype, server);
		if (sock >= 0)
			break;
	}

	freeaddrinfo(result);
	return sock;
}

const char *usock_port(int port)
{
	static char buffer[sizeof("65535\0")];

	if (port < 0 || port > 65535)
		return NULL;

	snprintf(buffer, sizeof(buffer), "%u", port);

	return buffer;
}

int usock(int type, const char *host, const char *service) {
	int socktype = ((type & 0xff) == USOCK_TCP) ? SOCK_STREAM : SOCK_DGRAM;
	bool server = !!(type & USOCK_SERVER);
	int sock;

	if (type & USOCK_UNIX)
		sock = usock_unix(host, socktype, server);
	else
		sock = usock_inet(type, host, service, socktype, server);

	if (sock < 0)
		return -1;

	usock_set_flags(sock, type);
	return sock;
}
コード例 #2
0
ファイル: usock.c プロジェクト: hh123okbb/SaksPi
static int usock_unix(const char *host, int socktype, bool server)
{
    struct sockaddr_un sun = {.sun_family = AF_UNIX};

    if (strlen(host) >= sizeof(sun.sun_path)) {
        errno = EINVAL;
        return -1;
    }
    strcpy(sun.sun_path, host);

    return usock_connect((struct sockaddr*)&sun, sizeof(sun), AF_UNIX, socktype, server);
}