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