diff options
author | brian <brian@FreeBSD.org> | 2001-01-14 00:54:48 +0000 |
---|---|---|
committer | brian <brian@FreeBSD.org> | 2001-01-14 00:54:48 +0000 |
commit | 187b13bfd2fa124b2ceb29d4e1e8e6032ba03662 (patch) | |
tree | 30b826763fdf682fb6dd4f0bcfcc5a95829bfad2 /usr.sbin/ppp | |
parent | 94588351ce2d8970ce221dba2766deee30ef192b (diff) | |
download | FreeBSD-src-187b13bfd2fa124b2ceb29d4e1e8e6032ba03662.zip FreeBSD-src-187b13bfd2fa124b2ceb29d4e1e8e6032ba03662.tar.gz |
Use fstat to check if descriptor 0 is a socket.
Suggested by: julian
Diffstat (limited to 'usr.sbin/ppp')
-rw-r--r-- | usr.sbin/ppp/ether.c | 52 | ||||
-rw-r--r-- | usr.sbin/ppp/physical.c | 11 | ||||
-rw-r--r-- | usr.sbin/ppp/tcp.c | 59 | ||||
-rw-r--r-- | usr.sbin/ppp/udp.c | 41 |
4 files changed, 102 insertions, 61 deletions
diff --git a/usr.sbin/ppp/ether.c b/usr.sbin/ppp/ether.c index cf9c999..e3ab77f 100644 --- a/usr.sbin/ppp/ether.c +++ b/usr.sbin/ppp/ether.c @@ -51,6 +51,7 @@ #include <sys/linker.h> #include <sys/module.h> #endif +#include <sys/stat.h> #include <sys/uio.h> #include <termios.h> #include <sys/time.h> @@ -623,29 +624,40 @@ ether_Create(struct physical *p) } else { /* See if we're a netgraph socket */ - struct sockaddr_ng ngsock; - struct sockaddr *sock = (struct sockaddr *)&ngsock; - int sz; - - sz = sizeof ngsock; - if (getsockname(p->fd, sock, &sz) != -1 && sock->sa_family == AF_NETGRAPH) { - /* - * It's a netgraph node... We can't determine hook names etc, so we - * stay pretty impartial.... - */ - log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name); - - if ((dev = malloc(sizeof *dev)) == NULL) { - log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n", - p->link.name, strerror(errno)); + struct stat st; + + if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) { + struct sockaddr_storage ssock; + struct sockaddr *sock = (struct sockaddr *)&ssock; + int sz; + + sz = sizeof ssock; + if (getsockname(p->fd, sock, &sz) == -1) { + log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name); + close(p->fd); + p->fd = -1; return NULL; } - memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev); - dev->cs = -1; - dev->timeout = 0; - dev->connected = CARRIER_OK; - *dev->hook = '\0'; + if (sock->sa_family == AF_NETGRAPH) { + /* + * It's a netgraph node... We can't determine hook names etc, so we + * stay pretty impartial.... + */ + log_Printf(LogPHASE, "%s: Link is a netgraph node\n", p->link.name); + + if ((dev = malloc(sizeof *dev)) == NULL) { + log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n", + p->link.name, strerror(errno)); + return NULL; + } + + memcpy(&dev->dev, &baseetherdevice, sizeof dev->dev); + dev->cs = -1; + dev->timeout = 0; + dev->connected = CARRIER_OK; + *dev->hook = '\0'; + } } } diff --git a/usr.sbin/ppp/physical.c b/usr.sbin/ppp/physical.c index 3d7811f..5e50224 100644 --- a/usr.sbin/ppp/physical.c +++ b/usr.sbin/ppp/physical.c @@ -119,15 +119,22 @@ struct { int (*DeviceSize)(void); } devices[] = { #ifndef NOI4B + /* + * This must come before ``tty'' so that the probe routine is + * able to identify it as a more specific type of terminal device. + */ { i4b_Create, i4b_iov2device, i4b_DeviceSize }, #endif { tty_Create, tty_iov2device, tty_DeviceSize }, #ifndef NONETGRAPH - /* This must come before ``udp'' & ``tcp'' */ + /* + * This must come before ``udp'' so that the probe routine is + * able to identify it as a more specific type of SOCK_DGRAM. + */ { ether_Create, ether_iov2device, ether_DeviceSize }, #endif #ifndef NOATM - /* and so must this */ + /* Ditto for ATM devices */ { atm_Create, atm_iov2device, atm_DeviceSize }, #endif { tcp_Create, tcp_iov2device, tcp_DeviceSize }, diff --git a/usr.sbin/ppp/tcp.c b/usr.sbin/ppp/tcp.c index 72d6eb1..b7482d1 100644 --- a/usr.sbin/ppp/tcp.c +++ b/usr.sbin/ppp/tcp.c @@ -36,6 +36,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include <sys/uio.h> #include <termios.h> #include <unistd.h> @@ -164,33 +165,43 @@ tcp_Create(struct physical *p) if (p->fd >= 0) { /* See if we're a tcp socket */ - int type, sz, err; - - sz = sizeof type; - if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 && - sz == sizeof type && type == SOCK_STREAM) { - struct sockaddr_in sock; - struct sockaddr *sockp = (struct sockaddr *)&sock; - - if (*p->name.full == '\0') { - sz = sizeof sock; - if (getpeername(p->fd, sockp, &sz) != 0 || - sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) { - log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n", - p->link.name); - return NULL; - } + struct stat st; + + if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) { + int type, sz; + + sz = sizeof type; + if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) { + log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name); + close(p->fd); + p->fd = -1; + return NULL; + } - log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name); + if (sz == sizeof type && type == SOCK_STREAM) { + struct sockaddr_in sock; + struct sockaddr *sockp = (struct sockaddr *)&sock; - snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp", - inet_ntoa(sock.sin_addr), ntohs(sock.sin_port)); - p->name.base = p->name.full; + if (*p->name.full == '\0') { + sz = sizeof sock; + if (getpeername(p->fd, sockp, &sz) != 0 || + sz != sizeof(struct sockaddr_in) || sock.sin_family != AF_INET) { + log_Printf(LogDEBUG, "%s: Link is SOCK_STREAM, but not inet\n", + p->link.name); + return NULL; + } + + log_Printf(LogPHASE, "%s: Link is a tcp socket\n", p->link.name); + + snprintf(p->name.full, sizeof p->name.full, "%s:%d/tcp", + inet_ntoa(sock.sin_addr), ntohs(sock.sin_port)); + p->name.base = p->name.full; + } + physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); + if (p->cfg.cd.necessity != CD_DEFAULT) + log_Printf(LogWARN, "Carrier settings ignored\n"); + return &tcpdevice; } - physical_SetupStack(p, tcpdevice.name, PHYSICAL_FORCE_ASYNC); - if (p->cfg.cd.necessity != CD_DEFAULT) - log_Printf(LogWARN, "Carrier settings ignored\n"); - return &tcpdevice; } } diff --git a/usr.sbin/ppp/udp.c b/usr.sbin/ppp/udp.c index 7334f59..6894e1e 100644 --- a/usr.sbin/ppp/udp.c +++ b/usr.sbin/ppp/udp.c @@ -37,6 +37,7 @@ #include <stdlib.h> #include <string.h> #include <sysexits.h> +#include <sys/stat.h> #include <sys/uio.h> #include <termios.h> #include <unistd.h> @@ -277,26 +278,36 @@ udp_Create(struct physical *p) } } else { /* See if we're a connected udp socket */ - int type, sz, err; - - sz = sizeof type; - if ((err = getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz)) == 0 && - sz == sizeof type && type == SOCK_DGRAM) { - if ((dev = malloc(sizeof *dev)) == NULL) { - log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n", - p->link.name, strerror(errno)); + struct stat st; + + if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) { + int type, sz; + + sz = sizeof type; + if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) { + log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name); + close(p->fd); + p->fd = -1; return NULL; } - /* We can't getpeername().... */ - dev->connected = UDP_MAYBEUNCONNECTED; + if (sz == sizeof type && type == SOCK_DGRAM) { + if ((dev = malloc(sizeof *dev)) == NULL) { + log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n", + p->link.name, strerror(errno)); + return NULL; + } + + /* We can't getpeername().... */ + dev->connected = UDP_MAYBEUNCONNECTED; - log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name); + log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name); - if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) { - log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n", - p->link.name); - p->link.lcp.cfg.openmode = OPEN_PASSIVE; + if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) { + log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n", + p->link.name); + p->link.lcp.cfg.openmode = OPEN_PASSIVE; + } } } } |