File: netbsd.c

package info (click to toggle)
xfce4-netload-plugin 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,656 kB
  • sloc: sh: 4,348; ansic: 2,598; makefile: 111
file content (171 lines) | stat: -rw-r--r-- 4,941 bytes parent folder | download | duplicates (14)
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
/*****************************************************************************
 *
 * init_osspecific()
 *
 * Init function
 *
 ****************************************************************************/

void init_osspecific(netdata* data)
{
    data->mib_name1[0] = CTL_NET;
    data->mib_name1[1] = PF_ROUTE;
    data->mib_name1[2] = 0;
    data->mib_name1[3] = 0;
    data->mib_name1[4] = NET_RT_IFLIST;
    data->mib_name1[5] = 0;
    
    data->mib_name2[0] = CTL_NET;
    data->mib_name2[1] = PF_ROUTE;
    data->mib_name2[2] = 0;
    data->mib_name2[3] = 0;
    data->mib_name2[4] = NET_RT_IFLIST;
    data->mib_name2[5] = 0;
    
#ifdef DEBUG
    fprintf( stderr, "The netload plugin was initialized for NetBSD.\n" );
#endif
}


/*****************************************************************************
 *
 * checkinterface()
 *
 * check if a given interface exists, return TRUE if it does and FALSE if not
 *
 ****************************************************************************/
int checkinterface(netdata* data)
{
    int validinterface = FALSE;
    char *lim, *next;
    struct if_msghdr *ifm, *nextifm;
    struct sockaddr_dl *sdl;
    size_t needed;
    char s[32];

    if (sysctl(data->mib_name1, 6, NULL, &needed, NULL, 0) < 0)
        return FALSE;
    if (data->alloc1 < (signed long) needed)
    {   
        if (data->buf1 != NULL)
            free (data->buf1);
        data->buf1 = malloc(needed);
        if (data->buf1 == NULL)
            return FALSE;
        data->alloc1 = needed;
        }

    if (sysctl(data->mib_name1, 6, data->buf1, &needed, NULL, 0) < 0)
        return FALSE;
    lim = data->buf1 + needed;
    next = data->buf1;
    while ((next < lim) && (validinterface == 0))
    {   
        ifm = (struct if_msghdr *)next;
        if (ifm->ifm_type != RTM_IFINFO)
            return FALSE;
        next += ifm->ifm_msglen;

        while (next < lim)
        {   
            nextifm = (struct if_msghdr *)next;
            if (nextifm->ifm_type != RTM_NEWADDR)
                break;
            next += nextifm->ifm_msglen;
        }

        if (ifm->ifm_flags & IFF_UP)
        {   
            sdl = (struct sockaddr_dl *)(ifm + 1);
            strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
            s[sdl->sdl_nlen] = '\0';
            /* search for the right network interface */
            if (sdl->sdl_family != AF_LINK)
                continue;
            if (strcmp(s, data->ifdata.if_name) != 0)
                continue;
            else
            {
                validinterface = TRUE;
                break; /* stop searching */
            }
        }
    }
    return validinterface;
}
/******************************************************************************
 *
 * get_stat()
 *
 * this code is based on gkrellm code (thanks guys!)
 *
 ****************************************************************************/

int get_stat(netdata* data)
{
    char *lim, *next;
    struct if_msghdr *ifm, *nextifm;
    struct sockaddr_dl *sdl;
    char s[32];
    size_t needed;
    unsigned long rx_o, tx_o;

    if (sysctl(data->mib_name2, 6, NULL, &needed, NULL, 0) < 0)
        return 1;
    if (data->alloc2 < (signed long) needed)
    {
        if (data->buf2 != NULL)
            free (data->buf2);
        data->buf2 = malloc(needed);
        if (data->buf2 == NULL)
            return 1;
        data->alloc2 = needed;
        }

    if (sysctl(data->mib_name2, 6, data->buf2, &needed, NULL, 0) < 0)
        return 1;
    lim = data->buf2 + needed;
    next = data->buf2;
    while (next < lim)
    {
        ifm = (struct if_msghdr *)next;
        if (ifm->ifm_type != RTM_IFINFO)
            return 1;
        next += ifm->ifm_msglen;

        while (next < lim)
        {
            nextifm = (struct if_msghdr *)next;
            if (nextifm->ifm_type != RTM_NEWADDR)
                break;
            next += nextifm->ifm_msglen;
        }

        if (ifm->ifm_flags & IFF_UP)
        {
            sdl = (struct sockaddr_dl *)(ifm + 1);
            strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
            s[sdl->sdl_nlen] = '\0';

            /* search for the right network interface */
            if (strcmp(s, data->ifdata.if_name) != 0)
                continue;

            rx_o = data->stats.rx_bytes; tx_o = data->stats.tx_bytes;
            /* write stats */
            data->stats.tx_packets = ifm->ifm_data.ifi_opackets;
            data->stats.rx_packets = ifm->ifm_data.ifi_ipackets;
            data->stats.rx_bytes = ifm->ifm_data.ifi_ibytes;
            data->stats.tx_bytes = ifm->ifm_data.ifi_obytes;
            data->stats.rx_errors = ifm->ifm_data.ifi_ierrors;
            data->stats.tx_errors = ifm->ifm_data.ifi_oerrors;

            if (rx_o > data->stats.rx_bytes)
                data->stats.rx_over++;
            if (tx_o > data->stats.tx_bytes)
                data->stats.tx_over++;
        }
    }
    return 0;
}