Use fstat to check if descriptor 0 is a socket.

Suggested by: julian
This commit is contained in:
Brian Somers 2001-01-14 00:54:48 +00:00
parent 6fe65214f2
commit 165fbe2672
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=71006
4 changed files with 99 additions and 58 deletions

View File

@ -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;
struct stat st;
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 (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
struct sockaddr_storage ssock;
struct sockaddr *sock = (struct sockaddr *)&ssock;
int sz;
if ((dev = malloc(sizeof *dev)) == NULL) {
log_Printf(LogWARN, "%s: Cannot allocate an ether device: %s\n",
p->link.name, strerror(errno));
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';
}
}
}

View File

@ -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 },

View File

@ -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;
struct stat st;
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 (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
int type, sz;
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;
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;
}
if (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;
}
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;
}
}

View File

@ -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;
struct stat st;
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));
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;
}
log_Printf(LogPHASE, "%s: Link is a udp socket\n", p->link.name);
/* We can't getpeername().... */
dev->connected = UDP_MAYBEUNCONNECTED;
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;
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;
}
}
}
}