Line data Source code
1 : /*
2 : Copyright (c) 2007, Adobe Systems, Incorporated
3 : All rights reserved.
4 :
5 : Redistribution and use in source and binary forms, with or without
6 : modification, are permitted provided that the following conditions are
7 : met:
8 :
9 : * Redistributions of source code must retain the above copyright
10 : notice, this list of conditions and the following disclaimer.
11 :
12 : * Redistributions in binary form must reproduce the above copyright
13 : notice, this list of conditions and the following disclaimer in the
14 : documentation and/or other materials provided with the distribution.
15 :
16 : * Neither the name of Adobe Systems, Network Resonance nor the names of its
17 : contributors may be used to endorse or promote products derived from
18 : this software without specific prior written permission.
19 :
20 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : */
32 :
33 :
34 : static char *RCSSTRING __UNUSED__="$Id: nr_socket_turn.c,v 1.2 2008/04/28 18:21:30 ekr Exp $";
35 :
36 : #ifdef USE_TURN
37 :
38 : #include <csi_platform.h>
39 : #include <stdio.h>
40 : #include <string.h>
41 : #include <sys/types.h>
42 : #include <assert.h>
43 :
44 : #include "stun.h"
45 : #include "turn_client_ctx.h"
46 : #include "nr_socket_turn.h"
47 :
48 :
49 : static char *nr_socket_turn_magic_cookie = "nr_socket_turn";
50 :
51 : typedef struct nr_socket_turn_ {
52 : char *magic_cookie;
53 : nr_turn_client_ctx *turn;
54 : } nr_socket_turn;
55 :
56 :
57 : static int nr_socket_turn_destroy(void **objp);
58 : static int nr_socket_turn_sendto(void *obj,const void *msg, size_t len,
59 : int flags, nr_transport_addr *to);
60 : static int nr_socket_turn_recvfrom(void *obj,void * restrict buf,
61 : size_t maxlen, size_t *len, int flags, nr_transport_addr *from);
62 : static int nr_socket_turn_getfd(void *obj, NR_SOCKET *fd);
63 : static int nr_socket_turn_getaddr(void *obj, nr_transport_addr *addrp);
64 : static int nr_socket_turn_close(void *obj);
65 :
66 : static nr_socket_vtbl nr_socket_turn_vtbl={
67 : 2,
68 : nr_socket_turn_destroy,
69 : nr_socket_turn_sendto,
70 : nr_socket_turn_recvfrom,
71 : nr_socket_turn_getfd,
72 : nr_socket_turn_getaddr,
73 : 0,
74 : 0,
75 : 0,
76 : nr_socket_turn_close,
77 : 0,
78 : 0
79 : };
80 :
81 0 : int nr_socket_turn_create(nr_socket *sock, nr_socket **sockp)
82 : {
83 : int r,_status;
84 0 : nr_socket_turn *sturn=0;
85 :
86 0 : if(!(sturn=RCALLOC(sizeof(nr_socket_turn))))
87 0 : ABORT(R_NO_MEMORY);
88 :
89 0 : sturn->magic_cookie = nr_socket_turn_magic_cookie;
90 :
91 0 : if(r=nr_socket_create_int(sturn, &nr_socket_turn_vtbl, sockp))
92 0 : ABORT(r);
93 :
94 0 : _status=0;
95 : abort:
96 0 : if(_status){
97 0 : nr_socket_turn_destroy((void **)&sturn);
98 : }
99 0 : return(_status);
100 : }
101 :
102 0 : static int nr_socket_turn_destroy(void **objp)
103 : {
104 : int _status;
105 : nr_socket_turn *sturn;
106 :
107 0 : if(!objp || !*objp)
108 0 : return(0);
109 :
110 0 : sturn=*objp;
111 0 : *objp=0;
112 :
113 0 : assert(sturn->magic_cookie == nr_socket_turn_magic_cookie);
114 :
115 : /* we don't own the socket, so don't destroy it */
116 :
117 0 : RFREE(sturn);
118 :
119 0 : _status=0;
120 0 : return(_status);
121 : }
122 :
123 0 : static int nr_socket_turn_sendto(void *obj,const void *msg, size_t len,
124 : int flags, nr_transport_addr *addr)
125 : {
126 : int r,_status;
127 0 : nr_socket_turn *sturn=obj;
128 :
129 0 : assert(sturn->magic_cookie == nr_socket_turn_magic_cookie);
130 0 : assert(sturn->turn);
131 :
132 0 : if ((r = nr_turn_client_send_indication(sturn->turn, msg, len, flags,
133 : addr)))
134 0 : ABORT(r);
135 :
136 0 : _status=0;
137 : abort:
138 0 : return(_status);
139 : }
140 :
141 0 : static int nr_socket_turn_recvfrom(void *obj,void * restrict buf,
142 : size_t maxlen, size_t *len, int flags, nr_transport_addr *addr)
143 : {
144 : /* Reading from TURN sockets is done by the indication
145 : processing code in turn_client_ctx. */
146 0 : assert(0);
147 :
148 : return(R_INTERNAL);
149 : }
150 :
151 0 : static int nr_socket_turn_getfd(void *obj, NR_SOCKET *fd)
152 : {
153 : /* You should never directly be touching this fd. */
154 0 : assert(0);
155 :
156 : return(R_INTERNAL);
157 : }
158 :
159 0 : static int nr_socket_turn_getaddr(void *obj, nr_transport_addr *addrp)
160 : {
161 0 : nr_socket_turn *sturn=obj;
162 : int r, _status;
163 :
164 0 : assert(sturn->magic_cookie == nr_socket_turn_magic_cookie);
165 0 : assert(sturn->turn);
166 :
167 : /* This returns the relayed address */
168 0 : if ((r=nr_turn_client_get_relayed_address(sturn->turn, addrp)))
169 0 : ABORT(r);
170 :
171 0 : _status=0;
172 : abort:
173 0 : return(_status);
174 : }
175 :
176 0 : static int nr_socket_turn_close(void *obj)
177 : {
178 : /* No-op */
179 : #ifndef NDEBUG
180 0 : nr_socket_turn *sturn=obj;
181 0 : assert(sturn->magic_cookie == nr_socket_turn_magic_cookie);
182 : #endif
183 :
184 0 : return 0;
185 : }
186 :
187 0 : int nr_socket_turn_set_ctx(nr_socket *sock, nr_turn_client_ctx *ctx)
188 : {
189 0 : nr_socket_turn *sturn=(nr_socket_turn*)sock->obj;
190 0 : assert(sturn->magic_cookie == nr_socket_turn_magic_cookie);
191 0 : assert(!sturn->turn);
192 :
193 0 : sturn->turn = ctx;
194 :
195 0 : return 0;
196 : }
197 :
198 : #endif /* USE_TURN */
|