4 static uchar camdbug
[256]; // camd send wrong order
7 static int camd33_send(uchar
*buf
, int ml
)
12 memset(buf
+ml
, 0, l
-ml
);
13 cs_ddump(buf
, l
, "send %d bytes to client", l
);
14 if (client
[cs_idx
].crypted
)
16 return(send(pfd
, buf
, l
, 0));
19 static int camd33_recv(uchar
*buf
, int l
)
23 if ((n
=recv(pfd
, buf
, l
, 0))>0)
25 client
[cs_idx
].last
=time((time_t *) 0);
26 if (client
[cs_idx
].crypted
)
29 cs_ddump(buf
, n
, "received %d bytes from client", n
);
33 static void camd33_request_emm()
37 if ((au
<0) || (au
>CS_MAXREADER
)) return; // TODO
38 if (reader
[au
].hexserial
[0])
42 mbuf
[1]=reader
[au
].caid
[0]>>8;
43 mbuf
[2]=reader
[au
].caid
[0]&0xff;
44 memcpy(mbuf
+3, reader
[au
].hexserial
, 4);
45 memcpy(mbuf
+7, &reader
[au
].prid
[0][1], 3);
46 memcpy(mbuf
+10, &reader
[au
].prid
[2][1], 3);
47 camd33_send(mbuf
, 13);
51 static int camd33_auth_client(in_addr_t ip
)
54 uchar
*usr
=NULL
, *pwd
=NULL
;
55 struct s_auth
*account
;
57 if (client
[cs_idx
].crypted
=cfg
->c33_crypted
)
60 for (p_ip
=cfg
->c33_plain
; (p_ip
) && (client
[cs_idx
].crypted
); p_ip
=p_ip
->next
)
61 if ((client
[cs_idx
].ip
>=p_ip
->ip
[0]) && (client
[cs_idx
].ip
<=p_ip
->ip
[1]))
62 client
[cs_idx
].crypted
=0;
64 if (client
[cs_idx
].crypted
)
65 aes_set_key(cfg
->c33_key
);
68 camd33_send(mbuf
, 1); // send login-request
70 for (rc
=0, camdbug
[0]=0, mbuf
[0]=1; (rc
<2) && (mbuf
[0]); rc
++)
72 i
=process_input(mbuf
, sizeof(mbuf
), 1);
73 if ((i
>0) && (!mbuf
[0]))
76 pwd
=usr
+strlen(usr
)+2;
79 memcpy(camdbug
+1, mbuf
, camdbug
[0]=i
);
81 for (rc
=-1, account
=cfg
->account
; (usr
) && (account
) && (rc
<0); account
=account
->next
)
82 if ((!strcmp(usr
, account
->usr
)) && (!strcmp(pwd
, account
->pwd
)))
83 rc
=cs_auth_client(account
, NULL
);
88 if (rc
<0) cs_auth_client(0, usr
? "invalid account" : "no user given");
93 static int get_request(uchar
*buf
, int n
)
99 memcpy(buf
, camdbug
+1, rc
=camdbug
[0]);
105 switch (rc
=process_input(buf
, 16, (w
) ? cfg
->ctimeout
: cfg
->cmaxidle
))
110 if ((w
) || cfg
->c33_passive
)
121 if (!memcmp(buf
+1, client
[cs_idx
].usr
, strlen(client
[cs_idx
].usr
)))
123 cs_log("%s still alive", cs_inet_ntoa(client
[cs_idx
].ip
));
131 case 3: w
=boundary(4, buf
[9]+10); break;
132 default: w
=n
; // garbage ?
134 w
=process_input(buf
+16, w
-16, 0);
143 static void camd33_send_dcw(ECM_REQUEST
*er
)
146 memcpy(mbuf
+1, req
+(er
->cpti
*REQ_SIZE
), 4); // get pin
147 memcpy(mbuf
+5, er
->cw
, 16);
148 camd33_send(mbuf
, 21);
149 if (!cfg
->c33_passive
)
150 camd33_request_emm();
153 static void camd33_process_ecm(uchar
*buf
, int l
)
156 if (!(er
=get_ecmtask()))
158 memcpy(req
+(er
->cpti
*REQ_SIZE
), buf
+3, 4); // save pin
160 er
->caid
=b2i(2, buf
+1);
161 memcpy(er
->ecm
, buf
+7, er
->l
);
165 static void camd33_process_emm(uchar
*buf
, int l
)
167 memset(&epg
, 0, sizeof(epg
));
169 memcpy(epg
.caid
, buf
+1, 2);
170 memcpy(epg
.hexserial
, buf
+3, 4);
171 memcpy(epg
.emm
, buf
+7, epg
.l
);
175 static void camd33_server()
179 req
=(uchar
*)malloc(CS_MAXPENDING
*REQ_SIZE
);
182 cs_log("Cannot allocate memory (errno=%d)", errno
);
185 memset(req
, 0, CS_MAXPENDING
*REQ_SIZE
);
187 camd33_auth_client(client
[cs_idx
].ip
);
189 while ((n
=get_request(mbuf
, sizeof(mbuf
)))>0)
194 camd33_process_ecm(mbuf
, n
);
197 camd33_process_emm(mbuf
, n
);
200 cs_debug("unknown command !");
203 cs_disconnect_client();
206 void module_camd33(struct s_module
*ph
)
209 ptab
.ports
[0].s_port
= cfg
->c33_port
;
211 ph
->ptab
->nports
= 1;
213 strcpy(ph
->desc
, "camd 3.3x");
214 ph
->type
=MOD_CONN_TCP
;
215 ph
->logtxt
=cfg
->c33_crypted
? ", crypted" : ", UNCRYPTED!";
218 ph
->s_ip
=cfg
->c33_srvip
;
219 ph
->s_handler
=camd33_server
;
220 ph
->recv
=camd33_recv
;
221 ph
->send_dcw
=camd33_send_dcw
;