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