c41359
#define _GNU_SOURCE
c41359
#include <sys/socket.h>
c41359
#include <dlfcn.h>
c41359
#include <errno.h>
c41359
#include <stdlib.h>
c41359
#include <string.h>
c41359
#include <netinet/in.h>
c41359
c41359
static int
c41359
port_is_okay(unsigned short port)
c41359
{
c41359
	char *p, *q;
c41359
	long l;
c41359
c41359
	p = getenv("NOPORT");
c41359
	while ((p != NULL) && (*p != '\0')) {
c41359
		l = strtol(p, &q, 10);
c41359
		if ((q == NULL) || (q == p)) {
c41359
			break;
c41359
		}
c41359
		if ((*q == '\0') || (*q == ',')) {
c41359
			if (port == l) {
c41359
				errno = ECONNREFUSED;
c41359
				return -1;
c41359
			}
c41359
		}
c41359
		p = q;
c41359
		p += strspn(p, ",");
c41359
	}
c41359
	return 0;
c41359
}
c41359
c41359
int
c41359
connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
c41359
{
c41359
	unsigned short port;
c41359
	static int (*next_connect)(int, const struct sockaddr *, socklen_t);
c41359
c41359
	if (next_connect == NULL) {
c41359
		next_connect = dlsym(RTLD_NEXT, "connect");
c41359
		if (next_connect == NULL) {
c41359
			errno = ENOSYS;
c41359
			return -1;
c41359
		}
c41359
	}
c41359
c41359
	if (getenv("NOPORT") == NULL) {
c41359
		return next_connect(sockfd, addr, addrlen);
c41359
	}
c41359
c41359
	switch (addr->sa_family) {
c41359
	case AF_INET:
c41359
		port = ntohs(((struct sockaddr_in *)addr)->sin_port);
c41359
		if (port_is_okay(port) != 0) {
c41359
			return -1;
c41359
		}
c41359
		break;
c41359
	case AF_INET6:
c41359
		port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
c41359
		if (port_is_okay(port) != 0) {
c41359
			return -1;
c41359
		}
c41359
		break;
c41359
	default:
c41359
		break;
c41359
	}
c41359
	return next_connect(sockfd, addr, addrlen);
c41359
}
c41359
c41359
ssize_t
c41359
sendto(int sockfd, const void *buf, size_t len, int flags,
c41359
       const struct sockaddr *dest_addr, socklen_t addrlen)
c41359
{
c41359
	unsigned short port;
c41359
	static int (*next_sendto)(int, const void *, size_t, int,
c41359
				  const struct sockaddr *, socklen_t);
c41359
c41359
	if (next_sendto == NULL) {
c41359
		next_sendto = dlsym(RTLD_NEXT, "sendto");
c41359
		if (next_sendto == NULL) {
c41359
			errno = ENOSYS;
c41359
			return -1;
c41359
		}
c41359
	}
c41359
c41359
	if (getenv("NOPORT") == NULL) {
c41359
		return next_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
c41359
	}
c41359
c41359
	if (dest_addr != NULL) {
c41359
		switch (dest_addr->sa_family) {
c41359
		case AF_INET:
c41359
			port = ((struct sockaddr_in *)dest_addr)->sin_port;
c41359
			port = ntohs(port);
c41359
			if (port_is_okay(port) != 0) {
c41359
				return -1;
c41359
			}
c41359
			break;
c41359
		case AF_INET6:
c41359
			port = ((struct sockaddr_in6 *)dest_addr)->sin6_port;
c41359
			port = ntohs(port);
c41359
			if (port_is_okay(port) != 0) {
c41359
				return -1;
c41359
			}
c41359
			break;
c41359
		default:
c41359
			break;
c41359
		}
c41359
	}
c41359
	return next_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
c41359
}