revert breaks some stupid old compilers
[oscam.git] / module-camd33.c
blob50218bc8f9173789ac9605f1896cf4572c93cd51
1 #define MODULE_LOG_PREFIX "camd33"
3 #include "globals.h"
4 #ifdef MODULE_CAMD33
5 #include "oscam-aes.h"
6 #include "oscam-client.h"
7 #include "oscam-ecm.h"
8 #include "oscam-emm.h"
9 #include "oscam-net.h"
10 #include "oscam-string.h"
12 #define REQ_SIZE 4
14 static int32_t camd33_send(uchar *buf, int32_t ml)
16 int32_t l;
17 if(!cur_client()->pfd) { return (-1); }
18 l = boundary(4, ml);
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)
28 int32_t n;
29 if(!client->pfd) { return (-1); }
30 if((n = cs_recv(client->pfd, buf, l, 0)) > 0)
32 client->last = time((time_t *) 0);
33 if(client->crypted)
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);
37 return (n);
40 static void camd33_request_emm(void)
42 uchar mbuf[20];
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)))
49 aureader = rdr;
50 break;
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]));
60 mbuf[0] = 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)
72 int32_t i, rc;
73 uchar *usr = NULL, *pwd = NULL;
74 struct s_auth *account;
75 uchar mbuf[1024];
76 struct s_client *cl = cur_client();
78 cl->crypted = cfg.c33_crypted;
80 if(cl->crypted)
81 { cl->crypted = !check_ip(cfg.c33_plain, cl->ip); }
83 if(cl->crypted)
85 if (!aes_set_key_alloc(&cl->aes_keys, (char *)cfg.c33_key))
87 cs_disconnect_client(cl);
88 return;
92 mbuf[0] = 0;
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]))
100 usr = mbuf + 1;
101 pwd = usr + strlen((char *)usr) + 2;
103 else
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); }
109 if(!rc)
110 { camd33_request_emm(); }
111 else
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)
120 uchar mbuf[128];
121 mbuf[0] = 2;
122 memcpy(mbuf + 1, &er->msgid, 4); // get pin
123 memcpy(mbuf + 5, er->cw, 16);
124 camd33_send(mbuf, 21);
125 if(!cfg.c33_passive)
126 { camd33_request_emm(); }
129 static void camd33_process_ecm(uchar *buf, int32_t l)
131 ECM_REQUEST *er;
132 if(l < 7)
133 { return; }
134 if(!(er = get_ecmtask()))
135 { return; }
136 memcpy(&er->msgid, buf + 3, 4); // save pin
137 er->ecmlen = l - 7;
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)
147 EMM_PACKET epg;
148 if(l < 7)
149 { return; }
150 memset(&epg, 0, sizeof(epg));
151 epg.emmlen = l - 7;
152 if(epg.emmlen < 3 || epg.emmlen > MAX_EMM_SIZE)
153 { return; }
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)
162 switch(mbuf[0])
164 case 2:
165 camd33_process_ecm(mbuf, n);
166 break;
167 case 3:
168 camd33_process_emm(mbuf, n);
169 break;
170 default:
171 cs_log_dbg(D_CLIENT, "unknown command !");
174 return NULL;
177 static void camd33_server_init(struct s_client *UNUSED(client))
179 uchar camdbug[256];
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));
187 ph->ptab.nports = 1;
188 ph->ptab.ports[0].s_port = cfg.c33_port;
190 ph->desc = "camd33";
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;
199 ph->num = R_CAMD33;
201 #endif