source: trunk/oscam.c@ 1829

Last change on this file since 1829 was 1829, checked in by merek, 10 years ago

Remove sleep in request_cw as this slows cards down und dosent helps cccam clients

File size: 70.2 KB
Line 
1#define CS_CORE
2#include "globals.h"
3#ifdef CS_WITH_GBOX
4# include "csgbox/gbox.h"
5# define CS_VERSION_X CS_VERSION "-gbx-" GBXVERSION
6#else
7# define CS_VERSION_X CS_VERSION
8#endif
9/*****************************************************************************
10 Globals
11*****************************************************************************/
12int pfd=0; // Primary FD, must be closed on exit
13int mfdr=0; // Master FD (read)
14int fd_m2c=0; // FD Master -> Client (for clients / read )
15int fd_c2m=0; // FD Client -> Master (for clients / write )
16int fd_c2l=0; // FD Client -> Logger (for clients / write )
17int cs_dblevel=0; // Debug Level (TODO !!)
18int cs_idx=0; // client index (0=master, ...)
19int cs_ptyp=0; // process-type
20struct s_module ph[CS_MAX_MOD]; // Protocols
21int maxph=0; // Protocols used
22int cs_hw=0; // hardware autodetect
23int is_server=0; // used in modules to specify function
24pid_t master_pid=0; // master pid OUTSIDE shm
25ushort len4caid[256]; // table for guessing caid (by len)
26char cs_confdir[128]=CS_CONFDIR;
27uchar mbuf[1024]; // global buffer
28ECM_REQUEST *ecmtask;
29#ifdef CS_ANTICASC
30struct s_acasc ac_stat[CS_MAXPID];
31#endif
32
33/*****************************************************************************
34 Shared Memory
35*****************************************************************************/
36int *ecmidx; // Shared Memory
37int *logidx; // Shared Memory
38int *oscam_sem; // sem (multicam.o)
39int *c_start; // idx of 1st client
40int *log_fd; // log-process is running
41struct s_ecm *ecmcache; // Shared Memory
42struct s_client *client; // Shared Memory
43struct s_reader *reader; // Shared Memory
44
45struct card_struct *Cards; // Shared Memory
46struct idstore_struct *idstore; // Shared Memory
47unsigned long *IgnoreList; // Shared Memory
48
49struct s_config *cfg; // Shared Memory
50#ifdef CS_ANTICASC
51struct s_acasc_shm *acasc; // anti-cascading table indexed by account.ac_idx
52#endif
53#ifdef CS_LOGHISTORY
54int *loghistidx; // ptr to current entry
55char *loghist; // ptr of log-history
56#endif
57int *mcl=0; // Master close log?
58
59static int shmsize = CS_ECMCACHESIZE*(sizeof(struct s_ecm)) +
60 CS_MAXPID*(sizeof(struct s_client)) +
61 CS_MAXREADER*(sizeof(struct s_reader)) +
62#ifdef CS_WITH_GBOX
63 CS_MAXCARDS*(sizeof(struct card_struct))+
64 CS_MAXIGNORE*(sizeof(long))+
65 CS_MAXPID*(sizeof(struct idstore_struct))+
66#endif
67#ifdef CS_ANTICASC
68 CS_MAXPID*(sizeof(struct s_acasc_shm)) +
69#endif
70#ifdef CS_LOGHISTORY
71 CS_MAXLOGHIST*CS_LOGHISTSIZE + sizeof(int) +
72#endif
73 sizeof(struct s_config)+(6*sizeof(int));
74
75#ifdef CS_NOSHM
76char cs_memfile[128]=CS_MMAPFILE;
77#endif
78
79/*****************************************************************************
80 Statics
81*****************************************************************************/
82static char mloc[128]={0};
83static int shmid=0; // Shared Memory ID
84static int cs_last_idx=0; // client index of last fork (master only)
85static char *logo = " ___ ____ ___ \n / _ \\/ ___| / __|__ _ _ __ ___ \n| | | \\___ \\| | / _` | '_ ` _ \\ \n| |_| |___) | |_| (_| | | | | | |\n \\___/|____/ \\___\\__,_|_| |_| |_|\n";
86
87static void cs_set_mloc(int ato, char *txt)
88{
89 if (ato>=0)
90 alarm(ato);
91 if (txt)
92 strcpy(mloc, txt);
93}
94
95char *cs_platform(char *buf)
96{
97 static char *hw=NULL;
98 if (!hw)
99 {
100#ifdef TUXBOX
101 struct stat st;
102 cs_hw=CS_HW_DBOX2; // dbox2, default for now
103 if (!stat("/dev/sci0", &st)) cs_hw=CS_HW_DREAM; // dreambox
104#ifdef TRIPLEDRAGON
105 if (!stat("/dev/stb/tdsc0", &st)) cs_hw=CS_HW_DRAGON; // tripledragon
106#endif
107 switch(cs_hw)
108 {
109#ifdef PPC
110 case CS_HW_DBOX2: hw="dbox2" ; break;
111#endif
112 case CS_HW_DREAM: hw="dreambox"; break;
113 case CS_HW_DRAGON: hw="tripledragon"; break;
114 }
115#endif
116 if (!hw) hw=CS_OS_HW;
117 }
118 sprintf(buf, "%s-%s-%s", CS_OS_CPU, hw, CS_OS_SYS);
119 return(buf);
120}
121
122static void usage()
123{
124 fprintf(stderr, "%s\n\n", logo);
125 fprintf(stderr, "OSCam cardserver v%s, build #%s (%s) - (w) 2009-2010 streamboard SVN\n", CS_VERSION_X, CS_SVN_VERSION, CS_OSTYPE);
126 fprintf(stderr, "\tsee http://streamboard.gmc.to:8001/wiki/ for more details\n");
127 fprintf(stderr, "\tbased on OSCam 0.99.x, (w) 2010 streamboard SVN\n");
128 fprintf(stderr, "\tbased on streamboard mp-cardserver v0.9d - (w) 2004-2007 by dukat\n");
129 fprintf(stderr, "\tinbuilt modules: ");
130#ifdef HAVE_DVBAPI
131 fprintf(stderr, "dvbapi ");
132#endif
133#ifdef WEBIF
134 fprintf(stderr, "webinterface ");
135#endif
136#ifdef CS_ANTICASC
137 fprintf(stderr, "anticascading ");
138#endif
139#ifdef LIBUSB
140 fprintf(stderr, "smartreader ");
141#endif
142#ifdef HAVE_PCSC
143 fprintf(stderr, "pcsc ");
144#endif
145#ifdef CS_WITH_GBOX
146 fprintf(stderr, "gbox ");
147#endif
148#ifdef IRDETO_GUESSING
149 fprintf(stderr, "irdeto-guessing ");
150#endif
151 fprintf(stderr, "\n\n");
152 fprintf(stderr, "oscam [-b] [-c config-dir] [-d]");
153#ifdef CS_NOSHM
154 fprintf(stderr, " [-m memory-file]");
155#endif
156 fprintf(stderr, "\n\n\t-b : start in background\n");
157 fprintf(stderr, "\t-c <dir> : read configuration from <dir>\n");
158 fprintf(stderr, "\t default = %s\n", CS_CONFDIR);
159 fprintf(stderr, "\t-d <level> : debug level mask\n");
160 fprintf(stderr, "\t 0 = no debugging (default)\n");
161 fprintf(stderr, "\t 1 = detailed error messages\n");
162 fprintf(stderr, "\t 2 = ATR parsing info, ECM, EMM and CW dumps\n");
163 fprintf(stderr, "\t 4 = traffic from/to the reader\n");
164 fprintf(stderr, "\t 8 = traffic from/to the clients\n");
165 fprintf(stderr, "\t 16 = traffic to the reader-device on IFD layer\n");
166 fprintf(stderr, "\t 32 = traffic to the reader-device on I/O layer\n");
167 fprintf(stderr, "\t 64 = EMM logging\n");
168 fprintf(stderr, "\t 255 = debug all\n");
169#ifdef CS_NOSHM
170 fprintf(stderr, "\t-m <file> : use <file> as mmaped memory file\n");
171 fprintf(stderr, "\t default = %s\n", CS_MMAPFILE);
172#endif
173 fprintf(stderr, "\n");
174 exit(1);
175}
176
177#ifdef NEED_DAEMON
178#ifdef OS_MACOSX
179// this is done because daemon is being deprecated starting with 10.5 and -Werror will always trigger an error
180static int daemon_compat(int nochdir, int noclose)
181#else
182static int daemon(int nochdir, int noclose)
183#endif
184{
185 int fd;
186
187 switch (fork())
188 {
189 case -1: return (-1);
190 case 0: break;
191 default: _exit(0);
192 }
193
194 if (setsid()==(-1))
195 return(-1);
196
197 if (!nochdir)
198 (void)chdir("/");
199
200 if (!noclose && (fd=open("/dev/null", O_RDWR, 0)) != -1)
201 {
202 (void)dup2(fd, STDIN_FILENO);
203 (void)dup2(fd, STDOUT_FILENO);
204 (void)dup2(fd, STDERR_FILENO);
205 if (fd>2)
206 (void)close(fd);
207 }
208 return(0);
209}
210#endif
211
212int recv_from_udpipe(uchar *buf)
213{
214 unsigned short n;
215 if (!pfd) return(-9);
216 if (!read(pfd, buf, 3)) cs_exit(1);
217 if (buf[0]!='U')
218 {
219 cs_log("INTERNAL PIPE-ERROR");
220 cs_exit(1);
221 }
222 memcpy(&n, buf+1, 2);
223 return(read(pfd, buf, n));
224}
225
226char *username(int idx)
227{
228 if (client[idx].usr[0])
229 return(client[idx].usr);
230 else
231 return("anonymous");
232}
233
234static int idx_from_ip(in_addr_t ip, in_port_t port)
235{
236 int i, idx;
237 for (i=idx=0; (i<CS_MAXPID) && (!idx); i++)
238 if ((client[i].ip==ip) && (client[i].port==port) &&
239 ((client[i].typ=='c') || (client[i].typ=='m')))
240 idx=i;
241 return(idx);
242}
243
244int idx_from_pid(pid_t pid)
245{
246 int i, idx;
247 for (i=0, idx=(-1); (i<CS_MAXPID) && (idx<0); i++)
248 if (client[i].pid==pid)
249 idx=i;
250 return(idx);
251}
252
253static long chk_caid(ushort caid, CAIDTAB *ctab)
254{
255 int n;
256 long rc;
257 for (rc=(-1), n=0; (n<CS_MAXCAIDTAB) && (rc<0); n++)
258 if ((caid & ctab->mask[n]) == ctab->caid[n])
259 rc=ctab->cmap[n] ? ctab->cmap[n] : caid;
260 return(rc);
261}
262
263int chk_bcaid(ECM_REQUEST *er, CAIDTAB *ctab)
264{
265 long caid;
266 if ((caid=chk_caid(er->caid, ctab))<0)
267 return(0);
268 er->caid=caid;
269 return(1);
270}
271
272/*
273 * void set_signal_handler(int sig, int flags, void (*sighandler)(int))
274 * flags: 1 = restart, 2 = don't modify if SIG_IGN, may be combined
275 */
276void set_signal_handler(int sig, int flags, void (*sighandler)(int))
277{
278#ifdef CS_SIGBSD
279 if ((signal(sig, sighandler)==SIG_IGN) && (flags & 2))
280 {
281 signal(sig, SIG_IGN);
282 siginterrupt(sig, 0);
283 }
284 else
285 siginterrupt(sig, (flags & 1) ? 0 : 1);
286#else
287 struct sigaction sa;
288 sigaction(sig, (struct sigaction *) 0, &sa);
289 if (!((flags & 2) && (sa.sa_handler==SIG_IGN)))
290 {
291 sigemptyset(&sa.sa_mask);
292 sa.sa_flags=(flags & 1) ? SA_RESTART : 0;
293 sa.sa_handler=sighandler;
294 sigaction(sig, &sa, (struct sigaction *) 0);
295 }
296#endif
297}
298
299static void cs_alarm()
300{
301 cs_debug("Got alarm signal");
302 cs_log("disconnect from %s (deadlock!)", cs_inet_ntoa(client[cs_idx].ip));
303 cs_exit(0);
304}
305
306static void cs_master_alarm()
307{
308 cs_log("PANIC: master deadlock! last location: %s", mloc);
309 fprintf(stderr, "PANIC: master deadlock! last location: %s", mloc);
310 fflush(stderr);
311 cs_exit(0);
312}
313
314static void cs_sigpipe()
315{
316 if ((cs_idx) && (master_pid!=getppid()))
317 cs_exit(0);
318 cs_log("Got sigpipe signal -> captured");
319}
320
321void cs_exit(int sig)
322{
323 int i;
324
325 set_signal_handler(SIGCHLD, 1, SIG_IGN);
326 set_signal_handler(SIGHUP , 1, SIG_IGN);
327 if (sig && (sig!=SIGQUIT))
328 cs_log("exit with signal %d", sig);
329 switch(client[cs_idx].typ)
330 {
331 case 'c': cs_statistics(cs_idx);
332 case 'm': break;
333 case 'n': *log_fd=0;
334 break;
335 case 's': *log_fd=0;
336 for (i=1; i<CS_MAXPID; i++)
337 if (client[i].pid)
338 kill(client[i].pid, SIGQUIT);
339 cs_log("cardserver down");
340#ifndef CS_NOSHM
341 if (ecmcache) shmdt((void *)ecmcache);
342#endif
343 break;
344 }
345 if (pfd) close(pfd);
346#ifdef CS_NOSHM
347 munmap((void *)ecmcache, (size_t)shmsize);
348 if (shmid) close(shmid);
349 unlink(CS_MMAPFILE); // ignore errors, last process must succeed
350#endif
351 exit(sig);
352}
353
354void cs_reinit_clients()
355{
356 int i;
357 struct s_auth *account;
358
359 for( i = 1; i < CS_MAXPID; i++ )
360 if( client[i].pid && client[i].typ == 'c' && client[i].usr[0] ) {
361 for (account = cfg->account; (account) ; account = account->next)
362 if (!strcmp(client[i].usr, account->usr))
363 break;
364
365 if (account && client[i].pcrc == crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), NULL), 16)) {
366 client[i].grp = account->grp;
367 client[i].au = account->au;
368 client[i].autoau = account->autoau;
369 client[i].expirationdate = account->expirationdate;
370 client[i].ncd_keepalive = account->ncd_keepalive;
371 client[i].c35_suppresscmd08 = account->c35_suppresscmd08;
372 client[i].tosleep = (60*account->tosleep);
373 client[i].monlvl = account->monlvl;
374 client[i].disabled = account->disabled;
375 client[i].fchid = account->fchid; // CHID filters
376 client[i].cltab = account->cltab; // Class
377
378 // newcamd module dosent like ident reloading
379 if(!client[i].ncd_server)
380 client[i].ftab = account->ftab; // Ident
381
382 client[i].sidtabok = account->sidtabok; // services
383 client[i].sidtabno = account->sidtabno; // services
384
385 memcpy(&client[i].ctab, &account->ctab, sizeof(client[i].ctab));
386 memcpy(&client[i].ttab, &account->ttab, sizeof(client[i].ttab));
387
388#ifdef CS_ANTICASC
389 client[i].ac_idx = account->ac_idx;
390 client[i].ac_penalty= account->ac_penalty;
391 client[i].ac_limit = (account->ac_users * 100 + 80) * cfg->ac_stime;
392#endif
393 } else {
394 if (ph[client[i].ctyp].type & MOD_CONN_NET) {
395 cs_debug("client '%s', pid=%d not found in db (or password changed)", client[i].usr, client[i].pid);
396 kill(client[i].pid, SIGQUIT);
397 }
398 }
399 }
400}
401
402static void cs_sighup()
403{
404 uchar dummy[1]={0x00};
405 write_to_pipe(fd_c2m, PIP_ID_HUP, dummy, 1);
406}
407
408static void cs_accounts_chk()
409{
410 int i;
411 init_userdb();
412 cs_reinit_clients();
413#ifdef CS_ANTICASC
414 for (i=0; i<CS_MAXPID; i++)
415 if (client[i].typ=='a')
416 {
417 kill(client[i].pid, SIGHUP);
418 break;
419 }
420#endif
421}
422
423static void cs_debug_level()
424{
425 int i;
426
427 //switch debuglevel forward one step if not set from outside
428 if(cfg->debuglvl == cs_dblevel) {
429 switch (cs_dblevel) {
430 case 0:
431 cs_dblevel = 1;
432 break;
433 case 64:
434 cs_dblevel = 255;
435 break;
436 case 255:
437 cs_dblevel = 0;
438 break;
439 default:
440 cs_dblevel <<= 1;
441 }
442 } else {
443 cs_dblevel = cfg->debuglvl;
444 }
445
446 cfg->debuglvl = cs_dblevel;
447
448 if (master_pid == getpid())
449 for (i=0; i<CS_MAXPID && client[i].pid; i++)
450 client[i].dbglvl = cs_dblevel;
451 else
452 client[cs_idx].dbglvl = cs_dblevel;
453 cs_log("%sdebug_level=%d", (master_pid == getpid())?"all ":"", cs_dblevel);
454}
455
456static void cs_card_info(int i)
457{
458 uchar dummy[1]={0x00};
459
460 for( i=1; i<CS_MAXPID; i++ )
461 if( client[i].pid && client[i].typ=='r' && client[i].fd_m2c ){
462 write_to_pipe(client[i].fd_m2c, PIP_ID_CIN, dummy, 1);
463 }
464
465 //kill(client[i].pid, SIGUSR2);
466}
467
468static void cs_child_chk(int i)
469{
470 while (waitpid(0, NULL, WNOHANG)>0);
471 for (i=1; i<CS_MAXPID; i++)
472 if (client[i].pid)
473 if (kill(client[i].pid, 0)) {
474 if ((client[i].typ!='c') && (client[i].typ!='m'))
475 {
476 char *txt="";
477 *log_fd=0;
478 switch(client[i].typ)
479 {
480#ifdef CS_ANTICASC
481 case 'a': txt="anticascader"; break;
482#endif
483 case 'l': txt="logger"; break;
484 case 'p': txt="proxy"; break;
485 case 'r': txt="reader"; break;
486 case 'n': txt="resolver"; break;
487#ifdef WEBIF
488 case 'h': txt="http"; break;
489#endif
490 }
491 cs_log("PANIC: %s lost !! (pid=%d)", txt, client[i].pid);
492 cs_exit(1);
493 }
494 else
495 {
496#ifdef CS_ANTICASC
497 char usr[32];
498 ushort ac_idx=0;
499 ushort ac_limit=0;
500 uchar ac_penalty=0;
501 if( cfg->ac_enabled )
502 {
503 cs_strncpy(usr, client[i].usr, sizeof(usr));
504 ac_idx = client[i].ac_idx;
505 ac_limit = client[i].ac_limit;
506 ac_penalty = client[i].ac_penalty;
507 }
508#endif
509 if (client[i].fd_m2c) close(client[i].fd_m2c);
510 if (client[i].ufd) close(client[i].ufd);
511 memset(&client[i], 0, sizeof(struct s_client));
512#ifdef CS_ANTICASC
513 if( cfg->ac_enabled )
514 {
515 client[i].ac_idx = ac_idx;
516 client[i].ac_limit = ac_limit;
517 client[i].ac_penalty = ac_penalty;
518 strcpy(client[i].usr, usr);
519 }
520#endif
521 client[i].au=(-1);
522 }
523 }
524 return;
525}
526
527int cs_fork(in_addr_t ip, in_port_t port)
528{
529 int i;
530 pid_t pid;
531 for (i=1; (i<CS_MAXPID) && (client[i].pid); i++);
532 if (i<CS_MAXPID)
533 {
534 int fdp[2];
535 memset(&client[i], 0, sizeof(struct s_client));
536 client[i].au=(-1);
537 if (pipe(fdp))
538 {
539 cs_log("Cannot create pipe (errno=%d)", errno);
540 cs_exit(1);
541 }
542 switch(pid=fork())
543 {
544 case -1:
545 cs_log("PANIC: Cannot fork() (errno=%d)", errno);
546 cs_exit(1);
547 case 0: // HERE is client
548 alarm(0);
549 set_signal_handler(SIGALRM, 0, cs_alarm);
550 set_signal_handler(SIGCHLD, 1, SIG_IGN);
551 set_signal_handler(SIGHUP , 1, SIG_IGN);
552 set_signal_handler(SIGINT , 1, SIG_IGN);
553 set_signal_handler(SIGUSR1, 1, cs_debug_level);
554 is_server=((ip) || (port<90)) ? 1 : 0;
555 fd_m2c=fdp[0];
556 close(fdp[1]);
557 close(mfdr);
558 if( port!=97 ) cs_close_log();
559 mfdr=0;
560 cs_ptyp=D_CLIENT;
561 cs_idx=i;
562#ifndef CS_NOSHM
563 shmid=0;
564#endif
565 break;
566 default: // HERE is master
567 client[i].fd_m2c=fdp[1];
568 client[i].dbglvl=cs_dblevel;
569 close(fdp[0]);
570 if (ip)
571 {
572 client[i].typ='c'; // dynamic client
573 client[i].ip=ip;
574 client[i].port=port;
575 cs_log("client(%d) connect from %s (pid=%d, pipfd=%d)",
576 i-cdiff, cs_inet_ntoa(ip), pid, client[i].fd_m2c);
577 }
578 else
579 {
580 client[i].stat=1;
581 switch(port)
582 {
583 case 99: client[i].typ='r'; // reader
584 client[i].sidtabok=reader[ridx].sidtabok;
585 client[i].sidtabno=reader[ridx].sidtabno;
586 reader[ridx].fd=client[i].fd_m2c;
587 reader[ridx].cs_idx=i;
588 reader[ridx].pid=pid;
589 if (reader[ridx].r_port)
590 cs_log("proxy started (pid=%d, server=%s)",
591 pid, reader[ridx].device);
592 else
593 {
594 if (reader[ridx].typ==R_MOUSE || reader[ridx].typ==R_SMART)
595 cs_log("reader started (pid=%d, device=%s, detect=%s%s, mhz=%d, cardmhz=%d)",
596 pid, reader[ridx].device,
597 reader[ridx].detect&0x80 ? "!" : "",
598 RDR_CD_TXT[reader[ridx].detect&0x7f],
599 reader[ridx].mhz,
600 reader[ridx].cardmhz);
601 else
602 cs_log("reader started (pid=%d, device=%s)",
603 pid, reader[ridx].device);
604 client[i].ip=client[0].ip;
605 strcpy(client[i].usr, client[0].usr);
606 }
607 cdiff=i;
608 break;
609 case 98: client[i].typ='n'; // resolver
610 client[i].ip=client[0].ip;
611 strcpy(client[i].usr, client[0].usr);
612 cs_log("resolver started (pid=%d, delay=%d sec)",
613 pid, cfg->resolvedelay);
614 cdiff=i;
615 break;
616 case 97: client[i].typ='l'; // logger
617 client[i].ip=client[0].ip;
618 strcpy(client[i].usr, client[0].usr);
619 cs_log("logger started (pid=%d)", pid);
620 cdiff=i;
621 break;
622#ifdef CS_ANTICASC
623 case 96: client[i].typ='a';
624 client[i].ip=client[0].ip;
625 strcpy(client[i].usr, client[0].usr);
626 cs_log("anticascader started (pid=%d, delay=%d min)",
627 pid, cfg->ac_stime);
628 cdiff=i;
629 break;
630#endif
631
632#ifdef WEBIF
633 case 95: client[i].typ='h'; // http
634 client[i].ip=client[0].ip;
635 strcpy(client[i].usr, client[0].usr);
636 cs_log("http started (pid=%d)",pid);
637 cdiff=i;
638 break;
639#endif
640
641 default: client[i].typ='c'; // static client
642 client[i].ip=client[0].ip;
643 client[i].ctyp=port;
644 cs_log("%s: initialized (pid=%d%s)", ph[port].desc,
645 pid, ph[port].logtxt ? ph[port].logtxt : "");
646 break;
647 }
648 }
649 client[i].login=client[i].last=time((time_t *)0);
650 client[i].pid=pid; // MUST be last -> wait4master()
651 cs_last_idx=i;
652 i=0;
653 }
654 }
655 else
656 {
657 cs_log("max connections reached -> reject client %s", cs_inet_ntoa(ip));
658 i=(-1);
659 }
660 return(i);
661}
662
663static void init_signal()
664{
665 int i;
666 for (i=1; i<NSIG; i++)
667 set_signal_handler(i, 3, cs_exit);
668 set_signal_handler(SIGWINCH, 1, SIG_IGN);
669 // set_signal_handler(SIGPIPE , 0, SIG_IGN);
670 set_signal_handler(SIGPIPE , 0, cs_sigpipe);
671 // set_signal_handler(SIGALRM , 0, cs_alarm);
672 set_signal_handler(SIGALRM , 0, cs_master_alarm);
673 set_signal_handler(SIGCHLD , 1, cs_child_chk);
674 // set_signal_handler(SIGHUP , 1, cs_accounts_chk);
675 set_signal_handler(SIGHUP , 1, cs_sighup);
676 set_signal_handler(SIGUSR1, 1, cs_debug_level);
677 set_signal_handler(SIGUSR2, 1, cs_card_info);
678 set_signal_handler(SIGCONT, 1, SIG_IGN);
679 cs_log("signal handling initialized (type=%s)",
680#ifdef CS_SIGBSD
681 "bsd"
682#else
683 "sysv"
684#endif
685 );
686 return;
687}
688
689static void init_shm()
690{
691#ifdef CS_NOSHM
692 //int i, fd;
693 char *buf;
694 if ((shmid=open(cs_memfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))<0)
695 {
696 fprintf(stderr, "Cannot create mmaped file (errno=%d)", errno);
697 cs_exit(1);
698 }
699
700 buf=(char *)malloc(shmsize);
701 memset(buf, 0, shmsize);
702 if (!write(shmid, buf, shmsize)) cs_exit(1);
703 free(buf);
704
705 ecmcache=(struct s_ecm *)mmap((void *)0, (size_t) shmsize,
706 PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
707#else
708 struct shmid_ds sd;
709 char *shmerr_txt="Cannot %s shared memory (errno=%d)\n";
710 if ((shmid=shmget(IPC_PRIVATE, shmsize, IPC_CREAT | 0600))<0)
711 {
712 fprintf(stderr, shmerr_txt, "create", errno);
713 shmid=0;
714 cs_exit(1);
715 }
716 if ((ecmcache=(struct s_ecm *)shmat(shmid, 0, 0))==(void *)(-1))
717 {
718 fprintf(stderr, shmerr_txt, "attach", errno);
719 cs_exit(1);
720 }
721 memset(ecmcache, 0, shmsize);
722 shmctl(shmid, IPC_RMID, &sd);
723#endif
724#ifdef CS_ANTICASC
725 acasc=(struct s_acasc_shm *)&ecmcache[CS_ECMCACHESIZE];
726 ecmidx=(int *)&acasc[CS_MAXPID];
727#else
728 ecmidx=(int *)&ecmcache[CS_ECMCACHESIZE];
729#endif
730 mcl=(int *)((void *)ecmidx+sizeof(int));
731 logidx=(int *)((void *)mcl+sizeof(int));
732 c_start=(int *)((void *)logidx+sizeof(int));
733 log_fd=(int *)((void *)c_start+sizeof(int));
734 oscam_sem=(int *)((void *)log_fd+sizeof(int));
735 client=(struct s_client *)((void *)oscam_sem+sizeof(int));
736 reader=(struct s_reader *)&client[CS_MAXPID];
737#ifdef CS_WITH_GBOX
738 Cards=(struct card_struct*)&reader[CS_MAXREADER];
739 IgnoreList=(unsigned long*)&Cards[CS_MAXCARDS];
740 idstore=(struct idstore_struct*)&IgnoreList[CS_MAXIGNORE];
741 cfg=(struct s_config *)&idstore[CS_MAXPID];
742#else
743 cfg=(struct s_config *)&reader[CS_MAXREADER];
744#endif
745#ifdef CS_LOGHISTORY
746 loghistidx=(int *)((void *)cfg+sizeof(struct s_config));
747 loghist=(char *)((void *)loghistidx+sizeof(int));
748#endif
749
750#ifdef DEBUG_SHM_POINTER
751 printf("SHM ALLOC: %x\n", shmsize);
752 printf("SHM START: %p\n", (void *) ecmcache);
753 printf("SHM ST1: %p %x (%x)\n", (void *) ecmidx, ((void *) ecmidx) - ((void *) ecmcache), CS_ECMCACHESIZE*(sizeof(struct s_ecm)));
754 printf("SHM ST2: %p %x (%x)\n", (void *) oscam_sem, ((void *) oscam_sem) - ((void *) ecmidx), sizeof(int));
755 printf("SHM ST3: %p %x (%x)\n", (void *) client, ((void *) client) - ((void *) oscam_sem), sizeof(int));
756 printf("SHM ST4: %p %x (%x)\n", (void *) reader, ((void *) reader) - ((void *) client), CS_MAXPID*(sizeof(struct s_client)));
757 printf("SHM ST5: %p %x (%x)\n", (void *) cfg, ((void *) cfg) - ((void *) reader), CS_MAXREADER*(sizeof(struct s_reader)));
758 printf("SHM ST6: %p %x (%x)\n", ((void *) cfg)+sizeof(struct s_config), sizeof(struct s_config), sizeof(struct s_config));
759 printf("SHM ENDE: %p\n", ((void *) cfg)+sizeof(struct s_config));
760 printf("SHM SIZE: %x\n", ((void *) cfg)-((void *) ecmcache) + sizeof(struct s_config));
761 fflush(stdout);
762#endif
763
764 *ecmidx=0;
765 *logidx=0;
766 *oscam_sem=0;
767 client[0].pid=getpid();
768 client[0].login=time((time_t *)0);
769 client[0].ip=cs_inet_addr("127.0.0.1");
770 client[0].typ='s';
771 client[0].au=(-1);
772 client[0].dbglvl=cs_dblevel;
773 strcpy(client[0].usr, "root");
774#ifdef CS_LOGHISTORY
775 *loghistidx=0;
776 memset(loghist, 0, CS_MAXLOGHIST*CS_LOGHISTSIZE);
777#endif
778}
779
780static int start_listener(struct s_module *ph, int port_idx)
781{
782 int ov=1, timeout, is_udp, i;
783 char ptxt[2][32];
784 //struct hostent *ptrh; /* pointer to a host table entry */
785 struct protoent *ptrp; /* pointer to a protocol table entry */
786 struct sockaddr_in sad; /* structure to hold server's address */
787
788 ptxt[0][0]=ptxt[1][0]='\0';
789 if (!ph->ptab->ports[port_idx].s_port)
790 {
791 cs_log("%s: disabled", ph->desc);
792 return(0);
793 }
794 is_udp=(ph->type==MOD_CONN_UDP);
795
796 memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
797 sad.sin_family = AF_INET; /* set family to Internet */
798 if (!ph->s_ip)
799 ph->s_ip=cfg->srvip;
800 if (ph->s_ip)
801 {
802 sad.sin_addr.s_addr=ph->s_ip;
803 sprintf(ptxt[0], ", ip=%s", inet_ntoa(sad.sin_addr));
804 }
805 else
806 sad.sin_addr.s_addr=INADDR_ANY;
807 timeout=cfg->bindwait;
808 //ph->fd=0;
809 ph->ptab->ports[port_idx].fd = 0;
810
811 if (ph->ptab->ports[port_idx].s_port > 0) /* test for illegal value */
812 sad.sin_port = htons((u_short)ph->ptab->ports[port_idx].s_port);
813 else
814 {
815 cs_log("%s: Bad port %d", ph->desc, ph->ptab->ports[port_idx].s_port);
816 return(0);
817 }
818
819 /* Map transport protocol name to protocol number */
820
821 if( (ptrp=getprotobyname(is_udp ? "udp" : "tcp")) )
822 ov=ptrp->p_proto;
823 else
824 ov=(is_udp) ? 17 : 6; // use defaults on error
825
826 if ((ph->ptab->ports[port_idx].fd=socket(PF_INET,is_udp ? SOCK_DGRAM : SOCK_STREAM, ov))<0)
827 {
828 cs_log("%s: Cannot create socket (errno=%d)", ph->desc, errno);
829 return(0);
830 }
831
832 ov=1;
833 if (setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov))<0)
834 {
835 cs_log("%s: setsockopt failed (errno=%d)", ph->desc, errno);
836 close(ph->ptab->ports[port_idx].fd);
837 return(ph->ptab->ports[port_idx].fd=0);
838 }
839
840#ifdef SO_REUSEPORT
841 setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_REUSEPORT, (void *)&ov, sizeof(ov));
842#endif
843
844#ifdef SO_PRIORITY
845 if (cfg->netprio)
846 if (!setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong)))
847 sprintf(ptxt[1], ", prio=%ld", cfg->netprio);
848#endif
849
850 if( !is_udp )
851 {
852 ulong keep_alive = 1;
853 setsockopt(ph->ptab->ports[port_idx].fd, SOL_SOCKET, SO_KEEPALIVE,
854 (void *)&keep_alive, sizeof(ulong));
855 }
856
857 while (timeout--)
858 {
859 if (bind(ph->ptab->ports[port_idx].fd, (struct sockaddr *)&sad, sizeof (sad))<0)
860 {
861 if (timeout)
862 {
863 cs_log("%s: Bind request failed, waiting another %d seconds",
864 ph->desc, timeout);
865 cs_sleepms(1000);
866 }
867 else
868 {
869 cs_log("%s: Bind request failed, giving up", ph->desc);
870 close(ph->ptab->ports[port_idx].fd);
871 return(ph->ptab->ports[port_idx].fd=0);
872 }
873 }
874 else timeout=0;
875 }
876
877 if (!is_udp)
878 if (listen(ph->ptab->ports[port_idx].fd, CS_QLEN)<0)
879 {
880 cs_log("%s: Cannot start listen mode (errno=%d)", ph->desc, errno);
881 close(ph->ptab->ports[port_idx].fd);
882 return(ph->ptab->ports[port_idx].fd=0);
883 }
884
885 cs_log("%s: initialized (fd=%d, port=%d%s%s%s)",
886 ph->desc, ph->ptab->ports[port_idx].fd,
887 ph->ptab->ports[port_idx].s_port,
888 ptxt[0], ptxt[1], ph->logtxt ? ph->logtxt : "");
889
890 for( i=0; i<ph->ptab->ports[port_idx].ftab.nfilts; i++ ) {
891 int j;
892 cs_log("CAID: %04X", ph->ptab->ports[port_idx].ftab.filts[i].caid );
893 for( j=0; j<ph->ptab->ports[port_idx].ftab.filts[i].nprids; j++ )
894 cs_log("provid #%d: %06X", j, ph->ptab->ports[port_idx].ftab.filts[i].prids[j]);
895 }
896 return(ph->ptab->ports[port_idx].fd);
897}
898
899static void cs_client_resolve()
900{
901 while (1)
902 {
903 struct hostent *rht;
904 struct s_auth *account;
905 struct sockaddr_in udp_sa;
906
907 for (account=cfg->account; account; account=account->next)
908 if (account->dyndns[0])
909 {
910 rht=gethostbyname((const char *)account->dyndns);
911 if (rht)
912 {
913 memcpy(&udp_sa.sin_addr, rht->h_addr, sizeof(udp_sa.sin_addr));
914 account->dynip=cs_inet_order(udp_sa.sin_addr.s_addr);
915 }
916 else
917 cs_log("can't resolve hostname %s (user: %s)", account->dyndns, account->usr);
918 client[cs_idx].last=time((time_t)0);
919 }
920 sleep(cfg->resolvedelay);
921 }
922}
923
924static void start_client_resolver()
925{
926 int i;
927 pthread_t tid;
928
929 i=pthread_create(&tid, (pthread_attr_t *)0, (void *)&cs_client_resolve, (void *) 0);
930 if (i)
931 cs_log("ERROR: can't create resolver-thread (err=%d)", i);
932 else
933 {
934 cs_log("resolver thread started");
935 pthread_detach(tid);
936 }
937}
938
939void cs_resolve()
940{
941 int i, idx;
942 struct hostent *rht;
943 struct s_auth;
944 for (i=0; i<CS_MAXREADER; i++)
945 if ((idx=reader[i].cs_idx) && (reader[i].typ & R_IS_NETWORK))
946 {
947 client[cs_idx].last=time((time_t)0);
948 rht=gethostbyname(reader[i].device);
949 if (rht)
950 {
951 memcpy(&client[idx].udp_sa.sin_addr, rht->h_addr,
952 sizeof(client[idx].udp_sa.sin_addr));
953 client[idx].ip=cs_inet_order(client[idx].udp_sa.sin_addr.s_addr);
954 }
955 else
956 cs_log("can't resolve %s", reader[i].device);
957 client[cs_idx].last=time((time_t)0);
958 }
959}
960
961static void cs_logger(void)
962{
963 *log_fd=client[cs_idx].fd_m2c;
964 while(1)
965 {
966 uchar *ptr;
967 //struct timeval tv;
968 fd_set fds;
969
970 FD_ZERO(&fds);
971 FD_SET(fd_m2c, &fds);
972 select(fd_m2c+1, &fds, 0, 0, 0);
973
974 if (master_pid!=getppid())
975 cs_exit(0);
976
977 if (FD_ISSET(fd_m2c, &fds))
978 {
979 int n;
980// switch(n=read_from_pipe(fd_m2c, &ptr, 1))
981 n=read_from_pipe(fd_m2c, &ptr, 1);
982//if (n!=PIP_ID_NUL) printf("received %d bytes\n", n); fflush(stdout);
983 switch(n)
984 {
985 case PIP_ID_LOG:
986 cs_write_log((char *)ptr);
987 break;
988 }
989 }
990 }
991}
992
993static void start_resolver()
994{
995 int i;
996
997 cs_sleepms(1000); // wait for reader
998 while(1)
999 {
1000 if (master_pid!=getppid())
1001 cs_exit(0);
1002 cs_resolve();
1003 for (i=0; i<cfg->resolvedelay; i++)
1004 if (master_pid!=getppid())
1005 cs_exit(0);
1006 else
1007 cs_sleepms(1000);
1008// sleep(cfg->resolvedelay);
1009 }
1010}
1011
1012#ifdef CS_ANTICASC
1013static void start_anticascader()
1014{
1015 int i;
1016
1017 use_ac_log=1;
1018 set_signal_handler(SIGHUP, 1, ac_init_stat);
1019
1020 ac_init_stat();
1021 while(1)
1022 {
1023 for( i=0; i<cfg->ac_stime*60; i++ )
1024 if( master_pid!=getppid() )
1025 cs_exit(0);
1026 else
1027 cs_sleepms(1000);
1028
1029 if (master_pid!=getppid())
1030 cs_exit(0);
1031
1032 ac_do_stat();
1033 }
1034}
1035#endif
1036
1037#ifdef WEBIF
1038static void cs_http()
1039{
1040 http_srv();
1041}
1042#endif
1043
1044static void init_cardreader()
1045{
1046 for (ridx=0; ridx<CS_MAXREADER; ridx++)
1047 if ((reader[ridx].device[0]) && (reader[ridx].enable == 1))
1048 switch(cs_fork(0, 99)) {
1049 case -1:
1050 cs_exit(1);
1051 case 0:
1052 break;
1053 default:
1054
1055 wait4master();
1056 start_cardreader();
1057 }
1058}
1059
1060static void init_service(int srv)
1061{
1062 switch(cs_fork(0, srv))
1063 {
1064 case -1:
1065 cs_exit(1);
1066 case 0:
1067 break;
1068 default:
1069 wait4master();
1070 switch(srv)
1071 {
1072#ifdef CS_ANTICASC
1073 case 96: start_anticascader();
1074#endif
1075 case 97: cs_logger();
1076 case 98: start_resolver();
1077#ifdef WEBIF
1078 case 95: cs_http();
1079#endif
1080 }
1081 }
1082}
1083
1084void wait4master()
1085{
1086 int i;
1087 for (i=0; (i<1000) && (client[cs_idx].pid!=getpid()); i++)
1088 cs_sleepms(1);
1089 if (client[cs_idx].pid!=getpid())
1090 {
1091 cs_log("PANIC: client not found in shared memory");
1092 cs_exit(1);
1093 }
1094 cs_debug("starting client %d with ip %s",
1095 cs_idx-cdiff, cs_inet_ntoa(client[cs_idx].ip));
1096}
1097
1098static void cs_fake_client(char *usr, int uniq, in_addr_t ip)
1099{
1100 /* Uniq = 1: only one connection per user
1101 *
1102 * Uniq = 2: set (new connected) user only to fake if source
1103 * ip is different (e.g. for newcamd clients with
1104 * different CAID's -> Ports)
1105 *
1106 * Uniq = 3: only one connection per user, but only the last
1107 * login will survive (old mpcs behavior)
1108 *
1109 * Uniq = 4: set user only to fake if source ip is
1110 * different, but only the last login will survive
1111 */
1112
1113 int i;
1114 for (i=cdiff+1; i<CS_MAXPID; i++)
1115 {
1116 if (client[i].pid && (client[i].typ == 'c') && !client[i].dup && !strcmp(client[i].usr, usr)
1117 && (uniq < 5) && ((uniq % 2) || (client[i].ip != ip)))
1118 {
1119 if (uniq == 3 || uniq == 4)
1120 {
1121 client[i].dup = 1;
1122 client[i].au = -1;
1123 cs_log("client(%d) duplicate user '%s' from %s set to fake (uniq=%d)", i-cdiff, usr, cs_inet_ntoa(ip), uniq);
1124 }
1125 else
1126 {
1127 client[cs_idx].dup = 1;
1128 client[cs_idx].au = -1;
1129 cs_log("client(%d) duplicate user '%s' from %s set to fake (uniq=%d)", cs_idx-cdiff, usr, cs_inet_ntoa(ip), uniq);
1130 break;
1131 }
1132
1133 }
1134 }
1135
1136}
1137
1138int cs_auth_client(struct s_auth *account, char *e_txt)
1139{
1140 int rc=0;
1141 char buf[32];
1142 char *t_crypt="encrypted";
1143 char *t_plain="plain";
1144 char *t_grant=" granted";
1145 char *t_reject=" rejected";
1146 char *t_msg[]= { buf, "invalid access", "invalid ip", "unknown reason" };
1147 client[cs_idx].grp=0xffffffff;
1148 client[cs_idx].au=(-1);
1149 switch((long)account)
1150 {
1151 case -2: // gbx-dummy
1152 client[cs_idx].dup=0;
1153 break;
1154 case 0: // reject access
1155 rc=1;
1156 cs_log("%s %s-client %s%s (%s)",
1157 client[cs_idx].crypted ? t_crypt : t_plain,
1158 ph[client[cs_idx].ctyp].desc,
1159 client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
1160 client[cs_idx].ip ? t_reject : t_reject+1,
1161 e_txt ? e_txt : t_msg[rc]);
1162 break;
1163 default: // grant/check access
1164 if (client[cs_idx].ip && account->dyndns[0])
1165 if (client[cs_idx].ip != account->dynip)
1166 rc=2;
1167 if (!rc)
1168 {
1169 client[cs_idx].dup=0;
1170 if (client[cs_idx].typ=='c')
1171 {
1172 client[cs_idx].expirationdate=account->expirationdate;
1173 client[cs_idx].disabled=account->disabled;
1174 client[cs_idx].c35_suppresscmd08 = account->c35_suppresscmd08;
1175 client[cs_idx].ncd_keepalive = account->ncd_keepalive;
1176 client[cs_idx].grp=account->grp;
1177 client[cs_idx].au=account->au;
1178 client[cs_idx].autoau=account->autoau;
1179 client[cs_idx].tosleep=(60*account->tosleep);
1180 memcpy(&client[cs_idx].ctab, &account->ctab, sizeof(client[cs_idx].ctab));
1181 if (account->uniq)
1182 cs_fake_client(account->usr, account->uniq, client[cs_idx].ip);
1183 client[cs_idx].ftab = account->ftab; // IDENT filter
1184 client[cs_idx].cltab = account->cltab; // CLASS filter
1185 client[cs_idx].fchid = account->fchid; // CHID filter
1186 client[cs_idx].sidtabok= account->sidtabok; // services
1187 client[cs_idx].sidtabno= account->sidtabno; // services
1188 client[cs_idx].pcrc = crc32(0L, MD5((uchar *)account->pwd, strlen(account->pwd), NULL), 16);
1189 memcpy(&client[cs_idx].ttab, &account->ttab, sizeof(client[cs_idx].ttab));
1190#ifdef CS_ANTICASC
1191 ac_init_client(account);
1192#endif
1193 }
1194 }
1195 client[cs_idx].monlvl=account->monlvl;
1196 strcpy(client[cs_idx].usr, account->usr);
1197 case -1: // anonymous grant access
1198 if (rc)
1199 t_grant=t_reject;
1200 else
1201 {
1202 if (client[cs_idx].typ=='m')
1203 sprintf(t_msg[0], "lvl=%d", client[cs_idx].monlvl);
1204 else
1205 {
1206 if(client[cs_idx].autoau)
1207 {
1208 if(client[cs_idx].ncd_server)
1209 {
1210 int r=0;
1211 for(r=0;r<CS_MAXREADER;r++)
1212 {
1213 if(reader[r].caid[0]==cfg->ncd_ptab.ports[client[cs_idx].port_idx].ftab.filts[0].caid)
1214 {
1215 client[cs_idx].au=r;
1216 break;
1217 }
1218 }
1219 if(client[cs_idx].au<0) sprintf(t_msg[0], "au(auto)=%d", client[cs_idx].au+1);
1220 else sprintf(t_msg[0], "au(auto)=%s", reader[client[cs_idx].au].label);
1221 }
1222 else
1223 {
1224 sprintf(t_msg[0], "au=auto");
1225 }
1226 }
1227 else
1228 {
1229 if(client[cs_idx].au<0) sprintf(t_msg[0], "au=%d", client[cs_idx].au+1);
1230 else sprintf(t_msg[0], "au=%s", reader[client[cs_idx].au].label);
1231 }
1232 }
1233 }
1234 if(client[cs_idx].ncd_server)
1235 {
1236 cs_log("%s %s:%d-client %s%s (%s, %s)",
1237 client[cs_idx].crypted ? t_crypt : t_plain,
1238 e_txt ? e_txt : ph[client[cs_idx].ctyp].desc,
1239 cfg->ncd_ptab.ports[client[cs_idx].port_idx].s_port,
1240 client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
1241 client[cs_idx].ip ? t_grant : t_grant+1,
1242 username(cs_idx), t_msg[rc]);
1243 }
1244 else
1245 {
1246 cs_log("%s %s-client %s%s (%s, %s)",
1247 client[cs_idx].crypted ? t_crypt : t_plain,
1248 e_txt ? e_txt : ph[client[cs_idx].ctyp].desc,
1249 client[cs_idx].ip ? cs_inet_ntoa(client[cs_idx].ip) : "",
1250 client[cs_idx].ip ? t_grant : t_grant+1,
1251 username(cs_idx), t_msg[rc]);
1252 }
1253
1254 break;
1255 }
1256 return(rc);
1257}
1258
1259void cs_disconnect_client(void)
1260{
1261 char buf[32]={0};
1262 if (client[cs_idx].ip)
1263 sprintf(buf, " from %s", cs_inet_ntoa(client[cs_idx].ip));
1264 cs_log("%s disconnected%s", username(cs_idx), buf);
1265 cs_exit(0);
1266}
1267
1268int check_ecmcache(ECM_REQUEST *er, ulong grp)
1269{
1270 int i;
1271// cs_ddump(ecmd5, CS_ECMSTORESIZE, "ECM search");
1272//cs_log("cache CHECK: grp=%lX", grp);
1273 for(i=0; i<CS_ECMCACHESIZE; i++)
1274 if ((grp & ecmcache[i].grp) &&
1275 ecmcache[i].caid==er->caid &&
1276 (!memcmp(ecmcache[i].ecmd5, er->ecmd5, CS_ECMSTORESIZE)))
1277 {
1278//cs_log("cache found: grp=%lX cgrp=%lX", grp, ecmcache[i].grp);
1279 memcpy(er->cw, ecmcache[i].cw, 16);
1280 return(1);
1281 }
1282 return(0);
1283}
1284
1285static void store_ecm(ECM_REQUEST *er)
1286{
1287 int rc;
1288 rc=*ecmidx;
1289 *ecmidx=(*ecmidx+1) % CS_ECMCACHESIZE;
1290 //cs_log("store ecm from reader %d", er->reader[0]);
1291 memcpy(ecmcache[rc].ecmd5, er->ecmd5, CS_ECMSTORESIZE);
1292 memcpy(ecmcache[rc].cw, er->cw, 16);
1293 ecmcache[rc].caid = er->caid;
1294 ecmcache[rc].grp = reader[er->reader[0]].grp;
1295 //cs_ddump(ecmcache[*ecmidx].ecmd5, CS_ECMSTORESIZE, "ECM stored (idx=%d)", *ecmidx);
1296}
1297
1298void store_logentry(char *txt)
1299{
1300#ifdef CS_LOGHISTORY
1301 char *ptr;
1302 ptr=(char *)(loghist+(*loghistidx*CS_LOGHISTSIZE));
1303 ptr[0]='\1'; // make username unusable
1304 ptr[1]='\0';
1305 if ((client[cs_idx].typ=='c') || (client[cs_idx].typ=='m'))
1306 cs_strncpy(ptr, client[cs_idx].usr, 31);
1307 cs_strncpy(ptr+32, txt, CS_LOGHISTSIZE-33);
1308 *loghistidx=(*loghistidx+1) % CS_MAXLOGHIST;
1309#endif
1310}
1311
1312/*
1313 * write_to_pipe():
1314 * write all kind of data to pipe specified by fd
1315 */
1316int write_to_pipe(int fd, int id, uchar *data, int n)
1317{
1318 uchar buf[1024+3+sizeof(int)];
1319
1320//printf("WRITE_START pid=%d", getpid()); fflush(stdout);
1321 if ((id<0) || (id>PIP_ID_MAX))
1322 return(PIP_ID_ERR);
1323 memcpy(buf, PIP_ID_TXT[id], 3);
1324 memcpy(buf+3, &n, sizeof(int));
1325 memcpy(buf+3+sizeof(int), data, n);
1326 n+=3+sizeof(int);
1327//n=write(fd, buf, n);
1328//printf("WRITE_END pid=%d", getpid()); fflush(stdout);
1329//return(n);
1330 if( !fd )
1331 cs_log("write_to_pipe: fd==0");
1332 return(write(fd, buf, n));
1333}
1334
1335/*
1336 * read_from_pipe():
1337 * read all kind of data from pipe specified by fd
1338 * special-flag redir: if set AND data is ECM: this will redirected to appr. client
1339 */
1340int read_from_pipe(int fd, uchar **data, int redir)
1341{
1342 int rc;
1343 static int hdr=0;
1344 static uchar buf[1024+1+3+sizeof(int)];
1345
1346 *data=(uchar *)0;
1347 rc=PIP_ID_NUL;
1348
1349 if (!hdr)
1350 {
1351 if (bytes_available(fd))
1352 {
1353 if (read(fd, buf, 3+sizeof(int))==3+sizeof(int))
1354 memcpy(&hdr, buf+3, sizeof(int));
1355 else
1356 cs_log("WARNING: pipe header to small !");
1357 }
1358 }
1359 if (hdr)
1360 {
1361 int l;
1362 for (l=0; (rc<0) && (PIP_ID_TXT[l]); l++)
1363 if (!memcmp(buf, PIP_ID_TXT[l], 3))
1364 rc=l;
1365
1366 if (rc<0)
1367 {
1368 fprintf(stderr, "WARNING: pipe garbage");
1369 fflush(stderr);
1370 cs_log("WARNING: pipe garbage");
1371 rc=PIP_ID_ERR;
1372 }
1373 else
1374 {
1375 l=hdr;
1376 if ((l+3-1+sizeof(int))>sizeof(buf))
1377 {
1378 cs_log("WARNING: packet size (%d) to large", l);
1379 l=sizeof(buf)+3-1+sizeof(int);
1380 }
1381 if (!bytes_available(fd))
1382 return(PIP_ID_NUL);
1383 hdr=0;
1384 if (read(fd, buf+3+sizeof(int), l)==l)
1385 *data=buf+3+sizeof(int);
1386 else
1387 {
1388 cs_log("WARNING: pipe data to small !");
1389 return(PIP_ID_ERR);
1390 }
1391 buf[l+3+sizeof(int)]=0;
1392 if ((redir) && (rc==PIP_ID_ECM))
1393 {
1394 //int idx;
1395 ECM_REQUEST *er;
1396 er=(ECM_REQUEST *)(buf+3+sizeof(int));
1397 if( er->cidx && client[er->cidx].fd_m2c )
1398 if (!write(client[er->cidx].fd_m2c, buf, l+3+sizeof(int))) cs_exit(1);
1399 rc=PIP_ID_DIR;
1400 }
1401 }
1402 }
1403 return(rc);
1404}
1405
1406/*
1407 * write_ecm_request():
1408 */
1409int write_ecm_request(int fd, ECM_REQUEST *er)
1410{
1411 return(write_to_pipe(fd, PIP_ID_ECM, (uchar *) er, sizeof(ECM_REQUEST)));
1412}
1413
1414int write_ecm_DCW(int fd, ECM_REQUEST *er)
1415{
1416 return(write_to_pipe(fd, PIP_ID_DCW, (uchar *) er, sizeof(ECM_REQUEST)));
1417}
1418
1419/*
1420 * This function writes the current CW from ECM struct to a cwl file.
1421 * The filename is re-calculated and file re-opened every time.
1422 * This will consume a bit cpu time, but nothing has to be stored between
1423 * each call. If not file exists, a header is prepended
1424 */
1425void logCWtoFile(ECM_REQUEST *er)
1426{
1427 FILE *pfCWL;
1428 char srvname[128];
1429 /* %s / %s _I %04X _ %s .cwl */
1430 char buf[256 + sizeof(srvname)];
1431 char date[7];
1432 unsigned char i, parity, writeheader = 0;
1433 time_t t;
1434 struct tm *timeinfo;
1435 struct s_srvid *this;
1436
1437 /*
1438 * search service name for that id and change characters
1439 * causing problems in file name
1440 */
1441 srvname[0] = 0;
1442 for (this=cfg->srvid; this; this = this->next) {
1443 if (this->srvid == er->srvid) {
1444 cs_strncpy(srvname, this->name, sizeof(srvname));
1445 srvname[sizeof(srvname)-1] = 0;
1446 for (i = 0; srvname[i]; i++)
1447 if (srvname[i] == ' ') srvname[i] = '_';
1448 break;
1449 }
1450 }
1451
1452 /* calc log file name */
1453 time(&t);
1454 timeinfo = localtime(&t);
1455 strftime(date, sizeof(date), "%y%m%d", timeinfo);
1456 sprintf(buf, "%s/%s_I%04X_%s.cwl", cfg->cwlogdir, date, er->srvid, srvname);
1457
1458 /* open failed, assuming file does not exist, yet */
1459 if((pfCWL = fopen(buf, "r")) == NULL) {
1460 writeheader = 1;
1461 } else {
1462 /* we need to close the file if it was opened correctly */
1463 fclose(pfCWL);
1464 }
1465
1466 if ((pfCWL = fopen(buf, "a+")) == NULL) {
1467 /* maybe this fails because the subdir does not exist. Is there a common function to create it?
1468 for the moment do not print to log on every ecm
1469 cs_log(""error opening cw logfile for writing: %s (errno %d)", buf, errno); */
1470 return;
1471 }
1472 if (writeheader) {
1473 /* no global macro for cardserver name :( */
1474 fprintf(pfCWL, "# OSCam cardserver v%s - http://streamboard.gmc.to:8001/oscam/wiki\n", CS_VERSION_X);
1475 fprintf(pfCWL, "# control word log file for use with tsdec offline decrypter\n");
1476 strftime(buf, sizeof(buf),"DATE %Y-%m-%d, TIME %H:%M:%S, TZ %Z\n", timeinfo);
1477 fprintf(pfCWL, "# %s", buf);
1478 fprintf(pfCWL, "# CAID 0x%04X, SID 0x%04X, SERVICE \"%s\"\n", er->caid, er->srvid, srvname);
1479 }
1480
1481 parity = er->ecm[0]&1;
1482 fprintf(pfCWL, "%d ", parity);
1483 for (i = parity * 8; i < 8 + parity * 8; i++)
1484 fprintf(pfCWL, "%02X ", er->cw[i]);
1485 /* better use incoming time er->tps rather than current time? */
1486 strftime(buf,sizeof(buf),"%H:%M:%S\n", timeinfo);
1487 fprintf(pfCWL, "# %s", buf);
1488 fflush(pfCWL);
1489 fclose(pfCWL);
1490}
1491
1492int write_ecm_answer(int fd, ECM_REQUEST *er)
1493{
1494 int i;
1495 uchar c;
1496 for (i=0; i<16; i+=4)
1497 {
1498 c=((er->cw[i]+er->cw[i+1]+er->cw[i+2]) & 0xff);
1499 if (er->cw[i+3]!=c)
1500 {
1501 cs_debug("notice: changed dcw checksum byte cw[%i] from %02x to %02x", i+3, er->cw[i+3],c);
1502 er->cw[i+3]=c;
1503 }
1504 }
1505
1506 er->reader[0]=ridx;
1507//cs_log("answer from reader %d (rc=%d)", er->reader[0], er->rc);
1508 er->caid=er->ocaid;
1509
1510#ifdef CS_WITH_GBOX
1511 if (er->rc==1||(er->gbxRidx&&er->rc==0)) {
1512#else
1513 if (er->rc==1) {
1514#endif
1515 store_ecm(er);
1516
1517 /* CWL logging only if cwlogdir is set in config */
1518 if (cfg->cwlogdir != NULL)
1519 logCWtoFile(er);
1520 }
1521
1522 return(write_ecm_request(fd, er));
1523}
1524
1525 /*
1526static int cs_read_timer(int fd, uchar *buf, int l, int msec)
1527{
1528 struct timeval tv;
1529 fd_set fds;
1530 int rc;
1531
1532 if (!fd) return(-1);
1533 tv.tv_sec = msec / 1000;
1534 tv.tv_usec = (msec % 1000) * 1000;
1535 FD_ZERO(&fds);
1536 FD_SET(pfd, &fds);
1537
1538 select(fd+1, &fds, 0, 0, &tv);
1539
1540 rc=0;
1541 if (FD_ISSET(pfd, &fds))
1542 if (!(rc=read(fd, buf, l)))
1543 rc=-1;
1544
1545 return(rc);
1546}*/
1547
1548ECM_REQUEST *get_ecmtask()
1549{
1550 int i, n;
1551 ECM_REQUEST *er=0;
1552
1553 if (!ecmtask)
1554 {
1555 n=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
1556 if( (ecmtask=(ECM_REQUEST *)malloc(n*sizeof(ECM_REQUEST))) )
1557 memset(ecmtask, 0, n*sizeof(ECM_REQUEST));
1558 }
1559
1560 n=(-1);
1561 if (!ecmtask)
1562 {
1563 cs_log("Cannot allocate memory (errno=%d)", errno);
1564 n=(-2);
1565 }
1566 else
1567 if (ph[client[cs_idx].ctyp].multi)
1568 {
1569 for (i=0; (n<0) && (i<CS_MAXPENDING); i++)
1570 if (ecmtask[i].rc<100)
1571 er=&ecmtask[n=i];
1572 }
1573 else
1574 er=&ecmtask[n=0];
1575
1576 if (n<0)
1577 cs_log("WARNING: ecm pending table overflow !");
1578 else
1579 {
1580 memset(er, 0, sizeof(ECM_REQUEST));
1581 er->rc=100;
1582 er->cpti=n;
1583 er->cidx=cs_idx;
1584 cs_ftime(&er->tps);
1585 }
1586 return(er);
1587}
1588
1589int send_dcw(ECM_REQUEST *er)
1590{
1591 static char *stxt[]={"found", "cache1", "cache2", "emu",
1592 "not found", "timeout", "sleeping",
1593 "fake", "invalid", "corrupt", "no card", "expdate", "disabled"};
1594 static char *stxtEx[]={"", "group", "caid", "ident", "class", "chid", "queue", "peer"};
1595 static char *stxtWh[]={"", "user ", "reader ", "server ", "lserver "};
1596 char sby[32]="";
1597 char erEx[32]="";
1598 char uname[38]="";
1599 struct timeb tpe;
1600 ushort lc, *lp;
1601 for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
1602 lc^=*lp;
1603 cs_ftime(&tpe);
1604
1605#ifdef CS_WITH_GBOX
1606 if(er->gbxFrom)
1607 snprintf(uname,sizeof(uname)-1, "%s(%04X)", username(cs_idx), er->gbxFrom);
1608 else
1609#endif
1610 snprintf(uname,sizeof(uname)-1, "%s", username(cs_idx));
1611 if (er->rc==0)
1612 {
1613#ifdef CS_WITH_GBOX
1614 if(reader[er->reader[0]].typ==R_GBOX)
1615 snprintf(sby, sizeof(sby)-1, " by %s(%04X)", reader[er->reader[0]].label,er->gbxCWFrom);
1616 else
1617#endif
1618 // add marker to reader if ECM_REQUEST was betatunneled
1619 if(er->btun)
1620 snprintf(sby, sizeof(sby)-1, " by %s(btun)", reader[er->reader[0]].label);
1621 else
1622 snprintf(sby, sizeof(sby)-1, " by %s", reader[er->reader[0]].label);
1623 }
1624 if (er->rc<4) er->rcEx=0;
1625 if (er->rcEx)
1626 snprintf(erEx, sizeof(erEx)-1, "rejected %s%s", stxtWh[er->rcEx>>4],
1627 stxtEx[er->rcEx&0xf]);
1628
1629 client[cs_idx].cwlastresptime = 1000*(tpe.time-er->tps.time)+tpe.millitm-er->tps.millitm;
1630
1631 if(cfg->mon_appendchaninfo)
1632 cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s - %s",
1633 uname, er->caid, er->prid, er->srvid, er->l, lc,
1634 er->rcEx?erEx:stxt[er->rc], client[cs_idx].cwlastresptime, sby, monitor_get_srvname(er->srvid, er->caid));
1635 else
1636 cs_log("%s (%04X&%06X/%04X/%02X:%04X): %s (%d ms)%s",
1637 uname, er->caid, er->prid, er->srvid, er->l, lc,
1638 er->rcEx?erEx:stxt[er->rc], client[cs_idx].cwlastresptime, sby);
1639
1640 if(!client[cs_idx].ncd_server && client[cs_idx].autoau && er->rcEx==0)
1641 {
1642 if(client[cs_idx].au>=0 && er->caid!=reader[client[cs_idx].au].caid[0])
1643 {
1644 client[cs_idx].au=(-1);
1645 }
1646
1647 client[cs_idx].au=er->reader[0];
1648 if(client[cs_idx].au<0)
1649 {
1650 int r=0;
1651 for(r=0;r<CS_MAXREADER;r++)
1652 {
1653 if(er->caid==reader[r].caid[0])
1654 {
1655 client[cs_idx].au=r;
1656 break;
1657 }
1658 }
1659 if(r==CS_MAXREADER)
1660 {
1661 client[cs_idx].au=(-1);
1662 }
1663 }
1664 }
1665
1666 er->caid = er->ocaid;
1667 switch(er->rc) {
1668 case 0:
1669 case 3:
1670 // 0 - found
1671 // 3 - emu FIXME: obsolete ?
1672 client[cs_idx].cwfound++;
1673 break;
1674
1675 case 1:
1676 case 2:
1677 // 1 - cache1
1678 // 2 - cache2
1679 client[cs_idx].cwcache++;
1680 break;
1681
1682 case 4:
1683 case 9:
1684 case 10:
1685 // 4 - not found
1686 // 9 - corrupt
1687 // 10 - no card
1688 if (er->rcEx)
1689 client[cs_idx].cwignored++;
1690 else
1691 client[cs_idx].cwnot++;
1692 break;
1693
1694 case 5:
1695 // 5 - timeout
1696 client[cs_idx].cwtout++;
1697 break;
1698
1699 default:
1700 client[cs_idx].cwignored++;
1701 }
1702
1703#ifdef CS_ANTICASC
1704 ac_chk(er, 1);
1705#endif
1706
1707 cs_ddump_mask (D_ATR, er->cw, 16, "cw:");
1708 if (er->rc==7) er->rc=0;
1709 ph[client[cs_idx].ctyp].send_dcw(er);
1710 return 0;
1711}
1712
1713void chk_dcw(int fd)
1714{
1715 ECM_REQUEST *er, *ert;
1716 if (read_from_pipe(fd, (uchar **)&er, 0)!=PIP_ID_ECM)
1717 return;
1718 //cs_log("dcw check from reader %d for idx %d (rc=%d)", er->reader[0], er->cpti, er->rc);
1719 ert=&ecmtask[er->cpti];
1720 if (ert->rc<100)
1721 return; // already done
1722 if( (er->caid!=ert->caid) || memcmp(er->ecm , ert->ecm , sizeof(er->ecm)) )
1723 return; // obsolete
1724 ert->rcEx=er->rcEx;
1725 if (er->rc>0) // found
1726 {
1727 switch(er->rc)
1728 {
1729 case 2:
1730 ert->rc=2;
1731 break;
1732 case 3:
1733 ert->rc=3;
1734 break;
1735 default:
1736 ert->rc=0;
1737 }
1738 ert->rcEx=0;
1739 ert->reader[0]=er->reader[0];
1740 memcpy(ert->cw , er->cw , sizeof(er->cw));
1741#ifdef CS_WITH_GBOX
1742 ert->gbxCWFrom=er->gbxCWFrom;
1743#endif
1744 }
1745 else // not found (from ONE of the readers !)
1746 {
1747 int i;
1748 ert->reader[er->reader[0]]=0;
1749 for (i=0; (ert) && (i<CS_MAXREADER); i++)
1750 if (ert->reader[i]) // we have still another chance
1751 ert=(ECM_REQUEST *)0;
1752 if (ert) ert->rc=4;
1753 }
1754 if (ert) send_dcw(ert);
1755 return;
1756}
1757
1758ulong chk_provid(uchar *ecm, ushort caid)
1759{
1760 int i;
1761 ulong provid=0;
1762 switch(caid)
1763 {
1764 case 0x100: // seca
1765 provid=b2i(2, ecm+3);
1766 break;
1767 case 0x500: // viaccess
1768 i=(ecm[4]==0xD2) ? ecm[5] + 2 : 0; // skip d2 nano
1769 if ((ecm[5+i]==3) && ((ecm[4+i]==0x90) || (ecm[4+i]==0x40)))
1770 provid=(b2i(3, ecm+6+i) & 0xFFFFF0);
1771 default:
1772 // cryptoworks ?
1773 if( caid&0x0d00 && ecm[8]==0x83 && ecm[9]==1 )
1774 provid=(ulong)ecm[10];
1775 }
1776 return(provid);
1777}
1778
1779#ifdef IRDETO_GUESSING
1780void guess_irdeto(ECM_REQUEST *er)
1781{
1782 uchar b3;
1783 int b47;
1784 //ushort chid;
1785 struct s_irdeto_quess *ptr;
1786
1787 b3 = er->ecm[3];
1788 ptr = cfg->itab[b3];
1789 if( !ptr ) {
1790 cs_debug("unknown irdeto byte 3: %02X", b3);
1791 return;
1792 }
1793 b47 = b2i(4, er->ecm+4);
1794 //chid = b2i(2, er->ecm+6);
1795 //cs_debug("ecm: b47=%08X, ptr->b47=%08X, ptr->caid=%04X", b47, ptr->b47, ptr->caid);
1796 while( ptr )
1797 {
1798 if( b47==ptr->b47 )
1799 {
1800 if( er->srvid && (er->srvid!=ptr->sid) )
1801 {
1802 cs_debug("sid mismatched (ecm: %04X, guess: %04X), wrong oscam.ird file?",
1803 er->srvid, ptr->sid);
1804 return;
1805 }
1806 er->caid=ptr->caid;
1807 er->srvid=ptr->sid;
1808 er->chid=(ushort)ptr->b47;
1809// cs_debug("quess_irdeto() found caid=%04X, sid=%04X, chid=%04X",
1810// er->caid, er->srvid, er->chid);
1811 return;
1812 }
1813 ptr=ptr->next;
1814 }
1815}
1816#endif
1817
1818void cs_betatunnel(ECM_REQUEST *er)
1819{
1820 int n;
1821 ulong mask_all = 0xFFFF;
1822 TUNTAB *ttab;
1823 ttab = &client[cs_idx].ttab;
1824 for (n = 0; (n < CS_MAXTUNTAB); n++) {
1825 if ((er->caid==ttab->bt_caidfrom[n]) && ((er->srvid==ttab->bt_srvid[n]) || (ttab->bt_srvid[n])==mask_all)) {
1826 uchar hack_n3[13] = {0x70, 0x51, 0xc7, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x87, 0x12, 0x07};
1827 uchar hack_n2[13] = {0x70, 0x51, 0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12, 0x07};
1828 er->caid = ttab->bt_caidto[n];
1829 er->prid = 0;
1830 er->l = (er->ecm[2]+3);
1831 memmove(er->ecm+14, er->ecm+4, er->l-1);
1832 if (er->l > 0x88) {
1833 memcpy(er->ecm+1, hack_n3, 13);
1834 if (er->ecm[0] == 0x81)
1835 er->ecm[12] += 1;
1836 }
1837 else {
1838 memcpy(er->ecm+1, hack_n2, 13);
1839 }
1840 er->l += 10;
1841 er->ecm[2] = er->l-3;
1842 er->btun = 1;
1843 client[cs_idx].cwtun++;
1844 cs_debug("ECM converted from: 0x%X to BetaCrypt: 0x%X for service id:0x%X",
1845 ttab->bt_caidfrom[n], ttab->bt_caidto[n], ttab->bt_srvid[n]);
1846 }
1847 }
1848}
1849
1850void guess_cardsystem(ECM_REQUEST *er)
1851{
1852 ushort last_hope=0;
1853
1854 // viaccess - check by provid-search
1855 if( (er->prid=chk_provid(er->ecm, 0x500)) )
1856 er->caid=0x500;
1857
1858 // nagra
1859 // is ecm[1] always 0x30 ?
1860 // is ecm[3] always 0x07 ?
1861 if ((er->ecm[6]==1) && (er->ecm[4]==er->ecm[2]-2))
1862 er->caid=0x1801;
1863
1864 // seca2 - very poor
1865 if ((er->ecm[8]==0x10) && ((er->ecm[9]&0xF1)==1))
1866 last_hope=0x100;
1867
1868 // is cryptoworks, but which caid ?
1869 if ((er->ecm[3]==0x81) && (er->ecm[4]==0xFF) &&
1870 (!er->ecm[5]) && (!er->ecm[6]) && (er->ecm[7]==er->ecm[2]-5))
1871 last_hope=0xd00;
1872
1873#ifdef IRDETO_GUESSING
1874 if (!er->caid && er->ecm[2]==0x31 && er->ecm[0x0b]==0x28)
1875 guess_irdeto(er);
1876#endif
1877
1878 if (!er->caid) // guess by len ..
1879 er->caid=len4caid[er->ecm[2]+3];
1880
1881 if (!er->caid)
1882 er->caid=last_hope;
1883}
1884
1885void request_cw(ECM_REQUEST *er, int flag, int reader_types)
1886{
1887 int i;
1888 if ((reader_types == 0) || (reader_types == 2))
1889 er->level=flag;
1890 flag=(flag)?3:1; // flag specifies with/without fallback-readers
1891 for (i=0; i<CS_MAXREADER; i++)
1892 {
1893 switch (reader_types)
1894 {
1895 // network and local cards
1896 default:
1897 case 0:
1898 if (er->reader[i]&flag){
1899 write_ecm_request(reader[i].fd, er);
1900 }
1901 break;
1902 // only local cards
1903 case 1:
1904 if (!(reader[i].typ & R_IS_NETWORK))
1905 if (er->reader[i]&flag)
1906 write_ecm_request(reader[i].fd, er);
1907 break;
1908 // only network
1909 case 2:
1910 if ((reader[i].typ & R_IS_NETWORK))
1911 if (er->reader[i]&flag)
1912 write_ecm_request(reader[i].fd, er);
1913 break;
1914 }
1915 }
1916}
1917
1918void get_cw(ECM_REQUEST *er)
1919{
1920 int i, j, m;
1921 time_t now = time((time_t)0);
1922
1923 client[cs_idx].lastecm = now;
1924
1925 if (!er->caid)
1926 guess_cardsystem(er);
1927
1928 /* Quickfix Area */
1929
1930 if( (er->caid & 0xFF00) == 0x600 && !er->chid )
1931 er->chid = (er->ecm[6]<<8)|er->ecm[7];
1932
1933 // quickfix for 0100:000065
1934 if (er->caid == 0x100 && er->prid == 0x65 && er->srvid == 0)
1935 er->srvid = 0x0642;
1936
1937 // Quickfixes for Opticum/Globo HD9500
1938 // Quickfix for 0500:030300
1939 if (er->caid == 0x500 && er->prid == 0x030300)
1940 er->prid = 0x030600;
1941
1942 // Quickfix for 0500:D20200
1943 if (er->caid == 0x500 && er->prid == 0xD20200)
1944 er->prid = 0x030600;
1945
1946 /* END quickfixes */
1947
1948 if (!er->prid)
1949 er->prid = chk_provid(er->ecm, er->caid);
1950
1951 // Set providerid for newcamd clients if none is given
1952 if( (!er->prid) && client[cs_idx].ncd_server ) {
1953 int pi = client[cs_idx].port_idx;
1954 if( pi >= 0 && cfg->ncd_ptab.nports && cfg->ncd_ptab.nports >= pi )
1955 er->prid = cfg->ncd_ptab.ports[pi].ftab.filts[0].prids[0];
1956 }
1957
1958 // CAID not supported or found
1959 if (!er->caid) {
1960 er->rc = 8;
1961 er->rcEx = E2_CAID;
1962 }
1963
1964 // user expired
1965 if(client[cs_idx].expirationdate && client[cs_idx].expirationdate < client[cs_idx].lastecm)
1966 er->rc = 11;
1967
1968 // user disabled
1969 if(client[cs_idx].disabled != 0)
1970 er->rc = 12;
1971
1972 // rc<100 -> ecm error
1973 if (er->rc > 99) {
1974
1975 m = er->caid;
1976 er->ocaid = er->caid;
1977 i = er->srvid;
1978
1979 if ((i != client[cs_idx].last_srvid) || (!client[cs_idx].lastswitch)) {
1980 client[cs_idx].lastswitch = now;
1981 if(cfg->usrfileflag)
1982 cs_statistics(cs_idx);
1983 }
1984
1985 // user sleeping
1986 if ((client[cs_idx].tosleep) && (now - client[cs_idx].lastswitch > client[cs_idx].tosleep))
1987 er->rc = 6;
1988
1989 client[cs_idx].last_srvid = i;
1990 client[cs_idx].last_caid = m;
1991
1992 for (j = 0; (j < 6) && (er->rc > 99); j++)
1993 {
1994 switch(j) {
1995
1996 case 0:
1997 // fake (uniq)
1998 if (client[cs_idx].dup)
1999 er->rc = 7;
2000 break;
2001
2002 case 1:
2003 // invalid (caid)
2004 if (!chk_bcaid(er, &client[cs_idx].ctab)) {
2005 er->rc = 8;
2006 er->rcEx = E2_CAID;
2007 }
2008 break;
2009
2010 case 2:
2011 // invalid (srvid)
2012 if (!chk_srvid(er, cs_idx))
2013 er->rc = 8;
2014 break;
2015
2016 case 3:
2017 // invalid (ufilters)
2018 if (!chk_ufilters(er))
2019 er->rc = 8;
2020 break;
2021
2022 case 4:
2023 // invalid (sfilter)
2024 if (!chk_sfilter(er, ph[client[cs_idx].ctyp].ptab))
2025 er->rc = 8;
2026 break;
2027
2028 case 5:
2029 // corrupt
2030 if( (i = er->l - (er->ecm[2] + 3)) ) {
2031 if (i > 0) {
2032 cs_debug("warning: ecm size adjusted from 0x%X to 0x%X",
2033 er->l, er->ecm[2] + 3);
2034 er->l = (er->ecm[2] + 3);
2035 }
2036 else
2037 er->rc = 9;
2038 }
2039 break;
2040 }
2041 }
2042
2043 /*BetaCrypt tunneling
2044 *moved behind the check routines,
2045 *because newcamd ECM will fail
2046 *if ECM is converted before
2047 */
2048 if (&client[cs_idx].ttab)
2049 cs_betatunnel(er);
2050
2051 // store ECM in cache
2052 memcpy(er->ecmd5, MD5(er->ecm, er->l, NULL), CS_ECMSTORESIZE);
2053
2054 // cache1
2055 if (check_ecmcache(er, client[cs_idx].grp))
2056 er->rc = 1;
2057
2058#ifdef CS_ANTICASC
2059 ac_chk(er, 0);
2060#endif
2061 }
2062
2063 if(er->rc > 99 && er->rc != 1) {
2064
2065 for (i = m = 0; i < CS_MAXREADER; i++)
2066 if (matching_reader(er, &reader[i]) && (i != ridx))
2067 m|=er->reader[i] = (reader[i].fallback)? 2: 1;
2068
2069 switch(m) {
2070
2071 // no reader -> not found
2072 case 0:
2073 er->rc = 4;
2074 if (!er->rcEx)
2075 er->rcEx = E2_GROUP;
2076 break;
2077
2078 // fallbacks only, switch them
2079 case 2:
2080 for (i = 0; i < CS_MAXREADER; i++)
2081 er->reader[i]>>=1;
2082 }
2083 }
2084
2085 if (er->rc < 100) {
2086 if (cfg->delay)
2087 cs_sleepms(cfg->delay);
2088
2089 send_dcw(er);
2090 return;
2091 }
2092
2093 er->rcEx = 0;
2094 request_cw(er, 0, cfg->preferlocalcards ? 1 : 0);
2095}
2096
2097void log_emm_request(int auidx)
2098{
2099// cs_log("%s send emm-request (reader=%s, caid=%04X)",
2100// cs_inet_ntoa(client[cs_idx].ip), reader[auidx].label, reader[auidx].caid[0]);
2101 cs_log("%s emm-request sent (reader=%s, caid=%04X)",
2102 username(cs_idx), reader[auidx].label, reader[auidx].caid[0]);
2103}
2104
2105void do_emm(EMM_PACKET *ep)
2106{
2107 int au;
2108 au = client[cs_idx].au;
2109
2110 cs_ddump_mask(D_ATR, ep->emm, ep->l, "emm:");
2111
2112 if ((au < 0) || (au >= CS_MAXREADER))
2113 return;
2114
2115 if (!reader_get_emm_type(ep, &reader[au])) //decodes ep->type and ep->hexserial from the EMM
2116 return;
2117
2118 cs_ddump_mask(D_EMM, ep->hexserial, 8, "emm UA/SA:");
2119
2120 switch (ep->type) {
2121 case UNKNOWN:
2122 cs_debug_mask(D_EMM, "emmtype UNKNOWN. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
2123 if (reader[au].blockemm_unknown) return;
2124 break;
2125
2126 case UNIQUE:
2127 cs_debug_mask(D_EMM, "emmtype UNIQUE. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
2128 if (reader[au].blockemm_u) return;
2129 break;
2130
2131 case SHARED:
2132 cs_debug_mask(D_EMM, "emmtype SHARED. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
2133 if (reader[au].blockemm_s) return;
2134 break;
2135
2136 // FIXME only camd33 delivers hexserial from the net, newcamd, camd35 copy
2137 // cardreader hexserial in; reader_get_emm_type overwrites this with real SA value if known!
2138 case GLOBAL:
2139 cs_debug_mask(D_EMM, "emmtype GLOBAL. Reader %s has serial %s.", reader[au].label, cs_hexdump(0, reader[au].hexserial, 8));
2140 if (reader[au].blockemm_g) return;
2141 break;
2142 }
2143
2144 client[cs_idx].lastemm = time((time_t)0);
2145 cs_ddump_mask(D_EMM, ep->emm, ep->l, "emm:");
2146
2147 if ((!reader[au].fd) || // reader has no fd
2148 (reader[au].caid[0] != b2i(2,ep->caid))) { // wrong caid
2149#ifdef WEBIF
2150 client[cs_idx].emmnok++;
2151#endif
2152 return;
2153 }
2154#ifdef WEBIF
2155 client[cs_idx].emmok++;
2156#endif
2157 ep->cidx = cs_idx;
2158 cs_debug_mask(D_EMM, "emm is being sent to reader %s.", reader[au].label);
2159 write_to_pipe(reader[au].fd, PIP_ID_EMM, (uchar *) ep, sizeof(EMM_PACKET));
2160}
2161
2162static int comp_timeb(struct timeb *tpa, struct timeb *tpb)
2163{
2164 if (tpa->time>tpb->time) return(1);
2165 if (tpa->time<tpb->time) return(-1);
2166 if (tpa->millitm>tpb->millitm) return(1);
2167 if (tpa->millitm<tpb->millitm) return(-1);
2168 return(0);
2169}
2170
2171static void build_delay(struct timeb *tpe, struct timeb *tpc)
2172{
2173 if (comp_timeb(tpe, tpc)>0)
2174 {
2175 tpe->time=tpc->time;
2176 tpe->millitm=tpc->millitm;
2177 }
2178}
2179
2180struct timeval *chk_pending(struct timeb tp_ctimeout)
2181{
2182 int i;
2183 ulong td;
2184 struct timeb tpn, tpe, tpc; // <n>ow, <e>nd, <c>heck
2185 static struct timeval tv;
2186
2187 ECM_REQUEST *er;
2188 cs_ftime(&tpn);
2189 tpe=tp_ctimeout; // latest delay -> disconnect
2190
2191 if (ecmtask)
2192 i=(ph[client[cs_idx].ctyp].multi)?CS_MAXPENDING:1;
2193 else
2194 i=0;
2195//cs_log("num pend=%d", i);
2196 for (--i; i>=0; i--)
2197 if (ecmtask[i].rc>=100) // check all pending ecm-requests
2198 {
2199 int act, j;
2200 er=&ecmtask[i];
2201 tpc=er->tps;
2202 tpc.millitm += (er->stage) ? cfg->ctimeout : cfg->ftimeout;
2203 tpc.time += tpc.millitm / 1000;
2204 tpc.millitm = tpc.millitm % 1000;
2205 if (!er->stage)
2206 {
2207 for (j=0, act=1; (act) && (j<CS_MAXREADER); j++)
2208 {
2209 if (cfg->preferlocalcards && !er->locals_done)
2210 {
2211 if ((er->reader[j]&1) && !(reader[j].typ & R_IS_NETWORK))
2212 act=0;
2213 }
2214 else if (cfg->preferlocalcards && er->locals_done)
2215 {
2216 if ((er->reader[j]&1) && (reader[j].typ & R_IS_NETWORK))
2217 act=0;
2218 }
2219 else
2220 {
2221 if (er->reader[j]&1)
2222 act=0;
2223 }
2224 }
2225//cs_log("stage 0, act=%d r0=%d, r1=%d, r2=%d, r3=%d, r4=%d r5=%d", act,
2226// er->reader[0], er->reader[1], er->reader[2],
2227// er->reader[3], er->reader[4], er->reader[5]);
2228 if (act)
2229 {
2230 int inc_stage = 1;
2231
2232 if (cfg->preferlocalcards && !er->locals_done)
2233 {
2234 int i;
2235
2236 er->locals_done = 1;
2237 for (i = 0; i < CS_MAXREADER; i++)
2238 {
2239 if (reader[i].typ & R_IS_NETWORK)
2240 {
2241 inc_stage = 0;
2242 }
2243 }
2244 }
2245 if (!inc_stage)
2246 {
2247 request_cw(er, er->stage, 2);
2248 tpc.millitm += 1000 * (tpn.time - er->tps.time) + tpn.millitm - er->tps.millitm;
2249 tpc.time += tpc.millitm / 1000;
2250 tpc.millitm = tpc.millitm % 1000;
2251 }
2252 else
2253 {
2254 er->locals_done = 0;
2255 er->stage++;
2256 request_cw(er, er->stage, cfg->preferlocalcards ? 1 : 0);
2257
2258 tpc.millitm += (cfg->ctimeout-cfg->ftimeout);
2259 tpc.time += tpc.millitm / 1000;
2260 tpc.millitm = tpc.millitm % 1000;
2261 }
2262 }
2263 }
2264 if (comp_timeb(&tpn, &tpc)>0) // action needed
2265 {
2266//cs_log("Action now %d.%03d", tpn.time, tpn.millitm);
2267//cs_log(" %d.%03d", tpc.time, tpc.millitm);
2268 if (er->stage)
2269 {
2270 er->rc=5; // timeout
2271 send_dcw(er);
2272 continue;
2273 }
2274 else
2275 {
2276 er->stage++;
2277 request_cw(er, er->stage, 0);
2278 tpc.millitm += (cfg->ctimeout-cfg->ftimeout);
2279 tpc.time += tpc.millitm / 1000;
2280 tpc.millitm = tpc.millitm % 1000;
2281 }
2282 }
2283 build_delay(&tpe, &tpc);
2284 }
2285 td=(tpe.time-tpn.time)*1000+(tpe.millitm-tpn.millitm)+5;
2286 tv.tv_sec = td/1000;
2287 tv.tv_usec = (td%1000)*1000;
2288//cs_log("delay %d.%06d", tv.tv_sec, tv.tv_usec);
2289 return(&tv);
2290}
2291
2292int process_input(uchar *buf, int l, int timeout)
2293{
2294 int rc;
2295 fd_set fds;
2296 struct timeb tp;
2297
2298 if (master_pid!=getppid()) cs_exit(0);
2299 if (!pfd) return(-1);
2300 cs_ftime(&tp);
2301 tp.time+=timeout;
2302 if (ph[client[cs_idx].ctyp].watchdog)
2303 alarm(cfg->cmaxidle + (cfg->ctimeout + 500) / 1000 + 1);
2304 while (1)
2305 {
2306 FD_ZERO(&fds);
2307 FD_SET(pfd, &fds);
2308 FD_SET(fd_m2c, &fds);
2309
2310 rc=select(((pfd>fd_m2c)?pfd:fd_m2c)+1, &fds, 0, 0, chk_pending(tp));
2311 if (master_pid!=getppid()) cs_exit(0);
2312 if (rc<0)
2313 {
2314 if (errno==EINTR) continue;
2315 else return(0);
2316 }
2317
2318 if (FD_ISSET(fd_m2c, &fds)) // read from pipe
2319 chk_dcw(fd_m2c);
2320
2321 if (FD_ISSET(pfd, &fds)) // read from client
2322 {
2323 rc=ph[client[cs_idx].ctyp].recv(buf, l);
2324 break;
2325 }
2326 if (tp.time<=time((time_t *)0)) // client maxidle reached
2327 {
2328 rc=(-9);
2329 break;
2330 }
2331 }
2332 if (ph[client[cs_idx].ctyp].watchdog)
2333 alarm(cfg->cmaxidle + (cfg->ctimeout + 500) / 1000 + 1);
2334 return(rc);
2335}
2336
2337static void process_master_pipe()
2338{
2339 int n;
2340 uchar *ptr;
2341
2342 switch(n=read_from_pipe(mfdr, &ptr, 1))
2343 {
2344 case PIP_ID_LOG:
2345 cs_write_log((char *)ptr);
2346 break;
2347 case PIP_ID_HUP:
2348 cs_accounts_chk();
2349 break;
2350 }
2351}
2352
2353void cs_log_config()
2354{
2355 uchar buf[2048];
2356
2357 if (cfg->nice!=99)
2358 sprintf((char *)buf, ", nice=%d", cfg->nice);
2359 else
2360 buf[0]='\0';
2361 cs_log("version=%s, build #%s, system=%s%s", CS_VERSION_X, CS_SVN_VERSION, cs_platform((char *)buf+64), buf);
2362 cs_log("max. clients=%d, client max. idle=%d sec",
2363#ifdef CS_ANTICASC
2364 CS_MAXPID-3, cfg->cmaxidle);
2365#else
2366 CS_MAXPID-2, cfg->cmaxidle);
2367#endif
2368 if( cfg->max_log_size )
2369 sprintf((char *)buf, "%d Kb", cfg->max_log_size);
2370 else
2371 strcpy((char *)buf, "unlimited");
2372 cs_log("max. logsize=%s", buf);
2373 cs_log("client timeout=%lu ms, fallback timeout=%lu ms, cache delay=%d ms",
2374 cfg->ctimeout, cfg->ftimeout, cfg->delay);
2375#ifdef CS_NOSHM
2376 cs_log("shared memory initialized (size=%d, fd=%d)", shmsize, shmid);
2377#else
2378 cs_log("shared memory initialized (size=%d, id=%d)", shmsize, shmid);
2379#endif
2380}
2381
2382void cs_waitforcardinit()
2383{
2384 if (cfg->waitforcards)
2385 {
2386 int card_init_done, i;
2387 cs_sleepms(3000); // short sleep for card detect to work proberly
2388 do {
2389 card_init_done = 1;
2390 for (i = 0; i < CS_MAXREADER; i++) {
2391 if (reader[i].card_status == CARD_NEED_INIT) {
2392 card_init_done = 0;
2393 break;
2394 }
2395 }
2396 cs_sleepms(300); // wait a little bit
2397 alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
2398 } while (!card_init_done);
2399 }
2400}
2401
2402int main (int argc, char *argv[])
2403{
2404 struct sockaddr_in cad; /* structure to hold client's address */
2405 int scad; /* length of address */
2406 //int fd; /* socket descriptors */
2407 int i, j, n;
2408 int bg=0;
2409 int gfd; //nph,
2410 int fdp[2];
2411 uchar buf[2048];
2412 void (*mod_def[])(struct s_module *)=
2413 {
2414 module_monitor,
2415 module_camd33,
2416 module_camd35,
2417 module_camd35_tcp,
2418 module_newcamd,
2419 module_cccam,
2420#ifdef CS_WITH_GBOX
2421 module_gbox,
2422#endif
2423 module_radegast,
2424 module_oscam_ser,
2425#ifdef HAVE_DVBAPI
2426 module_dvbapi,
2427#endif
2428 0
2429 };
2430
2431 while ((i=getopt(argc, argv, "bc:d:hm:"))!=EOF)
2432 {
2433 switch(i)
2434 {
2435 case 'b': bg=1;
2436 break;
2437 case 'c': cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
2438 break;
2439 case 'd': cs_dblevel=atoi(optarg);
2440 break;
2441 case 'm':
2442#ifdef CS_NOSHM
2443 cs_strncpy(cs_memfile, optarg, sizeof(cs_memfile));
2444 break;
2445#endif
2446 case 'h':
2447 default : usage();
2448 }
2449 }
2450 if (cs_confdir[strlen(cs_confdir)]!='/') strcat(cs_confdir, "/");
2451 init_shm();
2452 init_config();
2453 cfg->debuglvl = cs_dblevel; // give static debuglevel to outer world
2454 for (i=0; mod_def[i]; i++) // must be later BEFORE init_config()
2455 {
2456 memset(&ph[i], 0, sizeof(struct s_module));
2457 mod_def[i](&ph[i]);
2458 }
2459
2460 cs_log("auth size=%d", sizeof(struct s_auth));
2461
2462 init_sidtab();
2463 init_readerdb();
2464 init_userdb();
2465 init_signal();
2466 cs_set_mloc(30, "init");
2467 init_srvid();
2468 init_len4caid();
2469#ifdef IRDETO_GUESSING
2470 init_irdeto_guess_tab();
2471#endif
2472 cs_init_statistics(cfg->usrfile);
2473
2474 if (pipe(fdp))
2475 {
2476 cs_log("Cannot create pipe (errno=%d)", errno);
2477 cs_exit(1);
2478 }
2479 mfdr=fdp[0];
2480 fd_c2m=fdp[1];
2481 gfd=mfdr+1;
2482
2483#ifdef OS_MACOSX
2484 if (bg && daemon_compat(1,0))
2485#else
2486 if (bg && daemon(1,0))
2487#endif
2488 {
2489 cs_log("Error starting in background (errno=%d)", errno);
2490 cs_exit(1);
2491 }
2492 master_pid=client[0].pid=getpid();
2493 if (cfg->pidfile != NULL)
2494 {
2495 FILE *fp;
2496 if (!(fp=fopen(cfg->pidfile, "w")))
2497 {
2498 cs_log("Cannot open pid-file (errno=%d)", errno);
2499 cs_exit(1);
2500 }
2501 fprintf(fp, "%d\n", getpid());
2502 fclose(fp);
2503 }
2504
2505 for (i=0; i<CS_MAX_MOD; i++)
2506 if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
2507 for(j=0; j<ph[i].ptab->nports; j++)
2508 {
2509 start_listener(&ph[i], j);
2510 if( ph[i].ptab->ports[j].fd+1>gfd )
2511 gfd=ph[i].ptab->ports[j].fd+1;
2512 }
2513
2514 //set time for server to now to avoid 0 in monitor/webif
2515 client[0].last=time((time_t *)0);
2516
2517 start_client_resolver();
2518 init_service(97); // logger
2519 init_service(98); // resolver
2520#ifdef WEBIF
2521 init_service(95); // http
2522#endif
2523 init_cardreader();
2524
2525 cs_log("waiting for local card init");
2526 cs_waitforcardinit();
2527 cs_log("init for all local cards done");
2528
2529#ifdef CS_ANTICASC
2530 if( !cfg->ac_enabled )
2531 cs_log("anti cascading disabled");
2532 else
2533 {
2534 init_ac();
2535 init_service(96);
2536 }
2537#endif
2538
2539 for (i=0; i<CS_MAX_MOD; i++)
2540 if (ph[i].type & MOD_CONN_SERIAL) // for now: oscam_ser only
2541 if (ph[i].s_handler)
2542 ph[i].s_handler(i);
2543
2544 cs_close_log();
2545 *mcl=1;
2546 while (1)
2547 {
2548 fd_set fds;
2549
2550 do
2551 {
2552 FD_ZERO(&fds);
2553 FD_SET(mfdr, &fds);
2554 for (i=0; i<CS_MAX_MOD; i++)
2555 if ( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
2556 for (j=0; j<ph[i].ptab->nports; j++)
2557 if (ph[i].ptab->ports[j].fd)
2558 FD_SET(ph[i].ptab->ports[j].fd, &fds);
2559 errno=0;
2560 cs_set_mloc(0, "before select");
2561 select(gfd, &fds, 0, 0, 0);
2562 cs_set_mloc(60, "after select");
2563 } while (errno==EINTR);
2564 cs_set_mloc(-1, "event (global)");
2565
2566 client[0].last=time((time_t *)0);
2567 scad = sizeof(cad);
2568 if (FD_ISSET(mfdr, &fds))
2569 {
2570 cs_set_mloc(-1, "event: master-pipe");
2571 process_master_pipe();
2572 }
2573 for (i=0; i<CS_MAX_MOD; i++)
2574 {
2575 if( (ph[i].type & MOD_CONN_NET) && ph[i].ptab )
2576 {
2577 for( j=0; j<ph[i].ptab->nports; j++ )
2578 {
2579 if( ph[i].ptab->ports[j].fd && FD_ISSET(ph[i].ptab->ports[j].fd, &fds) )
2580 {
2581 if (ph[i].type==MOD_CONN_UDP)
2582 {
2583 cs_set_mloc(-1, "event: udp-socket");
2584 if ((n=recvfrom(ph[i].ptab->ports[j].fd, buf+3, sizeof(buf)-3, 0, (struct sockaddr *)&cad, (socklen_t *)&scad))>0)
2585 {
2586 int idx;
2587 idx=idx_from_ip(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port));
2588 if (!idx)
2589 {
2590 if (pipe(fdp))
2591 {
2592 cs_log("Cannot create pipe (errno=%d)", errno);
2593 cs_exit(1);
2594 }
2595 switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
2596 {
2597 case -1:
2598 close(fdp[0]);
2599 close(fdp[1]);
2600 break;
2601 case 0:
2602 client[idx=cs_last_idx].ufd=fdp[1];
2603 close(fdp[0]);
2604 break;
2605 default:
2606// close(fdp[1]); // now used to simulate event
2607 pfd=fdp[0];
2608 wait4master();
2609 client[cs_idx].ctyp=i;
2610 client[cs_idx].port_idx=j;
2611 client[cs_idx].udp_fd=ph[i].ptab->ports[j].fd;
2612 client[cs_idx].udp_sa=cad;
2613 if (ph[client[cs_idx].ctyp].watchdog)
2614 alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
2615 ph[i].s_handler(cad); // never return
2616 }
2617 }
2618 if (idx)
2619 {
2620 unsigned short rl;
2621 rl=n;
2622 buf[0]='U';
2623 memcpy(buf+1, &rl, 2);
2624 if (!write(client[idx].ufd, buf, n+3)) cs_exit(1);
2625 }
2626 }
2627 }
2628 else
2629 {
2630 cs_set_mloc(-1, "event: tcp-socket");
2631 if ((pfd=accept(ph[i].ptab->ports[j].fd, (struct sockaddr *)&cad, (socklen_t *)&scad))>0)
2632 {
2633 switch(cs_fork(cs_inet_order(cad.sin_addr.s_addr), ntohs(cad.sin_port)))
2634 {
2635 case -1:
2636 case 0:
2637 close(pfd);
2638 break;
2639 default:
2640 wait4master();
2641 client[cs_idx].ctyp=i;
2642 client[cs_idx].udp_fd=pfd;
2643 client[cs_idx].port_idx=j;
2644 if (ph[client[cs_idx].ctyp].watchdog)
2645 alarm(cfg->cmaxidle + cfg->ctimeout / 1000 + 1);
2646 ph[i].s_handler();
2647 }
2648 }
2649 }
2650 }
2651 }
2652 } // if (ph[i].type & MOD_CONN_NET)
2653 }
2654 }
2655 cs_exit(1);
2656}
Note: See TracBrowser for help on using the repository browser.