summaryrefslogtreecommitdiff
path: root/nDPId-test.c
blob: afe77adf7c8be63c40d085d65f71070ee490e6db (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <unistd.h>

#define NO_MAIN 1
#include "nDPIsrvd.c"
#include "nDPId.c"

enum
{
    PIPE_nDPId = 1,    /* nDPId mock pipefd array index */
    PIPE_nDPIsrvd = 0, /* nDPIsrvd mock pipefd array index */
    PIPE_WRITE = 1,
    PIPE_READ = 0,
    PIPE_COUNT = 2
};

static int epollfd = -1;
static int mock_pipefds[PIPE_COUNT] = {};
static int mock_servfds[PIPE_COUNT] = {};
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;

void mock_syslog_stderr(int p, const char * format, ...)
{
    va_list ap;

    (void)p;
    va_start(ap, format);
    pthread_mutex_lock(&log_mutex);
    vfprintf(stderr, format, ap);
    fprintf(stderr, "%s\n", "");
    pthread_mutex_unlock(&log_mutex);
    va_end(ap);
}

static int setup_pipe(int pipefd[PIPE_COUNT])
{
    if (pipe(pipefd) != 0)
    {
        return -1;
    }

    return 0;
}

static void * nDPIsrvd_mainloop_thread(void * const arg)
{
    (void)arg;
    struct remote_desc * mock_json_desc = NULL;
    struct remote_desc * mock_serv_desc = NULL;

    mock_json_desc = get_unused_remote_descriptor(JSON_SOCK, mock_pipefds[PIPE_nDPIsrvd]);
    if (mock_json_desc == NULL)
    {
        goto error;
    }

    mock_serv_desc = get_unused_remote_descriptor(SERV_SOCK, mock_servfds[PIPE_WRITE]);
    if (mock_serv_desc == NULL)
    {
        goto error;
    }
    strncpy(mock_serv_desc->event_serv.peer_addr, "0.0.0.0", sizeof(mock_serv_desc->event_serv.peer_addr));
    mock_serv_desc->event_serv.peer.sin_port = 0;

    if (add_event(epollfd, mock_pipefds[PIPE_nDPIsrvd], mock_json_desc) != 0)
    {
        goto error;
    }

    if (add_event(epollfd, mock_servfds[PIPE_WRITE], mock_serv_desc) != 0)
    {
        goto error;
    }

    if (mainloop(epollfd) != 0)
    {
        goto error;
    }

    while (handle_incoming_data(epollfd, mock_json_desc) == 0) {}

error:
    close(mock_servfds[PIPE_WRITE]);

    return NULL;
}

static void * distributor_mainloop_thread(void * const arg)
{
    char buf[NETWORK_BUFFER_MAX_SIZE];

    (void)arg;

    int dis_thread_shutdown = 0;
    int dis_epollfd = create_evq();
    int signalfd = setup_signalfd(dis_epollfd);

    struct epoll_event events[32];
    size_t const events_size = sizeof(events) / sizeof(events[0]);

    if (dis_epollfd < 0)
    {
        goto error;
    }
    if (add_event(dis_epollfd, mock_servfds[PIPE_READ], NULL) != 0)
    {
        goto error;
    }
    if (signalfd < 0)
    {
        goto error;
    }

    while (dis_thread_shutdown == 0)
    {
        int nready = epoll_wait(dis_epollfd, events, events_size, -1);

        for (int i = 0; i < nready; i++)
        {
            if ((events[i].events & EPOLLERR) != 0)
            {
                dis_thread_shutdown = 1;
                break;
            }
            if ((events[i].events & EPOLLIN) == 0)
            {
                dis_thread_shutdown = 1;
                break;
            }

            if (events[i].data.fd == mock_servfds[PIPE_READ])
            {
                ssize_t bytes_read = read(mock_servfds[PIPE_READ], buf, sizeof(buf));
                if (bytes_read <= 0)
                {
                    dis_thread_shutdown = 1;
                    break;
                }
                printf("%.*s", (int)bytes_read, buf);
            }
            else if (events[i].data.fd == signalfd)
            {
                struct signalfd_siginfo fdsi;
                ssize_t s;

                s = read(signalfd, &fdsi, sizeof(struct signalfd_siginfo));
                if (s != sizeof(struct signalfd_siginfo))
                {
                    dis_thread_shutdown = 1;
                    break;
                }

                if (fdsi.ssi_signo == SIGINT || fdsi.ssi_signo == SIGTERM || fdsi.ssi_signo == SIGQUIT)
                {
                    dis_thread_shutdown = 1;
                    break;
                }
            }
            else
            {
                dis_thread_shutdown = 1;
                break;
            }
        }
    }
    ssize_t bytes_read;
    while ((bytes_read = read(mock_servfds[PIPE_READ], buf, sizeof(buf))) > 0)
    {
        printf("%.*s", (int)bytes_read, buf);
    }

error:
    del_event(dis_epollfd, signalfd);
    del_event(dis_epollfd, mock_servfds[PIPE_READ]);
    close(dis_epollfd);
    close(signalfd);

    return NULL;
}

static void * nDPId_mainloop_thread(void * const arg)
{
    (void)arg;

    if (setup_reader_threads() != 0)
    {
        exit(EXIT_FAILURE);
    }

    /* Replace nDPId JSON socket fd with the one in our pipe and hope that no socket specific code-path triggered. */
    reader_threads[0].json_sockfd = mock_pipefds[PIPE_nDPId];
    reader_threads[0].json_sock_reconnect = 0;

    run_pcap_loop(&reader_threads[0]);
    free_reader_threads();

    close(mock_pipefds[PIPE_nDPId]);

    return NULL;
}

static void usage(char const * const arg0)
{
    printf("usage: %s [path-to-pcap-file]\n", arg0);
}

int main(int argc, char ** argv)
{
    if (argc != 2)
    {
        usage(argv[0]);
        return -1;
    }

    nDPId_options.reader_thread_count = 1; /* Please do not change this! Generating meaningful pcap diff's relies on a
                                              single reader thread! */
    nDPId_options.instance_alias = strdup("nDPId-test");
    nDPId_options.pcap_file_or_interface = strdup(argv[1]);
    if (validate_options(argv[0]) != 0)
    {
        return -1;
    }

    if (setup_pipe(mock_pipefds) != 0 || setup_pipe(mock_servfds) != 0)
    {
        return -1;
    }

    /* We do not have any sockets, any socket operation must fail! */
    json_sockfd = -1;
    serv_sockfd = -1;

    if (setup_remote_descriptors(2) != 0)
    {
        return -1;
    }

    epollfd = create_evq();
    if (epollfd < 0)
    {
        return -1;
    }

    pthread_t nDPId_thread;
    if (pthread_create(&nDPId_thread, NULL, nDPId_mainloop_thread, NULL) != 0)
    {
        return -1;
    }

    pthread_t nDPIsrvd_thread;
    if (pthread_create(&nDPIsrvd_thread, NULL, nDPIsrvd_mainloop_thread, NULL) != 0)
    {
        return -1;
    }

    pthread_t distributor_thread;
    if (pthread_create(&distributor_thread, NULL, distributor_mainloop_thread, NULL) != 0)
    {
        return -1;
    }

    if (pthread_join(nDPId_thread, NULL) != 0)
    {
        return -1;
    }

    pthread_kill(nDPIsrvd_thread, SIGINT);

    if (pthread_join(nDPIsrvd_thread, NULL) != 0)
    {
        return -1;
    }

    pthread_kill(distributor_thread, SIGINT);

    if (pthread_join(distributor_thread, NULL) != 0)
    {
        return -1;
    }

    return 0;
}