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