blob: 048cd45f7e571f19c9c203d9679280c5a8781aa7 [file] [log] [blame]
jjakoa760e322003-04-11 09:43:22 +00001/*
2 * Syslog functions.
jjako0fe0df02004-09-17 11:30:40 +00003 * Copyright (C) 2003, 2004 Mondru AB.
jjakoa760e322003-04-11 09:43:22 +00004 *
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 *
jjakoa760e322003-04-11 09:43:22 +000010 */
11
12#include <stdarg.h>
jjakoa760e322003-04-11 09:43:22 +000013#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
Harald Weltebed35df2011-11-02 13:06:18 +010023void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...)
24{
25 va_list args;
26 char buf[SYSERR_MSGSIZE];
jjakoa760e322003-04-11 09:43:22 +000027
Harald Weltebed35df2011-11-02 13:06:18 +010028 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),
34 buf);
35 else
36 syslog(pri, "%s: %d: %s", fn, ln, buf);
jjakoa760e322003-04-11 09:43:22 +000037}
38
39void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer,
Harald Weltebed35df2011-11-02 13:06:18 +010040 void *pack, unsigned len, char *fmt, ...)
41{
jjakoa760e322003-04-11 09:43:22 +000042
Harald Weltebed35df2011-11-02 13:06:18 +010043 va_list args;
44 char buf[SYSERR_MSGSIZE];
45 char buf2[SYSERR_MSGSIZE];
46 unsigned int n;
47 int pos;
48
49 va_start(args, fmt);
50 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
51 va_end(args);
52 buf[SYSERR_MSGSIZE - 1] = 0;
53
54 snprintf(buf2, SYSERR_MSGSIZE,
55 "Packet from %s:%u, length: %d, content:",
56 inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len);
57 buf2[SYSERR_MSGSIZE - 1] = 0;
58 pos = strlen(buf2);
59 for (n = 0; n < len; n++) {
60 if ((pos + 4) < SYSERR_MSGSIZE) {
61 sprintf((buf2 + pos), " %02hhx",
62 ((unsigned char *)pack)[n]);
63 pos += 3;
64 }
65 }
66 buf2[pos] = 0;
67
68 if (en)
69 syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en),
70 buf, buf2);
71 else
72 syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2);
jjakoa760e322003-04-11 09:43:22 +000073
74}