vnstat  2.9
About: vnStat is a console-based network traffic monitor (using the /proc filesystem).
  Fossies Dox: vnstat-2.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

datacache.c
Go to the documentation of this file.
1#include "common.h"
2#include "datacache.h"
3
4int datacache_add(datacache **dc, const char *interface, const short sync)
5{
6 datacache *newdc;
7
8 newdc = malloc(sizeof(datacache));
9 if (newdc == NULL) {
10 return 0;
11 }
12 newdc->next = *dc;
13 *dc = newdc;
14
15 strncpy_nt((*dc)->interface, interface, 32);
16 (*dc)->active = 1;
17 (*dc)->filled = 0;
18 (*dc)->syncneeded = sync;
19 (*dc)->currx = 0;
20 (*dc)->curtx = 0;
21 (*dc)->updated = time(NULL);
22 (*dc)->log = NULL;
23
24 return 1;
25}
26
27int datacache_remove(datacache **dc, const char *interface)
28{
29 int ret = 0;
30 datacache *dc_prev, *dc_head;
31
32 dc_head = *dc;
33 dc_prev = *dc;
34
35 if (*dc == NULL) {
36 return ret;
37 }
38
39 /* handle list head remove */
40 if (strcmp((*dc)->interface, interface) == 0) {
41 *dc = (*dc)->next;
42 xferlog_clear(&dc_prev->log);
43 free(dc_prev);
44 return 1;
45 }
46
47 *dc = (*dc)->next;
48
49 /* handle other locations */
50 while (*dc != NULL) {
51 if (strcmp((*dc)->interface, interface) == 0) {
52 dc_prev->next = (*dc)->next;
53 xferlog_clear(&(*dc)->log);
54 free(*dc);
55 ret = 1;
56 break;
57 }
58 dc_prev = *dc;
59 *dc = (*dc)->next;
60 }
61
62 *dc = dc_head;
63 return ret;
64}
65
67{
68 datacache *dc_prev;
69
70 while (*dc != NULL) {
71 dc_prev = *dc;
72 *dc = (*dc)->next;
73 xferlog_clear(&dc_prev->log);
74 free(dc_prev);
75 }
76}
77
79{
80 int count = 0;
81 datacache *cacheiterator = *dc;
82
83 while (cacheiterator != NULL) {
84 count++;
85 cacheiterator = cacheiterator->next;
86 }
87 return count;
88}
89
91{
92 int count = 0;
93 datacache *cacheiterator = *dc;
94
95 while (cacheiterator != NULL) {
96 if (cacheiterator->active) {
97 count++;
98 }
99 cacheiterator = cacheiterator->next;
100 }
101 return count;
102}
103
105{
106 int i = 1;
107 datacache *cacheiterator = *dc;
108
109 if (cacheiterator == NULL) {
110 printf("cache: empty\n");
111 return;
112 }
113
114 printf("cache: ");
115 while (cacheiterator != NULL) {
116 printf(" %d: \"%s\" (", i, cacheiterator->interface);
117 xferlog_debug(&cacheiterator->log, 0);
118 printf(") ");
119 cacheiterator = cacheiterator->next;
120 i++;
121 }
122 printf("\n");
123}
124
125int xferlog_add(xferlog **log, const time_t timestamp, const uint64_t rx, const uint64_t tx)
126{
127 xferlog *newlog;
128
129 if (*log == NULL || (*log)->timestamp != timestamp) {
130 newlog = malloc(sizeof(xferlog));
131 if (newlog == NULL) {
132 return 0;
133 }
134 newlog->next = *log;
135 *log = newlog;
136
137 newlog->timestamp = timestamp;
138 newlog->rx = 0;
139 newlog->tx = 0;
140 }
141
142 (*log)->rx += rx;
143 (*log)->tx += tx;
144
145 return 1;
146}
147
149{
150 xferlog *log_prev;
151
152 while (*log != NULL) {
153 log_prev = *log;
154 *log = (*log)->next;
155 free(log_prev);
156 }
157}
158
159void xferlog_debug(xferlog **log, const int newline)
160{
161 int i = 1;
162 xferlog *logiterator = *log;
163
164 if (newline && logiterator == NULL) {
165 printf(" xferlog: empty\n");
166 return;
167 }
168
169 if (newline) {
170 printf(" xferlog: ");
171 }
172 while (logiterator != NULL) {
173 printf("%d: %" PRIu64 " - %" PRIu64 " / %" PRIu64 "", i, (uint64_t)logiterator->timestamp, logiterator->rx, logiterator->tx);
174 if (logiterator->next != NULL) {
175 printf(", ");
176 }
177 logiterator = logiterator->next;
178 i++;
179 }
180 if (newline) {
181 printf("\n");
182 }
183}
char * strncpy_nt(char *dest, const char *src, size_t n)
Definition: common.c:253
int datacache_count(datacache **dc)
Definition: datacache.c:78
int xferlog_add(xferlog **log, const time_t timestamp, const uint64_t rx, const uint64_t tx)
Definition: datacache.c:125
void xferlog_debug(xferlog **log, const int newline)
Definition: datacache.c:159
int datacache_activecount(datacache **dc)
Definition: datacache.c:90
void xferlog_clear(xferlog **log)
Definition: datacache.c:148
int datacache_remove(datacache **dc, const char *interface)
Definition: datacache.c:27
void datacache_clear(datacache **dc)
Definition: datacache.c:66
void datacache_debug(datacache **dc)
Definition: datacache.c:104
int datacache_add(datacache **dc, const char *interface, const short sync)
Definition: datacache.c:4
short active
Definition: datacache.h:6
struct xferlog * log
Definition: datacache.h:11
struct datacache * next
Definition: datacache.h:12
char interface[32]
Definition: datacache.h:5
struct xferlog * next
Definition: datacache.h:18
time_t timestamp
Definition: datacache.h:16
uint64_t tx
Definition: datacache.h:17
uint64_t rx
Definition: datacache.h:17