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