source: trunk/module-constcw.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 4.2 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2#include "globals.h"
3
4extern struct s_reader *reader;
5int pserver = 0;
6
7int constcw_file_available(void)
8{
9 FILE *fp;
10
11 fp=fopen(reader[client[cs_idx].ridx].device, "r");
12 if (!fp) return (0);
13 fclose(fp);
14 return (1);
15}
16
17int constcw_analyse_file(ushort c_caid, uint c_prid, ushort c_sid, uchar *dcw)
18{
19 //CAID:PROVIDER:SID:PMT:PID:: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX
20
21 FILE *fp;
22 char token[4096];
23 uint caid, provid, sid, pmt, pid;
24 uchar cw[16];
25
26 // FIXME
27 c_prid = c_prid;
28
29 fp=fopen(reader[client[cs_idx].ridx].device, "r");
30 if (!fp) return (0);
31
32 while (fgets(token, sizeof(token), fp))
33 {
34 if (token[0]=='#') continue;
35
36 sscanf(token, "%x:%x:%x:%x:%x::%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", &caid, &provid, &sid, &pmt, &pid,
37 (uint*) &cw[0], (uint*) &cw[1], (uint*) &cw[2], (uint*) &cw[3],
38 (uint*) &cw[4], (uint*) &cw[5], (uint*) &cw[6], (uint*) &cw[7],
39 (uint*) &cw[8], (uint*) &cw[9], (uint*) &cw[10], (uint*) &cw[11],
40 (uint*) &cw[12], (uint*) &cw[13], (uint*) &cw[14], (uint*) &cw[15]);
41
42 //cs_log("Line found: %s", token);
43 if (c_caid == caid && c_sid == sid)
44 {
45 cs_log("Entry found: %04X:%06X:%04X %s", caid, provid, sid, cs_hexdump(1, cw, 16));
46 memcpy(dcw, cw, 16);
47 fclose(fp);
48 return 1;
49 }
50 }
51
52 fclose(fp);
53 return 0;
54}
55//************************************************************************************************************************
56//* client/server common functions
57//************************************************************************************************************************
58static int constcw_recv(uchar *buf, int l)
59{
60 int ret;
61
62 if (!client[cs_idx].udp_fd) return(-9);
63 ret = read(client[cs_idx].udp_fd, buf, l);
64 if (ret < 1) return(-1);
65 client[cs_idx].last = time(NULL);
66 return(ret);
67}
68
69//************************************************************************************************************************
70//* client functions
71//************************************************************************************************************************
72int constcw_client_init()
73{
74 int fdp[2];
75
76 client[cs_idx].pfd = 0;
77 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fdp))
78 {
79 cs_log("Socket creation failed (%s)", strerror(errno));
80 cs_exit(1);
81 }
82 client[cs_idx].udp_fd =fdp[0];
83 pserver = fdp[1];
84
85 memset((char *) &client[cs_idx].udp_sa, 0, sizeof(client[cs_idx].udp_sa));
86 client[cs_idx].udp_sa.sin_family = AF_INET;
87
88 // Oscam has no reader.au in s_reader like ki's mpcs ;)
89 // reader[ridx].au = 0;
90 // cs_log("local reader: %s (file: %s) constant cw au=0", reader[ridx].label, reader[ridx].device);
91 cs_log("local reader: %s (file: %s) constant cw", reader[client[cs_idx].ridx].label, reader[client[cs_idx].ridx].device);
92
93 client[cs_idx].pfd = client[cs_idx].udp_fd;
94
95 if (constcw_file_available())
96 {
97 reader[client[cs_idx].ridx].tcp_connected = 2;
98 reader[client[cs_idx].ridx].card_status = CARD_INSERTED;
99 }
100
101 return(0);
102}
103
104static int constcw_send_ecm(ECM_REQUEST *er, uchar *msgbuf)
105{
106 time_t t;
107
108 // FIXME
109 msgbuf = msgbuf;
110
111 t = time(NULL);
112 // Check if DCW exist in the files
113 //cs_log("Searching ConstCW for ECM: %04X:%06X:%04X (%d)", er->caid, er->prid, er->srvid, er->l);
114
115 if (constcw_analyse_file(er->caid, er->prid, er->srvid, er->cw)==0)
116 {
117 er->rc = 0;
118 er->rcEx = E1_READER<<4 | E2_SID;
119 //cs_sleepms(100);
120 }
121
122 //cs_sleepms(50);
123 write_ecm_answer(reader, fd_c2m, er);
124
125 client[cs_idx].last = t;
126 reader[client[cs_idx].ridx].last_g = t;
127 return(0);
128}
129
130static int constcw_recv_chk(uchar *dcw, int *rc, uchar *buf, int n)
131{
132 // FIXME
133 dcw = dcw;
134 n = n;
135 buf = buf;
136
137 *rc = 0;
138 return(-1);
139}
140
141void module_constcw(struct s_module *ph)
142{
143 strcpy(ph->desc, "constcw");
144 ph->type = MOD_NO_CONN;
145 ph->multi = 0;
146 ph->watchdog = 1;
147 ph->recv = constcw_recv;
148
149 ph->c_multi = 1;
150 ph->c_init = constcw_client_init;
151 ph->c_recv_chk = constcw_recv_chk;
152 ph->c_send_ecm = constcw_send_ecm;
153 ph->num=R_CONSTCW;
154}
Note: See TracBrowser for help on using the repository browser.