comparison sip-in/sip_log.c @ 47:62f39c7cee15

themwi-sip-in skeleton started
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 06 Sep 2022 20:33:56 -0800
parents
children 47976db01894
comparison
equal deleted inserted replaced
46:5427b26525cd 47:62f39c7cee15
1 /*
2 * In this module we implement debug logging of SIP Rx & Tx messages.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14
15 struct timeval cur_event_time;
16
17 static FILE *logfile;
18
19 open_sip_log_file(filename)
20 char *filename;
21 {
22 logfile = fopen(filename, "a");
23 if (!logfile) {
24 perror(filename);
25 return(-1);
26 }
27 return(0);
28 }
29
30 static void
31 log_common(msg, msglen, sin, dir)
32 char *msg, *dir;
33 unsigned msglen;
34 struct sockaddr_in *sin;
35 {
36 unsigned sec, ms;
37
38 sec = cur_event_time.tv_sec % 86400;
39 ms = cur_event_time.tv_usec / 1000;
40 fprintf(logfile, "Msg %s %s:%u %u bytes %02u:%02u:%02u.%03u\n", dir,
41 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), msglen,
42 sec / 3600, (sec / 60) % 60, sec % 60, ms);
43 fwrite(msg, 1, msglen, logfile);
44 putc('\n', logfile);
45 fflush(logfile);
46 }
47
48 void
49 log_sip_msg_rx(msg, msglen, sin)
50 char *msg;
51 unsigned msglen;
52 struct sockaddr_in *sin;
53 {
54 if (!logfile)
55 return;
56 log_common(msg, msglen, sin, "from");
57 }
58
59 void
60 log_sip_msg_tx(msg, msglen, sin)
61 char *msg;
62 unsigned msglen;
63 struct sockaddr_in *sin;
64 {
65 if (!logfile)
66 return;
67 log_common(msg, msglen, sin, "to");
68 }