revert breaks some stupid old compilers
[oscam.git] / module-pandora.c
blob80ff2b29eba5817be4f969192b7f3f5fd15f92c1
1 #define MODULE_LOG_PREFIX "pandora"
3 #include "globals.h"
5 #ifdef MODULE_PANDORA
7 #include "cscrypt/md5.h"
8 #include "oscam-client.h"
9 #include "oscam-ecm.h"
10 #include "oscam-net.h"
11 #include "oscam-string.h"
13 #define CWS_NETMSGSIZE 320
14 #define START_TIME 150000
15 #define MAX_TIME 500000
17 static void simple_crypt(uchar *buf, int len, uchar *key, int key_len)
19 int i, x;
20 for(i = 0, x = 0; i < len; i++)
22 buf[i] ^= key[x++];
23 if(x >= key_len)
24 { x = 0; }
28 static void pandora_process_request(struct s_client *cl, uchar *buf, int32_t l)
30 int ecmlen;
31 ECM_REQUEST *er;
32 uchar md5tmp[MD5_DIGEST_LENGTH];
34 if(l < 10 + CS_ECMSTORESIZE + 2)
35 { return; }
37 if(!(er = get_ecmtask()))
38 { return; }
39 er->caid = b2i(2, buf + 1);
40 er->srvid = b2i(2, buf + 3);
41 er->prid = b2i(4, buf + 5);
42 //er->ecmcrc32 = crc32(0L, buf+10, CS_ECMSTORESIZE);
43 er->chid = b2i(2, buf + 10 + CS_ECMSTORESIZE);
45 if(!cl->pand_ignore_ecm && (l >= 10 + CS_ECMSTORESIZE + 2 + 2))
47 ecmlen = b2i(2, buf + 10 + CS_ECMSTORESIZE + 2);
49 if(ecmlen < 0 || ecmlen > MAX_ECM_SIZE
50 || ((10 + CS_ECMSTORESIZE + 2 + 2 + ecmlen) > CWS_NETMSGSIZE)
51 || ((10 + CS_ECMSTORESIZE + 2 + 2 + ecmlen) > l))
53 er->ecmlen = 0;
55 else
57 if(!memcmp(buf + 10,
58 MD5(buf + 14 + CS_ECMSTORESIZE, ecmlen, md5tmp),
59 CS_ECMSTORESIZE))
61 er->ecmlen = ecmlen;
62 memcpy(er->ecm, buf + 14 + CS_ECMSTORESIZE, ecmlen);
63 //set_ecmhash(cl, er);
65 else
66 { er->ecmlen = 0; }
69 else
70 { er->ecmlen = 0; }
72 if(!er->ecmlen)
73 { usleep(cl->pand_autodelay); }
74 get_cw(cl, er);
77 static int pandora_recv(struct s_client *cl, uchar *buf, int32_t l)
79 int ret;
81 if(!cl->udp_fd)
82 { return (-9); }
83 if(cl->typ != 'c')
84 { ret = recv_from_udpipe(buf); }
85 else
87 ret = recvfrom(cl->udp_fd, buf, l, 0, (struct sockaddr *)&cl->udp_sa, &cl->udp_sa_len);
89 if(ret < 1)
90 { return (-1); }
92 simple_crypt(buf, ret, cl->pand_md5_key, 16);
93 cl->last = time((time_t *) 0);
95 if(cl->typ != 'c')
96 { pandora_process_request(cl, buf, ret); }
97 return (ret);
100 static void pandora_send_dcw(struct s_client *cl, ECM_REQUEST *er)
102 uchar msgbuf[CWS_NETMSGSIZE], len;
103 if(cfg.pand_skip_send_dw)
104 { return; }
105 if(er->rc < E_NOTFOUND)
107 msgbuf[0] = 2; //DW_FOUND
108 memcpy(&msgbuf[1], er->cw, 16);
109 len = 1 + 16;
110 cl->pand_autodelay = START_TIME;
112 else
114 msgbuf[0] = 0xFF; //DW_NOT_FOUND
115 len = 1;
116 if(cl->pand_autodelay < MAX_TIME)
117 { cl->pand_autodelay += 100000; }
119 simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
120 sendto(cl->udp_fd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
123 int pandora_auth_client(struct s_client *cl, IN_ADDR_T ip)
125 int ok;
126 struct s_auth *account;
128 #ifdef IPV6SUPPORT
129 // FIXME: Add IPv6 support
130 (void)ip; // Prevent warning about unused var "ip"
131 #else
132 if(!cl->pand_ignore_ecm && cfg.pand_allowed)
134 struct s_ip *p_ip;
135 for(ok = 0, p_ip = cfg.pand_allowed; (p_ip) && (!ok); p_ip
136 = p_ip->next)
137 { ok = ((ip >= p_ip->ip[0]) && (ip <= p_ip->ip[1])); }
139 if(!ok)
141 cs_auth_client(cl, (struct s_auth *) 0, "IP not allowed");
142 return 0;
145 #endif
147 for(ok = 0, account = cfg.account; cfg.pand_usr && account && !ok; account = account->next)
149 ok = streq(cfg.pand_usr, account->usr);
150 if(ok && cs_auth_client(cl, account, NULL))
151 { cs_disconnect_client(cl); }
153 if(!ok)
154 { cs_auth_client(cl, (struct s_auth *)(-1), NULL); }
155 return ok;
158 static void *pandora_server(struct s_client *cl, uchar *UNUSED(mbuf),
159 int32_t UNUSED(len))
161 uchar md5tmp[MD5_DIGEST_LENGTH];
162 if(!cl->init_done)
164 if(cfg.pand_pass)
166 cl->pand_autodelay = 150000;
167 memcpy(cl->pand_md5_key,
168 MD5((uchar *)cfg.pand_pass, strlen(cfg.pand_pass), md5tmp), 16);
169 cl->pand_ignore_ecm = (cfg.pand_ecm) ? 0 : 1;
170 cl->crypted = 1;
171 pandora_auth_client(cl, cl->ip);
172 cl->init_done = 1;
174 else
176 cs_log("Password for Pandora share MUST be set !!!");
179 return NULL;
182 /************************************************************************************************************************
183 * client functions
184 *************************************************************************************************************************/
185 int pandora_client_init(struct s_client *cl)
187 static struct sockaddr_in loc_sa;
188 int16_t p_proto;
189 char ptxt[16];
190 struct s_reader *rdr = cl->reader;
191 uchar md5tmp[MD5_DIGEST_LENGTH];
193 cl->pfd = 0;
194 if(rdr->r_port <= 0)
196 cs_log("invalid port %d for server %s", rdr->r_port, rdr->device);
197 return (1);
199 p_proto = IPPROTO_UDP;
201 set_null_ip(&cl->ip);
202 memset((char *) &loc_sa, 0, sizeof(loc_sa));
203 loc_sa.sin_family = AF_INET;
205 if(IP_ISSET(cfg.srvip))
206 { IP_ASSIGN(SIN_GET_ADDR(loc_sa), cfg.srvip); }
207 else
208 { loc_sa.sin_addr.s_addr = INADDR_ANY; }
209 loc_sa.sin_port = htons(rdr->l_port);
211 if((cl->udp_fd = socket(PF_INET, SOCK_DGRAM, p_proto)) < 0)
213 cs_log("Socket creation failed (errno=%d)", errno);
214 return 1;
217 int32_t opt = 1;
218 setsockopt(cl->udp_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
220 set_so_reuseport(cl->udp_fd);
222 set_socket_priority(cl->udp_fd, cfg.netprio);
224 if(rdr->l_port > 0)
226 if(bind(cl->udp_fd, (struct sockaddr *) &loc_sa, sizeof(loc_sa)) < 0)
228 cs_log("bind failed (errno=%d)", errno);
229 close(cl->udp_fd);
230 return (1);
232 snprintf(ptxt, sizeof(ptxt), ", port=%d", rdr->l_port);
234 else
235 { ptxt[0] = '\0'; }
237 memcpy(cl->pand_md5_key, MD5((uchar *)rdr->r_pwd, strlen(rdr->r_pwd), md5tmp), 16);
238 cl->crypted = 1;
240 //cl->grp = 0xFFFFFFFF;
241 //rdr->caid[0] = rdr->ctab.caid[0];
243 cl->pand_send_ecm = rdr->pand_send_ecm;
244 memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
245 #ifdef IPV6SUPPORT
246 ((struct sockaddr_in *)(&cl->udp_sa))->sin_family = AF_INET;
247 ((struct sockaddr_in *)(&cl->udp_sa))->sin_port = htons((u_short) rdr->r_port);
248 #else
249 cl->udp_sa.sin_family = AF_INET;
250 cl->udp_sa.sin_port = htons((u_short) rdr->r_port);
251 #endif
253 cs_log("proxy %s:%d pandora %s (%s)", rdr->device, rdr->r_port, rdr->pand_send_ecm ? "with ECM support" : "", ptxt);
255 cl->pfd = cl->udp_fd;
256 //set_nonblock(cl->udp_fd, true); //!!!!!
257 return (0);
260 static int pandora_send_ecm(struct s_client *cl, ECM_REQUEST *er)
262 uchar md5tmp[MD5_DIGEST_LENGTH];
263 uchar msgbuf[CWS_NETMSGSIZE];
264 int ret, len;
265 uchar adel;
266 adel = (cfg.ctimeout > 7) ? 7 : cfg.ctimeout;
268 msgbuf[0] = 1;
269 msgbuf[1] = er->caid >> 8;
270 msgbuf[2] = er->caid & 0xFF;
271 msgbuf[3] = er->srvid >> 8;
272 msgbuf[4] = er->srvid & 0xFF;
273 msgbuf[5] = er->prid >> 24;
274 msgbuf[6] = er->prid >> 16;
275 msgbuf[7] = er->prid >> 8;
276 msgbuf[8] = er->prid & 0xFF;
277 msgbuf[9] = adel;
278 memcpy(&msgbuf[10], MD5(er->ecm, er->ecmlen, md5tmp), CS_ECMSTORESIZE);
279 msgbuf[10 + CS_ECMSTORESIZE] = er->chid >> 8;
280 msgbuf[11 + CS_ECMSTORESIZE] = er->chid & 0xFF;
281 len = 12 + CS_ECMSTORESIZE;
282 if(cl->pand_send_ecm)
284 if(len+2+er->ecmlen > CWS_NETMSGSIZE)
285 { return -1; }
286 msgbuf[12 + CS_ECMSTORESIZE] = er->ecmlen >> 8;
287 msgbuf[13 + CS_ECMSTORESIZE] = er->ecmlen & 0xFF;
288 memcpy(&msgbuf[14 + CS_ECMSTORESIZE], er->ecm, er->ecmlen);
289 len += er->ecmlen + 2;
291 simple_crypt(msgbuf, len, cl->pand_md5_key, 16);
292 ret = sendto(cl->pfd, msgbuf, len, 0, (struct sockaddr *) &cl->udp_sa, cl->udp_sa_len);
293 return ((ret < len) ? (-1) : 0);
296 static int pandora_recv_chk(struct s_client *UNUSED(cl), uchar *dcw, int *rc,
297 uchar *buf, int UNUSED(n))
299 if(buf[0] != 0x2)
300 { return (-1); }
301 *rc = 1;
302 memcpy(dcw, buf + 1, 16);
303 return (0);
306 void module_pandora(struct s_module *ph)
308 ph->ptab.nports = 1;
309 ph->ptab.ports[0].s_port = cfg.pand_port;
310 ph->num = R_PANDORA;
312 ph->desc = "pandora";
313 ph->type = MOD_CONN_UDP;
314 ph->large_ecm_support = 1;
315 //ph->watchdog = 1;
316 IP_ASSIGN(ph->s_ip, cfg.pand_srvip);
317 ph->s_handler = pandora_server;
318 ph->recv = pandora_recv;
319 ph->send_dcw = pandora_send_dcw;
321 ph->c_init = pandora_client_init;
322 ph->c_recv_chk = pandora_recv_chk;
323 ph->c_send_ecm = pandora_send_ecm;
326 #endif