jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Syslog functions. |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003, 2004 Mondru AB. |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 4 | * |
| 5 | * The contents of this file may be used under the terms of the GNU |
| 6 | * General Public License Version 2, provided that the above copyright |
| 7 | * notice and this permission notice is included in all copies or |
| 8 | * substantial portions of the software. |
| 9 | * |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdarg.h> |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <stdio.h> |
| 16 | #include <syslog.h> |
| 17 | #include <string.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | |
| 21 | #include "syserr.h" |
| 22 | |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 23 | static FILE* err_log; |
| 24 | |
| 25 | void sys_err_setlogfile(FILE* log) |
| 26 | { |
| 27 | err_log = log; |
| 28 | } |
| 29 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 30 | void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...) |
| 31 | { |
| 32 | va_list args; |
| 33 | char buf[SYSERR_MSGSIZE]; |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 34 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 35 | va_start(args, fmt); |
| 36 | vsnprintf(buf, SYSERR_MSGSIZE, fmt, args); |
| 37 | va_end(args); |
| 38 | buf[SYSERR_MSGSIZE - 1] = 0; /* Make sure it is null terminated */ |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 39 | if (en) { |
| 40 | if (err_log) |
| 41 | fprintf(err_log, "%s: %d: %d (%s) %s\n", |
| 42 | fn, ln, en, strerror(en), buf); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 43 | syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), |
| 44 | buf); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 45 | } else { |
| 46 | if (err_log) |
| 47 | fprintf(err_log, "%s: %d: %s\n", fn, ln, buf); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 48 | syslog(pri, "%s: %d: %s", fn, ln, buf); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 49 | } |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 53 | void *pack, unsigned len, char *fmt, ...) |
| 54 | { |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 55 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 56 | va_list args; |
| 57 | char buf[SYSERR_MSGSIZE]; |
| 58 | char buf2[SYSERR_MSGSIZE]; |
| 59 | unsigned int n; |
| 60 | int pos; |
| 61 | |
| 62 | va_start(args, fmt); |
| 63 | vsnprintf(buf, SYSERR_MSGSIZE, fmt, args); |
| 64 | va_end(args); |
| 65 | buf[SYSERR_MSGSIZE - 1] = 0; |
| 66 | |
| 67 | snprintf(buf2, SYSERR_MSGSIZE, |
| 68 | "Packet from %s:%u, length: %d, content:", |
| 69 | inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len); |
| 70 | buf2[SYSERR_MSGSIZE - 1] = 0; |
| 71 | pos = strlen(buf2); |
| 72 | for (n = 0; n < len; n++) { |
| 73 | if ((pos + 4) < SYSERR_MSGSIZE) { |
| 74 | sprintf((buf2 + pos), " %02hhx", |
| 75 | ((unsigned char *)pack)[n]); |
| 76 | pos += 3; |
| 77 | } |
| 78 | } |
| 79 | buf2[pos] = 0; |
| 80 | |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 81 | if (en) { |
| 82 | if (err_log) |
| 83 | fprintf(err_log, "%s: %d: %d (%s) %s. %s\n", |
| 84 | fn, ln, en, strerror(en), buf, buf2); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 85 | syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en), |
| 86 | buf, buf2); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 87 | } else { |
| 88 | if (err_log) |
| 89 | fprintf(err_log, "%s: %d: %s. %s\n", fn, ln, buf, buf2); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 90 | syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame^] | 91 | } |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 92 | |
| 93 | } |