1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2006 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 /**@CFILE nua_registrar.c
26  * @brief REGISTER UAS
27  *
28  * @author Michael Jerris
29  *
30  * @date Created: Tue Oct  3 10:14:54 EEST 2006 ppessi
31  */
32 
33 #include "config.h"
34 
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <limits.h>
39 
40 #include <assert.h>
41 
42 #define TP_CLIENT_T struct nua_handle_s
43 #define TP_STACK_T struct nta_agent_s
44 
45 #include <sofia-sip/su_string.h>
46 #include <sofia-sip/sip_protos.h>
47 #include <sofia-sip/sip_status.h>
48 #include <sofia-sip/sip_util.h>
49 
50 #define NTA_INCOMING_MAGIC_T struct nua_handle_s
51 #define NTA_RELIABLE_MAGIC_T struct nua_handle_s
52 
53 #include "nua_stack.h"
54 
55 #include <sofia-sip/tport.h>
56 #include <sofia-sip/nta_tport.h>
57 
58 /* ---------------------------------------------------------------------- */
59 /* Registrar usage */
60 
61 struct registrar_usage
62 {
63   tport_t *tport;		 /**<  */
64   int pending;			 /**< Waiting for tport to close */
65 };
66 
nua_registrar_usage_name(nua_dialog_usage_t const * du)67 static char const *nua_registrar_usage_name(nua_dialog_usage_t const *du)
68 {
69   return "registrar";
70 }
71 
nua_registrar_usage_add(nua_handle_t * nh,nua_dialog_state_t * ds,nua_dialog_usage_t * du)72 static int nua_registrar_usage_add(nua_handle_t *nh,
73 				   nua_dialog_state_t *ds,
74 				   nua_dialog_usage_t *du)
75 {
76   return 0;
77 }
78 
nua_registrar_usage_remove(nua_handle_t * nh,nua_dialog_state_t * ds,nua_dialog_usage_t * du,nua_client_request_t * cr,nua_server_request_t * sr)79 static void nua_registrar_usage_remove(nua_handle_t *nh,
80 				       nua_dialog_state_t *ds,
81 				       nua_dialog_usage_t *du,
82 				       nua_client_request_t *cr,
83 				       nua_server_request_t *sr)
84 {
85   struct registrar_usage *ru;
86 
87   ru = nua_dialog_usage_private(du);
88 
89   if (!ru)
90     return;
91 
92   if (ru->pending)
93     tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
94 
95   tport_unref(ru->tport), ru->tport = NULL;
96 }
97 
nua_registrar_usage_refresh(nua_handle_t * nh,nua_dialog_state_t * ds,nua_dialog_usage_t * du,sip_time_t now)98 static void nua_registrar_usage_refresh(nua_handle_t *nh,
99 					nua_dialog_state_t *ds,
100 					nua_dialog_usage_t *du,
101 					sip_time_t now)
102 {
103 }
104 
105 /** Terminate registration usage.
106  *
107  * @retval >0  shutdown done
108  * @retval 0   shutdown in progress
109  * @retval <0  try again later
110  */
nua_registrar_usage_shutdown(nua_handle_t * nh,nua_dialog_state_t * ds,nua_dialog_usage_t * du)111 static int nua_registrar_usage_shutdown(nua_handle_t *nh,
112 					nua_dialog_state_t *ds,
113 					nua_dialog_usage_t *du)
114 {
115   return 1;
116 }
117 
118 static nua_usage_class const nua_registrar_usage[1] = {
119   {
120     sizeof (struct registrar_usage), sizeof nua_registrar_usage,
121     nua_registrar_usage_add,
122     nua_registrar_usage_remove,
123     nua_registrar_usage_name,
124     nua_base_usage_update_params,
125     NULL,
126     nua_registrar_usage_refresh,
127     nua_registrar_usage_shutdown
128   }};
129 
130 
131 /* ======================================================================== */
132 /* REGISTER */
133 
134 /** @NUA_EVENT nua_i_register
135  *
136  * Incoming REGISTER request.
137  *
138  * In order to receive #nua_i_register events, the application must enable
139  * the REGISTER method with NUTAG_ALLOW() tag, e.g.,
140  * @verbatim
141  * nua_set_params(nua;
142  *    NUTAG_APPL_METHOD("REGISTER"),
143  *    NUTAG_ALLOW("REGISTER"),
144  *    TAG_END());
145  * @endverbatim
146  *
147  * The nua_response() call responding to a REGISTER request must include
148  * NUTAG_WITH() (or NUTAG_WITH_THIS()/NUTAG_WITH_SAVED()) tag. Note that
149  * a successful response to REGISTER @b MUST include the @Contact header
150  * bound to the the AoR URI (in @To header).
151  *
152  * The REGISTER request does not create a dialog. Currently the processing
153  * of incoming REGISTER creates a new handle for each incoming request which
154  * is not assiciated with an existing dialog. If the handle @a nh is not
155  * bound, you should probably destroy it after responding to the REGISTER
156  * request.
157  *
158  * @param status status code of response sent automatically by stack
159  * @param phrase a short textual description of @a status code
160  * @param nh     operation handle associated with the request
161  * @param hmagic application context associated with the handle
162  *               (usually NULL)
163  * @param sip    incoming REGISTER request
164  * @param tags   empty
165  *
166  * @sa nua_respond(), @RFC3261 section 10.3,
167  * @Expires, @Contact, @CallID, @CSeq,
168  * @Path, @RFC3327, @ServiceRoute, @RFC3608, @RFC3680,
169  * nua_register(), #nua_i_register, nua_unregister(), #nua_i_unregister
170  *
171  * @since New in @VERSION_1_12_4
172  * @END_NUA_EVENT
173  */
174 
175 static int nua_registrar_server_preprocess(nua_server_request_t *sr);
176 static int nua_registrar_server_report(nua_server_request_t *, tagi_t const *);
177 
178 nua_server_methods_t const nua_register_server_methods =
179   {
180     SIP_METHOD_REGISTER,
181     nua_i_register,		/* Event */
182     {
183       0,			/* Do not create dialog */
184       0,			/* Initial request */
185       0,			/* Not a target refresh request  */
186       0,			/* Do not add Contact */
187     },
188     nua_base_server_init,
189     nua_registrar_server_preprocess,
190     nua_base_server_params,
191     nua_base_server_respond,
192     nua_registrar_server_report,
193   };
194 
195 static void
registrar_tport_error(nta_agent_t * nta,nua_handle_t * nh,tport_t * tp,msg_t * msg,int error)196 registrar_tport_error(nta_agent_t *nta, nua_handle_t *nh,
197 		      tport_t *tp, msg_t *msg, int error)
198 {
199   nua_dialog_state_t *ds = nh->nh_ds;
200   nua_dialog_usage_t *du;
201   struct registrar_usage *ru;
202 
203   SU_DEBUG_3(("tport error %d: %s\n", error, su_strerror(error)));
204 
205   du = nua_dialog_usage_get(ds, nua_registrar_usage, NULL);
206 
207   if (du == NULL)
208     return;
209 
210   ru = nua_dialog_usage_private(du);
211   if (ru->tport) {
212     tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
213     tport_unref(ru->tport), ru->tport = NULL;
214   }
215 
216   nua_stack_event(nh->nh_nua, nh, NULL,
217 		  nua_i_media_error, 500, "Transport error detected",
218 		  NULL);
219 }
220 
221 static int
nua_registrar_server_preprocess(nua_server_request_t * sr)222 nua_registrar_server_preprocess(nua_server_request_t *sr)
223 {
224   nua_handle_t *nh = sr->sr_owner;
225   nua_dialog_state_t *ds = sr->sr_owner->nh_ds;
226   nua_dialog_usage_t *du;
227   struct registrar_usage *ru;
228   tport_t *tport;
229 
230   tport = nta_incoming_transport(nh->nh_nua->nua_nta, sr->sr_irq, sr->sr_request.msg);
231 
232   if (!tport_is_tcp(tport)) {
233 	  tport_unref(tport);
234 	  return 0;
235   }
236 
237   du = nua_dialog_usage_get(ds, nua_registrar_usage, NULL);
238   if (du == NULL)
239     du = nua_dialog_usage_add(nh, ds, nua_registrar_usage, NULL);
240 
241   if (du == NULL)
242     return SR_STATUS1(sr, SIP_500_INTERNAL_SERVER_ERROR);
243 
244   ru = nua_dialog_usage_private(du);
245 
246   if (ru->tport && ru->tport != tport) {
247     tport_release(ru->tport, ru->pending, NULL, NULL, nh, 0), ru->pending = 0;
248     tport_unref(ru->tport), ru->tport = NULL;
249   }
250 
251   ru->tport = tport;
252   ru->pending = tport_pend(tport, NULL, registrar_tport_error, nh);
253 
254   tport_set_params(tport,
255 		   TPTAG_SDWN_ERROR(1),
256 		   TAG_END());
257 
258   return 0;
259 }
260 
261 static int
nua_registrar_server_report(nua_server_request_t * sr,tagi_t const * tags)262 nua_registrar_server_report(nua_server_request_t *sr, tagi_t const *tags)
263 {
264   return nua_base_server_report(sr, tags);
265 }
266