|
version 1.1, 2012/02/21 23:32:47
|
version 1.1.1.4, 2021/03/17 00:39:23
|
|
Line 10
|
Line 10
|
| #include "ppp.h" |
#include "ppp.h" |
| #include "util.h" |
#include "util.h" |
| #include <termios.h> |
#include <termios.h> |
| |
#include <paths.h> |
| |
|
| |
#include <libutil.h> |
| #include <netdb.h> |
#include <netdb.h> |
| #include <tcpd.h> |
#include <tcpd.h> |
| |
#include <sys/limits.h> |
| #include <sys/wait.h> |
#include <sys/wait.h> |
| #include <sys/sysctl.h> |
#include <sys/sysctl.h> |
| #include <net/route.h> |
#include <net/route.h> |
| #include <netinet/if_ether.h> |
#include <netinet/if_ether.h> |
| |
#include <net/ethernet.h> |
| |
#include <osreldate.h> |
| |
|
| /* |
/* |
| * DEFINITIONS |
* DEFINITIONS |
|
Line 32
|
Line 37
|
| * INTERNAL VARIABLES |
* INTERNAL VARIABLES |
| */ |
*/ |
| |
|
| |
#ifndef USE_NG_PRED1 |
| static const u_int16_t Crc16Table[256] = { |
static const u_int16_t Crc16Table[256] = { |
| /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, |
/* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, |
| /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, |
/* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, |
|
Line 66 static const u_int16_t Crc16Table[256] = {
|
Line 72 static const u_int16_t Crc16Table[256] = {
|
| /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, |
/* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, |
| /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 |
/* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 |
| }; |
}; |
| |
#endif |
| |
|
| static FILE *lockFp = NULL; |
static FILE *lockFp = NULL; |
| |
|
|
Line 73 static const u_int16_t Crc16Table[256] = {
|
Line 80 static const u_int16_t Crc16Table[256] = {
|
| * INTERNAL FUNCTIONS |
* INTERNAL FUNCTIONS |
| */ |
*/ |
| |
|
| static int UuLock(const char *devname); |
|
| static int UuUnlock(const char *devname); |
|
| |
|
| static void Escape(char *line); |
static void Escape(char *line); |
| static char *ReadLine(FILE *fp, int *lineNum, char *result, int resultsize); | static char *ReadLine(FILE *fp, int *lineNum, char *result, size_t resultsize); |
| |
|
| static char HexVal(char c); |
static char HexVal(char c); |
| |
|
| static void IndexConfFile(FILE *fp, struct configfile **cf); |
static void IndexConfFile(FILE *fp, struct configfile **cf); |
| |
|
| struct configfiles *ConfigFilesIndex=NULL; | static struct configfiles *ConfigFilesIndex=NULL; |
| |
|
| #undef isspace |
#undef isspace |
| #define isspace(c) (((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')?1:0) |
#define isspace(c) (((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')?1:0) |
|
Line 115 int
|
Line 119 int
|
| ExecCmd(int log, const char *label, const char *fmt, ...) |
ExecCmd(int log, const char *label, const char *fmt, ...) |
| { |
{ |
| int rtn; |
int rtn; |
| char cmd[BIG_LINE_SIZE]; | char cmd[LINE_MAX]; |
| char cmdn[BIG_LINE_SIZE]; | char cmdn[LINE_MAX]; |
| va_list ap; |
va_list ap; |
| |
|
| va_start(ap, fmt); |
va_start(ap, fmt); |
|
Line 130 ExecCmd(int log, const char *label, const char *fmt, .
|
Line 134 ExecCmd(int log, const char *label, const char *fmt, .
|
| |
|
| /* Hide any stdout output of command */ |
/* Hide any stdout output of command */ |
| |
|
| snprintf(cmdn + strlen(cmdn), sizeof(cmdn) - strlen(cmdn), " >/dev/null 2>&1"); | snprintf(cmdn + strlen(cmdn), sizeof(cmdn) - strlen(cmdn), " >%s 2>&1", _PATH_DEVNULL); |
| |
|
| /* Do command */ |
/* Do command */ |
| |
|
|
Line 150 int
|
Line 154 int
|
| ExecCmdNosh(int log, const char *label, const char *fmt, ...) |
ExecCmdNosh(int log, const char *label, const char *fmt, ...) |
| { |
{ |
| int rtn; |
int rtn; |
| char cmd[BIG_LINE_SIZE]; | char cmd[LINE_MAX]; |
| char *cmdp = &(cmd[0]); |
char *cmdp = &(cmd[0]); |
| char *argv[256]; |
char *argv[256]; |
| char **arg; |
char **arg; |
|
Line 202 ExecCmdNosh(int log, const char *label, const char *fm
|
Line 206 ExecCmdNosh(int log, const char *label, const char *fm
|
| (void)sigaction(SIGQUIT, &quitact, NULL); |
(void)sigaction(SIGQUIT, &quitact, NULL); |
| (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); |
(void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); |
| close(1); |
close(1); |
| open("/dev/null", O_WRONLY); | open(_PATH_DEVNULL, O_WRONLY); |
| close(2); |
close(2); |
| open("/dev/null", O_WRONLY); | open(_PATH_DEVNULL, O_WRONLY); |
| execv(argv[0], argv); |
execv(argv[0], argv); |
| exit(127); |
exit(127); |
| default: /* parent */ |
default: /* parent */ |
|
Line 375 Escape(char *line)
|
Line 379 Escape(char *line)
|
| |
|
| int |
int |
| ReadFile(const char *filename, const char *target, |
ReadFile(const char *filename, const char *target, |
| int (*func)(Context ctx, int ac, char *av[], const char *file, int line), Context ctx) | int (*func)(Context ctx, int ac, const char *const av[], const char *file, int line), Context ctx) |
| { |
{ |
| FILE *fp; |
FILE *fp; |
| int ac; |
int ac; |
|
Line 388 ReadFile(const char *filename, const char *target,
|
Line 392 ReadFile(const char *filename, const char *target,
|
| /* Open file */ |
/* Open file */ |
| |
|
| if ((fp = OpenConfFile(filename, &cf)) == NULL) |
if ((fp = OpenConfFile(filename, &cf)) == NULL) |
| return(-1); | return(-2); |
| |
|
| /* Find label */ |
/* Find label */ |
| |
|
|
Line 406 ReadFile(const char *filename, const char *target,
|
Line 410 ReadFile(const char *filename, const char *target,
|
| break; |
break; |
| } |
} |
| ac = ParseLine(line, av, sizeof(av) / sizeof(*av), 0); |
ac = ParseLine(line, av, sizeof(av) / sizeof(*av), 0); |
| (*func)(ctx, ac, av, filename, lineNum); | (*func)(ctx, ac, (const char *const *)av, filename, lineNum); |
| } |
} |
| |
|
| /* Done */ |
/* Done */ |
|
Line 555 OpenConfFile(const char *name, struct configfile **cf)
|
Line 559 OpenConfFile(const char *name, struct configfile **cf)
|
| char * |
char * |
| ReadFullLine(FILE *fp, int *lineNum, char *result, int resultsize) |
ReadFullLine(FILE *fp, int *lineNum, char *result, int resultsize) |
| { |
{ |
| int len, linelen, resultlinesize, continuation; | int len, resultlinesize, continuation; |
| | unsigned linelen; |
| char line[BIG_LINE_SIZE]; |
char line[BIG_LINE_SIZE]; |
| char real_line[BIG_LINE_SIZE]; |
char real_line[BIG_LINE_SIZE]; |
| char *resultline; |
char *resultline; |
|
Line 623 ReadFullLine(FILE *fp, int *lineNum, char *result, int
|
Line 628 ReadFullLine(FILE *fp, int *lineNum, char *result, int
|
| */ |
*/ |
| |
|
| static char * |
static char * |
| ReadLine(FILE *fp, int *lineNum, char *result, int resultsize) | ReadLine(FILE *fp, int *lineNum, char *result, size_t resultsize) |
| { |
{ |
| int empty; |
int empty; |
| char *s; |
char *s; |
|
Line 666 ReadLine(FILE *fp, int *lineNum, char *result, int res
|
Line 671 ReadLine(FILE *fp, int *lineNum, char *result, int res
|
| return(result); |
return(result); |
| } |
} |
| |
|
| |
#ifdef PHYSTYPE_MODEM |
| /* |
/* |
| * OpenSerialDevice() |
* OpenSerialDevice() |
| * |
* |
|
Line 737 ExclusiveOpenDevice(const char *label, const char *pat
|
Line 743 ExclusiveOpenDevice(const char *label, const char *pat
|
| |
|
| /* Lock device UUCP style, if it resides in /dev */ |
/* Lock device UUCP style, if it resides in /dev */ |
| |
|
| if (!strncmp(pathname, "/dev/", 5)) | if (!strncmp(pathname, _PATH_DEV, 5)) |
| { |
{ |
| |
int res; |
| ttyname = pathname + 5; |
ttyname = pathname + 5; |
| if (UuLock(ttyname) < 0) | if ((res = uu_lock(ttyname)) != UU_LOCK_OK) { |
| { | Log(LG_ERR, ("[%s] uu_lock(%s): %s", label, ttyname, uu_lockerr(res))); |
| Log(LG_ERR, ("[%s] can't lock device %s", label, ttyname)); | |
| return(-1); |
return(-1); |
| } |
} |
| locked = TRUE; |
locked = TRUE; |
|
Line 757 ExclusiveOpenDevice(const char *label, const char *pat
|
Line 763 ExclusiveOpenDevice(const char *label, const char *pat
|
| { |
{ |
| Perror("[%s] can't open %s", label, pathname); |
Perror("[%s] can't open %s", label, pathname); |
| if (locked) |
if (locked) |
| UuUnlock(ttyname); | uu_unlock(ttyname); |
| return(-1); |
return(-1); |
| } |
} |
| |
|
|
Line 768 ExclusiveOpenDevice(const char *label, const char *pat
|
Line 774 ExclusiveOpenDevice(const char *label, const char *pat
|
| Log(LG_ERR, ("[%s] can't open %s after %d secs", |
Log(LG_ERR, ("[%s] can't open %s after %d secs", |
| label, pathname, MAX_OPEN_DELAY)); |
label, pathname, MAX_OPEN_DELAY)); |
| if (locked) |
if (locked) |
| UuUnlock(ttyname); | uu_unlock(ttyname); |
| return(-1); |
return(-1); |
| } |
} |
| (void) fcntl(fd, F_SETFD, 1); |
(void) fcntl(fd, F_SETFD, 1); |
|
Line 810 ExclusiveCloseDevice(const char *label, int fd, const
|
Line 816 ExclusiveCloseDevice(const char *label, int fd, const
|
| |
|
| /* Remove lock */ |
/* Remove lock */ |
| |
|
| if (!strncmp(pathname, "/dev/", 5)) | if (!strncmp(pathname, _PATH_DEV, 5)) |
| { |
{ |
| ttyname = pathname + 5; |
ttyname = pathname + 5; |
| if (UuUnlock(ttyname) < 0) | if (uu_unlock(ttyname) < 0) |
| Perror("[%s] can't unlock %s", label, ttyname); |
Perror("[%s] can't unlock %s", label, ttyname); |
| } |
} |
| } |
} |
| |
#endif /* PHYSTYPE_MODEM */ |
| |
|
| /* |
|
| * UuLock() |
|
| * |
|
| * Try to atomically create lockfile. Returns negative if failed. |
|
| */ |
|
| |
|
| static int |
|
| UuLock(const char *ttyname) |
|
| { |
|
| int fd, pid; |
|
| char tbuf[sizeof(PATH_LOCKFILENAME) + MAX_FILENAME]; |
|
| char pid_buf[64]; |
|
| |
|
| snprintf(tbuf, sizeof(tbuf), PATH_LOCKFILENAME, ttyname); |
|
| if ((fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0664)) < 0) |
|
| { |
|
| |
|
| /* File is already locked; Check to see if the process |
|
| * holding the lock still exists */ |
|
| |
|
| if ((fd = open(tbuf, O_RDWR, 0)) < 0) |
|
| { |
|
| Perror("%s: open(%s)", __FUNCTION__, tbuf); |
|
| return(-1); |
|
| } |
|
| |
|
| if (read(fd, pid_buf, sizeof(pid_buf)) <= 0) |
|
| { |
|
| (void)close(fd); |
|
| Perror("%s: read", __FUNCTION__); |
|
| return(-1); |
|
| } |
|
| |
|
| pid = atoi(pid_buf); |
|
| |
|
| if (kill(pid, 0) == 0 || errno != ESRCH) |
|
| { |
|
| (void)close(fd); /* process is still running */ |
|
| return(-1); |
|
| } |
|
| |
|
| /* The process that locked the file isn't running, so we'll lock it */ |
|
| |
|
| if (lseek(fd, (off_t) 0, L_SET) < 0) |
|
| { |
|
| (void)close(fd); |
|
| Perror("%s: lseek", __FUNCTION__); |
|
| return(-1); |
|
| } |
|
| } |
|
| |
|
| /* Finish the locking process */ |
|
| |
|
| sprintf(pid_buf, "%10u\n", (int) gPid); |
|
| if (write(fd, pid_buf, strlen(pid_buf)) != strlen(pid_buf)) |
|
| { |
|
| (void)close(fd); |
|
| (void)unlink(tbuf); |
|
| Perror("%s: write", __FUNCTION__); |
|
| return(-1); |
|
| } |
|
| (void)close(fd); |
|
| return(0); |
|
| } |
|
| |
|
| /* |
/* |
| * UuUnlock() |
|
| */ |
|
| |
|
| static int |
|
| UuUnlock(const char *ttyname) |
|
| { |
|
| char tbuf[sizeof(PATH_LOCKFILENAME) + MAX_FILENAME]; |
|
| |
|
| (void) sprintf(tbuf, PATH_LOCKFILENAME, ttyname); |
|
| return(unlink(tbuf)); |
|
| } |
|
| |
|
| /* |
|
| * GenerateMagic() |
* GenerateMagic() |
| * |
* |
| * Generate random number which will be used as magic number. |
* Generate random number which will be used as magic number. |
|
Line 925 GenerateMagic(void)
|
Line 855 GenerateMagic(void)
|
| int |
int |
| PIDCheck(const char *filename, int killem) |
PIDCheck(const char *filename, int killem) |
| { |
{ |
| int fd = -1, n_tries; | int n_tries; |
| | struct pidfh *pfh = NULL; |
| |
|
| /* Sanity */ |
/* Sanity */ |
| |
|
|
Line 933 PIDCheck(const char *filename, int killem)
|
Line 864 PIDCheck(const char *filename, int killem)
|
| |
|
| /* Atomically open and lock file */ |
/* Atomically open and lock file */ |
| |
|
| for (n_tries = 0; | for (n_tries = 0; n_tries < MAX_LOCK_ATTEMPTS; n_tries++) |
| n_tries < MAX_LOCK_ATTEMPTS | |
| && (fd = open(filename, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644)) < 0; | |
| n_tries++) | |
| { |
{ |
| int nscan, old_pid; | pid_t old_pid; |
| FILE *fp; | |
| |
|
| /* Abort on any unexpected errors */ | pfh = pidfile_open(filename, 0644, &old_pid); |
| if (pfh == NULL) { |
| if (errno != EAGAIN) | if (errno == EEXIST) { |
| { | if (!killem) { |
| Perror("%s: open(%s)", __FUNCTION__, filename); | Log(LG_ERR, ("already running as process %d", old_pid)); |
| return(-1); | return(-1); |
| | } |
| | if (kill(old_pid, SIGTERM) < 0) |
| | switch (errno) { |
| | case ESRCH: |
| | Log(LG_ERR, ("process %d no longer exists", old_pid)); |
| | break; |
| | default: |
| | Perror("%s: kill(%d)", __FUNCTION__, old_pid); |
| | return(-1); |
| | } |
| | /* Wait and try again */ |
| | Log(LG_ERR, ("waiting for process %d to die...", old_pid)); |
| | sleep(1); |
| | } else { |
| | Perror("cannot open pid file"); |
| | return(-1); |
| | } |
| | } else { |
| | pidfile_write(pfh); |
| | break; |
| } |
} |
| |
|
| /* We're already running ... see who it is */ |
|
| |
|
| if ((fp = fopen(filename, "r")) == NULL) |
|
| { |
|
| Perror("%s: fopen(%s)", __FUNCTION__, filename); |
|
| return(-1); |
|
| } |
|
| |
|
| /* If there's a PID in there, sniff it out */ |
|
| |
|
| nscan = fscanf(fp, "%d", &old_pid); |
|
| fclose(fp); |
|
| if (nscan != 1) |
|
| { |
|
| Log(LG_ERR, ("%s: contents mangled", filename)); |
|
| return(-1); |
|
| } |
|
| |
|
| /* Maybe kill the other guy */ |
|
| |
|
| if (!killem) |
|
| { |
|
| Log(LG_ERR, ("already running as process %d", old_pid)); |
|
| return(-1); |
|
| } |
|
| if (kill(old_pid, SIGTERM) < 0) |
|
| switch (errno) |
|
| { |
|
| case ESRCH: |
|
| Log(LG_ERR, ("process %d no longer exists", old_pid)); |
|
| break; |
|
| default: |
|
| Perror("%s: kill(%d)", __FUNCTION__, old_pid); |
|
| return(-1); |
|
| } |
|
| |
|
| /* Wait and try again */ |
|
| |
|
| Log(LG_ERR, ("waiting for process %d to die...", old_pid)); |
|
| sleep(1); |
|
| } |
} |
| if (n_tries == MAX_LOCK_ATTEMPTS) |
if (n_tries == MAX_LOCK_ATTEMPTS) |
| { |
{ |
| Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); |
Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); |
| return(-1); |
return(-1); |
| } |
} |
| |
|
| /* Close on exec */ |
|
| |
|
| (void) fcntl(fd, F_SETFD, 1); |
|
| |
|
| /* Create a stream on top of file descriptor */ |
|
| |
|
| if ((lockFp = fdopen(fd, "r+")) == NULL) |
|
| { |
|
| Perror("%s: fdopen", __FUNCTION__); |
|
| return(-1); |
|
| } |
|
| setbuf(lockFp, NULL); |
|
| |
|
| /* Write my PID in there */ |
|
| |
|
| rewind(lockFp); |
|
| fprintf(lockFp, "%u\n", (u_int) gPid); |
|
| fflush(lockFp); |
|
| (void) ftruncate(fileno(lockFp), ftell(lockFp)); |
|
| return(0); |
return(0); |
| } |
} |
| |
|
|
Line 1093 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
Line 979 TcpGetListenPort(struct u_addr *addr, in_port_t port,
|
| |
|
| /* Make socket available for connections */ |
/* Make socket available for connections */ |
| |
|
| if (listen(sock, 2) < 0) | if (listen(sock, -1) < 0) |
| { |
{ |
| Perror("%s: listen", __FUNCTION__); |
Perror("%s: listen", __FUNCTION__); |
| (void) close(sock); |
(void) close(sock); |
|
Line 1166 ShowMesg(int log, const char *pref, const char *buf, i
|
Line 1052 ShowMesg(int log, const char *pref, const char *buf, i
|
| |
|
| if (len > 0) |
if (len > 0) |
| { |
{ |
| if (len > sizeof(mesg) - 1) | if (len > (int)(sizeof(mesg) - 1)) |
| len = sizeof(mesg) - 1; |
len = sizeof(mesg) - 1; |
| memcpy(mesg, buf, len); |
memcpy(mesg, buf, len); |
| mesg[len] = 0; |
mesg[len] = 0; |
|
Line 1186 Bin2Hex(const unsigned char *bin, size_t len)
|
Line 1072 Bin2Hex(const unsigned char *bin, size_t len)
|
| size_t i, j; |
size_t i, j; |
| char *buf; |
char *buf; |
| |
|
| buf = Malloc(MB_UTIL, len * 2 + 1); | if (len > 0) { |
| for (i = j = 0; i < len; i++) { | buf = Malloc(MB_UTIL, len * 2 + 1); |
| buf[j++] = hexconvtab[bin[i] >> 4]; | for (i = j = 0; i < len; i++) { |
| buf[j++] = hexconvtab[bin[i] & 15]; | buf[j++] = hexconvtab[bin[i] >> 4]; |
| | buf[j++] = hexconvtab[bin[i] & 15]; |
| | } |
| | buf[j] = 0; |
| | } else { |
| | buf = Malloc(MB_UTIL, 3); |
| | buf[0] = '0'; |
| | buf[1] = '0'; |
| | buf[2] = 0; |
| } |
} |
| buf[j] = 0; |
|
| return buf; |
return buf; |
| } |
} |
| |
|
|
Line 1202 Bin2Hex(const unsigned char *bin, size_t len)
|
Line 1095 Bin2Hex(const unsigned char *bin, size_t len)
|
| u_char * |
u_char * |
| Hex2Bin(char *hexstr) |
Hex2Bin(char *hexstr) |
| { |
{ |
| int i; | unsigned i; |
| u_char *binval; |
u_char *binval; |
| |
|
| binval = Malloc(MB_UTIL, strlen(hexstr) / 2); |
binval = Malloc(MB_UTIL, strlen(hexstr) / 2); |
|
Line 1228 HexVal(char c)
|
Line 1121 HexVal(char c)
|
| } |
} |
| } |
} |
| |
|
| |
#ifndef USE_NG_PRED1 |
| /* |
/* |
| * Crc16() |
* Crc16() |
| * |
* |
|
Line 1242 Crc16(u_short crc, u_char *cp, int len)
|
Line 1136 Crc16(u_short crc, u_char *cp, int len)
|
| crc = (crc >> 8) ^ Crc16Table[(crc ^ *cp++) & 0xff]; |
crc = (crc >> 8) ^ Crc16Table[(crc ^ *cp++) & 0xff]; |
| return(crc); |
return(crc); |
| } |
} |
| |
#endif |
| |
|
| /* |
/* |
| * GetAnyIpAddress() |
* GetAnyIpAddress() |
|
Line 1283 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
Line 1178 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
| close(s); |
close(s); |
| return(-1); |
return(-1); |
| } |
} |
| ipa = ((struct sockaddr_in *)&ifreq.ifr_ifru.ifru_addr)->sin_addr; | ipa = ((struct sockaddr_in *)(void *)&ifreq.ifr_ifru.ifru_addr)->sin_addr; |
| if ((ntohl(ipa.s_addr)>>24) == 127) |
if ((ntohl(ipa.s_addr)>>24) == 127) |
| ipa.s_addr = 0; /* We don't like 127.0.0.1 */ |
ipa.s_addr = 0; /* We don't like 127.0.0.1 */ |
| } |
} |
|
Line 1303 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
Line 1198 GetAnyIpAddress(struct u_addr *ipaddr, const char *ifn
|
| } |
} |
| |
|
| /* if used size is too close to allocated size retry with a larger buffer */ |
/* if used size is too close to allocated size retry with a larger buffer */ |
| if (ifc.ifc_len + 128 < buffsize) | if ((unsigned)ifc.ifc_len + 128 < buffsize) |
| break; |
break; |
| |
|
| Freee(ifs); |
Freee(ifs); |
|
Line 1403 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
Line 1298 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
| } |
} |
| |
|
| /* if used size is too close to allocated size retry with a larger buffer */ |
/* if used size is too close to allocated size retry with a larger buffer */ |
| if (ifc.ifc_len + 128 < buffsize) | if ((unsigned)ifc.ifc_len + 128 < buffsize) |
| break; |
break; |
| |
|
| Freee(ifs); |
Freee(ifs); |
|
Line 1434 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
Line 1329 GetEther(struct u_addr *addr, struct sockaddr_dl *hwad
|
| ifreq.ifr_addr = ifr->ifr_addr; |
ifreq.ifr_addr = ifr->ifr_addr; |
| |
|
| /* Check that the interface is up, and not point-to-point or loopback */ |
/* Check that the interface is up, and not point-to-point or loopback */ |
| if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) | if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) { |
| | Log(LG_IFACE2, ("ioctl(SIOCGIFFLAGS, %s): %d", ifr->ifr_name, errno)); |
| continue; |
continue; |
| |
} |
| if ((ifreq.ifr_flags & |
if ((ifreq.ifr_flags & |
| (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) |
(IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) |
| != (IFF_UP|IFF_BROADCAST)) |
!= (IFF_UP|IFF_BROADCAST)) |
|
Line 1514 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
Line 1411 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
| mib[5] = 0; |
mib[5] = 0; |
| #endif |
#endif |
| if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { |
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { |
| Log(LG_ERR, ("route-sysctl-estimate")); | Perror("route-sysctl-estimate"); |
| return (0); |
return (0); |
| } |
} |
| if (needed == 0) /* empty table */ |
if (needed == 0) /* empty table */ |
|
Line 1536 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
Line 1433 GetPeerEther(struct u_addr *addr, struct sockaddr_dl *
|
| } |
} |
| lim = buf + needed; |
lim = buf + needed; |
| for (next = buf; next < lim; next += rtm->rtm_msglen) { |
for (next = buf; next < lim; next += rtm->rtm_msglen) { |
| rtm = (struct rt_msghdr *)next; | rtm = (struct rt_msghdr *)(void *)next; |
| sin2 = (struct sockaddr_inarp *)(rtm + 1); |
sin2 = (struct sockaddr_inarp *)(rtm + 1); |
| if (addr->u.ip4.s_addr == sin2->sin_addr.s_addr) { |
if (addr->u.ip4.s_addr == sin2->sin_addr.s_addr) { |
| sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); | sdl = (struct sockaddr_dl *)(void *)((char *)sin2 + SA_SIZE(sin2)); |
| memcpy(hwaddr, sdl, sdl->sdl_len); |
memcpy(hwaddr, sdl, sdl->sdl_len); |
| found_entry = 1; |
found_entry = 1; |
| break; |
break; |
|
Line 1556 void
|
Line 1453 void
|
| ppp_util_ascify(char *buf, size_t bsiz, const char *data, size_t len) |
ppp_util_ascify(char *buf, size_t bsiz, const char *data, size_t len) |
| { |
{ |
| char *bp; |
char *bp; |
| int i; | unsigned i; |
| |
|
| for (bp = buf, i = 0; i < len; i++) { |
for (bp = buf, i = 0; i < len; i++) { |
| const char ch = (char)data[i]; |
const char ch = (char)data[i]; |
|
Line 1594 ppp_util_ascify(char *buf, size_t bsiz, const char *da
|
Line 1491 ppp_util_ascify(char *buf, size_t bsiz, const char *da
|
| *bp = '\0'; |
*bp = '\0'; |
| } |
} |
| |
|
| |
#ifndef HAVE_NTOA_R |
| |
/* |
| |
* Convert a binary representation of an ethernet address to an ASCII string. |
| |
*/ |
| |
char * |
| |
ether_ntoa_r(const struct ether_addr *n, char *a) |
| |
{ |
| |
int i; |
| |
|
| |
i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", n->octet[0], |
| |
n->octet[1], n->octet[2], n->octet[3], n->octet[4], n->octet[5]); |
| |
if (i < 17) |
| |
return (NULL); |
| |
return (a); |
| |
} |
| |
#endif |
| |
|
| |
int |
| |
IfaceSetFlag(const char *ifname, int value) |
| |
{ |
| |
struct ifreq my_ifr; |
| |
int s; |
| |
int flags; |
| |
|
| |
/* Get socket */ |
| |
if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0) { |
| |
Perror("Can't get socket to set flags"); |
| |
return(-1); |
| |
} |
| |
|
| |
memset(&my_ifr, 0, sizeof(my_ifr)); |
| |
(void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name)); |
| |
|
| |
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { |
| |
Perror("ioctl (SIOCGIFFLAGS)"); |
| |
close(s); |
| |
return (-1); |
| |
} |
| |
flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); |
| |
|
| |
if (value < 0) { |
| |
value = -value; |
| |
flags &= ~value; |
| |
} else |
| |
flags |= value; |
| |
my_ifr.ifr_flags = flags & 0xffff; |
| |
my_ifr.ifr_flagshigh = flags >> 16; |
| |
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) { |
| |
Perror("ioctl (SIOCSIFFLAGS)"); |
| |
close(s); |
| |
return (-1); |
| |
} |
| |
close(s); |
| |
return (0); |
| |
} |