File: data.c

package info (click to toggle)
ifstat 1.1-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 364 kB
  • ctags: 261
  • sloc: ansic: 2,364; sh: 341; makefile: 112
file content (154 lines) | stat: -rw-r--r-- 4,045 bytes parent folder | download | duplicates (4)
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
/*
 * ifstat - InterFace STATistics
 * Copyright (c) 2001, Gal Roualland <gael.roualland@dial.oleane.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: data.c,v 1.7 2003/02/07 00:10:37 gael Exp $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if STDC_HEADERS
#include <string.h>
#else
# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
char *strchr (), *strrchr ();
# ifndef HAVE_MEMCPY
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
#  define memmove(d, s, n) bcopy ((s), (d), (n))
# endif
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "ifstat.h"

const char *ifstat_version = VERSION;
int ifstat_quiet = 0;
char *ifstat_progname = "libifstat";

void _ifstat_error(char *format, ...) {
  va_list ap;
  
  fprintf(stderr, "%s: ", ifstat_progname);
  va_start(ap, format);
  vfprintf(stderr, format, ap);
  va_end(ap);
  putc('\n', stderr);
}

void (*ifstat_error) (char *format, ...) = &_ifstat_error;

void ifstat_perror(char *func) {
  ifstat_error("%s: %s", func, strerror(errno));
}

void ifstat_add_interface(struct ifstat_list *ifs, char *ifname, int flags) {
  struct ifstat_data *cur, *last;
  int len;

  /* check interface name */
  if (*ifname == '\0')
    return;
  len = strlen(ifname);
  
  last = NULL;
  for (cur = ifs->first; cur != NULL; cur = cur->next) {
    if (len == cur->namelen &&
	cur->name[0] == ifname[0] &&
	!strncmp(cur->name + 1, ifname + 1, len - 1) &&
	!(flags & IFSTAT_TOTAL) && !(cur->flags & IFSTAT_TOTAL))
      return;
    last = cur;
  }

  if ((cur = calloc(1, sizeof(struct ifstat_data))) == NULL) {
    ifstat_perror("malloc");
    exit(EXIT_FAILURE);
  }
  cur->name = strdup(ifname);
  cur->namelen = len;
  cur->flags = flags;
  if (last != NULL)
    last->next = cur;
  if (ifs->first == NULL)
    ifs->first = cur;
}

void ifstat_free_interface(struct ifstat_data *data) {
  free(data->name);
  free(data);
}

void ifstat_set_interface_stats(struct ifstat_data *data,
				unsigned long long bytesin,
				unsigned long long bytesout) {
  if (data->bout > bytesout || data->bin > bytesin) {
    if (!ifstat_quiet)
      ifstat_error("warning: rollover for interface %s, reinitialising.", data->name);
    data->obout = bytesout;
    data->obin = bytesin;
  } else {
    data->obout = data->bout;
    data->obin = data->bin;
  }
  data->bout = bytesout;
  data->bin = bytesin;
  data->flags |= IFSTAT_HASSTATS;
}

void ifstat_set_interface_index(struct ifstat_data *data,
				int index) {
  data->index = index;
  data->flags |= IFSTAT_HASINDEX;
}

struct ifstat_data *ifstat_get_interface(struct ifstat_list *ifs, char *ifname) {
  struct ifstat_data *ptr;
  int len = strlen(ifname);
  
  for (ptr = ifs->first; ptr != NULL; ptr = ptr->next)
    if (len == ptr->namelen &&
	ptr->name[0] == ifname[0] &&
	!strncmp(ptr->name + 1, ifname + 1, len - 1) &&
	!(ptr->flags & IFSTAT_TOTAL))
      return ptr;
  return NULL;
}

void ifstat_reset_interfaces(struct ifstat_list *ifs) {
  struct ifstat_data *ptr;
  int hasindex = 1;

  for (ptr = ifs->first; ptr != NULL; ptr = ptr->next) {
    if (!(ptr->flags & IFSTAT_HASINDEX))
      hasindex = 0;
    ptr->flags &= ~(IFSTAT_HASSTATS|IFSTAT_HASINDEX);
  }
  if (hasindex)
    ifs->flags |= IFSTAT_HASINDEX;
  else
    ifs->flags &= ~IFSTAT_HASINDEX;
}