source: trunk/module-radegast.c@ 4149

Last change on this file since 4149 was 4141, checked in by dingo35, 13 years ago

all: simplify debug system, add D_DVBAPI = -d128, eliminate cs_ptyp which complicates stuff unnecc

File size: 7.8 KB
Line 
1#include "globals.h"
2
3static int radegast_send(struct s_client * client, uchar *buf)
4{
5 int l=buf[1]+2;
6 return(send(client->pfd, buf, l, 0));
7}
8
9static int radegast_recv(struct s_client *client, uchar *buf, int l)
10{
11 int n;
12 if (!client->pfd) return(-1);
13 if (client->typ == 'c') { // server code
14 if ((n=recv(client->pfd, buf, l, 0))>0)
15 client->last=time((time_t *) 0);
16 } else { // client code
17 if ((n=recv(client->pfd, buf, l, 0))>0) {
18 cs_ddump_mask(D_CLIENT, buf, n, "radegast: received %d bytes from %s", n, remote_txt());
19 client->last = time((time_t *) 0);
20
21 if (buf[0] == 2) { // dcw received
22 if (buf[3] != 0x10) { // dcw ok
23 cs_log("radegast: no dcw");
24 n = -1;
25 }
26 }
27 }
28 }
29 return(n);
30}
31
32static int radegast_recv_chk(struct s_client *client, uchar *dcw, int *rc, uchar *buf, int UNUSED(n))
33{
34 if ((buf[0] == 2) && (buf[1] == 0x12)) {
35 memcpy(dcw, buf+4, 16);
36 cs_debug_mask(D_CLIENT, "radegast: recv chk - %s", cs_hexdump(0, dcw, 16));
37 *rc = 1;
38 return(client->reader->msg_idx);
39 }
40
41 return (-1);
42}
43
44static void radegast_auth_client(in_addr_t ip)
45{
46 int ok;
47 struct s_auth *account;
48 struct s_ip *p_ip;
49
50 for (ok=0, p_ip=cfg->rad_allowed; (p_ip) && (!ok); p_ip=p_ip->next)
51 ok=((ip>=p_ip->ip[0]) && (ip<=p_ip->ip[1]));
52
53 if (!ok)
54 {
55 cs_auth_client(cur_client(), (struct s_auth *)0, NULL);
56 cs_exit(0);
57 }
58
59 for (ok=0, account=cfg->account; (cfg->rad_usr[0]) && (account) && (!ok); account=account->next)
60 {
61 ok=(!strcmp(cfg->rad_usr, account->usr));
62 if (ok && cs_auth_client(cur_client(), account, NULL))
63 cs_exit(0);
64 }
65
66 if (!ok)
67 cs_auth_client(cur_client(), (struct s_auth *)(-1), NULL);
68}
69
70static int get_request(uchar *buf)
71{
72 int n, rc=0;
73 if ((n=process_input(buf, 2, cfg->cmaxidle))==2)
74 {
75 if ((n=process_input(buf+2, buf[1], 0))>=0)
76 n+=2;
77 if (n-2==buf[1])
78 rc=n;
79 else
80 cs_log("WARNING: protocol error (garbage)");
81 }
82 if (n>0)
83 {
84 cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from client", n);
85 }
86 return(rc);
87}
88
89static void radegast_send_dcw(struct s_client *client, ECM_REQUEST *er)
90{
91 uchar mbuf[1024];
92 mbuf[0]=0x02; // DCW
93 if (er->rc<4)
94 {
95 mbuf[1]=0x12; // len (overall)
96 mbuf[2]=0x05; // ACCESS
97 mbuf[3]=0x10; // len
98 memcpy(mbuf+4, er->cw, 16);
99 }
100 else
101 {
102 mbuf[1]=0x02; // len (overall)
103 mbuf[2]=0x04; // NO ACCESS
104 mbuf[3]=0x00; // len
105 }
106 radegast_send(client, mbuf);
107}
108
109static void radegast_process_ecm(uchar *buf, int l)
110{
111 int i, n, sl;
112 ECM_REQUEST *er;
113
114 if (!(er=get_ecmtask()))
115 return;
116 for (i=0; i<l; i+=(sl+2))
117 {
118 sl=buf[i+1];
119 switch(buf[i])
120 {
121 case 2: // CAID (upper byte only, oldstyle)
122 er->caid=buf[i+2]<<8;
123 break;
124 case 10: // CAID
125 er->caid=b2i(2, buf+i+2);
126 break;
127 case 3: // ECM DATA
128 er->l=sl;
129 memcpy(er->ecm, buf+i+2, er->l);
130 break;
131 case 6: // PROVID (ASCII)
132 n=(sl>6) ? 3 : (sl>>1);
133 er->prid=cs_atoi((char *) buf+i+2+sl-(n<<1), n, 0);
134 break;
135 case 7: // KEYNR (ASCII), not needed
136 break;
137 case 8: // ECM PROCESS PID ?? don't know, not needed
138 break;
139 }
140 }
141 if (l!=i)
142 cs_log("WARNING: ECM-request corrupt");
143 else
144 get_cw(cur_client(), er);
145}
146
147static void radegast_process_unknown(uchar *buf)
148{
149 uchar answer[2]={0x81, 0x00};
150 radegast_send(cur_client(), answer);
151 cs_log("unknown request %02X, len=%d", buf[0], buf[1]);
152}
153
154static void * radegast_server(void *cli)
155{
156 int n;
157 uchar mbuf[1024];
158
159 struct s_client * client = (struct s_client *) cli;
160 client->thread=pthread_self();
161 pthread_setspecific(getclient, cli);
162
163 radegast_auth_client(cur_client()->ip);
164 while ((n=get_request(mbuf))>0)
165 {
166 switch(mbuf[0])
167 {
168 case 1:
169 radegast_process_ecm(mbuf+2, mbuf[1]);
170 break;
171 default:
172 radegast_process_unknown(mbuf);
173 }
174 }
175 cs_disconnect_client(client);
176 return NULL;
177}
178
179static int radegast_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *UNUSED(buf))
180{
181 int n;
182 uchar provid_buf[8];
183 uchar header[22] = "\x02\x01\x00\x06\x08\x30\x30\x30\x30\x30\x30\x30\x30\x07\x04\x30\x30\x30\x38\x08\x01\x02";
184 uchar *ecmbuf = malloc(er->l + 30);
185 memset(ecmbuf, 0, er->l + 30);
186
187 ecmbuf[0] = 1;
188 ecmbuf[1] = er->l + 30 - 2;
189 memcpy(ecmbuf + 2, header, sizeof(header));
190 for(n = 0; n < 4; n++) {
191 sprintf((char*)provid_buf+(n*2), "%02X", ((uchar *)(&er->prid))[4 - 1 - n]);
192 }
193 ecmbuf[7] = provid_buf[0];
194 ecmbuf[8] = provid_buf[1];
195 ecmbuf[9] = provid_buf[2];
196 ecmbuf[10] = provid_buf[3];
197 ecmbuf[11] = provid_buf[4];
198 ecmbuf[12] = provid_buf[5];
199 ecmbuf[13] = provid_buf[6];
200 ecmbuf[14] = provid_buf[7];
201 ecmbuf[2 + sizeof(header)] = 0xa;
202 ecmbuf[3 + sizeof(header)] = 2;
203 ecmbuf[4 + sizeof(header)] = er->caid >> 8;
204 ecmbuf[5 + sizeof(header)] = er->caid & 0xff;
205 ecmbuf[6 + sizeof(header)] = 3;
206 ecmbuf[7 + sizeof(header)] = er->l;
207 memcpy(ecmbuf + 8 + sizeof(header), er->ecm, er->l);
208 ecmbuf[4] = er->caid >> 8;
209
210 client->reader->msg_idx = er->idx;
211 n = send(client->pfd, ecmbuf, er->l + 30, 0);
212
213 cs_log("radegast: sending ecm");
214 cs_ddump_mask(D_CLIENT, ecmbuf, er->l + 30, "ecm:");
215
216 free(ecmbuf);
217
218 return 0;
219}
220
221int radegast_cli_init(struct s_client *cl)
222{
223 *cl = *cl; //prevent compiler warning
224 struct sockaddr_in loc_sa;
225 struct protoent *ptrp;
226 int p_proto, handle;
227
228 cur_client()->pfd=0;
229 if (cur_client()->reader->r_port<=0)
230 {
231 cs_log("radegast: invalid port %d for server %s", cur_client()->reader->r_port, cur_client()->reader->device);
232 return(1);
233 }
234 if( (ptrp=getprotobyname("tcp")) )
235 p_proto=ptrp->p_proto;
236 else
237 p_proto=6;
238
239 cur_client()->ip=0;
240 memset((char *)&loc_sa,0,sizeof(loc_sa));
241 loc_sa.sin_family = AF_INET;
242#ifdef LALL
243 if (cfg->serverip[0])
244 loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
245 else
246#endif
247 loc_sa.sin_addr.s_addr = INADDR_ANY;
248 loc_sa.sin_port = htons(cur_client()->reader->l_port);
249
250 if ((cur_client()->udp_fd=socket(PF_INET, SOCK_STREAM, p_proto))<0)
251 {
252 cs_log("radegast: Socket creation failed (errno=%d)", errno);
253 cs_exit(1);
254 }
255
256#ifdef SO_PRIORITY
257 if (cfg->netprio)
258 setsockopt(cur_client()->udp_fd, SOL_SOCKET, SO_PRIORITY,
259 (void *)&cfg->netprio, sizeof(ulong));
260#endif
261 if (!cur_client()->reader->tcp_ito) {
262 ulong keep_alive = cur_client()->reader->tcp_ito?1:0;
263 setsockopt(cur_client()->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
264 (void *)&keep_alive, sizeof(ulong));
265 }
266
267 memset((char *)&cur_client()->udp_sa,0,sizeof(cur_client()->udp_sa));
268 cur_client()->udp_sa.sin_family = AF_INET;
269 cur_client()->udp_sa.sin_port = htons((u_short)cur_client()->reader->r_port);
270
271 cs_log("radegast: proxy %s:%d (fd=%d)",
272 cur_client()->reader->device, cur_client()->reader->r_port, cur_client()->udp_fd);
273
274 handle = network_tcp_connection_open();
275 if(handle < 0) return -1;
276
277 cur_client()->reader->tcp_connected = 2;
278 cur_client()->reader->card_status = CARD_INSERTED;
279 cur_client()->reader->last_g = cur_client()->reader->last_s = time((time_t *)0);
280
281 cs_debug_mask(D_CLIENT, "radegast: last_s=%d, last_g=%d", cur_client()->reader->last_s, cur_client()->reader->last_g);
282
283 cur_client()->pfd=cur_client()->udp_fd;
284
285 return(0);
286}
287
288void module_radegast(struct s_module *ph)
289{
290 static PTAB ptab; //since there is always only 1 radegast server running, this is threadsafe
291 ptab.ports[0].s_port = cfg->rad_port;
292 ph->ptab = &ptab;
293 ph->ptab->nports = 1;
294
295 strcpy(ph->desc, "radegast");
296 ph->type=MOD_CONN_TCP;
297 ph->multi=0;
298 ph->watchdog=1;
299 ph->s_ip=cfg->rad_srvip;
300 ph->s_handler=radegast_server;
301 ph->recv=radegast_recv;
302 ph->send_dcw=radegast_send_dcw;
303 ph->c_multi=0;
304 ph->c_init=radegast_cli_init;
305 ph->c_recv_chk=radegast_recv_chk;
306 ph->c_send_ecm=radegast_send_ecm;
307 ph->num=R_RADEGAST;
308}
Note: See TracBrowser for help on using the repository browser.