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