rtsp-stream: obtain stream position from pad
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-token.c
1 /* GStreamer
2  * Copyright (C) 2010 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:rtsp-token
21  * @short_description: Roles and permissions for a client
22  * @see_also: #GstRTSPClient, #GstRTSPPermissions, #GstRTSPAuth
23  *
24  * A #GstRTSPToken contains the permissions and roles of the user
25  * performing the current request. A token is usually created when a user is
26  * authenticated by the #GstRTSPAuth object and is then placed as the current
27  * token for the current request.
28  *
29  * #GstRTSPAuth can use the token and its contents to check authorization for
30  * various operations by comparing the token to the #GstRTSPPermissions of the
31  * object.
32  *
33  * The accepted values of the token are entirely defined by the #GstRTSPAuth
34  * object that implements the security policy.
35  *
36  * Last reviewed on 2013-07-15 (1.0.0)
37  */
38
39 #include <string.h>
40
41 #include "rtsp-token.h"
42
43 typedef struct _GstRTSPTokenImpl
44 {
45   GstRTSPToken token;
46
47   GstStructure *structure;
48 } GstRTSPTokenImpl;
49
50 #define GST_RTSP_TOKEN_STRUCTURE(t)  (((GstRTSPTokenImpl *)(t))->structure)
51
52 //GST_DEBUG_CATEGORY_STATIC (rtsp_token_debug);
53 //#define GST_CAT_DEFAULT rtsp_token_debug
54
55 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPToken, gst_rtsp_token);
56
57 static void gst_rtsp_token_init (GstRTSPTokenImpl * token,
58     GstStructure * structure);
59
60 static void
61 _gst_rtsp_token_free (GstRTSPToken * token)
62 {
63   GstRTSPTokenImpl *impl = (GstRTSPTokenImpl *) token;
64
65   gst_structure_set_parent_refcount (impl->structure, NULL);
66   gst_structure_free (impl->structure);
67
68   g_slice_free1 (sizeof (GstRTSPTokenImpl), token);
69 }
70
71 static GstRTSPToken *
72 _gst_rtsp_token_copy (GstRTSPTokenImpl * token)
73 {
74   GstRTSPTokenImpl *copy;
75   GstStructure *structure;
76
77   structure = gst_structure_copy (token->structure);
78
79   copy = g_slice_new0 (GstRTSPTokenImpl);
80   gst_rtsp_token_init (copy, structure);
81
82   return (GstRTSPToken *) copy;
83 }
84
85 static void
86 gst_rtsp_token_init (GstRTSPTokenImpl * token, GstStructure * structure)
87 {
88   gst_mini_object_init (GST_MINI_OBJECT_CAST (token), 0,
89       GST_TYPE_RTSP_TOKEN,
90       (GstMiniObjectCopyFunction) _gst_rtsp_token_copy, NULL,
91       (GstMiniObjectFreeFunction) _gst_rtsp_token_free);
92
93   token->structure = structure;
94   gst_structure_set_parent_refcount (token->structure,
95       &token->token.mini_object.refcount);
96 }
97
98 /**
99  * gst_rtsp_token_new_empty:
100  *
101  * Create a new empty Authorization token.
102  *
103  * Returns: (transfer full): a new empty authorization token.
104  */
105 GstRTSPToken *
106 gst_rtsp_token_new_empty (void)
107 {
108   GstRTSPTokenImpl *token;
109   GstStructure *s;
110
111   s = gst_structure_new_empty ("GstRTSPToken");
112   g_return_val_if_fail (s != NULL, NULL);
113
114   token = g_slice_new0 (GstRTSPTokenImpl);
115   gst_rtsp_token_init (token, s);
116
117   return (GstRTSPToken *) token;
118 }
119
120 /**
121  * gst_rtsp_token_new:
122  * @firstfield: the first fieldname
123  * @...: additional arguments
124  *
125  * Create a new Authorization token with the given fieldnames and values.
126  * Arguments are given similar to gst_structure_new().
127  *
128  * Returns: (transfer full): a new authorization token.
129  */
130 GstRTSPToken *
131 gst_rtsp_token_new (const gchar * firstfield, ...)
132 {
133   GstRTSPToken *result;
134   va_list var_args;
135
136   va_start (var_args, firstfield);
137   result = gst_rtsp_token_new_valist (firstfield, var_args);
138   va_end (var_args);
139
140   return result;
141 }
142
143 /**
144  * gst_rtsp_token_new_valist:
145  * @firstfield: the first fieldname
146  * @var_args: additional arguments
147  *
148  * Create a new Authorization token with the given fieldnames and values.
149  * Arguments are given similar to gst_structure_new_valist().
150  *
151  * Returns: (transfer full): a new authorization token.
152  */
153 GstRTSPToken *
154 gst_rtsp_token_new_valist (const gchar * firstfield, va_list var_args)
155 {
156   GstRTSPToken *token;
157   GstStructure *s;
158
159   g_return_val_if_fail (firstfield != NULL, NULL);
160
161   token = gst_rtsp_token_new_empty ();
162   s = GST_RTSP_TOKEN_STRUCTURE (token);
163   gst_structure_set_valist (s, firstfield, var_args);
164
165   return token;
166 }
167
168
169 /**
170  * gst_rtsp_token_get_structure:
171  * @token: The #GstRTSPToken.
172  *
173  * Access the structure of the token.
174  *
175  * Returns: (transfer none): The structure of the token. The structure is still
176  * owned by the token, which means that you should not free it and that the
177  * pointer becomes invalid when you free the token.
178  *
179  * MT safe.
180  */
181 const GstStructure *
182 gst_rtsp_token_get_structure (GstRTSPToken * token)
183 {
184   g_return_val_if_fail (GST_IS_RTSP_TOKEN (token), NULL);
185
186   return GST_RTSP_TOKEN_STRUCTURE (token);
187 }
188
189 /**
190  * gst_rtsp_token_writable_structure:
191  * @token: The #GstRTSPToken.
192  *
193  * Get a writable version of the structure.
194  *
195  * Returns: (transfer none): The structure of the token. The structure is still
196  * owned by the token, which means that you should not free it and that the
197  * pointer becomes invalid when you free the token. This function checks if
198  * @token is writable and will never return %NULL.
199  *
200  * MT safe.
201  */
202 GstStructure *
203 gst_rtsp_token_writable_structure (GstRTSPToken * token)
204 {
205   g_return_val_if_fail (GST_IS_RTSP_TOKEN (token), NULL);
206   g_return_val_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
207           (token)), NULL);
208
209   return GST_RTSP_TOKEN_STRUCTURE (token);
210 }
211
212 /**
213  * gst_rtsp_token_get_string:
214  * @token: a #GstRTSPToken
215  * @field: a field name
216  *
217  * Get the string value of @field in @token.
218  *
219  * Returns: (transfer none) (nullable): the string value of @field in
220  * @token or %NULL when @field is not defined in @token. The string
221  * becomes invalid when you free @token.
222  */
223 const gchar *
224 gst_rtsp_token_get_string (GstRTSPToken * token, const gchar * field)
225 {
226   return gst_structure_get_string (GST_RTSP_TOKEN_STRUCTURE (token), field);
227 }
228
229 /**
230  * gst_rtsp_token_is_allowed:
231  * @token: a #GstRTSPToken
232  * @field: a field name
233  *
234  * Check if @token has a boolean @field and if it is set to %TRUE.
235  *
236  * Returns: %TRUE if @token has a boolean field named @field set to %TRUE.
237  */
238 gboolean
239 gst_rtsp_token_is_allowed (GstRTSPToken * token, const gchar * field)
240 {
241   gboolean result;
242
243   g_return_val_if_fail (GST_IS_RTSP_TOKEN (token), FALSE);
244   g_return_val_if_fail (field != NULL, FALSE);
245
246   if (!gst_structure_get_boolean (GST_RTSP_TOKEN_STRUCTURE (token), field,
247           &result))
248     result = FALSE;
249
250   return result;
251 }