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 | |
| 23 | |
| 24 | void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...) { |
| 25 | va_list args; |
| 26 | char buf[SYSERR_MSGSIZE]; |
| 27 | |
| 28 | va_start(args, fmt); |
| 29 | vsnprintf(buf, SYSERR_MSGSIZE, fmt, args); |
| 30 | va_end(args); |
| 31 | buf[SYSERR_MSGSIZE-1] = 0; /* Make sure it is null terminated */ |
| 32 | if (en) |
| 33 | syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), buf); |
| 34 | else |
| 35 | syslog(pri, "%s: %d: %s", fn, ln, buf); |
| 36 | } |
| 37 | |
| 38 | void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer, |
| 39 | void *pack, unsigned len, char *fmt, ...) { |
| 40 | |
| 41 | va_list args; |
| 42 | char buf[SYSERR_MSGSIZE]; |
| 43 | char buf2[SYSERR_MSGSIZE]; |
Harald Welte | f54a1f4 | 2010-05-04 11:08:38 +0200 | [diff] [blame^] | 44 | unsigned int n; |
jjako | a760e32 | 2003-04-11 09:43:22 +0000 | [diff] [blame] | 45 | int pos; |
| 46 | |
| 47 | va_start(args, fmt); |
| 48 | vsnprintf(buf, SYSERR_MSGSIZE, fmt, args); |
| 49 | va_end(args); |
| 50 | buf[SYSERR_MSGSIZE-1] = 0; |
| 51 | |
| 52 | snprintf(buf2, SYSERR_MSGSIZE, "Packet from %s:%u, length: %d, content:", |
| 53 | inet_ntoa(peer->sin_addr), |
| 54 | ntohs(peer->sin_port), |
| 55 | len); |
| 56 | buf2[SYSERR_MSGSIZE-1] = 0; |
| 57 | pos = strlen(buf2); |
| 58 | for(n=0; n<len; n++) { |
| 59 | if ((pos+4)<SYSERR_MSGSIZE) { |
| 60 | sprintf((buf2+pos), " %02hhx", ((unsigned char*)pack)[n]); |
| 61 | pos += 3; |
| 62 | } |
| 63 | } |
| 64 | buf2[pos] = 0; |
| 65 | |
| 66 | if (en) |
| 67 | syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en), buf, buf2); |
| 68 | else |
| 69 | syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2); |
| 70 | |
| 71 | } |