version 1.1.1.4, 2021/03/17 00:39:23
|
version 1.1.1.4.2.1, 2023/09/27 11:08:01
|
Line 16
|
Line 16
|
#include <netdb.h> |
#include <netdb.h> |
#include <tcpd.h> |
#include <tcpd.h> |
#include <sys/limits.h> |
#include <sys/limits.h> |
#include <sys/wait.h> | #include <sys/socket.h> |
#include <sys/sysctl.h> |
#include <sys/sysctl.h> |
|
#include <sys/wait.h> |
#include <net/route.h> |
#include <net/route.h> |
#include <netinet/if_ether.h> |
#include <netinet/if_ether.h> |
#include <net/ethernet.h> |
#include <net/ethernet.h> |
Line 921 GetInetSocket(int type, struct u_addr *addr, in_port_t
|
Line 922 GetInetSocket(int type, struct u_addr *addr, in_port_t
|
|
|
/* Get and bind non-blocking socket */ |
/* Get and bind non-blocking socket */ |
|
|
if ((sock = socket(sa.ss_family, type, type == SOCK_STREAM ? IPPROTO_TCP : 0)) < 0) | if ((sock = socket(sa.ss_family, socktype(type), type == SOCK_STREAM ? IPPROTO_TCP : 0)) < 0) |
{ |
{ |
snprintf(ebuf, len, "socket: %s", strerror(errno)); |
snprintf(ebuf, len, "socket: %s", strerror(errno)); |
return(-1); |
return(-1); |
Line 979 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
Line 980 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
|
|
/* Make socket available for connections */ |
/* Make socket available for connections */ |
|
|
if (listen(sock, -1) < 0) | if (listen(sock, INT_MAX) < 0) |
{ |
{ |
Perror("%s: listen", __FUNCTION__); |
Perror("%s: listen", __FUNCTION__); |
(void) close(sock); |
(void) close(sock); |
Line 1545 IfaceSetFlag(const char *ifname, int value)
|
Line 1546 IfaceSetFlag(const char *ifname, int value)
|
} |
} |
close(s); |
close(s); |
return (0); |
return (0); |
|
} |
|
|
|
/* |
|
* Obtain some data, peer (source) and destination addresses of SOCK_DGRAM IPv4 UDP request. |
|
*/ |
|
ssize_t GetDataAddrs(int sock, void *dbuf, size_t dbufsize, |
|
struct sockaddr_storage *peer, socklen_t peer_len, |
|
struct u_addr *addr) |
|
{ |
|
struct { |
|
struct msghdr msg; |
|
struct iovec iov; |
|
} b; |
|
union { /* ensure correct alignment for space */ |
|
struct cmsghdr cm; |
|
char space[CMSG_SPACE(sizeof(struct in_addr))]; |
|
} buf; |
|
|
|
struct cmsghdr *p; |
|
ssize_t size; |
|
|
|
/* Sanity check */ |
|
if (addr->family != AF_INET) { |
|
errno = EAFNOSUPPORT; |
|
return (-1); |
|
} |
|
|
|
b.msg.msg_name = peer; |
|
b.msg.msg_namelen = peer_len; |
|
b.msg.msg_iov = &b.iov; |
|
b.msg.msg_iovlen = 1; |
|
b.msg.msg_control = &buf; |
|
b.msg.msg_controllen = sizeof(buf); |
|
b.msg.msg_flags = 0; |
|
|
|
b.iov.iov_base = dbuf; |
|
b.iov.iov_len = dbufsize; |
|
|
|
if ((size = recvmsg(sock, &b.msg, 0)) < 0) { |
|
Perror("%s: recvmsg: %s", __FUNCTION__, strerror(errno)); |
|
return (size); |
|
} |
|
|
|
p = CMSG_FIRSTHDR(&b.msg); |
|
if (p && p->cmsg_level == IPPROTO_IP && p->cmsg_type == IP_RECVDSTADDR) |
|
memcpy(&addr->u.ip4, CMSG_DATA(p), sizeof(addr->u.ip4)); |
|
|
|
return (size); |
|
} |
|
|
|
uint16_t GetSystemIfaceMTU(const char *ifname) |
|
{ |
|
struct ifreq ifr; |
|
static int sock = -1; |
|
|
|
if (sock == -1 && |
|
(sock = socket(PF_INET, socktype(SOCK_DGRAM), 0)) == -1) { |
|
Perror("[%s] %s: Socket creation error", ifname, __FUNCTION__); |
|
return (0); |
|
} |
|
|
|
memset(&ifr, 0, sizeof(ifr)); |
|
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
|
|
|
if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1) { |
|
Perror("[%s] %s: SIOCGIFMTU failed", ifname, __FUNCTION__); |
|
return (0); |
|
} |
|
/* Let _exit() close sock */ |
|
return (ifr.ifr_mtu); |
} |
} |