blob: 84189565f157d70818871b8a12b0da576907156a [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>
jjako06e9f122004-01-19 18:37:58 +000013#include <sys/types.h>
jjakoa760e322003-04-11 09:43:22 +000014#include <sys/socket.h>
15#include <netinet/in.h>
16#include <stdio.h>
17#include <syslog.h>
18#include <string.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21
22#include "syserr.h"
23
24
25void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...) {
26 va_list args;
27 char buf[SYSERR_MSGSIZE];
28
29 va_start(args, fmt);
30 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
31 va_end(args);
32 buf[SYSERR_MSGSIZE-1] = 0; /* Make sure it is null terminated */
33 if (en)
34 syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), buf);
35 else
36 syslog(pri, "%s: %d: %s", fn, ln, buf);
37}
38
39void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer,
40 void *pack, unsigned len, char *fmt, ...) {
41
42 va_list args;
43 char buf[SYSERR_MSGSIZE];
44 char buf2[SYSERR_MSGSIZE];
45 int n;
46 int pos;
47
48 va_start(args, fmt);
49 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
50 va_end(args);
51 buf[SYSERR_MSGSIZE-1] = 0;
52
53 snprintf(buf2, SYSERR_MSGSIZE, "Packet from %s:%u, length: %d, content:",
54 inet_ntoa(peer->sin_addr),
55 ntohs(peer->sin_port),
56 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", ((unsigned char*)pack)[n]);
62 pos += 3;
63 }
64 }
65 buf2[pos] = 0;
66
67 if (en)
68 syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en), buf, buf2);
69 else
70 syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2);
71
72}