1 #define MODULE_LOG_PREFIX "camd33"
6 #include "oscam-client.h"
10 #include "oscam-string.h"
14 static int32_t camd33_send(uint8_t *buf
, int32_t ml
)
18 if(!cur_client()->pfd
)
24 memset(buf
+ ml
, 0, l
- ml
);
25 cs_log_dump_dbg(D_CLIENT
, buf
, l
, "send %d bytes to client", l
);
27 if(cur_client()->crypted
)
29 aes_encrypt_idx(cur_client()->aes_keys
, buf
, l
);
32 return (send(cur_client()->pfd
, buf
, l
, 0));
35 static int32_t camd33_recv(struct s_client
*client
, uint8_t *buf
, int32_t l
)
44 if((n
= cs_recv(client
->pfd
, buf
, l
, 0)) > 0)
46 client
->last
= time((time_t *)0);
49 aes_encrypt_idx(cur_client()->aes_keys
, buf
, n
);
52 cs_log_dump_dbg(D_CLIENT
, buf
, n
, "received %d bytes from client", n
);
57 static void camd33_request_emm(void)
60 struct s_reader
*aureader
= NULL
, *rdr
= NULL
;
62 // TODO: just take the first reader in list
63 LL_ITER itr
= ll_iter_create(cur_client()->aureader_list
);
64 while((rdr
= ll_iter_next(&itr
)))
75 if(aureader
->hexserial
[0])
77 cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
78 username(cur_client()), aureader
->label
, aureader
->caid
,
79 aureader
->auprovid
? aureader
->auprovid
: b2i(4, aureader
->prid
[0]));
82 mbuf
[1] = aureader
->caid
>> 8;
83 mbuf
[2] = aureader
->caid
& 0xff;
85 memcpy(mbuf
+ 3, aureader
->hexserial
, 4);
86 memcpy(mbuf
+ 7, &aureader
->prid
[0][1], 3);
87 memcpy(mbuf
+ 10, &aureader
->prid
[2][1], 3);
88 camd33_send(mbuf
, 13);
92 static void camd33_auth_client(uint8_t *camdbug
)
95 uint8_t *usr
= NULL
, *pwd
= NULL
;
96 struct s_auth
*account
;
98 struct s_client
*cl
= cur_client();
100 cl
->crypted
= cfg
.c33_crypted
;
103 cl
->crypted
= !check_ip(cfg
.c33_plain
, cl
->ip
);
108 if (!aes_set_key_alloc(&cl
->aes_keys
, (char *)cfg
.c33_key
))
110 cs_disconnect_client(cl
);
116 camd33_send(mbuf
, 1); // send login-request
118 for(rc
= 0, camdbug
[0] = 0, mbuf
[0] = 1; (rc
< 2) && (mbuf
[0]); rc
++)
120 i
= process_input(mbuf
, sizeof(mbuf
), 1);
121 if((i
> 0) && (!mbuf
[0]))
124 pwd
= usr
+ cs_strlen((char *)usr
) + 2;
128 memcpy(camdbug
+ 1, mbuf
, camdbug
[0] = i
);
132 for(rc
= -1, account
= cfg
.account
; (usr
) && (account
) && (rc
< 0); account
= account
->next
)
134 if(streq((char *)usr
, account
->usr
) && streq((char *)pwd
, account
->pwd
))
136 rc
= cs_auth_client(cl
, account
, NULL
);
142 camd33_request_emm();
148 cs_auth_client(cl
, 0, usr
? "invalid account" : "no user given");
150 cs_disconnect_client(cl
);
154 static void camd33_send_dcw(struct s_client
*UNUSED(client
), ECM_REQUEST
*er
)
159 memcpy(mbuf
+ 1, &er
->msgid
, 4); // get pin
160 memcpy(mbuf
+ 5, er
->cw
, 16);
161 camd33_send(mbuf
, 21);
165 camd33_request_emm();
169 static void camd33_process_ecm(uint8_t *buf
, int32_t l
)
178 if(!(er
= get_ecmtask()))
183 memcpy(&er
->msgid
, buf
+ 3, 4); // save pin
186 if(er
->ecmlen
< 0 || er
->ecmlen
> MAX_ECM_SIZE
)
192 er
->caid
= b2i(2, buf
+ 1);
193 memcpy(er
->ecm
, buf
+ 7, er
->ecmlen
);
194 get_cw(cur_client(), er
);
197 static void camd33_process_emm(uint8_t *buf
, int32_t l
)
206 memset(&epg
, 0, sizeof(epg
));
209 if(epg
.emmlen
< 3 || epg
.emmlen
> MAX_EMM_SIZE
)
214 memcpy(epg
.caid
, buf
+ 1, 2);
215 memcpy(epg
.hexserial
, buf
+ 3, 4);
216 memcpy(epg
.emm
, buf
+ 7, epg
.emmlen
);
217 do_emm(cur_client(), &epg
);
220 static void *camd33_server(struct s_client
*UNUSED(client
), uint8_t *mbuf
, int32_t n
)
225 camd33_process_ecm(mbuf
, n
);
229 camd33_process_emm(mbuf
, n
);
233 cs_log_dbg(D_CLIENT
, "unknown command!");
239 static void camd33_server_init(struct s_client
*UNUSED(client
))
241 uint8_t camdbug
[256];
243 camd33_auth_client(camdbug
);
246 void module_camd33(struct s_module
*ph
)
248 cfg
.c33_crypted
= array_has_nonzero_byte(cfg
.c33_key
, sizeof(cfg
.c33_key
));
250 ph
->ptab
.ports
[0].s_port
= cfg
.c33_port
;
252 ph
->type
= MOD_CONN_TCP
;
253 ph
->large_ecm_support
= 1;
254 ph
->listenertype
= LIS_CAMD33TCP
;
255 IP_ASSIGN(ph
->s_ip
, cfg
.c33_srvip
);
256 ph
->s_handler
= camd33_server
;
257 ph
->s_init
= camd33_server_init
;
258 ph
->recv
= camd33_recv
;
259 ph
->send_dcw
= camd33_send_dcw
;