summaryrefslogtreecommitdiffstats
path: root/datatop/src/datatop_ip_table_poll.c
blob: 980eb4c0fcb88c7933fc9dfc91c8868b66949033 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/************************************************************************
Copyright (c) 2016, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************/

/**
 * @file datatop_ip_table_poll.c
 * @brief Adds ability for TP Tables, Rules and Routes data collection
          Unlike other polls, this is intended for running as a separate
          thread as it can cause delays of > 3sec per poll
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include "datatop_interface.h"
#include "datatop_fileops.h"
#include "datatop_str.h"
#include "datatop_polling.h"

#define DTOP_IPTRR_POLL_PERIOD  5.00

/**
* @struct dtop_ip_table_vars
* @brief Struct used to hold necessary variables for /proc/stat dpg
*
* @var dtop_ip_table_vars::line
* Array of strings where necessary dp names and values are held.
* @var dtop_ip_table_vars::line_count
* Number of lines the file is that the dpg represents.
*/
struct dtop_ip_table_vars {
	char *out_dir;
}dtop_ip_table_storage;

struct dtop_linked_list *ip_dpg_list = NULL;

pthread_mutex_t dtop_ip_table_lock;

/**
 * @brief Perform IP table command and store it in a file
 *
 * @param dpg Struct that polled data is added to.
 * @return DTOP_POLL_IO_ERR - Poll of dpg unsuccessful.
 * @return DTOP_POLL_OK - Poll of dpg successful.
 */
int dtop_ip_table_poll(struct dtop_data_point_gatherer *dpg)
{
  FILE *fd;
  FILE *fo = (FILE *)dpg->file;
  char buf[1001];

  time_t rawtime;
  struct tm * timeinfo;

  if(fo == NULL)
  {
    fprintf(stderr, "Could not fopen: %s\n", dpg->file);
	  return DTOP_POLL_IO_ERR;
  }

  time ( &rawtime );
  timeinfo = gmtime ( &rawtime );

  fprintf ( fo, "============\nStart: %s==========\n", asctime (timeinfo) );
  fflush(fo);

  /* redirect stderr to output file */
  dup2(fileno(fo), 2);

  fd = popen((char *)dpg->priv, "r");
  if(fd == NULL)
  {
    fprintf(stderr, "Could not popen: %s\n", (char *)dpg->priv);
	  return DTOP_POLL_IO_ERR;
  }

  while(fgets(buf, 1000, fd) != NULL)
  {
    fputs(buf, fo);
  }

  fprintf ( fo, "============\nEnd: %s==========\n\n", asctime (timeinfo) );
  fflush(fo);
  pclose(fd);
	return DTOP_POLL_OK;
}

/**
 * @brief Frees dynamically allocated IP table dpg.
 *
 * Frees the memory of the dpg along with it's data_points
 * and other malloc'd memory no longer needed.
 *
 * @param dpg Dpg to deconstruct and deallocate memory for.
 */
static void dtop_ip_table_dpg_deconstructor
			(struct dtop_data_point_gatherer *dpset)
{
	int i;
	free(dpset->prefix);
  if(dpset->file)
  {
     fclose((FILE *)dpset->file);
  }

	free(dpset);
}

/**
 * @brief Registers a new IP table dpg to the list.
 *
 * @param dpg Dpg to construct and allocate memory for.
 */
void dtop_ip_table_register(struct dtop_data_point_gatherer *dpg)
{
  if (dpg)
    ip_dpg_list = dtop_add_linked_list(dpg, ip_dpg_list);
}

/**
 * @brief Open the files for writing the output for each dpg.
 *
 * @param None
 */
int dtop_ip_table_init_files()
{
  struct dtop_data_point_gatherer *dpset;
  struct dtop_linked_list *curr_ptr = ip_dpg_list;
  FILE *fd;

  while(curr_ptr)
  {
    dpset = (struct dtop_data_point_gatherer *) curr_ptr->data;
    fd = fopen(dpset->prefix, "a+");
    if(!fd)
    {
      fprintf(stderr, "Could not fopen: %s\n", dpset->prefix);
      return DTOP_POLL_IO_ERR;
    }
    dpset->file = (char *)fd;
    curr_ptr = curr_ptr->next_ptr;
  }
  return DTOP_POLL_OK;
}

/**
 * @brief Perform cleanup of IP table dgp list at exit.
 *
 * @param None
 */
void dtop_ip_table_poll_cleanup()
{
  struct dtop_data_point_gatherer *dpset;
  struct dtop_linked_list *curr_ptr = ip_dpg_list;

  pthread_mutex_lock(&dtop_ip_table_lock);
  deconstruct_dpgs(ip_dpg_list);
  dtop_rem_linked_list(ip_dpg_list);
  pthread_mutex_unlock(&dtop_ip_table_lock);

}

/**
 * @brief The thread to poll for IP table data.
 *
 * @param arg ptr
 */
void *dtop_ip_table_start_poll(void *arg)
{
  time_t start_t, curr_t;
  double diff_t = 9999999.00; /* some high # > DTOP_IPTRR_POLL_PERIOD */
  int ret = DTOP_POLL_OK;

  if (pthread_mutex_init(&dtop_ip_table_lock, NULL) != 0)
  {
      printf("\n mutex init failed\n");
      return NULL;
  }

  atexit(dtop_ip_table_poll_cleanup);

  if(DTOP_POLL_OK != ( ret = dtop_ip_table_init_files()))
  {
    return NULL;
  }

  while(1)
  {
    struct dtop_linked_list *curr_ptr = ip_dpg_list;
    struct dtop_data_point_gatherer *dpset;

    pthread_mutex_lock(&dtop_ip_table_lock);

    if (diff_t >= DTOP_IPTRR_POLL_PERIOD)
    {
      printf("Poll for IP Tables, Rules & Routes\n");
      time(&start_t);
      while (curr_ptr)
      {
        dpset = (struct dtop_data_point_gatherer *) curr_ptr->data;
        dpset->poll(dpset);
        curr_ptr = curr_ptr->next_ptr;
      }
    }
    pthread_mutex_unlock(&dtop_ip_table_lock);

    /* sleep for 500 milliseconds */
    usleep(500 * 1000);
    time(&curr_t);
    diff_t = difftime(curr_t, start_t);
  }
  return NULL;
}

/**
 * @brief Creates a dpg for ip table command
 *
 * Dynamically allocates memory for dpg which is then added to a linked list
 * via the dtop_register(dpg) function call.
 *
 * @param data_points dtop_data_point struct that dpg points to.
 * @param storage dtop_ip_table_vars struct that holds relevant dpg variables.
 */
/*static void construct_ip_table_dpg(struct dtop_data_point
		*data_points, struct dtop_ip_table_vars *command, int dp_count)
*/
static void construct_ip_table_dpg(char *command)
{
	struct dtop_data_point_gatherer *dpg = malloc
		(sizeof(struct dtop_data_point_gatherer));
  char *file_name = (char *)malloc(strlen(command)+ 1 + 1 + strlen(dtop_ip_table_storage.out_dir) + 4);
  int i, fname_start_ind;

  strcpy(file_name, dtop_ip_table_storage.out_dir);
  strcat(file_name, "/");

  fname_start_ind = strlen(file_name);
  strcat(file_name, command);
  strcat(file_name, ".txt");

  for(i=fname_start_ind; file_name[i]; i++)
  {
    if(file_name[i] == ' ')
      file_name[i] = '_';
    if(file_name[i] == '/')
      file_name[i] = '-';
  }

	dpg->prefix = file_name;
	dpg->poll = dtop_ip_table_poll;
	dpg->priv = (char *)command;
  dpg->file = NULL;
	dpg->deconstruct = dtop_ip_table_dpg_deconstructor;

	dtop_ip_table_register(dpg);
}

/*
 * @brief Scans "/proc/stat" in order to autodetect dps.
 *
 * Searches through "/proc/stat" file for all available data
 * points to create as dp structs.
 *
 * @param storage dtop_ip_table_vars struct where relevant variables are stored.
 */

/**
 * @brief Calls dtop_search for "/proc/stat" file.
 */
void dtop_ip_table_init(char *out_dir)
{
	dtop_ip_table_storage.out_dir = out_dir;
  construct_ip_table_dpg("ip xfrm state show");
  construct_ip_table_dpg("ip xfrm policy show");
  construct_ip_table_dpg("ip addr");
  construct_ip_table_dpg("iptables -t raw -L -n -v");
  construct_ip_table_dpg("iptables -t mangle -L -n -v");
  construct_ip_table_dpg("iptables -L -n -v");
  construct_ip_table_dpg("iptables -t nat -L -n -v");
  construct_ip_table_dpg("ip6tables -t raw -L -n -v");
  construct_ip_table_dpg("ip6tables -t mangle -L -n -v");
  construct_ip_table_dpg("ip6tables -L -n -v");
  construct_ip_table_dpg("ip6tables -t nat -L -n -v");
  construct_ip_table_dpg("ip rule show");
  construct_ip_table_dpg("ip -6 rule show");
  construct_ip_table_dpg("ip route show table all");
  construct_ip_table_dpg("ip -6 route show table all");
  construct_ip_table_dpg("ip route show table rmnet_data0");
  construct_ip_table_dpg("ip route show table rmnet_data1");
  construct_ip_table_dpg("ip route show table rmnet_data2");
  construct_ip_table_dpg("ip route show table rmnet_data6");
  construct_ip_table_dpg("ip route show table rmnet_data7");
  construct_ip_table_dpg("ip route show table r_rmnet_data0");
  construct_ip_table_dpg("ip route show table r_rmnet_data1");
  construct_ip_table_dpg("ip route show table r_rmnet_data2");
  construct_ip_table_dpg("ip route show table r_rmnet_data6");
  construct_ip_table_dpg("ip route show table r_rmnet_data7");
  construct_ip_table_dpg("ip -6 route show table rmnet_data0");
  construct_ip_table_dpg("ip -6 route show table rmnet_data1");
  construct_ip_table_dpg("ip -6 route show table rmnet_data2");
  construct_ip_table_dpg("ip -6 route show table rmnet_data6");
  construct_ip_table_dpg("ip -6 route show table rmnet_data7");
  construct_ip_table_dpg("ip -6 route show table r_rmnet_data0");
  construct_ip_table_dpg("ip -6 route show table r_rmnet_data1");
  construct_ip_table_dpg("ip -6 route show table r_rmnet_data2");
  construct_ip_table_dpg("ip -6 route show table r_rmnet_data6");
  construct_ip_table_dpg("ip -6 route show table r_rmnet_data7");
  construct_ip_table_dpg("ip route show table wlan0");
  construct_ip_table_dpg("ip -6 route show table wlan0");
  construct_ip_table_dpg("ip route show table dummy0");
  construct_ip_table_dpg("ip -6 route show table dummy0");
  construct_ip_table_dpg("cat /proc/net/xfrm_stat");
  construct_ip_table_dpg("cat /proc/sys/net/ipv4/ip_forward");
  construct_ip_table_dpg("cat /proc/sys/net/ipv6/conf/all/forwarding");

  printf("Poll for IP Tables, Rules & Routes every 5 seconds\n");
}