-
Notifications
You must be signed in to change notification settings - Fork 30
/
module-emulator-icam.c
153 lines (125 loc) · 3.41 KB
/
module-emulator-icam.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
144
145
146
147
148
149
150
151
152
153
#define MODULE_LOG_PREFIX "emu"
#include "globals.h"
#include "oscam-net.h"
#include "oscam-chk.h"
#include "module-emulator-icam.h"
#include "oscam-ecm.h"
#include "oscam-client.h"
#include "ffdecsa/ffdecsa.h"
#ifdef WITH_EMU
#ifdef MODULE_RADEGAST
static int32_t gRadegastFd = 0;
static uint8_t gLast_ecm_paket[EMU_STREAM_SERVER_MAX_CONNECTIONS][8];
#endif
bool caid_is_icam(uint16_t caid)
{
if (caid == 0x098C || caid == 0x098D || caid == 0x09C4)
return true;
return false;
}
void icam_write_cw(ECM_REQUEST *er, int32_t connid)
{
SAFE_MUTEX_LOCK(&emu_fixed_key_data_mutex[connid]);
if (emu_fixed_key_data[connid].icam_csa_ks == NULL)
{
emu_fixed_key_data[connid].icam_csa_ks = get_key_struct();
}
bool icam = (er->ecm[2] - er->ecm[4]) == 4;
if (er->ecm[0] == 0x80)
{
if (icam)
{
set_even_control_word_ecm(emu_fixed_key_data[connid].icam_csa_ks, er->cw, er->ecm[0x15]);
}
else
{
set_even_control_word(emu_fixed_key_data[connid].icam_csa_ks, er->cw);
}
}
else if (icam)
{
set_odd_control_word_ecm(emu_fixed_key_data[connid].icam_csa_ks, er->cw + 8, er->ecm[0x15]);
}
else
{
set_odd_control_word(emu_fixed_key_data[connid].icam_csa_ks, er->cw + 8);
}
emu_fixed_key_data[connid].icam_csa_used = 1;
SAFE_MUTEX_UNLOCK(&emu_fixed_key_data_mutex[connid]);
}
#ifdef MODULE_RADEGAST
void icam_ecm(emu_stream_client_data *cdata)
{
uint16_t section_length = SCT_LEN(cdata->ecm_data);
uint16_t packet_len;
static uint8_t header_len = 2;
static uint8_t payload_static_len = 12;
if (memcmp(gLast_ecm_paket[cdata->connid], cdata->ecm_data, 8) != 0)
{
memcpy(gLast_ecm_paket[cdata->connid], cdata->ecm_data, 8);
if (gRadegastFd <= 0)
icam_connect_to_radegast();
packet_len = header_len + payload_static_len + section_length;
uint8_t outgoing_data[packet_len];
outgoing_data[0] = 1;
outgoing_data[1] = payload_static_len + section_length;
outgoing_data[2] = 10; // caid
outgoing_data[3] = 2;
outgoing_data[4] = cdata->caid >> 8;
outgoing_data[5] = cdata->caid & 0xFF;
outgoing_data[6] = 9; // srvid
outgoing_data[7] = 4;
outgoing_data[8] = cdata->srvid & 0xFF;
outgoing_data[10] = cdata->srvid >> 8;
outgoing_data[12] = 3;
outgoing_data[13] = section_length;
memcpy(outgoing_data + header_len + payload_static_len, cdata->ecm_data, section_length);
if (!icam_send_to_radegast(outgoing_data, packet_len))
{
icam_close_radegast_connection();
if (icam_connect_to_radegast())
{
icam_send_to_radegast(outgoing_data, packet_len);
}
}
}
}
bool icam_connect_to_radegast(void)
{
struct sockaddr_in cservaddr;
if (gRadegastFd == 0)
gRadegastFd = socket(AF_INET, SOCK_STREAM, 0);
if (gRadegastFd < 0)
{
gRadegastFd = 0;
return false;
}
int32_t flags = fcntl(gRadegastFd, F_GETFL);
fcntl(gRadegastFd, F_SETFL, flags | O_NONBLOCK);
bzero(&cservaddr, sizeof(cservaddr));
cservaddr.sin_family = AF_INET;
SIN_GET_ADDR(cservaddr) = cfg.rad_srvip;
cservaddr.sin_port = htons(cfg.rad_port);
connect(gRadegastFd,(struct sockaddr *)&cservaddr, sizeof(cservaddr));
return true;
}
void icam_reset(int32_t connid)
{
memset(gLast_ecm_paket[connid], 0, 8);
}
void icam_close_radegast_connection(void)
{
close(gRadegastFd);
gRadegastFd = 0;
}
bool icam_send_to_radegast(uint8_t* data, int len)
{
if (send(gRadegastFd, data, len, 0) < 0)
{
cs_log("icam_send_to_radegast: Send failure");
return false;
}
return true;
}
#endif // MODULE_RADEGAST
#endif // WITH_EMU