source: trunk/module-camd33.c@ 8455

Last change on this file since 8455 was 8455, checked in by gf, 9 years ago

Make ptab in struct s_module embedded not a pointer.

This blows up bss (static) memory nearly twice but we'll get that
back and lots more once we slim down struct s_port in the next
commit.

  • Property svn:eol-style set to LF
File size: 4.4 KB
Line 
1#include "globals.h"
2#ifdef MODULE_CAMD33
3#include "oscam-aes.h"
4#include "oscam-client.h"
5#include "oscam-ecm.h"
6#include "oscam-emm.h"
7#include "oscam-net.h"
8#include "oscam-string.h"
9
10#define REQ_SIZE 4
11
12static int32_t camd33_send(uchar *buf, int32_t ml)
13{
14 int32_t l;
15 if (!cur_client()->pfd) return(-1);
16 l=boundary(4, ml);
17 memset(buf+ml, 0, l-ml);
18 cs_ddump_mask(D_CLIENT, buf, l, "send %d bytes to client", l);
19 if (cur_client()->crypted)
20 aes_encrypt_idx(cur_client(), buf, l);
21 return(send(cur_client()->pfd, buf, l, 0));
22}
23
24static int32_t camd33_recv(struct s_client * client, uchar *buf, int32_t l)
25{
26 int32_t n;
27 if (!client->pfd) return(-1);
28 if ((n=recv(client->pfd, buf, l, 0))>0)
29 {
30 client->last=time((time_t *) 0);
31 if (client->crypted)
32 aes_encrypt_idx(cur_client(), buf, n);
33 }
34 cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
35 return(n);
36}
37
38static void camd33_request_emm(void)
39{
40 uchar mbuf[20];
41 struct s_reader *aureader = NULL, *rdr = NULL;
42
43 //TODO: just take the first reader in list
44 LL_ITER itr = ll_iter_create(cur_client()->aureader_list);
45 while ((rdr = ll_iter_next(&itr))) {
46 aureader=rdr;
47 break;
48 }
49
50 if (!aureader) return;
51
52 if (aureader->hexserial[0])
53 {
54 cs_log("%s emm-request sent (reader=%s, caid=%04X, auprovid=%06X)",
55 username(cur_client()), aureader->label, aureader->caid,
56 aureader->auprovid ? aureader->auprovid : b2i(4, aureader->prid[0]));
57 mbuf[0]=0;
58 mbuf[1]=aureader->caid>>8;
59 mbuf[2]=aureader->caid&0xff;
60 memcpy(mbuf+3, aureader->hexserial, 4);
61 memcpy(mbuf+7, &aureader->prid[0][1], 3);
62 memcpy(mbuf+10, &aureader->prid[2][1], 3);
63 camd33_send(mbuf, 13);
64 }
65}
66
67static void camd33_auth_client(uchar *camdbug)
68{
69 int32_t i, rc;
70 uchar *usr=NULL, *pwd=NULL;
71 struct s_auth *account;
72 uchar mbuf[1024];
73 struct s_client *cl = cur_client();
74
75 cl->crypted=cfg.c33_crypted;
76
77 if (cl->crypted)
78 cl->crypted = !check_ip(cfg.c33_plain, cl->ip);
79
80 if (cl->crypted)
81 aes_set_key(cl, (char *)cfg.c33_key);
82
83 mbuf[0]=0;
84 camd33_send(mbuf, 1); // send login-request
85
86 for (rc=0, camdbug[0]=0, mbuf[0]=1; (rc<2) && (mbuf[0]); rc++)
87 {
88 i=process_input(mbuf, sizeof(mbuf), 1);
89 if ((i>0) && (!mbuf[0]))
90 {
91 usr=mbuf+1;
92 pwd=usr+strlen((char *)usr)+2;
93 }
94 else
95 memcpy(camdbug+1, mbuf, camdbug[0]=i);
96 }
97 for (rc=-1, account=cfg.account; (usr) && (account) && (rc<0); account=account->next)
98 if (streq((char *)usr, account->usr) && streq((char *)pwd, account->pwd))
99 rc = cs_auth_client(cl, account, NULL);
100 if (!rc)
101 camd33_request_emm();
102 else
103 {
104 if (rc<0) cs_auth_client(cl, 0, usr ? "invalid account" : "no user given");
105 cs_disconnect_client(cl);
106 }
107}
108
109static void camd33_send_dcw(struct s_client *UNUSED(client), ECM_REQUEST *er)
110{
111 uchar mbuf[1024];
112 mbuf[0]=2;
113 memcpy(mbuf+1, &er->msgid, 4); // get pin
114 memcpy(mbuf+5, er->cw, 16);
115 camd33_send(mbuf, 21);
116 if (!cfg.c33_passive)
117 camd33_request_emm();
118}
119
120static void camd33_process_ecm(uchar *buf, int32_t l)
121{
122 ECM_REQUEST *er;
123 if (!(er=get_ecmtask()))
124 return;
125 memcpy(&er->msgid, buf+3, 4); // save pin
126 er->ecmlen=l-7;
127 er->caid=b2i(2, buf+1);
128 memcpy(er->ecm , buf+7, er->ecmlen);
129 get_cw(cur_client(), er);
130}
131
132static void camd33_process_emm(uchar *buf, int32_t l)
133{
134 EMM_PACKET epg;
135 memset(&epg, 0, sizeof(epg));
136 epg.emmlen=l-7;
137 memcpy(epg.caid , buf+1, 2);
138 memcpy(epg.hexserial, buf+3, 4);
139 memcpy(epg.emm , buf+7, epg.emmlen);
140 do_emm(cur_client(), &epg);
141}
142
143static void * camd33_server(struct s_client *UNUSED(client), uchar *mbuf, int32_t n)
144{
145 switch(mbuf[0]) {
146 case 2:
147 camd33_process_ecm(mbuf, n);
148 break;
149 case 3:
150 camd33_process_emm(mbuf, n);
151 break;
152 default:
153 cs_debug_mask(D_CLIENT, "unknown command !");
154 }
155
156 return NULL;
157}
158
159static void camd33_server_init(struct s_client *UNUSED(client)) {
160 uchar camdbug[256];
161
162 camd33_auth_client(camdbug);
163}
164
165void module_camd33(struct s_module *ph)
166{
167 cfg.c33_crypted = check_filled(cfg.c33_key, sizeof(cfg.c33_key));
168 ph->ptab.nports = 1;
169 ph->ptab.ports[0].s_port = cfg.c33_port;
170
171 ph->desc="camd33";
172 ph->type=MOD_CONN_TCP;
173 ph->large_ecm_support = 1;
174 ph->listenertype = LIS_CAMD33TCP;
175 IP_ASSIGN(ph->s_ip, cfg.c33_srvip);
176 ph->s_handler=camd33_server;
177 ph->s_init=camd33_server_init;
178 ph->recv=camd33_recv;
179 ph->send_dcw=camd33_send_dcw;
180 ph->num=R_CAMD33;
181}
182#endif
Note: See TracBrowser for help on using the repository browser.