rtspconnection.c 12.6 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* GStreamer
 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
Wim Taymans's avatar
Wim Taymans committed
21
#include <errno.h>
Wim Taymans's avatar
Wim Taymans committed
22
23
24
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
25

26
27
28
/* we include this here to get the G_OS_* defines */
#include <glib.h>

29
30
31
#ifdef G_OS_WIN32
#include <winsock2.h>
#else
32
33
#include <netdb.h>
#include <sys/socket.h>
34
35
#include <netinet/in.h>
#include <arpa/inet.h>
36
#endif
Wim Taymans's avatar
Wim Taymans committed
37

38

Wim Taymans's avatar
Wim Taymans committed
39
40
#include "rtspconnection.h"

41
42
43
44
45
46
#ifdef G_OS_WIN32
#define CLOSE_SOCKET(sock) closesocket(sock);
#else
#define CLOSE_SOCKET(sock) close(sock);
#endif

47
#ifdef G_OS_WIN32
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* note that inet_aton is deprecated on unix because
 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
 * address. */
static int
inet_aton (const char *c, struct in_addr *paddr)
{
  paddr->s_addr = inet_addr (c);

  if (paddr->s_addr == INADDR_NONE)
    return 0;

  return 1;
}
#endif

Wim Taymans's avatar
Wim Taymans committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
RTSPResult
rtsp_connection_open (RTSPUrl * url, RTSPConnection ** conn)
{
  gint fd;
  struct sockaddr_in sin;
  struct hostent *hostinfo;
  char **addrs;
  gchar *ip;
  struct in_addr addr;
  gint ret;

  if (url == NULL || conn == NULL)
    return RTSP_EINVAL;

  if (url->protocol != RTSP_PROTO_TCP)
    return RTSP_ENOTIMPL;

  /* first check if it already is an IP address */
  if (inet_aton (url->host, &addr)) {
    ip = url->host;
  } else {
    hostinfo = gethostbyname (url->host);
    if (!hostinfo)
      goto not_resolved;        /* h_errno set */

    if (hostinfo->h_addrtype != AF_INET)
      goto not_ip;              /* host not an IP host */

    addrs = hostinfo->h_addr_list;
    ip = inet_ntoa (*(struct in_addr *) *addrs);
  }

  fd = socket (AF_INET, SOCK_STREAM, 0);
  if (fd == -1)
    goto sys_error;

  memset (&sin, 0, sizeof (sin));
  sin.sin_family = AF_INET;     /* network socket */
  sin.sin_port = htons (url->port);     /* on port */
  sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */

  ret = connect (fd, (struct sockaddr *) &sin, sizeof (sin));
  if (ret != 0)
    goto sys_error;

  return rtsp_connection_create (fd, conn);

sys_error:
  {
    return RTSP_ESYS;
  }
not_resolved:
  {
    g_warning ("could not resolve host \"%s\"\n", url->host);
    return RTSP_ESYS;
  }
not_ip:
  {
    g_warning ("host \"%s\" is not IP\n", url->host);
    return RTSP_ESYS;
  }
}

RTSPResult
rtsp_connection_create (gint fd, RTSPConnection ** conn)
{
  RTSPConnection *newconn;

Wim Taymans's avatar
Wim Taymans committed
131
  /* FIXME check fd, must be connected SOCK_STREAM */
Wim Taymans's avatar
Wim Taymans committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

  newconn = g_new (RTSPConnection, 1);

  newconn->fd = fd;
  newconn->cseq = 0;
  newconn->session_id[0] = 0;
  newconn->state = RTSP_STATE_INIT;

  *conn = newconn;

  return RTSP_OK;
}

static void
append_header (gint key, gchar * value, GString * str)
{
  const gchar *keystr = rtsp_header_as_text (key);

  g_string_append_printf (str, "%s: %s\r\n", keystr, value);
}

RTSPResult
rtsp_connection_send (RTSPConnection * conn, RTSPMessage * message)
{
  GString *str;
Wim Taymans's avatar
Wim Taymans committed
157
158
  gint towrite;
  gchar *data;
Wim Taymans's avatar
Wim Taymans committed
159
160
161
162

  if (conn == NULL || message == NULL)
    return RTSP_EINVAL;

163
#ifdef G_OS_WIN32
164
165
166
167
168
169
170
171
172
173
  WSADATA w;
  int error = WSAStartup (0x0202, &w);

  if (error)
    goto startup_error;

  if (w.wVersion != 0x0202)
    goto version_error;
#endif

Wim Taymans's avatar
Wim Taymans committed
174
175
  str = g_string_new ("");

Wim Taymans's avatar
Wim Taymans committed
176
  /* create request string, add CSeq */
Wim Taymans's avatar
Wim Taymans committed
177
178
179
180
181
  g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
      "CSeq: %d\r\n",
      rtsp_method_as_text (message->type_data.request.method),
      message->type_data.request.uri, conn->cseq);

Wim Taymans's avatar
Wim Taymans committed
182
  /* append session id if we have one */
Wim Taymans's avatar
Wim Taymans committed
183
184
185
186
  if (conn->session_id[0] != '\0') {
    rtsp_message_add_header (message, RTSP_HDR_SESSION, conn->session_id);
  }

Wim Taymans's avatar
Wim Taymans committed
187
  /* append headers */
Wim Taymans's avatar
Wim Taymans committed
188
189
  g_hash_table_foreach (message->hdr_fields, (GHFunc) append_header, str);

Wim Taymans's avatar
Wim Taymans committed
190
191
192
  /* append Content-Length and body if needed */
  if (message->body != NULL && message->body_size > 0) {
    gchar *len;
Wim Taymans's avatar
Wim Taymans committed
193

Wim Taymans's avatar
Wim Taymans committed
194
195
196
197
198
    len = g_strdup_printf ("%d", message->body_size);
    append_header (RTSP_HDR_CONTENT_LENGTH, len, str);
    g_free (len);
    /* header ends here */
    g_string_append (str, "\r\n");
199
200
    str =
        g_string_append_len (str, (gchar *) message->body, message->body_size);
Wim Taymans's avatar
Wim Taymans committed
201
202
203
204
205
206
207
208
  } else {
    /* just end headers */
    g_string_append (str, "\r\n");
  }

  /* write request */
  towrite = str->len;
  data = str->str;
Wim Taymans's avatar
Wim Taymans committed
209

Wim Taymans's avatar
Wim Taymans committed
210
211
212
213
214
215
216
217
218
219
220
221
  while (towrite > 0) {
    gint written;

    written = write (conn->fd, data, towrite);
    if (written < 0) {
      if (errno != EAGAIN && errno != EINTR)
        goto write_error;
    } else {
      towrite -= written;
      data += written;
    }
  }
Wim Taymans's avatar
Wim Taymans committed
222
223
  g_string_free (str, TRUE);

Wim Taymans's avatar
Wim Taymans committed
224
225
  conn->cseq++;

Wim Taymans's avatar
Wim Taymans committed
226
  return RTSP_OK;
Wim Taymans's avatar
Wim Taymans committed
227

228
#ifdef G_OS_WIN32
229
230
startup_error:
  {
231
232
    g_warning ("Error %d on WSAStartup", error);
    return RTSP_EWSASTART;
233
234
235
  }
version_error:
  {
236
237
    g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
        w.wVersion);
238
    WSACleanup ();
239
    return RTSP_EWSAVERSION;
240
241
  }
#endif
Wim Taymans's avatar
Wim Taymans committed
242
243
244
245
246
write_error:
  {
    g_string_free (str, TRUE);
    return RTSP_ESYS;
  }
Wim Taymans's avatar
Wim Taymans committed
247
248
249
250
251
252
253
}

static RTSPResult
read_line (gint fd, gchar * buffer, guint size)
{
  gint idx;
  gchar c;
Wim Taymans's avatar
Wim Taymans committed
254
  gint r;
Wim Taymans's avatar
Wim Taymans committed
255
256
257

  idx = 0;
  while (TRUE) {
Wim Taymans's avatar
Wim Taymans committed
258
259
260
261
262
263
264
265
266
    r = read (fd, &c, 1);
    if (r < 1) {
      if (errno != EAGAIN && errno != EINTR)
        goto read_error;
    } else {
      if (c == '\n')            /* end on \n */
        break;
      if (c == '\r')            /* ignore \r */
        continue;
Wim Taymans's avatar
Wim Taymans committed
267

Wim Taymans's avatar
Wim Taymans committed
268
269
270
      if (idx < size - 1)
        buffer[idx++] = c;
    }
Wim Taymans's avatar
Wim Taymans committed
271
272
273
274
275
  }
  buffer[idx] = '\0';

  return RTSP_OK;

Wim Taymans's avatar
Wim Taymans committed
276
read_error:
Wim Taymans's avatar
Wim Taymans committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  {
    return RTSP_ESYS;
  }
}

static void
read_string (gchar * dest, gint size, gchar ** src)
{
  gint idx;

  idx = 0;
  /* skip spaces */
  while (g_ascii_isspace (**src))
    (*src)++;

  while (!g_ascii_isspace (**src) && **src != '\0') {
    if (idx < size - 1)
      dest[idx++] = **src;
    (*src)++;
  }
  if (size > 0)
    dest[idx] = '\0';
}

static void
read_key (gchar * dest, gint size, gchar ** src)
{
  gint idx;

  idx = 0;
  while (**src != ':' && **src != '\0') {
    if (idx < size - 1)
      dest[idx++] = **src;
    (*src)++;
  }
  if (size > 0)
    dest[idx] = '\0';
}

static RTSPResult
parse_response_status (gchar * buffer, RTSPMessage * msg)
{
  gchar versionstr[20];
  gchar codestr[4];
  gint code;
  gchar *bptr;

  bptr = buffer;

  read_string (versionstr, sizeof (versionstr), &bptr);
  if (strcmp (versionstr, "RTSP/1.0") != 0)
    goto wrong_version;

  read_string (codestr, sizeof (codestr), &bptr);
  code = atoi (codestr);

  while (g_ascii_isspace (*bptr))
    bptr++;

  rtsp_message_init_response (code, bptr, NULL, msg);

  return RTSP_OK;

wrong_version:
  {
    return RTSP_EINVAL;
  }
}

Wim Taymans's avatar
Wim Taymans committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
static RTSPResult
parse_request_line (gchar * buffer, RTSPMessage * msg)
{
  gchar versionstr[20];
  gchar methodstr[20];
  gchar urlstr[4096];
  gchar *bptr;
  RTSPMethod method;

  bptr = buffer;

  read_string (methodstr, sizeof (methodstr), &bptr);
  method = rtsp_find_method (methodstr);
  if (method == -1)
    goto wrong_method;

  read_string (urlstr, sizeof (urlstr), &bptr);

  read_string (versionstr, sizeof (versionstr), &bptr);
  if (strcmp (versionstr, "RTSP/1.0") != 0)
    goto wrong_version;

  rtsp_message_init_request (method, urlstr, msg);

  return RTSP_OK;

wrong_method:
  {
    return RTSP_EINVAL;
  }
wrong_version:
  {
    return RTSP_EINVAL;
  }
}

/* parsing lines means reading a Key: Value pair */
Wim Taymans's avatar
Wim Taymans committed
383
384
385
386
387
388
389
390
391
static RTSPResult
parse_line (gchar * buffer, RTSPMessage * msg)
{
  gchar key[32];
  gchar *bptr;
  RTSPHeaderField field;

  bptr = buffer;

Wim Taymans's avatar
Wim Taymans committed
392
  /* read key */
Wim Taymans's avatar
Wim Taymans committed
393
394
395
396
397
398
399
  read_key (key, sizeof (key), &bptr);
  if (*bptr != ':')
    return RTSP_EINVAL;

  bptr++;

  field = rtsp_find_header_field (key);
400
  if (field != -1) {
Wim Taymans's avatar
Wim Taymans committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    while (g_ascii_isspace (*bptr))
      bptr++;
    rtsp_message_add_header (msg, field, bptr);
  }

  return RTSP_OK;
}

static RTSPResult
read_body (gint fd, glong content_length, RTSPMessage * msg)
{
  gchar *body, *bodyptr;
  gint to_read, r;

  if (content_length <= 0) {
Wim Taymans's avatar
Wim Taymans committed
416
417
418
    body = NULL;
    content_length = 0;
    goto done;
Wim Taymans's avatar
Wim Taymans committed
419
420
  }

421
422
  body = g_malloc (content_length + 1);
  body[content_length] = '\0';
Wim Taymans's avatar
Wim Taymans committed
423
424
425
426
  bodyptr = body;
  to_read = content_length;
  while (to_read > 0) {
    r = read (fd, bodyptr, to_read);
Wim Taymans's avatar
Wim Taymans committed
427
428
429
430
431
432
433
    if (r < 0) {
      if (errno != EAGAIN && errno != EINTR)
        goto read_error;
    } else {
      to_read -= r;
      bodyptr += r;
    }
Wim Taymans's avatar
Wim Taymans committed
434
  }
435
  content_length += 1;
Wim Taymans's avatar
Wim Taymans committed
436

Wim Taymans's avatar
Wim Taymans committed
437
done:
438
  rtsp_message_set_body (msg, (guint8 *) body, content_length);
Wim Taymans's avatar
Wim Taymans committed
439
440

  return RTSP_OK;
Wim Taymans's avatar
Wim Taymans committed
441
442
443
444
445
446

read_error:
  {
    g_free (body);
    return RTSP_ESYS;
  }
Wim Taymans's avatar
Wim Taymans committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
}

RTSPResult
rtsp_connection_receive (RTSPConnection * conn, RTSPMessage * msg)
{
  gchar buffer[4096];
  gint line;
  gchar *hdrval;
  glong content_length;
  RTSPResult res;
  gboolean need_body;

  if (conn == NULL || msg == NULL)
    return RTSP_EINVAL;

  line = 0;

  need_body = TRUE;

Wim Taymans's avatar
Wim Taymans committed
466
  res = RTSP_OK;
Wim Taymans's avatar
Wim Taymans committed
467
  /* parse first line and headers */
Wim Taymans's avatar
Wim Taymans committed
468
  while (res == RTSP_OK) {
Wim Taymans's avatar
Wim Taymans committed
469
470
471
    gchar c;
    gint ret;

Wim Taymans's avatar
Wim Taymans committed
472
    /* read first character, this identifies data messages */
Wim Taymans's avatar
Wim Taymans committed
473
474
475
476
477
478
    ret = read (conn->fd, &c, 1);
    if (ret < 0)
      goto read_error;
    if (ret < 1)
      break;

Wim Taymans's avatar
Wim Taymans committed
479
    /* check for data packet, first character is $ */
Wim Taymans's avatar
Wim Taymans committed
480
481
482
    if (c == '$') {
      guint16 size;

Wim Taymans's avatar
Wim Taymans committed
483
484
485
      /* data packets are $<1 byte channel><2 bytes length,BE><data bytes> */

      /* read channel, which is the next char */
Wim Taymans's avatar
Wim Taymans committed
486
487
488
489
490
491
      ret = read (conn->fd, &c, 1);
      if (ret < 0)
        goto read_error;
      if (ret < 1)
        goto error;

Wim Taymans's avatar
Wim Taymans committed
492
      /* now we create a data message */
Wim Taymans's avatar
Wim Taymans committed
493
494
      rtsp_message_init_data ((gint) c, msg);

Wim Taymans's avatar
Wim Taymans committed
495
      /* next two bytes are the length of the data */
Wim Taymans's avatar
Wim Taymans committed
496
497
498
499
500
501
502
503
      ret = read (conn->fd, &size, 2);
      if (ret < 0)
        goto read_error;
      if (ret < 2)
        goto error;

      size = GUINT16_FROM_BE (size);

Wim Taymans's avatar
Wim Taymans committed
504
505
      /* and read the body */
      res = read_body (conn->fd, size, msg);
Wim Taymans's avatar
Wim Taymans committed
506
507
508
509
510
      need_body = FALSE;
      break;
    } else {
      gint offset = 0;

Wim Taymans's avatar
Wim Taymans committed
511
      /* we have a regular response */
Wim Taymans's avatar
Wim Taymans committed
512
513
514
515
516
517
518
519
      if (c != '\r') {
        buffer[0] = c;
        offset = 1;
      }
      /* should not happen */
      if (c == '\n')
        break;

Wim Taymans's avatar
Wim Taymans committed
520
521
522
523
      /* read lines */
      res = read_line (conn->fd, buffer + offset, sizeof (buffer) - offset);
      if (res != RTSP_OK)
        goto read_error;
Wim Taymans's avatar
Wim Taymans committed
524
525
526
527
528

      if (buffer[0] == '\0')
        break;

      if (line == 0) {
Wim Taymans's avatar
Wim Taymans committed
529
        /* first line, check for response status */
Wim Taymans's avatar
Wim Taymans committed
530
        if (g_str_has_prefix (buffer, "RTSP")) {
Wim Taymans's avatar
Wim Taymans committed
531
          res = parse_response_status (buffer, msg);
Wim Taymans's avatar
Wim Taymans committed
532
        } else {
Wim Taymans's avatar
Wim Taymans committed
533
          res = parse_request_line (buffer, msg);
Wim Taymans's avatar
Wim Taymans committed
534
535
        }
      } else {
Wim Taymans's avatar
Wim Taymans committed
536
        /* else just parse the line */
Wim Taymans's avatar
Wim Taymans committed
537
538
539
540
541
542
        parse_line (buffer, msg);
      }
    }
    line++;
  }

Wim Taymans's avatar
Wim Taymans committed
543
  /* read the rest of the body if needed */
Wim Taymans's avatar
Wim Taymans committed
544
  if (need_body) {
Wim Taymans's avatar
Wim Taymans committed
545
546
547
548
    /* see if there is a Content-Length header */
    if (rtsp_message_get_header (msg, RTSP_HDR_CONTENT_LENGTH,
            &hdrval) == RTSP_OK) {
      /* there is, read the body */
Wim Taymans's avatar
Wim Taymans committed
549
      content_length = atol (hdrval);
Wim Taymans's avatar
Wim Taymans committed
550
      res = read_body (conn->fd, content_length, msg);
Wim Taymans's avatar
Wim Taymans committed
551
552
    }

Wim Taymans's avatar
Wim Taymans committed
553
    /* save session id in the connection for further use */
Wim Taymans's avatar
Wim Taymans committed
554
555
556
557
558
    {
      gchar *session_id;

      if (rtsp_message_get_header (msg, RTSP_HDR_SESSION,
              &session_id) == RTSP_OK) {
559
560
561
562
563
564
565
566
567
568
        gint sesslen, maxlen, i;

        sesslen = strlen (session_id);
        maxlen = sizeof (conn->session_id) - 1;
        /* the sessionid can have attributes marked with ;
         * Make sure we strip them */
        for (i = 0; i < sesslen; i++) {
          if (session_id[i] == ';')
            maxlen = i;
        }
Wim Taymans's avatar
Wim Taymans committed
569
570
571
572

        /* make sure to not overflow */
        strncpy (conn->session_id, session_id, maxlen);
        conn->session_id[maxlen] = '\0';
Wim Taymans's avatar
Wim Taymans committed
573
574
575
      }
    }
  }
Wim Taymans's avatar
Wim Taymans committed
576
  return res;
Wim Taymans's avatar
Wim Taymans committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

error:
  {
    return RTSP_EPARSE;
  }
read_error:
  {
    return RTSP_ESYS;
  }
}

RTSPResult
rtsp_connection_close (RTSPConnection * conn)
{
  gint res;

  if (conn == NULL)
    return RTSP_EINVAL;
595

596
  res = CLOSE_SOCKET (conn->fd);
597
#ifdef G_OS_WIN32
598
599
  WSACleanup ();
#endif
Wim Taymans's avatar
Wim Taymans committed
600
601
602
603
604
605
606
607
608
609
610
  conn->fd = -1;
  if (res != 0)
    goto sys_error;

  return RTSP_OK;

sys_error:
  {
    return RTSP_ESYS;
  }
}
Wim Taymans's avatar
Wim Taymans committed
611
612
613
614
615
616
617

RTSPResult
rtsp_connection_free (RTSPConnection * conn)
{
  if (conn == NULL)
    return RTSP_EINVAL;

618
619
620
621
#ifdef G_OS_WIN32
  WSACleanup ();
#endif

Wim Taymans's avatar
Wim Taymans committed
622
  g_free (conn);
623
624

  return RTSP_OK;
Wim Taymans's avatar
Wim Taymans committed
625
}