31 #include "netif/ppp/ppp_opts.h" 48 #include <sys/param.h> 49 #include <sys/types.h> 52 #include <sys/resource.h> 55 #include <netinet/in.h> 57 #include <sys/mkdev.h> 63 #include "netif/ppp/ppp_impl.h" 65 #include "netif/ppp/fsm.h" 66 #include "netif/ppp/lcp.h" 69 extern char *strerror();
72 static void ppp_logit(
int level,
const char *fmt, va_list args);
73 static void ppp_log_write(
int level,
char *buf);
75 static void ppp_vslp_printer(
void *arg,
const char *fmt, ...);
76 static void ppp_format_packet(
const u_char *p,
int len,
77 void (*printer) (
void *,
const char *, ...),
void *arg);
89 size_t ppp_strlcpy(
char *dest,
const char *src,
size_t len) {
90 size_t ret = strlen(src);
96 strncpy(dest, src, len - 1);
107 size_t ppp_strlcat(
char *dest,
const char *src,
size_t len) {
108 size_t dlen = strlen(dest);
110 return dlen + ppp_strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
122 int ppp_slprintf(
char *buf,
int buflen,
const char *fmt, ...) {
127 n = ppp_vslprintf(buf, buflen, fmt, args);
135 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) 137 int ppp_vslprintf(
char *buf,
int buflen,
const char *fmt, va_list args) {
139 int width, prec, fillch;
140 int base, len, neg, quoted;
141 unsigned long val = 0;
144 const unsigned char *p;
150 static char hexchars[] =
"0123456789abcdef";
152 struct buffer_info bufinfo;
158 for (f = fmt; *f !=
'%' && *f != 0; ++f)
164 memcpy(buf, fmt, len);
180 width = va_arg(args,
int);
184 width = width * 10 + c -
'0';
191 prec = va_arg(args,
int);
196 prec = prec * 10 + c -
'0';
210 val = va_arg(args,
long);
213 val = (
unsigned long)-(
long)val;
218 val = va_arg(args,
unsigned long);
229 i = va_arg(args,
int);
238 val = va_arg(args,
unsigned int);
242 val = va_arg(args,
unsigned int);
247 val = va_arg(args,
unsigned int);
252 val = (
unsigned long) va_arg(args,
void *);
258 str = va_arg(args,
char *);
261 num[0] = va_arg(args,
int);
267 str = strerror(errno);
271 ip = va_arg(args, u32_t);
273 ppp_slprintf(num,
sizeof(num),
"%d.%d.%d.%d", (ip >> 24) & 0xff,
274 (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
288 p = va_arg(args,
unsigned char *);
290 p = (
const unsigned char *)
"<NULL>";
291 if (fillch ==
'0' && prec >= 0) {
294 n = strlen((
const char *)p);
295 if (prec >= 0 && n > prec)
298 while (n > 0 && buflen > 0) {
301 if (!quoted && c >= 0x80) {
306 if (quoted && (c ==
'"' || c ==
'\\'))
308 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
312 case '\t': OUTCHAR(
't');
break;
313 case '\n': OUTCHAR(
'n');
break;
314 case '\b': OUTCHAR(
'b');
break;
315 case '\f': OUTCHAR(
'f');
break;
318 OUTCHAR(hexchars[c >> 4]);
319 OUTCHAR(hexchars[c & 0xf]);
336 bufinfo.len = buflen + 1;
337 p = va_arg(args,
unsigned char *);
338 n = va_arg(args,
int);
339 ppp_format_packet(p, n, ppp_vslp_printer, &bufinfo);
341 buflen = bufinfo.len - 1;
345 p = va_arg(args,
unsigned char *);
346 for (n = prec; n > 0; --n) {
350 OUTCHAR(hexchars[(c >> 4) & 0xf]);
351 OUTCHAR(hexchars[c & 0xf]);
362 str = num +
sizeof(num);
364 while (str > num + neg) {
365 *--str = hexchars[val % base];
367 if (--prec <= 0 && val == 0)
381 len = num +
sizeof(num) - 1 - str;
384 if (prec >= 0 && len > prec)
390 if ((n = width - len) > 0) {
398 memcpy(buf, str, len);
410 static void ppp_vslp_printer(
void *arg,
const char *fmt, ...) {
413 struct buffer_info *bi;
416 bi = (
struct buffer_info *) arg;
417 n = ppp_vslprintf(bi->ptr, bi->len, fmt, pvar);
431 log_packet(p, len, prefix, level)
437 init_pr_log(prefix, level);
438 ppp_format_packet(p, len, pr_log, &level);
448 static void ppp_format_packet(
const u_char *p,
int len,
449 void (*printer) (
void *,
const char *, ...),
void *arg) {
452 const struct protent *protp;
457 for (i = 0; (protp = protocols[i]) != NULL; ++i)
458 if (proto == protp->protocol)
461 printer(arg,
"[%s", protp->name);
462 n = (*protp->printpkt)(p, len, printer, arg);
467 for (i = 0; (protp = protocols[i]) != NULL; ++i)
468 if (proto == (protp->protocol & ~0x8000))
470 if (protp != 0 && protp->data_name != 0) {
471 printer(arg,
"[%s data]", protp->data_name);
473 printer(arg,
"%.8B ...", p);
475 printer(arg,
"%.*B", len, p);
478 printer(arg,
"[proto=0x%x]", proto);
483 printer(arg,
"%.32B ...", p);
485 printer(arg,
"%.*B", len, p);
494 static char line[256];
499 init_pr_log(prefix, level)
504 if (prefix != NULL) {
505 ppp_strlcpy(line, prefix,
sizeof(line));
506 linep = line + strlen(line);
516 ppp_log_write(llevel, line);
524 pr_log (
void *arg,
const char *fmt, ...)
532 n = ppp_vslprintf(buf,
sizeof(buf), fmt, pvar);
536 eol = strchr(buf,
'\n');
538 l = (eol == NULL)? n: eol - buf;
539 if (linep + l < line +
sizeof(line)) {
541 memcpy(linep, buf, l);
547 eol = strchr(p,
'\n');
550 ppp_log_write(llevel, line);
554 while (eol != NULL) {
556 ppp_log_write(llevel, p);
558 eol = strchr(p,
'\n');
574 void ppp_print_string(
const u_char *p,
int len,
void (*printer) (
void *,
const char *, ...),
void *arg) {
578 for (; len > 0; --len) {
580 if (
' ' <= c && c <=
'~') {
581 if (c ==
'\\' || c ==
'"')
583 printer(arg,
"%c", c);
596 printer(arg,
"\\%.3o", (u8_t)c);
607 static void ppp_logit(
int level,
const char *fmt, va_list args) {
610 ppp_vslprintf(buf,
sizeof(buf), fmt, args);
611 ppp_log_write(level, buf);
614 static void ppp_log_write(
int level,
char *buf) {
617 PPPDEBUG(level, (
"%s\n", buf) );
619 if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
622 if (n > 0 && buf[n-1] ==
'\n')
624 if (write(log_to_fd, buf, n) != n
625 || write(log_to_fd,
"\n", 1) != 1)
634 void ppp_fatal(
const char *fmt, ...) {
638 ppp_logit(LOG_ERR, fmt, pvar);
641 LWIP_ASSERT(
"ppp_fatal", 0);
647 void ppp_error(
const char *fmt, ...) {
651 ppp_logit(LOG_ERR, fmt, pvar);
661 void ppp_warn(
const char *fmt, ...) {
665 ppp_logit(LOG_WARNING, fmt, pvar);
672 void ppp_notice(
const char *fmt, ...) {
676 ppp_logit(LOG_NOTICE, fmt, pvar);
683 void ppp_info(
const char *fmt, ...) {
687 ppp_logit(LOG_INFO, fmt, pvar);
694 void ppp_dbglog(
const char *fmt, ...) {
698 ppp_logit(LOG_DEBUG, fmt, pvar);
707 void ppp_dump_packet(ppp_pcb *pcb,
const char *tag,
unsigned char *p,
int len) {
713 proto = (p[0] << 8) + p[1];
714 if (proto < 0xC000 && (proto & ~0x8000) == proto)
720 if (proto == PPP_LCP && pcb->phase == PPP_PHASE_RUNNING && len >= 2 + HEADERLEN) {
721 unsigned char *lcp = p + 2;
722 int l = (lcp[2] << 8) + lcp[3];
724 if ((lcp[0] == ECHOREQ || lcp[0] == ECHOREP)
725 && l >= HEADERLEN && l <= len - 2)
729 ppp_dbglog(
"%s %P", tag, p, len);
740 complete_read(
int fd,
void *buf,
size_t count)
746 for (done = 0; done < count; ) {
747 nb = read(fd, ptr, count - done);
764 #define LOCK_DIR "/var/lock" 767 #define LOCK_DIR "/var/spool/locks" 769 #define LOCK_DIR "/var/spool/lock" 774 static char lock_file[MAXPATHLEN];
786 result = mklock (dev, (
void *) 0);
788 ppp_strlcpy(lock_file, dev,
sizeof(lock_file));
793 ppp_notice(
"Device %s is locked by pid %d", dev, result);
795 ppp_error(
"Can't create lock file %s", lock_file);
800 char lock_buffer[12];
806 if (stat(dev, &sbuf) < 0) {
807 ppp_error(
"Can't get device number for %s: %m", dev);
810 if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
811 ppp_error(
"Can't lock %s: not a character device", dev);
814 ppp_slprintf(lock_file,
sizeof(lock_file),
"%s/LK.%03d.%03d.%03d",
815 LOCK_DIR, major(sbuf.st_dev),
816 major(sbuf.st_rdev), minor(sbuf.st_rdev));
819 char lockdev[MAXPATHLEN];
821 if ((p = strstr(dev,
"dev/")) != NULL) {
823 strncpy(lockdev, dev, MAXPATHLEN-1);
824 lockdev[MAXPATHLEN-1] = 0;
825 while ((p = strrchr(lockdev,
'/')) != NULL) {
830 if ((p = strrchr(dev,
'/')) != NULL)
833 ppp_slprintf(lock_file,
sizeof(lock_file),
"%s/LCK..%s", LOCK_DIR, dev);
836 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
837 if (errno != EEXIST) {
838 ppp_error(
"Can't create lock file %s: %m", lock_file);
843 fd = open(lock_file, O_RDONLY, 0);
847 ppp_error(
"Can't open existing lock file %s: %m", lock_file);
851 n = read(fd, lock_buffer, 11);
853 n = read(fd, &pid,
sizeof(pid));
858 ppp_error(
"Can't read pid from lock file %s", lock_file);
865 pid = atoi(lock_buffer);
870 || (kill(pid, 0) == -1 && errno == ESRCH)) {
871 if (unlink (lock_file) == 0) {
872 ppp_notice(
"Removed stale lock on %s (pid %d)", dev, pid);
875 ppp_warn(
"Couldn't remove stale lock on %s", dev);
877 ppp_notice(
"Device %s is locked by pid %d", dev, pid);
888 ppp_slprintf(lock_buffer,
sizeof(lock_buffer),
"%10d\n", pid);
889 write (fd, lock_buffer, 11);
891 write(fd, &pid,
sizeof (pid));
918 char lock_buffer[12];
920 if (lock_file[0] == 0)
922 fd = open(lock_file, O_WRONLY, 0);
924 ppp_error(
"Couldn't reopen lock file %s: %m", lock_file);
930 ppp_slprintf(lock_buffer,
sizeof(lock_buffer),
"%10d\n", pid);
931 write (fd, lock_buffer, 11);
933 write(fd, &pid,
sizeof(pid));
949 (void) rmlock(lock_file, (
void *) 0);
#define LWIP_UNUSED_ARG(x)