]> git.sesse.net Git - vlc/blob - libs/srtp/recv.c
API cleanup
[vlc] / libs / srtp / recv.c
1 /*
2  * Secure RTP with libgcrypt
3  * Copyright (C) 2007  RĂ©mi Denis-Courmont <rdenis # simphalempin , com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdint.h>
25 #include <stddef.h>
26 #include "srtp.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <unistd.h>
32 #include <netinet/in.h>
33
34 int main (void)
35 {
36     int fd = socket (AF_INET, SOCK_DGRAM, 0);
37     struct sockaddr_in addr;
38     memset (&addr, 0, sizeof (addr));
39     addr.sin_family = AF_INET;
40 #ifdef HAVE_SA_LEN
41     addr.sin_len = sizeof (addr);
42 #endif
43     addr.sin_port = htons (10000);
44     addr.sin_addr.s_addr = htonl (0x7f000001);
45     if (bind (fd, (struct sockaddr *)&addr, sizeof (addr)))
46         return 1;
47
48     static const uint8_t key[16] =
49         "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
50         "\x12\x34\x56\x78\x9A\xBC\xDE\xF0";
51     static const uint8_t salt[14] =
52         "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78";
53
54     srtp_session_t *s = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
55                                      SRTP_PRF_AES_CM, 0);
56     if (s == NULL)
57         return 1;
58     if (srtp_setkey (s, key, 16, salt, 14))
59         goto error;
60
61     uint8_t buf[1500];
62     size_t len;
63 #if 0
64     memset (buf, 0, sizeof (buf));
65     buf[0] = 2 << 6;
66     buf[1] = 1;
67     memcpy (buf + 2, &(uint16_t){ htons (9527) }, 2);
68     memcpy (buf + 8, "\xde\xad\xbe\xef", 4);
69     memcpy (buf + 4, &(uint32_t){ htonl (1) }, 4);
70     strcpy ((char *)buf + 12, "a\n");
71     len = 12 + strlen ((char *)buf + 12) + 1;
72 #endif
73     for (;;)
74     {
75         len = read (fd, buf, sizeof (buf));
76         int val = srtp_recv (s, buf, &len);
77         if (val)
78         {
79             fprintf (stderr, "Cannot decrypt: %s\n", strerror (val));
80             continue;
81         }
82
83         puts ((char *)buf + 12);
84         //if (srtp_send (s, buf, &len, sizeof (buf)) || srtp_recv (s, buf, &len))
85         //    break;
86         puts ((char *)buf + 12);
87     }
88
89 error:
90     srtp_destroy (s);
91     close (fd);
92     return 0;
93 }