blob: 7223414a7ea9c2ce67d9ea47f6b8733cd1ab4481 [file] [log] [blame]
jjakoa760e322003-04-11 09:43:22 +00001/*
2 * Syslog functions.
3 * Copyright (C) 2003 Mondru AB.
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 *
10 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
12 *
13 * Contributor(s):
14 *
15 */
16
17#include <stdarg.h>
jjako06e9f122004-01-19 18:37:58 +000018#include <sys/types.h>
jjakoa760e322003-04-11 09:43:22 +000019#include <sys/socket.h>
20#include <netinet/in.h>
21#include <stdio.h>
22#include <syslog.h>
23#include <string.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26
27#include "syserr.h"
28
29
30void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...) {
31 va_list args;
32 char buf[SYSERR_MSGSIZE];
33
34 va_start(args, fmt);
35 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
36 va_end(args);
37 buf[SYSERR_MSGSIZE-1] = 0; /* Make sure it is null terminated */
38 if (en)
39 syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), buf);
40 else
41 syslog(pri, "%s: %d: %s", fn, ln, buf);
42}
43
44void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer,
45 void *pack, unsigned len, char *fmt, ...) {
46
47 va_list args;
48 char buf[SYSERR_MSGSIZE];
49 char buf2[SYSERR_MSGSIZE];
50 int n;
51 int pos;
52
53 va_start(args, fmt);
54 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
55 va_end(args);
56 buf[SYSERR_MSGSIZE-1] = 0;
57
58 snprintf(buf2, SYSERR_MSGSIZE, "Packet from %s:%u, length: %d, content:",
59 inet_ntoa(peer->sin_addr),
60 ntohs(peer->sin_port),
61 len);
62 buf2[SYSERR_MSGSIZE-1] = 0;
63 pos = strlen(buf2);
64 for(n=0; n<len; n++) {
65 if ((pos+4)<SYSERR_MSGSIZE) {
66 sprintf((buf2+pos), " %02hhx", ((unsigned char*)pack)[n]);
67 pos += 3;
68 }
69 }
70 buf2[pos] = 0;
71
72 if (en)
73 syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en), buf, buf2);
74 else
75 syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2);
76
77}