Kannel: Open Source WAP and SMS gateway  svn-r5335
wap-maps.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * gw/wap-maps.c - URL mapping
59  *
60  * Bruno Rodrigues <bruno.rodrigues@litux.org>
61  */
62 
63 #include "gwlib/gwlib.h"
64 #include "wap-maps.h"
65 
74 };
75 
81 };
82 
83 
84 /*
85  * XXX All mapping functions should be implemented with Dicts instead of
86  * Lists! Linear scans in lists are pretty slow against hash table lookups,
87  * espacially when you have *lot* of entries, which is the case in URL re-
88  * writting in general.
89  * TODO: identify a hash key that can be used and use that as lookup.
90  */
91 
92 /* mapping storrage */
93 static List *url_map = NULL;
94 static Dict *user_map = NULL;
95 
96 
97 /********************************************************************
98  * Creation and destruction of mapping entries
99  */
100 
102  Octstr *send_msisdn_query,
103  Octstr *send_msisdn_header,
104  Octstr *send_msisdn_format,
105  int accept_cookies) {
106  struct url_map_struct *entry;
107 
108  if (url_map == NULL)
110 
111  entry = gw_malloc(sizeof(*entry));
112  entry->name = name;
113  entry->url = url;
114  entry->map_url = map_url;
119 
120  gwlist_append(url_map, entry);
121 }
122 
123 
124 static void wap_user_map_destroy(void *i)
125 {
126  struct user_map_struct *entry = i;
127 
128  octstr_destroy(entry->name);
129  octstr_destroy(entry->user);
130  octstr_destroy(entry->pass);
131  octstr_destroy(entry->msisdn);
132  gw_free(entry);
133 }
134 
135 
137  Octstr *msisdn) {
138  struct user_map_struct *entry;
139 
140  if (user_map == NULL)
142 
143  entry = gw_malloc(sizeof(*entry));
144  entry->name = name;
145  entry->user = user;
146  entry->pass = pass;
147  entry->msisdn = msisdn;
148  dict_put(user_map, entry->user, entry);
149 }
150 
151 
152 void wap_map_destroy(void)
153 {
154  long i;
155  struct url_map_struct *entry;
156 
157  if (url_map != NULL) {
158  for (i = 0; i < gwlist_len(url_map); i++) {
159  entry = gwlist_get(url_map, i);
160  octstr_destroy(entry->name);
161  octstr_destroy(entry->url);
162  octstr_destroy(entry->map_url);
166  gw_free(entry);
167  }
168  gwlist_destroy(url_map, NULL);
169  }
170  url_map = NULL;
171 }
172 
173 
175 {
177  user_map = NULL;
178 }
179 
180 
181 /********************************************************************
182  * Public functions
183  */
184 
185 void wap_map_url_config(char *s)
186 {
187  char *in, *out;
188 
189  s = gw_strdup(s);
190  in = strtok(s, " \t");
191  if (!in)
192  return;
193  out = strtok(NULL, " \t");
194  if (!out)
195  return;
196  wap_map_add_url(octstr_imm("unknown"), octstr_create(in),
197  octstr_create(out), NULL, NULL, NULL, 0);
198  gw_free(s);
199 }
200 
202 {
203  wap_map_add_url(octstr_imm("Device Home"), octstr_imm("DEVICE:home*"),
204  octstr_create(to), NULL, NULL, NULL, -1);
205 }
206 
207 
211 {
212  long i;
213  Octstr *newurl, *tmp1, *tmp2;
214 
215  newurl = tmp1 = tmp2 = NULL;
217  *accept_cookies = -1;
218 
219  debug("wsp",0,"WSP: Mapping url <%s>", octstr_get_cstr(*osp));
220  for (i = 0; url_map && i < gwlist_len(url_map); i++) {
221  struct url_map_struct *entry;
222  entry = gwlist_get(url_map, i);
223 
224  /*
225  debug("wsp",0,"WSP: matching <%s> with <%s>",
226  octstr_get_cstr(entry->url), octstr_get_cstr(entry->map_url));
227  */
228 
229  /* DAVI: I only have '*' terminated entry->url implementation for now */
230  tmp1 = octstr_duplicate(entry->url);
231  octstr_delete(tmp1, octstr_len(tmp1)-1, 1); /* remove last '*' */
232  tmp2 = octstr_copy(*osp, 0, octstr_len(tmp1));
233 
234  debug("wsp",0,"WSP: Matching <%s> with <%s>",
235  octstr_get_cstr(tmp1), octstr_get_cstr(tmp2));
236 
237  if (octstr_case_compare(tmp2, tmp1) == 0) {
238  /* rewrite url if configured to do so */
239  if (entry->map_url != NULL) {
240  if (octstr_get_char(entry->map_url,
241  octstr_len(entry->map_url)-1) == '*') {
242  newurl = octstr_duplicate(entry->map_url);
243  octstr_delete(newurl, octstr_len(newurl)-1, 1);
244  octstr_append(newurl, octstr_copy(*osp,
245  octstr_len(entry->url)-1,
246  octstr_len(*osp)-octstr_len(entry->url)+1));
247  } else {
248  newurl = octstr_duplicate(entry->map_url);
249  }
250  debug("wsp",0,"WSP: URL Rewriten from <%s> to <%s>",
251  octstr_get_cstr(*osp), octstr_get_cstr(newurl));
252  octstr_destroy(*osp);
253  *osp = newurl;
254  }
255  *accept_cookies = entry->accept_cookies;
259  octstr_destroy(tmp1);
260  octstr_destroy(tmp2);
261  break;
262  }
263  octstr_destroy(tmp1);
264  octstr_destroy(tmp2);
265  }
266 }
267 
268 int wap_map_user(Octstr **msisdn, Octstr *user, Octstr *pass)
269 {
270  struct user_map_struct *entry;
271 
272  entry = dict_get(user_map, user);
273  if (entry != NULL && octstr_compare(pass, entry->pass) == 0) {
274  *msisdn = octstr_duplicate(entry->msisdn);
275  return 1;
276  }
277  return 0;
278 }
279 
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void wap_map_url_config_device_home(char *to)
Definition: wap-maps.c:201
int wap_map_user(Octstr **msisdn, Octstr *user, Octstr *pass)
Definition: wap-maps.c:268
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
void gwlist_append(List *list, void *item)
Definition: list.c:179
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
long gwlist_len(List *list)
Definition: list.c:166
static void wap_user_map_destroy(void *i)
Definition: wap-maps.c:124
void * gwlist_get(List *list, long pos)
Definition: list.c:292
static Dict * user_map
Definition: wap-maps.c:94
Octstr * send_msisdn_query
Definition: wap-maps.c:70
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
void wap_map_add_user(Octstr *name, Octstr *user, Octstr *pass, Octstr *msisdn)
Definition: wap-maps.c:136
void wap_map_add_url(Octstr *name, Octstr *url, Octstr *map_url, Octstr *send_msisdn_query, Octstr *send_msisdn_header, Octstr *send_msisdn_format, int accept_cookies)
Definition: wap-maps.c:101
Octstr * send_msisdn_format
Definition: wap-maps.c:72
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Octstr * msisdn
Definition: wap-maps.c:80
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
Octstr * url
Definition: wap-maps.c:68
void wap_map_url_config(char *s)
Definition: wap-maps.c:185
Definition: dict.c:116
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void wap_map_user_destroy(void)
Definition: wap-maps.c:174
char * name
Definition: smsc_cimd2.c:212
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:903
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
int accept_cookies
Definition: wap-maps.c:73
Octstr * map_url
Definition: wap-maps.c:69
#define octstr_create(cstr)
Definition: octstr.h:125
static List * url_map
Definition: wap-maps.c:93
void wap_map_destroy(void)
Definition: wap-maps.c:152
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
void dict_destroy(Dict *dict)
Definition: dict.c:215
Definition: octstr.c:118
Octstr * name
Definition: wap-maps.c:77
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
Octstr * name
Definition: wap-maps.c:67
#define gwlist_create()
Definition: list.h:136
Octstr * user
Definition: wap-maps.c:78
Octstr * pass
Definition: wap-maps.c:79
Octstr * send_msisdn_header
Definition: wap-maps.c:71
static Octstr * url
Definition: test_xmlrpc.c:84
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
Definition: list.c:102
void wap_map_url(Octstr **osp, Octstr **send_msisdn_query, Octstr **send_msisdn_header, Octstr **send_msisdn_format, int *accept_cookies)
Definition: wap-maps.c:208
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.