-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.c
186 lines (156 loc) · 4.24 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
#define MAX_UNIX_FDS 32
#ifdef ENABLE_TITAN
extern void mncc_dissector(int fd, bool is_out, const char *fn, const uint8_t *data, unsigned int len);
extern void pcu_dissector(int fd, bool is_out, const char *fn, const uint8_t *data, unsigned int len);
#endif
/***********************************************************************
* Utility functions
***********************************************************************/
/* taken from libosmocore */
static char hexd_buff[4096];
static const char hex_chars[] = "0123456789abcdef";
static char *hexdump(const unsigned char *buf, int len, char *delim)
{
int i;
char *cur = hexd_buff;
hexd_buff[0] = 0;
for (i = 0; i < len; i++) {
const char *delimp = delim;
int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
if (len_remain < 3)
break;
*cur++ = hex_chars[buf[i] >> 4];
*cur++ = hex_chars[buf[i] & 0xf];
while (len_remain > 1 && *delimp) {
*cur++ = *delimp++;
len_remain--;
}
*cur = 0;
}
hexd_buff[sizeof(hexd_buff)-1] = 0;
return hexd_buff;
}
static void default_dissector(int fd, bool is_out, const char *fn, const uint8_t *data, unsigned int len)
{
fprintf(stderr, "%d %s %c %s\n", fd, fn, is_out ? 'W' : 'R', hexdump(data, len, ""));
}
static struct sock_state unix_fds[MAX_UNIX_FDS];
__attribute__ ((constructor)) static void udtrace_init(void)
{
int i;
LOG("Unix Domain Socket Trace initialized (TITAN support "
#ifdef ENABLE_TITAN
"enabled"
#else
"DISABLED"
#endif
")\n");
for (i = 0; i < ARRAY_SIZE(unix_fds); i++) {
unix_fds[i] = (struct sock_state) { -1, NULL, NULL };
}
}
/* add a file descriptor from the list of to-be-traced ones */
void udtrace_add_fd(int fd)
{
struct sock_state *ss;
/* Find an unused state in unix_fds */
ss = udtrace_sstate_by_fd(-1);
if (!ss) {
LOG("Couldn't add UNIX FD %d (no space in unix_fds)\n", fd);
return;
}
LOG("Adding FD %d\n", fd);
ss->fd = fd;
}
/* add a file descriptor from the list of to-be-traced ones */
void udtrace_add_fd_child(int pfd, int cfd)
{
struct sock_state *pss, *css;
/* Find the parent socket state first */
pss = udtrace_sstate_by_fd(pfd);
if (!pss) {
LOG("Couldn't find parent UNIX FD %d for %d\n", pfd, cfd);
return;
}
/* Find an unused state in unix_fds */
css = udtrace_sstate_by_fd(-1);
if (!css) {
LOG("Couldn't add UNIX FD %d (no space in unix_fds)\n", cfd);
return;
}
LOG("Adding FD %d as a child of %d\n", cfd, pfd);
css->dissector = pss->dissector;
css->path = strdup(pss->path);
css->fd = cfd;
}
/* delete a file descriptor from the list of to-be-traced ones */
void udtrace_del_fd(int fd)
{
struct sock_state *ss;
/* Find the corresponding state in unix_fds */
ss = udtrace_sstate_by_fd(fd);
if (!ss) {
LOG("Couldn't delete UNIX FD %d (no such FD in unix_fds)\n", fd);
return;
}
LOG("Removing FD %d\n", fd);
free((void *) ss->path);
*ss = (struct sock_state) { -1, NULL, NULL };
}
static void udtrace_resolve_dissector(struct sock_state *ss)
{
/* actual useful dissectors resovled by path */
if (0) { }
#ifdef ENABLE_TITAN
else if (strstr(ss->path, "mncc"))
ss->dissector = mncc_dissector;
else if (strstr(ss->path, "pcu"))
ss->dissector = pcu_dissector;
#endif
else
ss->dissector = &default_dissector;
}
/* set the path of a given fd */
void udtrace_fd_set_path(int fd, const char *path)
{
struct sock_state *ss;
/* Find the corresponding state in unix_fds */
ss = udtrace_sstate_by_fd(fd);
if (!ss) {
LOG("Couldn't set path for UNIX FD %d (no such FD in unix_fds)\n", fd);
return;
}
ss->path = strdup(path);
udtrace_resolve_dissector(ss);
}
struct sock_state *udtrace_sstate_by_fd(int fd)
{
int i;
for (i = 0; i < ARRAY_SIZE(unix_fds); i++) {
if (unix_fds[i].fd == fd)
return &unix_fds[i];
}
return NULL;
}
/* is the given file descriptor part of the to-be-traced unix domain fd's? */
bool is_unix_socket(int fd)
{
if (udtrace_sstate_by_fd(fd))
return true;
else
return false;
}
void udtrace_data(int fd, bool is_out, const char *fn, const uint8_t *data, unsigned int len)
{
struct sock_state *ss = udtrace_sstate_by_fd(fd);
if (!data || !len || !ss)
return;
ss->dissector(fd, is_out, fn, data, len);
}