summaryrefslogtreecommitdiff
path: root/src/devreader-bsd.cpp
blob: b542704123d8ee5f497fc27f09c9cca0c21435e1 (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
/***************************************************************************
 *                                                                         *
 *   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.                                   * 
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * Free/Net/OpenBSD specific network code taken out of gkrellm:            *
 * Copyright by Bill Wilson <bill@gkrellm.net>                             *
 * FreeBSD code by Hajimu Umemoto <ume@mahoroba.org>                       *
 * NetBSD code by Anthony Mallet <anthony.mallet@useless-ficus.net>        *
 * Hajimu Umemoto merged Free/Net/OpenBSD code                             *
 * adapted for nload by Roland Riegel <feedback@roland-riegel.de>          *
 ***************************************************************************/

#include "devreader-bsd.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <net/if_dl.h>
#include <net/route.h>

#include <string>
#include <list>

using namespace std;

DevReaderBsd::DevReaderBsd(const string& deviceName)
    : DevReader(deviceName)
{
}

DevReaderBsd::~DevReaderBsd()
{
}

list<string> DevReaderBsd::findAllDevices()
{
    list<string> interfaceNames;

    struct if_msghdr *ifm, *nextifm;
    struct sockaddr_dl *sdl;
    char *lim, *next;
    size_t needed;
    int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
    char *buf = 0;
    
    if(sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
        return interfaceNames;
            
    buf = (char *) malloc(needed);
    if(buf == NULL)
        return interfaceNames;
            
    if(sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
        return interfaceNames;
            
    lim = buf + needed;
            
    next = buf;
    while(next < lim)
    {
        ifm = (struct if_msghdr *) next;
        if(ifm->ifm_type != RTM_IFINFO)
            break;
                    
        next += ifm->ifm_msglen;
                    
        while(next < lim)
        {
            nextifm = (struct if_msghdr *) next;
            if(nextifm->ifm_type != RTM_NEWADDR)
                break;
            next += nextifm->ifm_msglen;
        }
                    
        sdl = (struct sockaddr_dl *) (ifm + 1);
        if(sdl->sdl_family != AF_LINK)
            continue;
        
        interfaceNames.push_back(string(sdl->sdl_data));
    }

    free(buf);

    return interfaceNames;
}

void DevReaderBsd::readFromDevice(DataFrame& dataFrame)
{
    struct if_msghdr *ifm, *nextifm;
    struct sockaddr_dl *sdl;
    char *lim, *next;
    size_t needed;
    int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
    char *buf = 0;
    
    if(m_deviceName.empty())
        return;
	
    do
    {
        if(sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
            break;
		
        buf = (char *) malloc(needed);
        if(buf == NULL)
            break;
		
        if(sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
            break;
		
        lim = buf + needed;
		
        next = buf;
        while(next < lim)
        {
            ifm = (struct if_msghdr *) next;
            if(ifm->ifm_type != RTM_IFINFO)
                break;
			
            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);
                if(sdl->sdl_family != AF_LINK)
                    continue;
                    
                if(strncmp(m_deviceName.c_str(), sdl->sdl_data, sdl->sdl_nlen) == 0)
                {
                    dataFrame.setTotalDataIn(ifm->ifm_data.ifi_ibytes);
                    dataFrame.setTotalDataOut(ifm->ifm_data.ifi_obytes);

                    dataFrame.setTotalPacketsIn(ifm->ifm_data.ifi_ipackets);
                    dataFrame.setTotalPacketsOut(ifm->ifm_data.ifi_opackets);

                    dataFrame.setTotalErrorsIn(ifm->ifm_data.ifi_ierrors);
                    dataFrame.setTotalErrorsOut(ifm->ifm_data.ifi_oerrors);

                    dataFrame.setTotalDropsIn(0); // ifi_iqdrops ?
                    dataFrame.setTotalDropsOut(0);

                    dataFrame.setValid(true);

                    break;
                }
            }
        }
    } while(0);
    
    free(buf);
}