1 #define MODULE_LOG_PREFIX "camd33"
6 #include "oscam-client.h"
10 #include "oscam-string.h"
14 static int32_t camd33_send(uchar
*buf
, int32_t ml
)
17 if(!cur_client()->pfd
) { return (-1); }
19 memset(buf
+ ml
, 0, l
- ml
);
20 cs_log_dump_dbg(D_CLIENT
, buf
, l
, "send %d bytes to client", l
);
21 if(cur_client()->crypted
)
22 { aes_encrypt_idx(cur_client()->aes_keys
, buf
, l
); }
23 return (send(cur_client()->pfd
, buf
, l
, 0));
26 static int32_t camd33_recv(struct s_client
*client
, uchar
*buf
, int32_t l
)
29 if(!client
->pfd
) { return (-1); }
30 if((n
= cs_recv(client
->pfd
, buf
, l
, 0)) > 0)
32 client
->last
= time((time_t *) 0);
34 { aes_encrypt_idx(cur_client()->aes_keys
, buf
, n
); }
36 cs_log_dump_dbg(D_CLIENT
, buf
, n
, "received %d bytes from client", n
);
40 static void camd33_request_emm(void)
43 struct s_reader
*aureader
= NULL
, *rdr
= NULL
;
45 //TODO: just take the first reader in list
46 LL_ITER itr
= ll_iter_create(cur_client()->aureader_list
);
47 while((rdr
= ll_iter_next(&itr
)))
53 if(!aureader
) { return; }
55 if(aureader
->hexserial
[0])
57 cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
58 username(cur_client()), aureader
->label
, aureader
->caid
,
59 aureader
->auprovid
? aureader
->auprovid
: b2i(4, aureader
->prid
[0]));
61 mbuf
[1] = aureader
->caid
>> 8;
62 mbuf
[2] = aureader
->caid
& 0xff;
63 memcpy(mbuf
+ 3, aureader
->hexserial
, 4);
64 memcpy(mbuf
+ 7, &aureader
->prid
[0][1], 3);
65 memcpy(mbuf
+ 10, &aureader
->prid
[2][1], 3);
66 camd33_send(mbuf
, 13);
70 static void camd33_auth_client(uchar
*camdbug
)
73 uchar
*usr
= NULL
, *pwd
= NULL
;
74 struct s_auth
*account
;
76 struct s_client
*cl
= cur_client();
78 cl
->crypted
= cfg
.c33_crypted
;
81 { cl
->crypted
= !check_ip(cfg
.c33_plain
, cl
->ip
); }
85 if (!aes_set_key_alloc(&cl
->aes_keys
, (char *)cfg
.c33_key
))
87 cs_disconnect_client(cl
);
93 camd33_send(mbuf
, 1); // send login-request
95 for(rc
= 0, camdbug
[0] = 0, mbuf
[0] = 1; (rc
< 2) && (mbuf
[0]); rc
++)
97 i
= process_input(mbuf
, sizeof(mbuf
), 1);
98 if((i
> 0) && (!mbuf
[0]))
101 pwd
= usr
+ strlen((char *)usr
) + 2;
104 { memcpy(camdbug
+ 1, mbuf
, camdbug
[0] = i
); }
106 for(rc
= -1, account
= cfg
.account
; (usr
) && (account
) && (rc
< 0); account
= account
->next
)
107 if(streq((char *)usr
, account
->usr
) && streq((char *)pwd
, account
->pwd
))
108 { rc
= cs_auth_client(cl
, account
, NULL
); }
110 { camd33_request_emm(); }
113 if(rc
< 0) { cs_auth_client(cl
, 0, usr
? "invalid account" : "no user given"); }
114 cs_disconnect_client(cl
);
118 static void camd33_send_dcw(struct s_client
*UNUSED(client
), ECM_REQUEST
*er
)
122 memcpy(mbuf
+ 1, &er
->msgid
, 4); // get pin
123 memcpy(mbuf
+ 5, er
->cw
, 16);
124 camd33_send(mbuf
, 21);
126 { camd33_request_emm(); }
129 static void camd33_process_ecm(uchar
*buf
, int32_t l
)
134 if(!(er
= get_ecmtask()))
136 memcpy(&er
->msgid
, buf
+ 3, 4); // save pin
138 if(er
->ecmlen
< 0 || er
->ecmlen
> MAX_ECM_SIZE
)
139 { NULLFREE(er
); return; }
140 er
->caid
= b2i(2, buf
+ 1);
141 memcpy(er
->ecm
, buf
+ 7, er
->ecmlen
);
142 get_cw(cur_client(), er
);
145 static void camd33_process_emm(uchar
*buf
, int32_t l
)
150 memset(&epg
, 0, sizeof(epg
));
152 if(epg
.emmlen
< 3 || epg
.emmlen
> MAX_EMM_SIZE
)
154 memcpy(epg
.caid
, buf
+ 1, 2);
155 memcpy(epg
.hexserial
, buf
+ 3, 4);
156 memcpy(epg
.emm
, buf
+ 7, epg
.emmlen
);
157 do_emm(cur_client(), &epg
);
160 static void *camd33_server(struct s_client
*UNUSED(client
), uchar
*mbuf
, int32_t n
)
165 camd33_process_ecm(mbuf
, n
);
168 camd33_process_emm(mbuf
, n
);
171 cs_log_dbg(D_CLIENT
, "unknown command !");
177 static void camd33_server_init(struct s_client
*UNUSED(client
))
181 camd33_auth_client(camdbug
);
184 void module_camd33(struct s_module
*ph
)
186 cfg
.c33_crypted
= array_has_nonzero_byte(cfg
.c33_key
, sizeof(cfg
.c33_key
));
188 ph
->ptab
.ports
[0].s_port
= cfg
.c33_port
;
191 ph
->type
= MOD_CONN_TCP
;
192 ph
->large_ecm_support
= 1;
193 ph
->listenertype
= LIS_CAMD33TCP
;
194 IP_ASSIGN(ph
->s_ip
, cfg
.c33_srvip
);
195 ph
->s_handler
= camd33_server
;
196 ph
->s_init
= camd33_server_init
;
197 ph
->recv
= camd33_recv
;
198 ph
->send_dcw
= camd33_send_dcw
;