1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include <sys/time.h>
|
---|
3 | #include "globals.h"
|
---|
4 |
|
---|
5 | void aes_set_key(char *key)
|
---|
6 | {
|
---|
7 | AES_set_decrypt_key((const unsigned char *)key, 128, &client[cs_idx].aeskey_decrypt);
|
---|
8 | AES_set_encrypt_key((const unsigned char *)key, 128, &client[cs_idx].aeskey);
|
---|
9 | }
|
---|
10 |
|
---|
11 | void aes_decrypt(uchar *buf, int n)
|
---|
12 | {
|
---|
13 | int i;
|
---|
14 | for(i=0; i<n; i+=16)
|
---|
15 | AES_decrypt(buf+i, buf+i, &client[cs_idx].aeskey_decrypt);
|
---|
16 | }
|
---|
17 |
|
---|
18 | void aes_encrypt_idx(int idx, uchar *buf, int n)
|
---|
19 | {
|
---|
20 | int i;
|
---|
21 | for(i=0; i<n; i+=16)
|
---|
22 | AES_encrypt(buf+i, buf+i, &client[idx].aeskey);
|
---|
23 | }
|
---|
24 |
|
---|
25 | void replace_aes_entry(struct s_reader *rdr, ushort caid, uint32 ident, int keyid, uchar *aesKey, int decryptorencrypt)
|
---|
26 | {
|
---|
27 | AES_ENTRY *new_entry;
|
---|
28 | AES_ENTRY *next,*current;
|
---|
29 |
|
---|
30 | current=rdr->aes_list;
|
---|
31 | while(current) {
|
---|
32 | if(current->caid==caid && current->ident==ident && current->keyid==keyid)
|
---|
33 | break;
|
---|
34 | current=current->next;
|
---|
35 | }
|
---|
36 |
|
---|
37 | if(current){ //existing key
|
---|
38 | if(memcmp(aesKey,"\xFF\xFF",2)) {
|
---|
39 | if(decryptorencrypt==1) {
|
---|
40 | AES_set_decrypt_key((const unsigned char *)aesKey, 128, &(current->key));
|
---|
41 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
42 | } else if(decryptorencrypt==2) {
|
---|
43 | AES_set_encrypt_key((const unsigned char *)aesKey, 128, &(current->key));
|
---|
44 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
45 | }
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | memset(&(current->key),0,sizeof(AES_KEY));
|
---|
49 | // cs_log("adding fake key");
|
---|
50 | }
|
---|
51 |
|
---|
52 | }else { // new key
|
---|
53 |
|
---|
54 | // create de AES key entry for the linked list
|
---|
55 | new_entry=malloc(sizeof(AES_ENTRY));
|
---|
56 | if(!new_entry) {
|
---|
57 | cs_log("Error alocation memory for AES key entry");
|
---|
58 | return;
|
---|
59 | }
|
---|
60 |
|
---|
61 | new_entry->caid=caid;
|
---|
62 | new_entry->ident=ident;
|
---|
63 | new_entry->keyid=keyid;
|
---|
64 | if(memcmp(aesKey,"\xFF\xFF",2)) {
|
---|
65 | if(decryptorencrypt==1) {
|
---|
66 | AES_set_decrypt_key((const unsigned char *)aesKey, 128, &(new_entry->key));
|
---|
67 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
68 | } else if(decryptorencrypt==2) {
|
---|
69 | AES_set_encrypt_key((const unsigned char *)aesKey, 128, &(new_entry->key));
|
---|
70 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | memset(&new_entry->key,0,sizeof(AES_KEY));
|
---|
75 | // cs_log("adding fake key");
|
---|
76 | }
|
---|
77 | new_entry->next=NULL;
|
---|
78 |
|
---|
79 | //if list is empty, new_entry is the new head
|
---|
80 | if(!rdr->aes_list) {
|
---|
81 | rdr->aes_list=new_entry;
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | //happend it to the list
|
---|
86 | current=rdr->aes_list;
|
---|
87 | next=current->next;
|
---|
88 | while(next) {
|
---|
89 | current=next;
|
---|
90 | next=current->next;
|
---|
91 | }
|
---|
92 | current->next=new_entry;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | void add_aes_entry(struct s_reader *rdr, ushort caid, uint32 ident, int keyid, uchar *aesKey)
|
---|
98 | {
|
---|
99 | AES_ENTRY *new_entry;
|
---|
100 | AES_ENTRY *next,*current;
|
---|
101 |
|
---|
102 | // create de AES key entry for the linked list
|
---|
103 | new_entry=malloc(sizeof(AES_ENTRY));
|
---|
104 | if(!new_entry) {
|
---|
105 | cs_log("Error alocation memory for AES key entry");
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | new_entry->caid=caid;
|
---|
110 | new_entry->ident=ident;
|
---|
111 | new_entry->keyid=keyid;
|
---|
112 | if(memcmp(aesKey,"\xFF\xFF",2)) {
|
---|
113 | AES_set_decrypt_key((const unsigned char *)aesKey, 128, &(new_entry->key));
|
---|
114 | // cs_log("adding key : %s",cs_hexdump(1,aesKey,16));
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | memset(&new_entry->key,0,sizeof(AES_KEY));
|
---|
118 | // cs_log("adding fake key");
|
---|
119 | }
|
---|
120 | new_entry->next=NULL;
|
---|
121 |
|
---|
122 | //if list is empty, new_entry is the new head
|
---|
123 | if(!rdr->aes_list) {
|
---|
124 | rdr->aes_list=new_entry;
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | //happend it to the list
|
---|
129 | current=rdr->aes_list;
|
---|
130 | next=current->next;
|
---|
131 | while(next) {
|
---|
132 | current=next;
|
---|
133 | next=current->next;
|
---|
134 | }
|
---|
135 |
|
---|
136 | current->next=new_entry;
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 | void parse_aes_entry(struct s_reader *rdr,char *value) {
|
---|
141 | ushort caid;
|
---|
142 | ushort dummy;
|
---|
143 | uint32 ident;
|
---|
144 | int len;
|
---|
145 | char *tmp;
|
---|
146 | int nb_keys,key_id;
|
---|
147 | uchar aes_key[16];
|
---|
148 | char *save=NULL;
|
---|
149 |
|
---|
150 | tmp=strtok_r(value,"@",&save);
|
---|
151 | caid=a2i(tmp,2);
|
---|
152 | tmp=strtok_r(NULL,":",&save);
|
---|
153 | ident=a2i(tmp,3);
|
---|
154 |
|
---|
155 | // now we need to split the key ane add the entry to the reader.
|
---|
156 | nb_keys=0;
|
---|
157 | key_id=0;
|
---|
158 | while((tmp=strtok_r(NULL,",",&save))) {
|
---|
159 | dummy=0;
|
---|
160 | len=strlen(tmp);
|
---|
161 | if(len!=32) {
|
---|
162 | dummy=a2i(tmp,1);
|
---|
163 | // FF means the card will do the AES decrypt
|
---|
164 | // 00 means we don't have the aes.
|
---|
165 | if((dummy!=0xFF && dummy!=0x00) || len>2) {
|
---|
166 | key_id++;
|
---|
167 | cs_log("AES key length error .. not adding");
|
---|
168 | continue;
|
---|
169 | }
|
---|
170 | if(dummy==0x00) {
|
---|
171 | key_id++;
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | nb_keys++;
|
---|
176 | if(dummy)
|
---|
177 | memset(aes_key,0xFF,16);
|
---|
178 | else
|
---|
179 | key_atob(tmp,aes_key);
|
---|
180 | // now add the key to the reader... TBD
|
---|
181 | add_aes_entry(rdr,caid,ident,key_id,aes_key);
|
---|
182 | key_id++;
|
---|
183 | }
|
---|
184 |
|
---|
185 | cs_log("%d AES key(s) added on reader %s for %04x:%06x", nb_keys, rdr->label, caid, ident);
|
---|
186 | }
|
---|
187 |
|
---|
188 | void parse_aes_keys(struct s_reader *rdr,char *value)
|
---|
189 | {
|
---|
190 | // value format is caid1@ident1:key0,key1;caid2@indent2:key0,key1
|
---|
191 | char *entry;
|
---|
192 | char *save=NULL;
|
---|
193 |
|
---|
194 | rdr->aes_list=NULL;
|
---|
195 | for (entry=strtok_r(value, ";",&save); entry; entry=strtok_r(NULL, ";",&save)) {
|
---|
196 | parse_aes_entry(rdr,entry);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | AES_ENTRY *current;
|
---|
201 | current=rdr->aes_list;
|
---|
202 | while(current) {
|
---|
203 | cs_log("**************************");
|
---|
204 | cs_log("current = %p",current);
|
---|
205 | cs_log("CAID = %04x",current->caid);
|
---|
206 | cs_log("IDENT = %06x",current->ident);
|
---|
207 | cs_log("keyID = %d",current->keyid);
|
---|
208 | cs_log("next = %p",current->next);
|
---|
209 | cs_log("**************************");
|
---|
210 | current=current->next;
|
---|
211 | }
|
---|
212 | */
|
---|
213 | }
|
---|
214 |
|
---|
215 | int aes_encrypt_from_list(AES_ENTRY *list, ushort caid, uint32 provid,int keyid, uchar *buf, int n)
|
---|
216 | {
|
---|
217 | AES_ENTRY *current;
|
---|
218 | AES_KEY dummy;
|
---|
219 | int i;
|
---|
220 | int ok=1;
|
---|
221 | int error=0;
|
---|
222 |
|
---|
223 | current=list;
|
---|
224 | while(current) {
|
---|
225 | if(current->caid==caid && current->ident==provid && current->keyid==keyid)
|
---|
226 | break;
|
---|
227 | current=current->next;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if(!current) {
|
---|
231 | cs_log("AES Encrypt : key id %d not found for CAID %04X , provider %06x",keyid,caid,provid);
|
---|
232 | return error; // we don't have the key to decode this buffer.
|
---|
233 | }
|
---|
234 | else {
|
---|
235 | // hack for card that do the AES encryption themsleves
|
---|
236 | memset(&dummy,0,sizeof(AES_KEY));
|
---|
237 | if(!memcmp(¤t->key,&dummy,sizeof(AES_KEY))) {
|
---|
238 | return ok;
|
---|
239 | }
|
---|
240 | // decode the key
|
---|
241 | for(i=0; i<n; i+=16)
|
---|
242 | AES_encrypt(buf+i, buf+i, &(current->key));
|
---|
243 | }
|
---|
244 | return ok; // all ok, key encoded.
|
---|
245 | }
|
---|
246 |
|
---|
247 | int aes_decrypt_from_list(AES_ENTRY *list, ushort caid, uint32 provid,int keyid, uchar *buf, int n)
|
---|
248 | {
|
---|
249 | AES_ENTRY *current;
|
---|
250 | AES_KEY dummy;
|
---|
251 | int i;
|
---|
252 | int ok=1;
|
---|
253 | int error=0;
|
---|
254 |
|
---|
255 | current=list;
|
---|
256 | while(current) {
|
---|
257 | if(current->caid==caid && current->ident==provid && current->keyid==keyid)
|
---|
258 | break;
|
---|
259 | current=current->next;
|
---|
260 | }
|
---|
261 |
|
---|
262 | if(!current) {
|
---|
263 | cs_log("AES Decrypt : key id %d not found for CAID %04X , provider %06x",keyid,caid,provid);
|
---|
264 | return error; // we don't have the key to decode this buffer.
|
---|
265 | }
|
---|
266 | else {
|
---|
267 | // hack for card that do the AES decrypt themsleves
|
---|
268 | memset(&dummy,0,sizeof(AES_KEY));
|
---|
269 | if(!memcmp(¤t->key,&dummy,sizeof(AES_KEY))) {
|
---|
270 | return ok;
|
---|
271 | }
|
---|
272 | // decode the key
|
---|
273 | for(i=0; i<n; i+=16)
|
---|
274 | AES_decrypt(buf+i, buf+i, &(current->key));
|
---|
275 | }
|
---|
276 | return ok; // all ok, key decoded.
|
---|
277 | }
|
---|
278 |
|
---|
279 | int aes_present(AES_ENTRY *list, ushort caid, uint32 provid,int keyid)
|
---|
280 | {
|
---|
281 | AES_ENTRY *current;
|
---|
282 | int ok=1;
|
---|
283 | int error=0;
|
---|
284 |
|
---|
285 | current=list;
|
---|
286 | while(current) {
|
---|
287 | if(current->caid==caid && current->ident==provid && current->keyid==keyid)
|
---|
288 | break;
|
---|
289 | current=current->next;
|
---|
290 | }
|
---|
291 |
|
---|
292 | if(!current) {
|
---|
293 | cs_log("AES Decrypt : key id %d not found for CAID %04X , provider %06x",keyid,caid,provid);
|
---|
294 | return error; // we don't have the key to decode this buffer.
|
---|
295 | }
|
---|
296 |
|
---|
297 | return ok;
|
---|
298 | }
|
---|
299 |
|
---|
300 | void aes_clear_entries(struct s_reader *rdr)
|
---|
301 | {
|
---|
302 |
|
---|
303 | AES_ENTRY *current;
|
---|
304 | AES_ENTRY *next;
|
---|
305 |
|
---|
306 | current=NULL;
|
---|
307 | next=rdr->aes_list;
|
---|
308 | while(next) {
|
---|
309 | current=next;
|
---|
310 | next=current->next;
|
---|
311 | free(current);
|
---|
312 | }
|
---|
313 | rdr->aes_list=NULL;
|
---|
314 | }
|
---|
315 |
|
---|
316 | char *remote_txt(void)
|
---|
317 | {
|
---|
318 | if (client[cs_idx].is_server)
|
---|
319 | return("client");
|
---|
320 | else
|
---|
321 | return("remote server");
|
---|
322 | }
|
---|
323 |
|
---|
324 | char *trim(txt)
|
---|
325 | char *txt;
|
---|
326 | {
|
---|
327 | register int l;
|
---|
328 | register char *p1, *p2;
|
---|
329 |
|
---|
330 | if (*txt==' ')
|
---|
331 | {
|
---|
332 | for (p1=p2=txt;
|
---|
333 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
334 | p1++);
|
---|
335 | while (*p1)
|
---|
336 | *p2++=*p1++;
|
---|
337 | *p2='\0';
|
---|
338 | }
|
---|
339 | if ((l=strlen(txt))>0)
|
---|
340 | for (p1=txt+l-1;
|
---|
341 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
342 | *p1--='\0');
|
---|
343 |
|
---|
344 | return(txt);
|
---|
345 | }
|
---|
346 |
|
---|
347 | char *strtolower(char *txt)
|
---|
348 | {
|
---|
349 | char *p;
|
---|
350 | for (p=txt; *p; p++)
|
---|
351 | if (isupper((uchar)*p)) *p=tolower((uchar)*p);
|
---|
352 | return(txt);
|
---|
353 | }
|
---|
354 |
|
---|
355 | int gethexval(char c)
|
---|
356 | {
|
---|
357 | if ((c>='0') && (c<='9')) return(c-'0');
|
---|
358 | if ((c>='A') && (c<='F')) return(c-'A'+10);
|
---|
359 | if ((c>='a') && (c<='f')) return(c-'a'+10);
|
---|
360 | return(-1);
|
---|
361 | }
|
---|
362 |
|
---|
363 | int cs_atob(uchar *buf, char *asc, int n)
|
---|
364 | {
|
---|
365 | int i, rc;
|
---|
366 | for (i=0; i<n; i++)
|
---|
367 | {
|
---|
368 | if ((rc=(gethexval(asc[i<<1])<<4)|gethexval(asc[(i<<1)+1]))&0x100)
|
---|
369 | return(-1);
|
---|
370 | buf[i]=rc;
|
---|
371 | }
|
---|
372 | return(n);
|
---|
373 | }
|
---|
374 |
|
---|
375 | ulong cs_atoi(char *asc, int l, int val_on_err)
|
---|
376 | {
|
---|
377 | int i, n=0;
|
---|
378 | ulong rc=0;
|
---|
379 | for (i=((l-1)<<1), errno=0; (i>=0) && (n<4); i-=2)
|
---|
380 | {
|
---|
381 | int b;
|
---|
382 | b=(gethexval(asc[i])<<4) | gethexval(asc[i+1]);
|
---|
383 | if (b<0)
|
---|
384 | {
|
---|
385 | errno=EINVAL;
|
---|
386 | rc=(val_on_err) ? 0xFFFFFFFF : 0;
|
---|
387 | break;
|
---|
388 | }
|
---|
389 | rc|=b<<(n<<3);
|
---|
390 | n++;
|
---|
391 | }
|
---|
392 | return(rc);
|
---|
393 | }
|
---|
394 |
|
---|
395 | int byte_atob(char *asc)
|
---|
396 | {
|
---|
397 | int rc;
|
---|
398 |
|
---|
399 | if (strlen(trim(asc))!=2)
|
---|
400 | rc=(-1);
|
---|
401 | else
|
---|
402 | if ((rc=(gethexval(asc[0])<<4)|gethexval(asc[1]))&0x100)
|
---|
403 | rc=(-1);
|
---|
404 | return(rc);
|
---|
405 | }
|
---|
406 |
|
---|
407 | long word_atob(char *asc)
|
---|
408 | {
|
---|
409 | long rc;
|
---|
410 |
|
---|
411 | if (strlen(trim(asc))!=4)
|
---|
412 | rc=(-1);
|
---|
413 | else
|
---|
414 | {
|
---|
415 | rc=gethexval(asc[0])<<12 | gethexval(asc[1])<<8 |
|
---|
416 | gethexval(asc[2])<<4 | gethexval(asc[3]);
|
---|
417 | if (rc&0x10000)
|
---|
418 | rc=(-1);
|
---|
419 | }
|
---|
420 | return(rc);
|
---|
421 | }
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * dynamic word_atob
|
---|
425 | * converts an 1-4 digit asc hexstring
|
---|
426 | */
|
---|
427 | long dyn_word_atob(char *asc)
|
---|
428 | {
|
---|
429 | long rc = (-1);
|
---|
430 | int i, len = strlen(trim(asc));
|
---|
431 |
|
---|
432 | if (len <= 4 && len > 0) {
|
---|
433 | for(i = 0, rc = 0; i < len; i++)
|
---|
434 | rc = rc << 4 | gethexval(asc[i]);
|
---|
435 |
|
---|
436 | if (rc & 0x10000)
|
---|
437 | rc = (-1);
|
---|
438 | }
|
---|
439 | return(rc);
|
---|
440 | }
|
---|
441 |
|
---|
442 | int key_atob(char *asc, uchar *bin)
|
---|
443 | {
|
---|
444 | int i, n1, n2, rc;
|
---|
445 | for (i=rc=0; i<32; i+=2)
|
---|
446 | {
|
---|
447 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
448 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
449 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
450 | }
|
---|
451 | return(rc);
|
---|
452 | }
|
---|
453 |
|
---|
454 | int key_atob14(char *asc, uchar *bin)
|
---|
455 | {
|
---|
456 | int i, n1, n2, rc;
|
---|
457 | for (i=rc=0; i<28; i+=2)
|
---|
458 | {
|
---|
459 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
460 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
461 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
462 | }
|
---|
463 | return(rc);
|
---|
464 | }
|
---|
465 |
|
---|
466 | int key_atob_l(char *asc, uchar *bin, int l)
|
---|
467 | {
|
---|
468 | int i, n1, n2, rc;
|
---|
469 | for (i=rc=0; i<l; i+=2)
|
---|
470 | {
|
---|
471 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
472 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
473 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
474 | }
|
---|
475 | return(rc);
|
---|
476 | }
|
---|
477 |
|
---|
478 | char *key_btoa(char *asc, uchar *bin)
|
---|
479 | {
|
---|
480 | int i;//, n1, n2, rc;
|
---|
481 | static char buf[33];
|
---|
482 | if (!asc)
|
---|
483 | asc=buf;
|
---|
484 | for (i=0; i<16; i++)
|
---|
485 | sprintf(asc+(i<<1), "%02X", bin[i]);
|
---|
486 | return(asc);
|
---|
487 | }
|
---|
488 |
|
---|
489 | char *cs_hexdump(int m, const uchar *buf, int n)
|
---|
490 | {
|
---|
491 | int i;
|
---|
492 | static char dump[520];
|
---|
493 |
|
---|
494 | dump[i=0]='\0';
|
---|
495 | m=(m)?3:2;
|
---|
496 | if (m*n>=(int)sizeof(dump)) n=(sizeof(dump)/m)-1;
|
---|
497 | while (i<n)
|
---|
498 | sprintf(dump+(m*i++), "%02X%s", *buf++, (m>2)?" ":"");
|
---|
499 | return(dump);
|
---|
500 | }
|
---|
501 |
|
---|
502 | static int inet_byteorder=0;
|
---|
503 | in_addr_t cs_inet_order(in_addr_t n)
|
---|
504 | {
|
---|
505 | if (!inet_byteorder)
|
---|
506 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
507 | switch (inet_byteorder)
|
---|
508 | {
|
---|
509 | case 1:
|
---|
510 | break;
|
---|
511 | case 2:
|
---|
512 | n=((n&0xff000000) >> 24 ) |
|
---|
513 | ((n&0x00ff0000) >> 8 ) |
|
---|
514 | ((n&0x0000ff00) << 8 ) |
|
---|
515 | ((n&0x000000ff) << 24 );
|
---|
516 | break;
|
---|
517 | }
|
---|
518 | return(n);
|
---|
519 | }
|
---|
520 |
|
---|
521 | char *cs_inet_ntoa(in_addr_t n)
|
---|
522 | {
|
---|
523 | struct in_addr in;
|
---|
524 | in.s_addr=cs_inet_order(n);
|
---|
525 | return((char *)inet_ntoa(in));
|
---|
526 | }
|
---|
527 |
|
---|
528 | in_addr_t cs_inet_addr(char *txt)
|
---|
529 | {
|
---|
530 | if (!inet_byteorder)
|
---|
531 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
532 | if (inet_byteorder == 1)
|
---|
533 | return(inet_addr(txt));
|
---|
534 | else
|
---|
535 | return(inet_network(txt));
|
---|
536 | }
|
---|
537 |
|
---|
538 | ulong b2i(int n, uchar *b)
|
---|
539 | {
|
---|
540 | switch(n)
|
---|
541 | {
|
---|
542 | case 2:
|
---|
543 | return ((b[0]<<8) | b[1]);
|
---|
544 | case 3:
|
---|
545 | return ((b[0]<<16) | (b[1]<<8) | b[2]);
|
---|
546 | case 4:
|
---|
547 | return (((b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]) & 0xffffffffL);
|
---|
548 | default:
|
---|
549 | cs_log("Error in b2i, n=%i",n);
|
---|
550 | }
|
---|
551 | return 0;
|
---|
552 | }
|
---|
553 |
|
---|
554 | ullong b2ll(int n, uchar *b)
|
---|
555 | {
|
---|
556 | int i;
|
---|
557 | ullong k=0;
|
---|
558 | for(i=0; i<n; k+=b[i++])
|
---|
559 | k<<=8;
|
---|
560 | return(k);
|
---|
561 | }
|
---|
562 |
|
---|
563 | uchar *i2b(int n, ulong i)
|
---|
564 | {
|
---|
565 | static uchar b[4];
|
---|
566 | switch(n)
|
---|
567 | {
|
---|
568 | case 2:
|
---|
569 | b[0]=(i>> 8) & 0xff;
|
---|
570 | b[1]=(i ) & 0xff;
|
---|
571 | break;
|
---|
572 | case 3:
|
---|
573 | b[0]=(i>>16) & 0xff;
|
---|
574 | b[1]=(i>> 8) & 0xff;
|
---|
575 | b[2]=(i ) & 0xff;
|
---|
576 | case 4:
|
---|
577 | b[0]=(i>>24) & 0xff;
|
---|
578 | b[1]=(i>>16) & 0xff;
|
---|
579 | b[2]=(i>> 8) & 0xff;
|
---|
580 | b[3]=(i ) & 0xff;
|
---|
581 | break;
|
---|
582 | }
|
---|
583 | return(b);
|
---|
584 | }
|
---|
585 |
|
---|
586 | ulong a2i(char *asc, int bytes)
|
---|
587 | {
|
---|
588 | int i, n;
|
---|
589 | ulong rc;
|
---|
590 | for (rc=i=0, n=strlen(trim(asc))-1; i<(abs(bytes)<<1); n--, i++)
|
---|
591 | if (n>=0)
|
---|
592 | {
|
---|
593 | int rcl;
|
---|
594 | if ((rcl=gethexval(asc[n]))<0)
|
---|
595 | {
|
---|
596 | errno=EINVAL;
|
---|
597 | return(0x1F1F1F);
|
---|
598 | }
|
---|
599 | rc|=(rcl<<(i<<2));
|
---|
600 | }
|
---|
601 | else
|
---|
602 | if (bytes<0)
|
---|
603 | rc|=(0xf<<(i<<2));
|
---|
604 | errno=0;
|
---|
605 | return(rc);
|
---|
606 | }
|
---|
607 |
|
---|
608 | int boundary(int exp, int n)
|
---|
609 | {
|
---|
610 | return((((n-1)>>exp)+1)<<exp);
|
---|
611 | }
|
---|
612 |
|
---|
613 | void cs_ftime(struct timeb *tp)
|
---|
614 | {
|
---|
615 | #ifdef NO_FTIME
|
---|
616 | struct timeval tv;
|
---|
617 | gettimeofday(&tv, (struct timezone *)0);
|
---|
618 | tp->time=tv.tv_sec;
|
---|
619 | tp->millitm=tv.tv_usec/1000;
|
---|
620 | #else
|
---|
621 | ftime(tp);
|
---|
622 | #endif
|
---|
623 | }
|
---|
624 |
|
---|
625 | void cs_sleepms(unsigned int msec)
|
---|
626 | {
|
---|
627 | //does not interfere with signals like sleep and usleep do
|
---|
628 | struct timespec req_ts;
|
---|
629 | req_ts.tv_sec = msec/1000;
|
---|
630 | req_ts.tv_nsec = (msec % 1000) * 1000000L;
|
---|
631 | nanosleep (&req_ts, NULL);
|
---|
632 | }
|
---|
633 |
|
---|
634 | void cs_sleepus(unsigned int usec)
|
---|
635 | {
|
---|
636 | //does not interfere with signals like sleep and usleep do
|
---|
637 | struct timespec req_ts;
|
---|
638 | req_ts.tv_sec = usec/1000000;
|
---|
639 | req_ts.tv_nsec = (usec % 1000000) * 1000L;
|
---|
640 | nanosleep (&req_ts, NULL);
|
---|
641 | }
|
---|
642 |
|
---|
643 | int bytes_available(int fd)
|
---|
644 | {
|
---|
645 | fd_set rfds;
|
---|
646 | fd_set erfds;
|
---|
647 | int select_ret;
|
---|
648 | int in_fd;
|
---|
649 |
|
---|
650 | in_fd=fd;
|
---|
651 |
|
---|
652 | FD_ZERO(&rfds);
|
---|
653 | FD_SET(in_fd, &rfds);
|
---|
654 |
|
---|
655 | FD_ZERO(&erfds);
|
---|
656 | FD_SET(in_fd, &erfds);
|
---|
657 |
|
---|
658 | select_ret = select(in_fd+1, &rfds, NULL, &erfds, NULL);
|
---|
659 | if (select_ret==-1)
|
---|
660 | {
|
---|
661 | cs_log("ERROR reading from fd %d select_ret=%i, errno=%d",in_fd, select_ret, errno);
|
---|
662 | return 0;
|
---|
663 | }
|
---|
664 |
|
---|
665 | if (FD_ISSET(in_fd, &erfds))
|
---|
666 | {
|
---|
667 | cs_log("ERROR reading from fd %d select_ret=%i, errno=%d",in_fd, select_ret, errno);
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 | if (FD_ISSET(in_fd,&rfds))
|
---|
671 | return 1;
|
---|
672 | else
|
---|
673 | return 0;
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | #ifdef OS_CYGWIN32
|
---|
678 | #include <windows.h>
|
---|
679 | void cs_setpriority(int prio)
|
---|
680 | {
|
---|
681 | HANDLE WinId;
|
---|
682 | ulong wprio;
|
---|
683 | switch((prio+20)/10)
|
---|
684 | {
|
---|
685 | case 0: wprio=REALTIME_PRIORITY_CLASS; break;
|
---|
686 | case 1: wprio=HIGH_PRIORITY_CLASS; break;
|
---|
687 | case 2: wprio=NORMAL_PRIORITY_CLASS; break;
|
---|
688 | default: wprio=IDLE_PRIORITY_CLASS; break;
|
---|
689 | }
|
---|
690 | WinId=GetCurrentProcess();
|
---|
691 | SetPriorityClass(WinId, wprio);
|
---|
692 | }
|
---|
693 | #else
|
---|
694 | void cs_setpriority(int prio)
|
---|
695 | {
|
---|
696 | #ifdef PRIO_PROCESS
|
---|
697 | setpriority(PRIO_PROCESS, 0, prio); // ignore errors
|
---|
698 | #endif
|
---|
699 | }
|
---|
700 | #endif
|
---|
701 |
|
---|
702 | #ifdef WEBIF
|
---|
703 | /* Helper function for urldecode.*/
|
---|
704 | int x2i(int i){
|
---|
705 | i=toupper(i);
|
---|
706 | i = i - '0';
|
---|
707 | if(i > 9) i = i - 'A' + '9' + 1;
|
---|
708 | return i;
|
---|
709 | }
|
---|
710 |
|
---|
711 | /* Decodes values in a http url */
|
---|
712 | void urldecode(char *s){
|
---|
713 | int c, c1, n;
|
---|
714 | char *s0,*t;
|
---|
715 | t = s0 = s;
|
---|
716 | n = strlen(s);
|
---|
717 | while(n >0){
|
---|
718 | c = *s++;
|
---|
719 | if(c == '+') c = ' ';
|
---|
720 | else if(c == '%' && n > 2){
|
---|
721 | c = *s++;
|
---|
722 | c1 = c;
|
---|
723 | c = *s++;
|
---|
724 | c = 16*x2i(c1) + x2i(c);
|
---|
725 | n -= 2;
|
---|
726 | }
|
---|
727 | *t++ = c;
|
---|
728 | n--;
|
---|
729 | }
|
---|
730 | *t = 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Helper function for urlencode.*/
|
---|
734 | char to_hex(char code){
|
---|
735 | static const char hex[] = "0123456789abcdef";
|
---|
736 | return hex[(int)code & 15];
|
---|
737 | }
|
---|
738 |
|
---|
739 | /* Encode values in a http url. Note: Be sure to free() the returned string after use */
|
---|
740 | char *urlencode(char *str){
|
---|
741 | char *pstr = str, *buf = (char *) malloc((strlen(str) * 3 + 1) * sizeof(char)), *pbuf = buf;
|
---|
742 | while (*pstr) {
|
---|
743 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr;
|
---|
744 | else if (*pstr == ' ') *pbuf++ = '+';
|
---|
745 | else {
|
---|
746 | *pbuf++ = '%';
|
---|
747 | *pbuf++ = to_hex(*pstr >> 4);
|
---|
748 | *pbuf++ = to_hex(*pstr & 15);
|
---|
749 | }
|
---|
750 | ++pstr;
|
---|
751 | }
|
---|
752 | *pbuf = '\0';
|
---|
753 | pbuf = (char *) malloc((strlen(buf) + 1) * sizeof(char));
|
---|
754 | strcpy(pbuf, buf);
|
---|
755 | free(buf);
|
---|
756 | return pbuf;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* Converts a char array to a char array with hex values (needed for example for md5). The hex2ascii
|
---|
760 | array is a lookup table with the corresponding hex string on the array position of the integer representation
|
---|
761 | of the ascii value. Note that you need to "free" the resulting array after usage or you'll get a memory leak!*/
|
---|
762 | char *char_to_hex(const unsigned char* p_array, unsigned int p_array_len, char hex2ascii[256][2]) {
|
---|
763 | unsigned char* str = (unsigned char*)malloc(p_array_len*2+1);
|
---|
764 | str[p_array_len*2] = '\0';
|
---|
765 | const unsigned char* p_end = p_array + p_array_len;
|
---|
766 | size_t pos=0;
|
---|
767 | const unsigned char* p;
|
---|
768 | for( p = p_array; p != p_end; p++, pos+=2 ) {
|
---|
769 | str[pos] = hex2ascii[*p][0];
|
---|
770 | str[pos+1] = hex2ascii[*p][1];
|
---|
771 | }
|
---|
772 | return (char*)str;
|
---|
773 | }
|
---|
774 |
|
---|
775 | /* Creates a random string with specified length. Note that dst must be one larger than size to hold the trailing \0*/
|
---|
776 | void create_rand_str(char *dst, int size){
|
---|
777 | static const char text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
778 | int i;
|
---|
779 | for (i = 0; i < size; ++i){
|
---|
780 | dst[i] = text[rand() % (sizeof(text) - 1)];
|
---|
781 | }
|
---|
782 | dst[i] = '\0';
|
---|
783 | }
|
---|
784 | #endif
|
---|
785 |
|
---|
786 | /* Converts a long value to a char array in bitwise representation.
|
---|
787 | Note that the result array MUST be at least 33 bit large and that
|
---|
788 | this function assumes long values to hold only values up to 32bits and to be positive!
|
---|
789 | the result of e.g. long 7 is 11100000000000000000000000000000 this means the array
|
---|
790 | is reversed */
|
---|
791 | void long2bitchar(long value, char *result){
|
---|
792 | int pos;
|
---|
793 | for (pos=0;pos<32;pos++) result[pos]='0';
|
---|
794 | result[pos] = '\0';
|
---|
795 |
|
---|
796 | pos=0;
|
---|
797 | while (value > 0 && pos < 32){
|
---|
798 | if(value % 2 == 1) result[pos]='1';
|
---|
799 | else result[pos]='0';
|
---|
800 | value=value / 2;
|
---|
801 | pos++;
|
---|
802 | }
|
---|
803 | }
|
---|
804 |
|
---|
805 | /* Return 1 if the file exists, else 0 */
|
---|
806 | int file_exists(const char * filename){
|
---|
807 | FILE *file;
|
---|
808 | if ((file = fopen(filename, "r"))){
|
---|
809 | fclose(file);
|
---|
810 | return 1;
|
---|
811 | }
|
---|
812 | return 0;
|
---|
813 | }
|
---|
814 |
|
---|
815 | /* Clears the s_ip structure provided. The pointer will be set to NULL so everything is cleared.*/
|
---|
816 | void clear_sip(struct s_ip **sip){
|
---|
817 | struct s_ip *cip = *sip, *lip;
|
---|
818 | for (*sip = NULL; cip != NULL; cip = lip){
|
---|
819 | lip = cip->next;
|
---|
820 | free(cip);
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | /* Clears the s_ftab struct provided by setting nfilts and nprids to zero. */
|
---|
825 | void clear_ftab(struct s_ftab *ftab){
|
---|
826 | int i, j;
|
---|
827 | for (i = 0; i < CS_MAXFILTERS; i++) {
|
---|
828 | ftab->filts[i].caid = 0;
|
---|
829 | for (j = 0; j < CS_MAXPROV; j++)
|
---|
830 | ftab->filts[i].prids[j] = 0;
|
---|
831 | ftab->filts[i].nprids = 0;
|
---|
832 | }
|
---|
833 | ftab->nfilts = 0;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /* Clears the s_ptab struct provided by setting nfilts and nprids to zero. */
|
---|
837 | void clear_ptab(struct s_ptab *ptab){
|
---|
838 | int i;
|
---|
839 | for (i = 0; i < ptab->nports; i++) {
|
---|
840 | ptab->ports[i].ftab.nfilts = 0;
|
---|
841 | ptab->ports[i].ftab.filts[0].nprids = 0;
|
---|
842 | }
|
---|
843 | ptab->nports = 0;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /* Clears given caidtab */
|
---|
847 | void clear_caidtab(struct s_caidtab *ctab){
|
---|
848 | int i;
|
---|
849 | for (i = 0; i < CS_MAXCAIDTAB; i++) {
|
---|
850 | ctab->caid[i] = 0;
|
---|
851 | ctab->mask[i] = 0;
|
---|
852 | ctab->cmap[i] = 0;
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | /* Clears given tuntab */
|
---|
857 | void clear_tuntab(struct s_tuntab *ttab){
|
---|
858 | int i;
|
---|
859 | for (i = 0; i < CS_MAXTUNTAB; i++) {
|
---|
860 | ttab->bt_caidfrom[i] = 0;
|
---|
861 | ttab->bt_caidto[i] = 0;
|
---|
862 | ttab->bt_srvid[i] = 0;
|
---|
863 | }
|
---|
864 | }
|
---|
865 | /* Overwrites destfile with tmpfile. If forceBakOverWrite = 0, the bakfile will not be overwritten if it exists, else it will be.*/
|
---|
866 | int safe_overwrite_with_bak(char *destfile, char *tmpfile, char *bakfile, int forceBakOverWrite){
|
---|
867 | if (file_exists(destfile)) {
|
---|
868 | if(forceBakOverWrite != 0 && file_exists(bakfile)){
|
---|
869 | if(remove(bakfile) < 0) cs_log("Error removing backup conf file %s (errno=%d)! Will try to proceed nonetheless...", bakfile, errno);
|
---|
870 | }
|
---|
871 | if(file_exists(bakfile)){
|
---|
872 | if(remove(destfile) < 0) {
|
---|
873 | cs_log("Error removing original conf file %s (errno=%d). Will maintain original one!", destfile, errno);
|
---|
874 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
875 | return(1);
|
---|
876 | }
|
---|
877 | } else {
|
---|
878 | if(rename(destfile, bakfile) < 0){
|
---|
879 | cs_log("Error renaming original conf file %s to %s (errno=%d). Will maintain original one!", destfile, bakfile, errno);
|
---|
880 | if(remove(tmpfile) < 0) cs_log("Error removing temp conf file %s (errno=%d)!", tmpfile, errno);
|
---|
881 | return(1);
|
---|
882 | }
|
---|
883 | }
|
---|
884 | if(rename(tmpfile, destfile) < 0){
|
---|
885 | cs_log("Error renaming new conf file %s to %s (errno=%d). The config will be missing upon next startup as this is non-recoverable!", tmpfile, destfile, errno);
|
---|
886 | return(1);
|
---|
887 | }
|
---|
888 | } else {
|
---|
889 | if(rename(tmpfile, destfile) < 0){
|
---|
890 | cs_log("Error renaming new conf file %s to %s (errno=%d). The config will be missing upon next startup as this is non-recoverable!", tmpfile, destfile, errno);
|
---|
891 | return(1);
|
---|
892 | }
|
---|
893 | }
|
---|
894 | return(0);
|
---|
895 | }
|
---|
896 |
|
---|
897 | /* Replacement of fprintf which adds necessary whitespace to fill up the varname to a fixed width.
|
---|
898 | If varname is longer than varnameWidth, no whitespace is added*/
|
---|
899 | void fprintf_conf(FILE *f, int varnameWidth, const char *varname, const char *fmtstring, ...){
|
---|
900 | int varlen = strlen(varname);
|
---|
901 | int max = (varlen > varnameWidth) ? varlen : varnameWidth;
|
---|
902 | char varnamebuf[max + 3];
|
---|
903 | char *ptr = varnamebuf + varlen;
|
---|
904 | va_list argptr;
|
---|
905 |
|
---|
906 | strcpy(varnamebuf, varname);
|
---|
907 | while(varlen < varnameWidth){
|
---|
908 | ptr[0] = ' ';
|
---|
909 | ++ptr;
|
---|
910 | ++varlen;
|
---|
911 | }
|
---|
912 | strcpy(ptr, "= ");
|
---|
913 | fwrite(varnamebuf, sizeof(char), strlen(varnamebuf), f);
|
---|
914 | if(strlen(fmtstring) > 0){
|
---|
915 | va_start(argptr, fmtstring);
|
---|
916 | vfprintf(f, fmtstring, argptr);
|
---|
917 | va_end(argptr);
|
---|
918 | }
|
---|
919 | }
|
---|
920 |
|
---|
921 | /* Ordinary strncpy does not terminate the string if the source is exactly as long or longer as the specified size. This can raise security issues.
|
---|
922 | This function is a replacement which makes sure that a \0 is always added. num should be the real size of char array (do not subtract -1). */
|
---|
923 | void cs_strncpy(char * destination, const char * source, size_t num){
|
---|
924 | uint32 l, size = strlen(source);
|
---|
925 | if(size > num - 1) l = num - 1;
|
---|
926 | else l = size;
|
---|
927 | memcpy(destination, source, l);
|
---|
928 | destination[l] = '\0';
|
---|
929 | }
|
---|
930 |
|
---|
931 | char *get_servicename(int srvid, int caid){
|
---|
932 | int i;
|
---|
933 | struct s_srvid *this = cfg->srvid;
|
---|
934 | static char name[83];
|
---|
935 |
|
---|
936 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
937 | if (this->srvid == srvid)
|
---|
938 | for (i=0; i<this->ncaid; i++)
|
---|
939 | if (this->caid[i] == caid)
|
---|
940 | cs_strncpy(name, this->name, 32);
|
---|
941 |
|
---|
942 | if (!name[0]) sprintf(name, "%04X:%04X unknown", caid, srvid);
|
---|
943 | if (!srvid) name[0] = '\0';
|
---|
944 | return(name);
|
---|
945 | }
|
---|
946 |
|
---|
947 | char *get_tiername(int tierid, int caid){
|
---|
948 | int i;
|
---|
949 | struct s_tierid *this = cfg->tierid;
|
---|
950 | static char name[83];
|
---|
951 |
|
---|
952 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
953 | if (this->tierid == tierid)
|
---|
954 | for (i=0; i<this->ncaid; i++)
|
---|
955 | if (this->caid[i] == caid)
|
---|
956 | cs_strncpy(name, this->name, 32);
|
---|
957 |
|
---|
958 | //if (!name[0]) sprintf(name, "%04X:%04X unknown", caid, tierid);
|
---|
959 | if (!tierid) name[0] = '\0';
|
---|
960 | return(name);
|
---|
961 | }
|
---|
962 |
|
---|
963 | char *get_provider(int caid, ulong provid){
|
---|
964 | struct s_provid *this = cfg->provid;
|
---|
965 | static char name[83];
|
---|
966 |
|
---|
967 | for (name[0] = 0; this && (!name[0]); this = this->next)
|
---|
968 | if (this->caid == caid && this->provid == provid)
|
---|
969 | snprintf(name, 83, "%s / %s / %s", this->prov, this->sat, this->lang);
|
---|
970 |
|
---|
971 | if (!name[0]) snprintf(name, 83, "%04X:%06lX unknown", caid, provid);
|
---|
972 | if (!caid) name[0] = '\0';
|
---|
973 | return(name);
|
---|
974 | }
|
---|
975 |
|
---|
976 | void make_non_blocking(int fd) {
|
---|
977 | int fl;
|
---|
978 | fl=fcntl(fd, F_GETFL);
|
---|
979 | fcntl(fd, F_SETFL, fl | O_NONBLOCK | O_NDELAY);
|
---|
980 | }
|
---|
981 |
|
---|
982 | unsigned int seed;
|
---|
983 |
|
---|
984 | uchar fast_rnd() {
|
---|
985 | unsigned int offset = 12923;
|
---|
986 | unsigned int multiplier = 4079;
|
---|
987 |
|
---|
988 | seed = seed * multiplier + offset;
|
---|
989 | return (uchar) (seed % 0xFF);
|
---|
990 | }
|
---|