~ubuntu-app-review-contributors/ubuntu-app-reviews/xynaplayer

« back to all changes in this revision

Viewing changes to src/smuc_net.c

  • Committer: App Bot
  • Date: 2012-06-19 10:49:54 UTC
  • Revision ID: appbot@holba.ch-20120619104954-idsp44dih422qhp4
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    xynaplayer for rot13(NalK)
 
3
    Copyright (C) 2009  Ying-Chun Liu (PaulLiu) <grandpaul@gmail.com>
 
4
 
 
5
    This program is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include <stdio.h>
 
20
#include <string.h>
 
21
#include <stdlib.h>
 
22
 
 
23
#include <unistd.h>
 
24
 
 
25
#include <errno.h>
 
26
 
 
27
#include <netdb.h>
 
28
 
 
29
#include <glib.h>
 
30
#include <glib/gprintf.h>
 
31
 
 
32
#include "smuc_net.h"
 
33
 
 
34
char* smucNC = "00000001";
 
35
char* smucCNONCE = "0a4f113b";
 
36
 
 
37
in_addr_t smuc_addr(const char *cp) {
 
38
  struct addrinfo hints;
 
39
  struct addrinfo *res=NULL,*resi=NULL;
 
40
  int result;
 
41
  in_addr_t ret;
 
42
  struct sockaddr_in *inaddr=NULL;
 
43
 
 
44
  ret = INADDR_NONE;
 
45
  memset(&hints,0,sizeof(struct addrinfo));
 
46
  hints.ai_family = AF_INET;
 
47
 
 
48
  result = getaddrinfo(cp,NULL,&hints,&res);
 
49
 
 
50
  if (result != 0) {
 
51
    return ret;
 
52
  }
 
53
 
 
54
  for (resi = res; resi != NULL; resi = resi->ai_next) {
 
55
    if (resi->ai_addrlen >= sizeof(struct sockaddr_in) && resi->ai_addr != NULL) {
 
56
      inaddr = (struct sockaddr_in *)(resi->ai_addr);
 
57
      ret = inaddr->sin_addr.s_addr;
 
58
      break;
 
59
    }
 
60
  }
 
61
  if (res != NULL) {
 
62
    freeaddrinfo(res);
 
63
    res=NULL;
 
64
  }
 
65
  return ret;
 
66
}
 
67
 
 
68
ssize_t smuc_recvn(int fd, void *vptr, size_t n) {
 
69
  size_t nleft;
 
70
  ssize_t nread;
 
71
  char *ptr;
 
72
 
 
73
  ptr = (char*)vptr;
 
74
  nleft=n;
 
75
  while (nleft>0) {
 
76
    nread = read(fd,ptr,nleft);
 
77
    if (nread<0) {
 
78
      if (errno==EINTR)
 
79
        nread=0;
 
80
      else
 
81
        return (-1);
 
82
    } else if (nread==0) {
 
83
      break;
 
84
    }
 
85
 
 
86
    nleft-=nread;
 
87
    ptr+=nread;
 
88
  }
 
89
  return (n-nleft);
 
90
}
 
91
 
 
92
ssize_t smuc_recvline(int fd,void *vptr, size_t maxlen) {
 
93
  ssize_t n,rc;
 
94
  char c,*ptr;
 
95
 
 
96
  ptr = (char *)vptr;
 
97
  for (n=1; n<maxlen; n++) {
 
98
  smuc_recvline_again:
 
99
    rc = read(fd,&c,1);
 
100
    if ( rc == 1) {
 
101
      *ptr++ = c;
 
102
      if (c=='\n')
 
103
        break;
 
104
    } else if (rc==0) {
 
105
      if (n==1)
 
106
        return (0);
 
107
      else
 
108
        break;
 
109
    } else {
 
110
      if (errno == EINTR)
 
111
        goto smuc_recvline_again;
 
112
      return (-1);
 
113
    }
 
114
  }
 
115
  *ptr = '\0';
 
116
  return (n);
 
117
}
 
118
 
 
119
int smuc_readInt32(int fd) {
 
120
  unsigned char c[4];
 
121
  int ret,i;
 
122
  ret=0;
 
123
 
 
124
  smuc_recvn(fd,c,4);
 
125
  for (i=0; i<4; i++) {
 
126
    ret = ret + ((((int) (c[i])) & 0x00ff) << (i*8));
 
127
  }
 
128
  return ret;
 
129
}
 
130
 
 
131
SmucServerNonce* smucGetNonce(char *ip,int port) {
 
132
  int sockfd;
 
133
  struct sockaddr_in servaddr;
 
134
  char readBuf[1024];
 
135
  char writeBuf[1024];
 
136
  int result;
 
137
  char *ci,*cj;
 
138
  char *saveptr1,*saveptr2;
 
139
  SmucServerNonce *ret=NULL;
 
140
 
 
141
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
142
  if (sockfd == -1) {
 
143
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
144
    return NULL;
 
145
  }
 
146
 
 
147
  memset(&servaddr,0,sizeof(servaddr));
 
148
  servaddr.sin_family = AF_INET;
 
149
  servaddr.sin_port = htons(port);
 
150
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
151
 
 
152
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
153
  if (result<0) {
 
154
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
155
    close(sockfd);
 
156
    return NULL;
 
157
  }
 
158
  strcpy(writeBuf,"GET /Stream/Control HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
159
  strcat(writeBuf,ip);
 
160
  strcat(writeBuf,"\n\n");
 
161
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
162
  if (result<0) {
 
163
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
164
    close(sockfd);
 
165
    return NULL;
 
166
  }
 
167
  
 
168
  while (smuc_recvline(sockfd,readBuf,sizeof(readBuf)-1) > 0) {
 
169
    if (strcmp(readBuf,"\n")==0) {
 
170
      break;
 
171
    }
 
172
    if (g_str_has_prefix(readBuf,"WWW-Authenticate: Digest ")) {
 
173
      ret = (SmucServerNonce*)malloc(sizeof(SmucServerNonce));
 
174
      memset(ret,0,sizeof(SmucServerNonce));
 
175
      for (ci = strtok_r(&(readBuf[25]),",",&saveptr1); ci != NULL ; ci = strtok_r(NULL," ,",&saveptr1)) {
 
176
        char *x;
 
177
 
 
178
        x = g_strstrip(ci);
 
179
        cj = strtok_r(x,"=\"",&saveptr2);
 
180
        if (strcmp(cj,"realm")==0) {
 
181
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
182
          if (cj != NULL) {
 
183
            strcpy(ret->realm,cj);
 
184
          }
 
185
        } else if (strcmp(cj,"domain")==0) {
 
186
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
187
          if (cj != NULL) {
 
188
            strcpy(ret->domain,cj);
 
189
          }
 
190
        } else if (strcmp(cj,"qop")==0) {
 
191
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
192
          if (cj != NULL) {
 
193
            strcpy(ret->qop,cj);
 
194
          }
 
195
        } else if (strcmp(cj,"nonce")==0) {
 
196
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
197
          if (cj != NULL) {
 
198
            strcpy(ret->nonce,cj);
 
199
          }
 
200
        } else if (strcmp(cj,"opaque")==0) {
 
201
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
202
          if (cj != NULL) {
 
203
            strcpy(ret->opaque,cj);
 
204
          }
 
205
        } else if (strcmp(cj,"algorithm")==0) {
 
206
          cj = strtok_r(NULL,"=\"",&saveptr2);
 
207
          if (cj != NULL) {
 
208
            strcpy(ret->algorithm,cj);
 
209
          }
 
210
        }
 
211
      }
 
212
    }
 
213
  }
 
214
 
 
215
  close(sockfd);
 
216
 
 
217
  return ret;
 
218
  
 
219
}
 
220
 
 
221
int smucGetControlFD(SmucServerNonce *nonce,char *ip,int port,char *user,char *password) {
 
222
  int sockfd=-1;
 
223
  struct sockaddr_in servaddr;
 
224
  char writeBuf[1024];
 
225
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
226
  char tmpbuf[1024];
 
227
  int result;
 
228
  char method[]="GET";
 
229
  char uri[]="/Stream/Control";
 
230
 
 
231
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
232
  if (sockfd == -1) {
 
233
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
234
    return sockfd;
 
235
  }
 
236
 
 
237
  memset(&servaddr,0,sizeof(servaddr));
 
238
  servaddr.sin_family = AF_INET;
 
239
  servaddr.sin_port = htons(port);
 
240
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
241
 
 
242
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
243
  if (result<0) {
 
244
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
245
    close(sockfd);
 
246
    return -1;
 
247
  }
 
248
  strcpy(writeBuf,method);
 
249
  strcat(writeBuf," ");
 
250
  strcat(writeBuf,uri);
 
251
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
252
  strcat(writeBuf,ip);
 
253
  strcat(writeBuf,"\n");
 
254
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
255
  if (result<0) {
 
256
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
257
    close(sockfd);
 
258
    return -1;
 
259
  }
 
260
 
 
261
  /* calculate response */
 
262
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
263
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
264
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri);
 
265
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
266
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
267
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
268
  g_free(HA1);
 
269
  HA1=NULL;
 
270
  g_free(HA2);
 
271
  HA2=NULL;
 
272
 
 
273
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
274
  g_free(response);
 
275
  response=NULL;
 
276
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
277
  if (result<0) {
 
278
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
279
    close(sockfd);
 
280
    return -1;
 
281
  }
 
282
 
 
283
  result = send(sockfd,"\n",1,0);
 
284
  if (result<0) {
 
285
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
286
    close(sockfd);
 
287
    return -1;
 
288
  }
 
289
  return (sockfd);
 
290
}
 
291
 
 
292
int smucGetSession(int controlfd) {
 
293
  int session=-1;
 
294
  char readBuf[1024];
 
295
  while (smuc_recvline(controlfd,readBuf,sizeof(readBuf)-1) > 0) {
 
296
    if (g_str_has_prefix(readBuf,"Session: ")) {
 
297
      if (sscanf(&(readBuf[9]),"%d",&session)!=1) {
 
298
        session=-1;
 
299
      } else {
 
300
        break;
 
301
      }
 
302
    }
 
303
  }
 
304
  return session;
 
305
}
 
306
 
 
307
int smucGetVideoFD(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int session) {
 
308
  int sockfd=-1;
 
309
  struct sockaddr_in servaddr;
 
310
  char readBuf[1024];
 
311
  char writeBuf[1024];
 
312
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
313
  char tmpbuf[1024];
 
314
  int result;
 
315
  char method[]="GET";
 
316
  char uri_o[]="/Stream/Video";
 
317
  char uri[100]="/Stream/Video?Session=0";
 
318
 
 
319
  snprintf(uri,sizeof(uri),"%s?Session=%d",uri_o,session);
 
320
 
 
321
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
322
  if (sockfd == -1) {
 
323
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
324
    return sockfd;
 
325
  }
 
326
 
 
327
  memset(&servaddr,0,sizeof(servaddr));
 
328
  servaddr.sin_family = AF_INET;
 
329
  servaddr.sin_port = htons(port);
 
330
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
331
 
 
332
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
333
  if (result<0) {
 
334
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
335
    close(sockfd);
 
336
    return -1;
 
337
  }
 
338
  strcpy(writeBuf,method);
 
339
  strcat(writeBuf," ");
 
340
  strcat(writeBuf,uri);
 
341
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
342
  strcat(writeBuf,ip);
 
343
  strcat(writeBuf,"\n");
 
344
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
345
  if (result<0) {
 
346
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
347
    close(sockfd);
 
348
    return -1;
 
349
  }
 
350
 
 
351
  /* calculate response */
 
352
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
353
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
354
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
355
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
356
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
357
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
358
  g_free(HA1);
 
359
  HA1=NULL;
 
360
  g_free(HA2);
 
361
  HA2=NULL;
 
362
 
 
363
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
364
  g_free(response);
 
365
  response=NULL;
 
366
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
367
  if (result<0) {
 
368
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
369
    close(sockfd);
 
370
    return -1;
 
371
  }
 
372
 
 
373
  result = send(sockfd,"\n",1,0);
 
374
  if (result<0) {
 
375
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
376
    close(sockfd);
 
377
    return -1;
 
378
  }
 
379
 
 
380
  while (smuc_recvline(sockfd,readBuf,sizeof(readBuf)-1) > 0) {
 
381
    if (strcmp(readBuf,"\n")==0) {
 
382
      break;
 
383
    }
 
384
  }
 
385
 
 
386
  while (smuc_recvline(sockfd,readBuf,sizeof(readBuf)-1) > 0) {
 
387
    if (strcmp(readBuf,"</html>\n")==0) {
 
388
      break;
 
389
    }
 
390
  }
 
391
 
 
392
  return (sockfd);
 
393
}
 
394
 
 
395
int smucGetAudioFD(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int session) {
 
396
  int sockfd=-1;
 
397
  struct sockaddr_in servaddr;
 
398
  char readBuf[1024];
 
399
  char writeBuf[1024];
 
400
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
401
  char tmpbuf[1024];
 
402
  int result;
 
403
  char method[]="GET";
 
404
  char uri_o[]="/Stream/Audio";
 
405
  char uri[100]="/Stream/Audio?Session=0";
 
406
 
 
407
  snprintf(uri,sizeof(uri),"%s?Session=%d",uri_o,session);
 
408
 
 
409
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
410
  if (sockfd == -1) {
 
411
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
412
    return sockfd;
 
413
  }
 
414
 
 
415
  memset(&servaddr,0,sizeof(servaddr));
 
416
  servaddr.sin_family = AF_INET;
 
417
  servaddr.sin_port = htons(port);
 
418
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
419
 
 
420
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
421
  if (result<0) {
 
422
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
423
    close(sockfd);
 
424
    return -1;
 
425
  }
 
426
  strcpy(writeBuf,method);
 
427
  strcat(writeBuf," ");
 
428
  strcat(writeBuf,uri);
 
429
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
430
  strcat(writeBuf,ip);
 
431
  strcat(writeBuf,"\n");
 
432
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
433
  if (result<0) {
 
434
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
435
    close(sockfd);
 
436
    return -1;
 
437
  }
 
438
 
 
439
  /* calculate response */
 
440
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
441
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
442
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
443
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
444
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
445
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
446
  g_free(HA1);
 
447
  HA1=NULL;
 
448
  g_free(HA2);
 
449
  HA2=NULL;
 
450
 
 
451
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
452
  g_free(response);
 
453
  response=NULL;
 
454
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
455
  if (result<0) {
 
456
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
457
    close(sockfd);
 
458
    return -1;
 
459
  }
 
460
 
 
461
  result = send(sockfd,"\n",1,0);
 
462
  if (result<0) {
 
463
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
464
    close(sockfd);
 
465
    return -1;
 
466
  }
 
467
 
 
468
  while (smuc_recvline(sockfd,readBuf,sizeof(readBuf)-1) > 0) {
 
469
    if (strcmp(readBuf,"\n")==0) {
 
470
      break;
 
471
    }
 
472
  }
 
473
 
 
474
  while (smuc_recvline(sockfd,readBuf,sizeof(readBuf)-1) > 0) {
 
475
    if (strcmp(readBuf,"</html>\n")==0) {
 
476
      break;
 
477
    }
 
478
  }
 
479
 
 
480
  return (sockfd);
 
481
}
 
482
 
 
483
int smucDoControlPost(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,char *post) {
 
484
  int sockfd=-1;
 
485
  struct sockaddr_in servaddr;
 
486
  char writeBuf[1024];
 
487
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
488
  char tmpbuf[1024];
 
489
  int result;
 
490
  char method[]="POST";
 
491
  char uri_o[]="/Stream/Control";
 
492
  char uri[100]="/Stream/Control";
 
493
 
 
494
  snprintf(uri,sizeof(uri),"%s",uri_o);
 
495
 
 
496
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
497
  if (sockfd == -1) {
 
498
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
499
    return sockfd;
 
500
  }
 
501
 
 
502
  memset(&servaddr,0,sizeof(servaddr));
 
503
  servaddr.sin_family = AF_INET;
 
504
  servaddr.sin_port = htons(port);
 
505
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
506
 
 
507
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
508
  if (result<0) {
 
509
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
510
    close(sockfd);
 
511
    return -1;
 
512
  }
 
513
  strcpy(writeBuf,method);
 
514
  strcat(writeBuf," ");
 
515
  strcat(writeBuf,uri);
 
516
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
517
  strcat(writeBuf,ip);
 
518
  strcat(writeBuf,"\n");
 
519
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
520
  if (result<0) {
 
521
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
522
    close(sockfd);
 
523
    return -1;
 
524
  }
 
525
 
 
526
  /* calculate response */
 
527
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
528
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
529
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
530
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
531
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
532
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
533
  g_free(HA1);
 
534
  HA1=NULL;
 
535
  g_free(HA2);
 
536
  HA2=NULL;
 
537
 
 
538
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
539
  g_free(response);
 
540
  response=NULL;
 
541
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
542
  if (result<0) {
 
543
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
544
    close(sockfd);
 
545
    return -1;
 
546
  }
 
547
 
 
548
  strcpy(writeBuf,"Content-Type: application/x-rtsp-tunnelled\n");
 
549
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
550
  if (result<0) {
 
551
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send Content-Type");
 
552
    close(sockfd);
 
553
    return -1;
 
554
  }
 
555
 
 
556
  snprintf(writeBuf,sizeof(writeBuf)-1,"Content-Length: %d\n",strlen(post));
 
557
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
558
  if (result<0) {
 
559
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send Content-Length: %d",strlen(post));
 
560
    close(sockfd);
 
561
    return -1;
 
562
  }
 
563
 
 
564
  result = send(sockfd,"\n",1,0);
 
565
  if (result<0) {
 
566
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
567
    close(sockfd);
 
568
    return -1;
 
569
  }
 
570
 
 
571
  result = send(sockfd,post,strlen(post),0);
 
572
  if (result<0) {
 
573
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send post");
 
574
    close(sockfd);
 
575
    return -1;
 
576
  }
 
577
 
 
578
  close(sockfd);
 
579
 
 
580
  return 0;
 
581
}
 
582
 
 
583
int smucPlay(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int session) {
 
584
  int result=0;
 
585
  char post1[1024];
 
586
 
 
587
  snprintf(post1,sizeof(post1)-1,"PLAY rtsp://%s/live RTSP/1.0\nCSeq: 1\nSession: %d\nRange: npt=0.000-\n\n",ip,session);
 
588
 
 
589
  result = smucDoControlPost(nonce,ip,port,user,password,post1);
 
590
 
 
591
  return result;
 
592
}
 
593
 
 
594
 
 
595
int smucTuneChannel(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int cmd,int channel) {
 
596
  int sockfd=-1;
 
597
  struct sockaddr_in servaddr;
 
598
  char writeBuf[1024];
 
599
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
600
  char tmpbuf[1024];
 
601
  int result;
 
602
  char method[]="GET";
 
603
  char uri_o[]="/cgi/cgiTunerCtrl";
 
604
  char uri[100]="/cgi/cgiTunerCtrl?cmd=0&channel=10";
 
605
 
 
606
  if (cmd==0) {
 
607
    snprintf(uri,sizeof(uri),"%s?cmd=%d&channel=%d",uri_o,cmd,channel);
 
608
  } else {
 
609
    snprintf(uri,sizeof(uri),"%s?cmd=%d",uri_o,cmd);
 
610
  }
 
611
 
 
612
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
613
  if (sockfd == -1) {
 
614
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
615
    return sockfd;
 
616
  }
 
617
 
 
618
  memset(&servaddr,0,sizeof(servaddr));
 
619
  servaddr.sin_family = AF_INET;
 
620
  servaddr.sin_port = htons(port);
 
621
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
622
 
 
623
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
624
  if (result<0) {
 
625
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
626
    close(sockfd);
 
627
    return -1;
 
628
  }
 
629
  strcpy(writeBuf,method);
 
630
  strcat(writeBuf," ");
 
631
  strcat(writeBuf,uri);
 
632
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
633
  strcat(writeBuf,ip);
 
634
  strcat(writeBuf,"\n");
 
635
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
636
  if (result<0) {
 
637
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
638
    close(sockfd);
 
639
    return -1;
 
640
  }
 
641
 
 
642
  /* calculate response */
 
643
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
644
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
645
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
646
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
647
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
648
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
649
  g_free(HA1);
 
650
  HA1=NULL;
 
651
  g_free(HA2);
 
652
  HA2=NULL;
 
653
 
 
654
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
655
  g_free(response);
 
656
  response=NULL;
 
657
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
658
  if (result<0) {
 
659
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
660
    close(sockfd);
 
661
    return -1;
 
662
  }
 
663
 
 
664
  result = send(sockfd,"\n",1,0);
 
665
  if (result<0) {
 
666
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
667
    close(sockfd);
 
668
    return -1;
 
669
  }
 
670
 
 
671
  close(sockfd);
 
672
 
 
673
  return 0;
 
674
}
 
675
 
 
676
int smucTuneAV(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int videochn,int audiochn) {
 
677
  int sockfd=-1;
 
678
  struct sockaddr_in servaddr;
 
679
  char writeBuf[1024];
 
680
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
681
  char tmpbuf[1024];
 
682
  int result;
 
683
  char method[]="GET";
 
684
  char uri_o[]="/cgi/cgiAVCtrl";
 
685
  char uri[100]="/cgi/cgiAVCtrl?videochn=10&audiochn=10";
 
686
 
 
687
  snprintf(uri,sizeof(uri),"%s?videochn=%d&audiochn=%d",uri_o,videochn,audiochn);
 
688
 
 
689
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
690
  if (sockfd == -1) {
 
691
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
692
    return sockfd;
 
693
  }
 
694
 
 
695
  memset(&servaddr,0,sizeof(servaddr));
 
696
  servaddr.sin_family = AF_INET;
 
697
  servaddr.sin_port = htons(port);
 
698
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
699
 
 
700
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
701
  if (result<0) {
 
702
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
703
    close(sockfd);
 
704
    return -1;
 
705
  }
 
706
  strcpy(writeBuf,method);
 
707
  strcat(writeBuf," ");
 
708
  strcat(writeBuf,uri);
 
709
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
710
  strcat(writeBuf,ip);
 
711
  strcat(writeBuf,"\n");
 
712
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
713
  if (result<0) {
 
714
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
715
    close(sockfd);
 
716
    return -1;
 
717
  }
 
718
 
 
719
  /* calculate response */
 
720
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
721
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
722
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
723
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
724
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
725
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
726
  g_free(HA1);
 
727
  HA1=NULL;
 
728
  g_free(HA2);
 
729
  HA2=NULL;
 
730
 
 
731
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
732
  g_free(response);
 
733
  response=NULL;
 
734
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
735
  if (result<0) {
 
736
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
737
    close(sockfd);
 
738
    return -1;
 
739
  }
 
740
 
 
741
  result = send(sockfd,"\n",1,0);
 
742
  if (result<0) {
 
743
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
744
    close(sockfd);
 
745
    return -1;
 
746
  }
 
747
 
 
748
  close(sockfd);
 
749
 
 
750
  return 0;
 
751
}
 
752
 
 
753
int smucEstimateBandwith(SmucServerNonce *nonce,char *ip,int port,char *user,char *password,int framesize,int quality) {
 
754
  int sockfd=-1;
 
755
  struct sockaddr_in servaddr;
 
756
  char writeBuf[1024];
 
757
  char *HA1=NULL,*HA2=NULL,*response=NULL;
 
758
  char tmpbuf[1024];
 
759
  int result;
 
760
  char method[]="GET";
 
761
  char uri_o[]="/cgi/cgiEstBandwith";
 
762
  char uri[100]="/cgi/cgiEstBandwith?frameSize=1&quality=4";
 
763
  char readBuf[1024];
 
764
 
 
765
  snprintf(uri,sizeof(uri),"%s?frameSize=%d&quality=%d",uri_o,framesize,quality);
 
766
 
 
767
  sockfd = socket(AF_INET,SOCK_STREAM,0);
 
768
  if (sockfd == -1) {
 
769
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_ERROR,"Error: cannot new socket");
 
770
    return sockfd;
 
771
  }
 
772
 
 
773
  memset(&servaddr,0,sizeof(servaddr));
 
774
  servaddr.sin_family = AF_INET;
 
775
  servaddr.sin_port = htons(port);
 
776
  servaddr.sin_addr.s_addr = smuc_addr(ip);
 
777
 
 
778
  result = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
 
779
  if (result<0) {
 
780
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: cannot connect to %s",ip);
 
781
    close(sockfd);
 
782
    return -1;
 
783
  }
 
784
  strcpy(writeBuf,method);
 
785
  strcat(writeBuf," ");
 
786
  strcat(writeBuf,uri);
 
787
  strcat(writeBuf," HTTP/1.0\nAccept: */*\nUser-Agent: Apexx Player\nHost: ");
 
788
  strcat(writeBuf,ip);
 
789
  strcat(writeBuf,"\n");
 
790
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
791
  if (result<0) {
 
792
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send GET");
 
793
    close(sockfd);
 
794
    return -1;
 
795
  }
 
796
 
 
797
  /* calculate response */
 
798
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s",user,nonce->realm,password);
 
799
  HA1 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
800
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s",method,uri_o);
 
801
  HA2 = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
802
  snprintf(tmpbuf,sizeof(tmpbuf)-1,"%s:%s:%s:%s:%s:%s",HA1,nonce->nonce,smucNC,smucCNONCE,nonce->qop,HA2);
 
803
  response = g_compute_checksum_for_string(G_CHECKSUM_MD5,tmpbuf,-1);
 
804
  g_free(HA1);
 
805
  HA1=NULL;
 
806
  g_free(HA2);
 
807
  HA2=NULL;
 
808
 
 
809
  snprintf(writeBuf,sizeof(writeBuf)-1,"Authorization: Digest username=\"%s\", realm=\"%s\", algorithm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=%s, cnonce=\"%s\", opaque=\"%s\", response=\"%s\", qop=\"%s\"\n",user,nonce->realm,nonce->algorithm,nonce->nonce,uri_o,smucNC,smucCNONCE,nonce->opaque,response,nonce->qop);
 
810
  g_free(response);
 
811
  response=NULL;
 
812
  result = send (sockfd,writeBuf,strlen(writeBuf),0);
 
813
  if (result<0) {
 
814
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send USER/PASSWORD");
 
815
    close(sockfd);
 
816
    return -1;
 
817
  }
 
818
 
 
819
  result = send(sockfd,"\n",1,0);
 
820
  if (result<0) {
 
821
    g_log(G_LOG_DOMAIN,G_LOG_LEVEL_CRITICAL,"Error: send \\n");
 
822
    close(sockfd);
 
823
    return -1;
 
824
  }
 
825
 
 
826
  while (recv(sockfd,readBuf,sizeof(readBuf),0) > 0) {
 
827
  }
 
828
 
 
829
  close(sockfd);
 
830
 
 
831
  return 0;
 
832
}