~ubuntu-branches/ubuntu/trusty/netams/trusty

« back to all changes in this revision

Viewing changes to src/flow.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander GQ Gerasiov
  • Date: 2009-11-26 02:08:19 UTC
  • Revision ID: james.westby@ubuntu.com-20091126020819-rpmeddg4y237ofxv
Tags: upstream-3.4.3+dfsg1
ImportĀ upstreamĀ versionĀ 3.4.3+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
***     Authentication, authorization, accounting + firewalling package
 
3
***     Copyright 1998-2002 Anton Vinokurov <anton@netams.com>
 
4
***     Copyright 2002-2008 NeTAMS Development Team
 
5
***     This code is GPL v3
 
6
***     For latest version and more info, visit this project web page
 
7
***     located at http://www.netams.com
 
8
***
 
9
*************************************************************************/
 
10
/* $Id: flow.c,v 1.130 2008-02-23 08:35:02 anton Exp $ */
 
11
 
 
12
#include "netams.h"
 
13
#include "flow.h"
 
14
 
 
15
Flow::Flow() {
 
16
        init();
 
17
}
 
18
 
 
19
Flow::Flow(u_char instance, const u_char slots, const struct attribute *new_list) {
 
20
        init();
 
21
        ds_id = instance;       
 
22
   
 
23
        if((list = (struct attribute *)aMalloc(sizeof(struct attribute)*slots)) == NULL)
 
24
                return;
 
25
        if(new_list != NULL) {
 
26
                bcopy(new_list, list, sizeof(struct attribute)*slots);
 
27
                freeSlot = totalSlots = slots;
 
28
        } else
 
29
                totalSlots = slots;
 
30
}
 
31
 
 
32
void Flow::init() {
 
33
        list    = NULL;
 
34
        freeSlot        = 0;
 
35
        totalSlots      = 0;
 
36
}
 
37
 
 
38
Flow::~Flow() {
 
39
        removeAll();
 
40
}
 
41
 
 
42
const int Flow::grow_list(u_char new_size) {
 
43
        struct attribute        *new_list;
 
44
 
 
45
        if(new_size==0)
 
46
                new_size = totalSlots+1;
 
47
 
 
48
        new_list = (struct attribute *)aMalloc(sizeof(struct attribute)*new_size);
 
49
        
 
50
        if(new_list == NULL)
 
51
                return 0;
 
52
 
 
53
        if(list != NULL) {
 
54
                bcopy(list, new_list, sizeof(struct attribute)*totalSlots);
 
55
                aFree(list);
 
56
        }
 
57
        aDebug(DEBUG_FLOW, "Flow %p growth from %u to %u slots\n", this, totalSlots, new_size);
 
58
        totalSlots = new_size;
 
59
        list = new_list;
 
60
 
 
61
        return 1;
 
62
}
 
63
 
 
64
void* Flow::put(const u_char id, const union attribute_value *value) {
 
65
        struct attribute        *tmp;
 
66
 
 
67
 
 
68
        if((list == NULL) && !grow_list())
 
69
                return NULL;
 
70
 
 
71
        if((freeSlot == totalSlots) && !grow_list())
 
72
                return NULL;
 
73
 
 
74
        tmp = &list[freeSlot++];
 
75
        tmp->id                 = id;
 
76
        if(value) 
 
77
                tmp->value = *value;
 
78
        
 
79
        return (void*)&tmp->value;
 
80
}
 
81
 
 
82
const union attribute_value *Flow::get(const u_char id) {
 
83
        int             i;
 
84
 
 
85
        if((i = index(id)) != -1)
 
86
                return &list[i].value;
 
87
        return NULL;
 
88
}
 
89
 
 
90
const int Flow::contains(const u_char id) {
 
91
        return (index(id) == -1) ? 0 : 1;
 
92
}
 
93
 
 
94
const unsigned int Flow::elements() {
 
95
        return freeSlot;
 
96
}
 
97
 
 
98
const int Flow::index(const u_char id) {
 
99
        unsigned int    i;
 
100
        int             idx;
 
101
 
 
102
        if(list == NULL)
 
103
                return -1;
 
104
 
 
105
        for(i = 0, idx = -1; i < freeSlot; i++)
 
106
                if(list[i].id == id) {
 
107
                        idx = i;
 
108
                        break;
 
109
                }
 
110
        return idx;
 
111
}
 
112
 
 
113
void Flow::removeAll() {
 
114
        aFree(list);
 
115
        list            = NULL;
 
116
        freeSlot        = 0;
 
117
        totalSlots      = 0;
 
118
}
 
119
 
 
120
void Flow::reuse() {
 
121
    bzero(list, sizeof(struct attribute)*freeSlot);
 
122
    freeSlot    = 0;
 
123
}
 
124
 
 
125
void Flow::copy(Flow *src) {
 
126
        freeSlot = src->freeSlot;
 
127
 
 
128
        if(freeSlot > totalSlots) 
 
129
                grow_list(freeSlot);
 
130
 
 
131
        bcopy(src->list, list, sizeof(struct attribute)*freeSlot);
 
132
}
 
133
 
 
134
#ifdef DEBUG
 
135
void Flow::Debug(u_char debug) {
 
136
        union attribute_value   *value;
 
137
        char buf1[32], buf2[32];
 
138
 
 
139
        aDebug(debug, "ds:%u flow:%p slots:%u\n",ds_id, this, freeSlot);
 
140
        for(u_char i=0; i< freeSlot; i++) {
 
141
                value = &list[i].value;
 
142
                switch(list[i].id) {
 
143
                case ATTR_FLOW_INFO: {
 
144
                        struct flow_info_value *flow_info=(struct flow_info_value *)value;
 
145
                        aDebug(debug, "FLOW_INFO: packets:%lu octets:%llu flow_first:%lu flow_last:%lu direction:%u\n",
 
146
                                flow_info->packets, flow_info->octets, flow_info->flow_first, flow_info->flow_last, flow_info->direction);
 
147
                        }
 
148
                        break;
 
149
                case ATTR_IPV4_INFO: {
 
150
                        struct ipv4_info_value *ipv4_info=(struct ipv4_info_value*)value;
 
151
                        inet_ntop(AF_INET, &(ipv4_info->ip_src), buf1, 32);
 
152
                        inet_ntop(AF_INET, &(ipv4_info->ip_dst), buf2, 32);
 
153
                        aDebug(debug,"IPV4_INFO: ip_src:%s ip_dst:%s ip_p:%u ip_tos:%u\n",
 
154
                                buf1, buf2, ipv4_info->ip_p, ipv4_info->ip_tos);
 
155
                        }
 
156
                        break;
 
157
                case ATTR_TCP_INFO: {
 
158
                        struct tcp_info_value *tcp_info=(struct tcp_info_value*)value;
 
159
                        aDebug(debug, "TCP_INFO: src_port:%u dst_port:%u\n",
 
160
                                ntohs(tcp_info->src_port), ntohs(tcp_info->dst_port));
 
161
                        }
 
162
                        break;
 
163
                case ATTR_MAC_INFO: {
 
164
                        struct mac_info_value *mac_info=(struct mac_info_value*)value;
 
165
                        aDebug(debug, "MAC_INFO: src:%s dst:%s\n", 
 
166
                                ether_ntoa((struct ether_addr*)mac_info->src), ether_ntoa((struct ether_addr*)mac_info->dst));
 
167
                        }
 
168
                        break;
 
169
                case ATTR_VLAN_INFO: {
 
170
                        struct vlan_info_value *vlan_info=(struct vlan_info_value*)value;
 
171
                        aDebug(debug, "VLAN_INFO: dot1x:%u\n",
 
172
                                ntohs(vlan_info->dot1x));
 
173
                        }
 
174
                        break;
 
175
                case ATTR_AS_INFO: {
 
176
                        struct as_info_value *as_info=(struct as_info_value*)value;
 
177
                        inet_ntop(AF_INET, &(as_info->as_src), buf1, 32);
 
178
                        inet_ntop(AF_INET, &(as_info->as_dst), buf2, 32);
 
179
                        aDebug(debug, "AS_INFO: as_src:%s as_dst:%s\n",
 
180
                                buf1, buf2);
 
181
                        }
 
182
                        break;
 
183
                case ATTR_IFINDEX_INFO: {
 
184
                        struct ifindex_info_value *ifindex_info=(struct ifindex_info_value*)value;
 
185
                        aDebug(debug, "IFINDEX_INFO: if_in:%u if_out:%u\n",
 
186
                                ntohs(ifindex_info->if_in), ntohs(ifindex_info->if_out));
 
187
                        }
 
188
                        break;
 
189
                case ATTR_MULTICAST_INFO: {
 
190
                        struct multicast_info_value *multicast_info=(struct multicast_info_value*)value;
 
191
                        aDebug(debug, "MULTICAST_INFO: dst_packets:%llu dst_bytes:%llu igmp_type:%u\n",
 
192
                                multicast_info->dst_packets, multicast_info->dst_bytes, multicast_info->igmp_type);
 
193
                        }
 
194
                        break;
 
195
                case ATTR_NEXTHOP_INFO: {
 
196
                        struct nexthop_info_value *nexthop_info=(struct nexthop_info_value*)value;
 
197
                        inet_ntop(AF_INET, &(nexthop_info->ipv4_nexthop), buf1, 32);
 
198
                        inet_ntop(AF_INET, &(nexthop_info->bgpv4_nexthop), buf2, 32);
 
199
                        aDebug(debug, "NEXTHOP_INFO: ipv4_nexthop:%s bgpv4_nexthop:%s\n",
 
200
                                buf1, buf2);
 
201
                        }
 
202
                        break;
 
203
                case ATTR_LAYER7_INFO: {
 
204
                        struct layer7_info_value *layer7_info=(struct layer7_info_value*)value;
 
205
                        aDebug(debug, "LAYER7_INFO: type:%u value:%s\n",
 
206
                                layer7_info->type, layer7_info->value);
 
207
                        }
 
208
                        break;
 
209
                default:
 
210
                        aDebug(debug, "UNKNOWN: id:%u\n", list[i].id);
 
211
                }
 
212
        }
 
213
}
 
214
#endif