예제 #1
0
PANCAKE_API Byte *PancakeNetworkGetInterfaceName(struct sockaddr *addr) {
	Byte *name;

	PancakeAssert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6 || addr->sa_family == AF_UNIX);

	switch(addr->sa_family) {
		case AF_INET: {
			Byte textAddress[INET_ADDRSTRLEN + 1];

			inet_ntop(AF_INET, &((struct sockaddr_in*) addr)->sin_addr, textAddress, INET_ADDRSTRLEN);

			// IPv4:<address>:<port>
			name = PancakeAllocate(sizeof("IPv4::12345") + INET_ADDRSTRLEN);
			sprintf(name, "IPv4:%s:%i", textAddress, (Int32) ntohs(((struct sockaddr_in*) addr)->sin_port));
		} break;
		case AF_INET6: {
			Byte textAddress[INET6_ADDRSTRLEN + 1];

			inet_ntop(AF_INET6, &((struct sockaddr_in6*) addr)->sin6_addr, textAddress, INET6_ADDRSTRLEN);

			// IPv6:[<address>]:<port>
			name = PancakeAllocate(sizeof("IPv6:[]:12345") + INET6_ADDRSTRLEN);
			sprintf(name, "IPv6:[%s]:%i", textAddress, (Int32) ntohs(((struct sockaddr_in6*) addr)->sin6_port));
		} break;
		case AF_UNIX: {
			// UNIX:<address>
			name = PancakeAllocate(sizeof("UNIX:") + sizeof(((struct sockaddr_un*) addr)->sun_path));
			sprintf(name, "UNIX:%s", ((struct sockaddr_un*) addr)->sun_path);
		} break;
	}

	return name;
}
STATIC UByte PancakeHTTPRewriteGetHTTPVersion(PancakeSocket *sock, PancakeHTTPRewriteVariable *var, PancakeHTTPRewriteValue *value) {
	PancakeHTTPRequest *request = (PancakeHTTPRequest*) sock->data;

	PancakeAssert(request->HTTPVersion == PANCAKE_HTTP_10 || request->HTTPVersion == PANCAKE_HTTP_11);

	value->intv = request->HTTPVersion == PANCAKE_HTTP_10 ? 10 : 11;
	return 1;
}