1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #include <osipparser2/internal.h>
21 
22 #include <osipparser2/osip_port.h>
23 #include <osipparser2/osip_message.h>
24 #include <osipparser2/osip_parser.h>
25 
26 #ifndef MINISIZE
osip_record_route_init(osip_record_route_t ** record_route)27 int osip_record_route_init(osip_record_route_t **record_route) {
28   return osip_from_init((osip_from_t **) record_route);
29 }
30 #endif
31 
32 /* adds the record_route header to message.         */
33 /* INPUT : const char *hvalue | value of header.    */
34 /* OUTPUT: osip_message_t *sip | structure to save results.  */
35 /* returns -1 on error. */
osip_message_set_record_route(osip_message_t * sip,const char * hvalue)36 int osip_message_set_record_route(osip_message_t *sip, const char *hvalue) {
37   osip_record_route_t *record_route;
38   int i;
39 
40   if (hvalue == NULL || hvalue[0] == '\0')
41     return OSIP_SUCCESS;
42 
43   i = osip_record_route_init(&record_route);
44 
45   if (i != 0)
46     return i;
47 
48   i = osip_record_route_parse(record_route, hvalue);
49 
50   if (i != 0) {
51     osip_record_route_free(record_route);
52     return i;
53   }
54 
55   sip->message_property = 2;
56   osip_list_add(&sip->record_routes, record_route, -1);
57   return OSIP_SUCCESS;
58 }
59 
60 #ifndef MINISIZE
61 /* returns the record_route header.    */
62 /* INPUT : osip_message_t *sip | sip message.   */
63 /* returns null on error. */
osip_message_get_record_route(const osip_message_t * sip,int pos,osip_record_route_t ** dest)64 int osip_message_get_record_route(const osip_message_t *sip, int pos, osip_record_route_t **dest) {
65   osip_record_route_t *record_route;
66 
67   *dest = NULL;
68 
69   if (osip_list_size(&sip->record_routes) <= pos)
70     return OSIP_UNDEFINED_ERROR; /* does not exist */
71 
72   record_route = (osip_record_route_t *) osip_list_get(&sip->record_routes, pos);
73   *dest = record_route;
74   return pos;
75 }
76 #endif
77 
78 #ifndef MINISIZE
osip_record_route_parse(osip_record_route_t * record_route,const char * hvalue)79 int osip_record_route_parse(osip_record_route_t *record_route, const char *hvalue) {
80   return osip_from_parse((osip_from_t *) record_route, hvalue);
81 }
82 
83 /* returns the record_route header as a string.          */
84 /* INPUT : osip_record_route_t *record_route | record_route header.  */
85 /* returns -1 on error. */
osip_record_route_to_str(const osip_record_route_t * record_route,char ** dest)86 int osip_record_route_to_str(const osip_record_route_t *record_route, char **dest) {
87   char *url;
88   char *buf;
89   int i;
90   size_t len;
91 
92   *dest = NULL;
93 
94   if ((record_route == NULL) || (record_route->url == NULL))
95     return OSIP_BADPARAMETER;
96 
97   i = osip_uri_to_str(record_route->url, &url);
98 
99   if (i != 0)
100     return i;
101 
102   if (record_route->displayname == NULL)
103     len = strlen(url) + 5;
104 
105   else
106     len = strlen(url) + strlen(record_route->displayname) + 5;
107 
108   buf = (char *) osip_malloc(len);
109 
110   if (buf == NULL) {
111     osip_free(url);
112     return OSIP_NOMEM;
113   }
114 
115   /* route and record-route always use brackets */
116   if (record_route->displayname != NULL)
117     sprintf(buf, "%s <%s>", record_route->displayname, url);
118 
119   else
120     sprintf(buf, "<%s>", url);
121 
122   osip_free(url);
123 
124   {
125     size_t plen;
126     char *tmp;
127     osip_list_iterator_t it;
128     osip_generic_param_t *u_param = (osip_generic_param_t *) osip_list_get_first(&record_route->gen_params, &it);
129 
130     while (u_param != OSIP_SUCCESS) {
131       if (u_param->gvalue == NULL)
132         plen = strlen(u_param->gname) + 2;
133 
134       else
135         plen = strlen(u_param->gname) + strlen(u_param->gvalue) + 3;
136 
137       len = len + plen;
138       buf = (char *) osip_realloc(buf, len);
139       tmp = buf;
140       tmp = tmp + strlen(tmp);
141 
142       if (u_param->gvalue == NULL)
143         snprintf(tmp, len - (tmp - buf), ";%s", u_param->gname);
144 
145       else
146         snprintf(tmp, len - (tmp - buf), ";%s=%s", u_param->gname, u_param->gvalue);
147 
148       u_param = (osip_generic_param_t *) osip_list_get_next(&it);
149     }
150   }
151   *dest = buf;
152   return OSIP_SUCCESS;
153 }
154 
155 /* deallocates a osip_record_route_t structure.  */
156 /* INPUT : osip_record_route_t *record_route | record_route header. */
osip_record_route_free(osip_record_route_t * record_route)157 void osip_record_route_free(osip_record_route_t *record_route) {
158   osip_from_free((osip_from_t *) record_route);
159 }
160 
161 #endif
162