source: trunk/oscam-conf-chk.c@ 8456

Last change on this file since 8456 was 8456, checked in by gf, 9 years ago

Slim down struct s_port.

struct s_port contains filter array and couple of other fields that are
used only for newcamd and nothing else. They waste more than 2k in common
case.

To stop losing this much space, create private structure only for
newcamd fields and allocate it dynamically. This lowers struct s_port
size a lot and of course struct s_ptab even more because it contains
array of struct s_ports's.

New structure sizes (32-bit):

                  before  after
  struct s_port     2144     12
  struct s_ptab    68612    388
  struct s_config 141260   4812

Size report (32-bit):

    text     data      bss      dec    hex filename
  998222     1944   602344  1602510 1873ce before/oscam-1.20-unstable_svn8453-i486-slackware-linux
  998174     1944    61800  1061918 10341e  after/oscam-1.20-unstable_svn8453-i486-slackware-linux

bloat-o-meter report (32-bit):

  add/remove: 0/1 grow/shrink: 13/16 up/down: 7897/-548383 (-540486)
  function                                     old     new   delta
  modules                                     1840    9520   +7680
  ...
  cfg                                       141260    4812 -136448
  static.ptab                               411672       - -411672
File size: 11.1 KB
Line 
1#include "globals.h"
2#include "oscam-conf-chk.h"
3#include "oscam-garbage.h"
4#include "oscam-net.h"
5#include "oscam-string.h"
6
7void chk_iprange(char *value, struct s_ip **base)
8{
9 int32_t i = 0;
10 char *ptr1, *ptr2, *saveptr1 = NULL;
11 struct s_ip *fip, *lip, *cip;
12
13 if (!cs_malloc(&cip, sizeof(struct s_ip)))
14 return;
15 fip = cip;
16
17 for (ptr1=strtok_r(value, ",", &saveptr1); ptr1; ptr1=strtok_r(NULL, ",", &saveptr1)) {
18 if (i == 0)
19 ++i;
20 else {
21 if (!cs_malloc(&cip, sizeof(struct s_ip)))
22 break;
23 lip->next = cip;
24 }
25
26 if( (ptr2=strchr(trim(ptr1), '-')) ) {
27 *ptr2++ ='\0';
28 cs_inet_addr(trim(ptr1), &cip->ip[0]);
29 cs_inet_addr(trim(ptr2), &cip->ip[1]);
30 } else {
31 cs_inet_addr(ptr1, &cip->ip[0]);
32 IP_ASSIGN(cip->ip[1], cip->ip[0]);
33 }
34 lip = cip;
35 }
36 lip = *base;
37 *base = fip;
38 clear_sip(&lip);
39}
40
41void chk_caidtab(char *caidasc, CAIDTAB *ctab)
42{
43 int32_t i;
44 char *ptr1, *ptr2, *ptr3, *saveptr1 = NULL;
45 CAIDTAB newctab;
46 memset(&newctab, 0, sizeof(CAIDTAB));
47 for (i = 1; i < CS_MAXCAIDTAB; newctab.mask[i++] = 0xffff);
48
49 for (i = 0, ptr1 = strtok_r(caidasc, ",", &saveptr1); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok_r(NULL, ",", &saveptr1)) {
50 uint32_t caid, mask, cmap;
51 if( (ptr3 = strchr(trim(ptr1), ':')) )
52 *ptr3++ = '\0';
53 else
54 ptr3 = "";
55
56 if( (ptr2 = strchr(trim(ptr1), '&')) )
57 *ptr2++ = '\0';
58 else
59 ptr2 = "";
60
61 if (((caid = a2i(ptr1, 2)) | (mask = a2i(ptr2,-2)) | (cmap = a2i(ptr3, 2))) < 0x10000) {
62 newctab.caid[i] = caid;
63 newctab.mask[i] = mask;
64 newctab.cmap[i++] = cmap;
65 }
66 }
67 memcpy(ctab, &newctab, sizeof(CAIDTAB));
68}
69
70void chk_caidvaluetab(char *lbrlt, CAIDVALUETAB *tab, int32_t minvalue)
71{
72 int32_t i;
73 char *ptr1, *ptr2, *saveptr1 = NULL;
74 CAIDVALUETAB newtab;
75 memset(&newtab, 0, sizeof(CAIDVALUETAB));
76
77 for (i = 0, ptr1 = strtok_r(lbrlt, ",", &saveptr1); (i < CS_MAX_CAIDVALUETAB) && (ptr1); ptr1 = strtok_r(NULL, ",", &saveptr1)) {
78 int32_t caid, value;
79
80 if( (ptr2 = strchr(trim(ptr1), ':')) )
81 *ptr2++ = '\0';
82 else
83 ptr2 = "";
84
85 if (((caid = a2i(ptr1, 2)) < 0xFFFF) | ((value = atoi(ptr2)) < 10000)) {
86 newtab.caid[i] = caid;
87 if (value < minvalue) value = minvalue;
88 newtab.value[i] = value;
89 newtab.n = ++i;
90 }
91 }
92 memcpy(tab, &newtab, sizeof(CAIDVALUETAB));
93}
94
95#ifdef CS_CACHEEX
96void chk_cacheex_valuetab(char *lbrlt, CECSPVALUETAB *tab) {
97 //[caid][&mask][@provid][$servid][:awtime][:]dwtime
98 int32_t i;
99 char *ptr = NULL, *saveptr1 = NULL;
100 CECSPVALUETAB newtab;
101 memset(&newtab, 0, sizeof(CECSPVALUETAB));
102
103 for (i = 0, ptr = strtok_r(lbrlt, ",", &saveptr1); (i < CS_MAXCAIDTAB) && (ptr); ptr = strtok_r(NULL, ",", &saveptr1),i++) {
104 int32_t caid=-1, cmask=-1, srvid = -1;
105 int32_t j,provid = -1;
106 int16_t awtime=-1, dwtime = -1;
107 char *ptr1 = NULL, *ptr2 = NULL, *ptr3 = NULL, *ptr4 = NULL, *ptr5 = NULL, *saveptr2 = NULL;
108
109 if( (ptr4 = strchr(trim(ptr), ':')) ) {
110 //awtime & dwtime
111 *ptr4++ = '\0';
112 for (j = 0, ptr5 = strtok_r(ptr4, ":", &saveptr2); (j < 2) && ptr5; ptr5 = strtok_r(NULL, ":", &saveptr2), j++) {
113 if (!j) {
114 dwtime = atoi(ptr5);
115 }
116 if (j){
117 awtime = dwtime;
118 dwtime = atoi(ptr5);
119 }
120 }
121 }
122 if( (ptr3 = strchr(trim(ptr), '$')) ) {
123 *ptr3++ = '\0';
124 srvid = a2i(ptr3, 4);
125 }
126 if( (ptr2 = strchr(trim(ptr), '@')) ) {
127 *ptr2++ = '\0';
128 provid = a2i(ptr2, 6);
129 }
130 if ( (ptr1 = strchr(ptr, '&')) ) {
131 *ptr1++ = '\0';
132 cmask = a2i(ptr1,-2);
133 }
134 if (!ptr1 && !ptr2 && !ptr3 && !ptr4) //only dwtime
135 dwtime = atoi(ptr);
136 else
137 caid = a2i(ptr, 2);
138 if ((i==0 && (caid <= 0)) || (caid > 0)) {
139 newtab.caid[i] = caid;
140 newtab.cmask[i] = cmask;
141 newtab.prid[i] = provid;
142 newtab.srvid[i] = srvid;
143 newtab.awtime[i] = awtime;
144 newtab.dwtime[i] = dwtime;
145 newtab.n = i + 1;
146 }
147
148 }
149 memcpy(tab, &newtab, sizeof(CECSPVALUETAB));
150 }
151
152void chk_cacheex_hitvaluetab(char *lbrlt, CECSPVALUETAB *tab) {
153 //[caid][&mask][@provid][$servid]
154 int32_t i;
155 char *ptr = NULL, *saveptr1 = NULL;
156 CECSPVALUETAB newtab;
157 memset(&newtab, 0, sizeof(CECSPVALUETAB));
158
159 for (i = 0, ptr = strtok_r(lbrlt, ",", &saveptr1); (i < CS_MAXCAIDTAB) && (ptr); ptr = strtok_r(NULL, ",", &saveptr1),i++) {
160 int32_t caid=-1, cmask=-1, srvid = -1;
161 int32_t provid = -1;
162 char *ptr1 = NULL, *ptr2 = NULL, *ptr3 = NULL;
163
164 if( (ptr3 = strchr(trim(ptr), '$')) ) {
165 *ptr3++ = '\0';
166 srvid = a2i(ptr3, 4);
167 }
168 if( (ptr2 = strchr(trim(ptr), '@')) ) {
169 *ptr2++ = '\0';
170 provid = a2i(ptr2, 6);
171 }
172 if ( (ptr1 = strchr(ptr, '&')) ) {
173 *ptr1++ = '\0';
174 cmask = a2i(ptr1,-2);
175 }
176 caid = a2i(ptr, 2);
177 if (caid > 0) {
178 newtab.caid[i] = caid;
179 newtab.cmask[i] = cmask;
180 newtab.prid[i] = provid;
181 newtab.srvid[i] = srvid;
182 newtab.n = i + 1;
183 }
184
185 }
186 memcpy(tab, &newtab, sizeof(CECSPVALUETAB));
187 }
188#endif
189
190void chk_tuntab(char *tunasc, TUNTAB *ttab)
191{
192 int32_t i;
193 char *ptr1, *ptr2, *ptr3, *saveptr1 = NULL;
194 TUNTAB newttab;
195 memset(&newttab, 0 , sizeof(TUNTAB));
196
197 for (i = 0, ptr1 = strtok_r(tunasc, ",", &saveptr1); (i < CS_MAXTUNTAB) && (ptr1); ptr1 = strtok_r(NULL, ",", &saveptr1)) {
198 uint32_t bt_caidfrom, bt_caidto, bt_srvid;
199 if( (ptr3 = strchr(trim(ptr1), ':')) )
200 *ptr3++ = '\0';
201 else
202 ptr3 = "";
203
204 if( (ptr2 = strchr(trim(ptr1), '.')) )
205 *ptr2++ = '\0';
206 else
207 ptr2 = "";
208
209 if ((bt_caidfrom = a2i(ptr1, 2)) | (bt_srvid = a2i(ptr2,-2)) | (bt_caidto = a2i(ptr3, 2))) {
210 newttab.bt_caidfrom[i] = bt_caidfrom;
211 newttab.bt_caidto[i] = bt_caidto;
212 newttab.bt_srvid[i++] = bt_srvid;
213 newttab.n = i;
214 }
215 }
216 memcpy(ttab, &newttab, sizeof(TUNTAB));
217}
218
219void chk_services(char *labels, SIDTABS *sidtabs)
220{
221 int32_t i;
222 char *ptr, *saveptr1 = NULL;
223 SIDTAB *sidtab;
224 SIDTABBITS newsidok, newsidno;
225 newsidok = newsidno = 0;
226 for (ptr=strtok_r(labels, ",", &saveptr1); ptr; ptr=strtok_r(NULL, ",", &saveptr1)) {
227 for (trim(ptr), i = 0, sidtab = cfg.sidtab; sidtab; sidtab = sidtab->next, i++) {
228 if (!strcmp(sidtab->label, ptr)) newsidok|=((SIDTABBITS)1<<i);
229 if ((ptr[0]=='!') && (!strcmp(sidtab->label, ptr+1))) newsidno|=((SIDTABBITS)1<<i);
230 }
231 }
232 sidtabs->ok = newsidok;
233 sidtabs->no = newsidno;
234}
235
236void chk_ftab(char *zFilterAsc, FTAB *ftab, const char *zType, const char *zName, const char *zFiltName)
237{
238 int32_t i, j;
239 char *ptr1, *ptr2, *ptr3, *saveptr1 = NULL;
240 char *ptr[CS_MAXFILTERS] = {0};
241 FTAB newftab;
242 memset(&newftab, 0, sizeof(FTAB));
243
244 for( i = 0, ptr1 = strtok_r(zFilterAsc, ";", &saveptr1); (i < CS_MAXFILTERS) && (ptr1); ptr1 = strtok_r(NULL, ";", &saveptr1), i++ ) {
245 ptr[i] = ptr1;
246 if( (ptr2 = strchr(trim(ptr1), ':')) ) {
247 *ptr2++ ='\0';
248 newftab.filts[i].caid = (uint16_t)a2i(ptr1, 4);
249 ptr[i] = ptr2;
250 }
251 else if (zFiltName && zFiltName[0] == 'c') {
252 cs_log("PANIC: CAID field not found in CHID parameter!");
253 return;
254 }
255 newftab.nfilts++;
256 }
257
258 if( newftab.nfilts ) {
259 cs_debug_mask(D_CLIENT, "%s '%s' %s filter(s):", zType, zName, zFiltName);
260 }
261 for( i = 0; i < newftab.nfilts; i++ ) {
262 cs_debug_mask(D_CLIENT, "CAID #%d: %04X", i, newftab.filts[i].caid);
263 for( j = 0, ptr3 = strtok_r(ptr[i], ",", &saveptr1); (j < CS_MAXPROV) && (ptr3); ptr3 = strtok_r(NULL, ",", &saveptr1), j++ ) {
264 newftab.filts[i].prids[j] = a2i(ptr3,6);
265 newftab.filts[i].nprids++;
266 cs_debug_mask(D_CLIENT, "%s #%d: %06X", zFiltName, j, newftab.filts[i].prids[j]);
267 }
268 }
269 memcpy(ftab, &newftab, sizeof(FTAB));
270}
271
272void chk_cltab(char *classasc, CLASSTAB *clstab)
273{
274 int32_t i;
275 char *ptr1, *saveptr1 = NULL;
276 CLASSTAB newclstab;
277 memset(&newclstab, 0, sizeof(newclstab));
278 newclstab.an = newclstab.bn = 0;
279 for( i = 0, ptr1 = strtok_r(classasc, ",", &saveptr1); (i < CS_MAXCAIDTAB) && (ptr1); ptr1 = strtok_r(NULL, ",", &saveptr1) ) {
280 ptr1 = trim(ptr1);
281 if( ptr1[0] == '!' )
282 newclstab.bclass[newclstab.bn++] = (uchar)a2i(ptr1+1, 2);
283 else
284 newclstab.aclass[newclstab.an++] = (uchar)a2i(ptr1, 2);
285 }
286 memcpy(clstab, &newclstab, sizeof(CLASSTAB));
287}
288
289void chk_port_tab(char *portasc, PTAB *ptab)
290{
291 int32_t i, j, nfilts, ifilt, iport;
292 PTAB *newptab;
293 char *ptr1, *ptr2, *ptr3, *saveptr1 = NULL;
294 char *ptr[CS_MAXPORTS] = {0};
295 int32_t port[CS_MAXPORTS] = {0};
296 if (!cs_malloc(&newptab, sizeof(PTAB)))
297 return;
298
299 for (nfilts = i = 0, ptr1 = strtok_r(portasc, ";", &saveptr1); (i < CS_MAXPORTS) && (ptr1); ptr1 = strtok_r(NULL, ";", &saveptr1), i++) {
300 ptr[i] = ptr1;
301
302 if (!newptab->ports[i].ncd && !cs_malloc(&newptab->ports[i].ncd, sizeof(struct ncd_port)))
303 break;
304
305 if( (ptr2=strchr(trim(ptr1), '@')) ) {
306 *ptr2++ ='\0';
307 newptab->ports[i].s_port = atoi(ptr1);
308
309 //checking for des key for port
310 newptab->ports[i].ncd->ncd_key_is_set = false;
311 if( (ptr3=strchr(trim(ptr1), '{')) ) {
312 *ptr3++='\0';
313 if (key_atob_l(ptr3, newptab->ports[i].ncd->ncd_key, sizeof(newptab->ports[i].ncd->ncd_key) * 2))
314 fprintf(stderr, "newcamd: error in DES Key for port %s -> ignored\n", ptr1);
315 else
316 newptab->ports[i].ncd->ncd_key_is_set = true;
317 }
318
319 ptr[i] = ptr2;
320 port[i] = newptab->ports[i].s_port;
321 newptab->nports++;
322 }
323 nfilts++;
324 }
325
326 if( nfilts == 1 && strlen(portasc) < 6 && newptab->ports[0].s_port == 0 ) {
327 newptab->ports[0].s_port = atoi(portasc);
328 newptab->nports = 1;
329 }
330
331 iport = ifilt = 0;
332 for (i=0; i<nfilts; i++) {
333 if( port[i] != 0 )
334 iport = i;
335 for (j = 0, ptr3 = strtok_r(ptr[i], ",", &saveptr1); (j < CS_MAXPROV) && (ptr3); ptr3 = strtok_r(NULL, ",", &saveptr1), j++) {
336 if( (ptr2=strchr(trim(ptr3), ':')) ) {
337 *ptr2++='\0';
338 newptab->ports[iport].ncd->ncd_ftab.nfilts++;
339 ifilt = newptab->ports[iport].ncd->ncd_ftab.nfilts-1;
340 newptab->ports[iport].ncd->ncd_ftab.filts[ifilt].caid = (uint16_t)a2i(ptr3, 4);
341 newptab->ports[iport].ncd->ncd_ftab.filts[ifilt].prids[j] = a2i(ptr2, 6);
342 } else {
343 newptab->ports[iport].ncd->ncd_ftab.filts[ifilt].prids[j] = a2i(ptr3, 6);
344 }
345 newptab->ports[iport].ncd->ncd_ftab.filts[ifilt].nprids++;
346 }
347 }
348 memcpy(ptab, newptab, sizeof(PTAB));
349 free(newptab);
350}
351
352/* Clears the s_ip structure provided. The pointer will be set to NULL so everything is cleared.*/
353void clear_sip(struct s_ip **sip) {
354 struct s_ip *cip = *sip;
355 for (*sip = NULL; cip != NULL; cip = cip->next){
356 add_garbage(cip);
357 }
358}
359
360/* Clears the s_ftab struct provided by setting nfilts and nprids to zero. */
361void clear_ftab(struct s_ftab *ftab) {
362 int32_t i, j;
363 for (i = 0; i < CS_MAXFILTERS; i++) {
364 ftab->filts[i].caid = 0;
365 for (j = 0; j < CS_MAXPROV; j++)
366 ftab->filts[i].prids[j] = 0;
367 ftab->filts[i].nprids = 0;
368 }
369 ftab->nfilts = 0;
370}
371
372/* Clears the s_ptab struct provided by setting nfilts and nprids to zero. */
373void clear_ptab(struct s_ptab *ptab) {
374 int32_t i = ptab->nports;
375 ptab->nports = 0;
376 for (; i >= 0; --i) {
377 if (ptab->ports[i].ncd) {
378 ptab->ports[i].ncd->ncd_ftab.nfilts = 0;
379 ptab->ports[i].ncd->ncd_ftab.filts[0].nprids = 0;
380 free(ptab->ports[i].ncd);
381 ptab->ports[i].ncd = NULL;
382 }
383 }
384}
385
386/* Clears given caidtab */
387void clear_caidtab(struct s_caidtab *ctab) {
388 memset(ctab, 0, sizeof(struct s_caidtab));
389 int32_t i;
390 for (i = 1; i < CS_MAXCAIDTAB; ctab->mask[i++] = 0xffff);
391}
392
393#ifdef CS_CACHEEX
394/* Clears given csptab */
395void clear_cacheextab(CECSPVALUETAB *ctab) {
396 memset(ctab, -1, sizeof(CECSPVALUETAB));
397 ctab->n = 0;
398}
399#endif
400
401/* Clears given tuntab */
402void clear_tuntab(struct s_tuntab *ttab) {
403 memset(ttab, 0, sizeof(struct s_tuntab));
404}
Note: See TracBrowser for help on using the repository browser.