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 | #include "routers_mgmt.h" |
---|
27 | |
---|
28 | RouterPtr notExistRouter(RouterPtr rL, unsigned long address) |
---|
29 | { |
---|
30 | RouterPtr tmp = rL; |
---|
31 | int i = 0; |
---|
32 | for ( ; tmp; tmp=tmp->next) { |
---|
33 | i++; |
---|
34 | if ( tmp->IpAddress == address ){ |
---|
35 | return tmp; |
---|
36 | } |
---|
37 | } |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | |
---|
41 | RouterPtr addRouter(RouterPtr routersList, unsigned long address) |
---|
42 | { |
---|
43 | RouterPtr tmp = (RouterPtr) malloc(sizeof(struct Router)); |
---|
44 | if (tmp==NULL) { |
---|
45 | syslog(LOG_ERR, "ERROR in malloc in add_routers function\n"); |
---|
46 | exit(1); |
---|
47 | } else { |
---|
48 | tmp->IpAddress = address; |
---|
49 | tmp->tplList = NULL; |
---|
50 | tmp->tplOptList = NULL; |
---|
51 | tmp->sampled = 0; |
---|
52 | tmp->next = routersList; |
---|
53 | if (routersList!=NULL) { routersList->prev = tmp;} |
---|
54 | tmp->prev = NULL; |
---|
55 | return tmp; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | RouterPtr updateRouter(RouterPtr routersList, unsigned long address, |
---|
60 | unsigned long spled) |
---|
61 | { |
---|
62 | RouterPtr tmp = NULL; |
---|
63 | if ((tmp = notExistRouter(routersList, |
---|
64 | address))==NULL) { |
---|
65 | syslog(LOG_INFO,"Router Address not registered : %lu.%lu.%lu.%lu)", |
---|
66 | (address>>24), |
---|
67 | (address<<8>>24), |
---|
68 | (address<<16>>24), |
---|
69 | (address<<24>>24)); |
---|
70 | return routersList; |
---|
71 | } else { |
---|
72 | tmp->sampled = spled; |
---|
73 | return tmp; |
---|
74 | } |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | TplFlowSetPtr newRouterTplList() |
---|
79 | { |
---|
80 | TplFlowSetPtr tmp = (TplFlowSetPtr) malloc(sizeof(struct TemplateFlowSet)); |
---|
81 | tmp->sourceId = 0; |
---|
82 | tmp->templateFlowSetId = 99; |
---|
83 | tmp->fieldCount = 0; |
---|
84 | tmp->fieldSet = NULL; |
---|
85 | tmp->lastField = NULL; |
---|
86 | tmp->next = NULL; |
---|
87 | tmp->prev = NULL; |
---|
88 | return tmp; |
---|
89 | } |
---|
90 | |
---|
91 | TplFlowSetPtr deleteTplFlSet(TplFlowSetPtr ptpl) |
---|
92 | { |
---|
93 | FieldPtr tmp = NULL; |
---|
94 | if (ptpl) { |
---|
95 | while ( ptpl->fieldSet != NULL ){ |
---|
96 | if (!(ptpl->lastField->prev)) { |
---|
97 | freeField(ptpl->lastField); |
---|
98 | ptpl->lastField = NULL; |
---|
99 | ptpl->fieldSet = NULL; |
---|
100 | } else { |
---|
101 | tmp = ptpl->lastField->prev; |
---|
102 | tmp->next = NULL; |
---|
103 | freeField(ptpl->lastField); |
---|
104 | ptpl->lastField = tmp; |
---|
105 | } |
---|
106 | } |
---|
107 | free(ptpl); |
---|
108 | ptpl = NULL; |
---|
109 | } |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | |
---|
113 | TplOptionPtr newRouterTplOptList() |
---|
114 | { |
---|
115 | TplOptionPtr tmp = (TplOptionPtr) malloc(sizeof(struct TemplateOption)); |
---|
116 | tmp->sourceId = 0; |
---|
117 | tmp->templateOptionId = 66; |
---|
118 | tmp->length = 0; |
---|
119 | tmp->optionScopeLg = 0; |
---|
120 | tmp->optionLg = 0; |
---|
121 | tmp->fieldSet = NULL; |
---|
122 | tmp->lastField = NULL; |
---|
123 | tmp->next = NULL; |
---|
124 | tmp->prev = NULL; |
---|
125 | return tmp; |
---|
126 | } |
---|
127 | |
---|
128 | TplOptionPtr deleteTplOption(TplOptionPtr ptplo) |
---|
129 | { |
---|
130 | FieldPtr tmp = NULL; |
---|
131 | if (ptplo) { |
---|
132 | while ( ptplo->fieldSet != NULL ){ |
---|
133 | if (!(ptplo->lastField->prev)) { |
---|
134 | freeField(ptplo->lastField); |
---|
135 | ptplo->lastField = NULL; |
---|
136 | ptplo->fieldSet = NULL; |
---|
137 | } else { |
---|
138 | tmp = ptplo->lastField->prev; |
---|
139 | tmp->next = NULL; |
---|
140 | freeField(ptplo->lastField); |
---|
141 | ptplo->lastField = tmp; |
---|
142 | } |
---|
143 | } |
---|
144 | free(ptplo); |
---|
145 | ptplo = NULL; |
---|
146 | } |
---|
147 | return NULL; |
---|
148 | } |
---|
149 | |
---|
150 | int getSNMPIndexList(char *filename, RouterPtr routersListPtr) |
---|
151 | { |
---|
152 | FILE *indexFile = NULL; |
---|
153 | char line[200]; |
---|
154 | int cptLine = 0; |
---|
155 | char tindex[5]; |
---|
156 | char ttoken[2]; |
---|
157 | char tid[2]; |
---|
158 | char tad[16]; |
---|
159 | int index = 0; |
---|
160 | unsigned short n0, n1, n2, n3; |
---|
161 | unsigned char buffer4[4]; |
---|
162 | unsigned long ipAddress; |
---|
163 | RouterPtr routerTmp = NULL; |
---|
164 | |
---|
165 | if (!(indexFile = fopen(filename, "r"))) { |
---|
166 | fprintf (stderr, "error during %s opening\n", filename); |
---|
167 | exit(1); |
---|
168 | } |
---|
169 | while ( fgets(line, 200, indexFile) != 0) { |
---|
170 | cptLine++; |
---|
171 | if ( strspn(line, "R") == 1 ) { |
---|
172 | if (sscanf(line, "%s %s\n", |
---|
173 | ttoken, |
---|
174 | tad) == 0) { |
---|
175 | fprintf(stderr, "Error in file %s, line %d\n", |
---|
176 | filename, cptLine); |
---|
177 | exit(1); |
---|
178 | } |
---|
179 | if (sscanf(tad,"%hu.%hu.%hu.%hu\n",&n0,&n1,&n2,&n3) == 0){ |
---|
180 | fprintf(stderr, "Address error in file %s, line %d\n", |
---|
181 | filename, cptLine); |
---|
182 | exit(1); |
---|
183 | } |
---|
184 | buffer4[3] = (unsigned char)n0; |
---|
185 | buffer4[2] = (unsigned char)n1; |
---|
186 | buffer4[1] = (unsigned char)n2; |
---|
187 | buffer4[0] = (unsigned char)n3; |
---|
188 | ipAddress = *((unsigned long*)(&buffer4)); |
---|
189 | if ((routerTmp = notExistRouter(routersListPtr, |
---|
190 | ipAddress))==NULL){ |
---|
191 | fprintf(stderr, "Error in file %s, line %d : IP unknown : %lu\n", |
---|
192 | filename, cptLine, ipAddress); |
---|
193 | exit(1); |
---|
194 | } |
---|
195 | }else{ |
---|
196 | if ( strspn(line, "I") == 1 ) { |
---|
197 | if (sscanf(line, "%s %s %s\n", |
---|
198 | ttoken, |
---|
199 | tindex, |
---|
200 | tid) == 0) { |
---|
201 | fprintf(stderr, "Error in file %s, line %d\n", |
---|
202 | filename, cptLine); |
---|
203 | exit(1); |
---|
204 | } |
---|
205 | index = atoi(tindex); |
---|
206 | if ( strcmp(tid, "B") == 0 ) { |
---|
207 | routerTmp->snmpIndexList[index] = 0; |
---|
208 | } else if ( strcmp(tid, "C") == 0 ) { |
---|
209 | routerTmp->snmpIndexList[index] = 1; |
---|
210 | } else { |
---|
211 | fprintf(stderr, "Error in file %s, line %d : bad code B or C \n", |
---|
212 | filename, cptLine); |
---|
213 | exit(1); |
---|
214 | } |
---|
215 | } else { |
---|
216 | fprintf(stderr, "Error in file %s, line %d : bad index line \n", |
---|
217 | filename, cptLine); |
---|
218 | exit(1); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | return 1; |
---|
223 | } |
---|