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