Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
utils.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 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  * utils.c - generally useful, non-application specific functions for Gateway
59  *
60  * Kalle Marjola <rpr@wapit.com>
61  */
62 
63 #ifndef GW_UTILS_H
64 #define GW_UTILS_H
65 
66 #include <stddef.h>
67 #include <stdio.h>
68 #include "octstr.h"
69 #include <termios.h>
70 
71 /*
72  * Octet and MultibyteInteger (variable length) functions
73  */
74 
75 typedef unsigned char Octet; /* 8-bit basic data */
76 typedef unsigned long MultibyteInt; /* limited to 32 bits, not 35 */
77 
78 /* get value of a multibyte ineteger. Note that it MUST be a valid
79  * numbers, otherwise an overflow may occur as the function keeps
80  * on reading the number until continue-bit (high bit) is not set.
81  * Does not fail, always returns some number, but may overflow. */
82 MultibyteInt get_variable_value(Octet *source, int *len);
83 
84 /* write given multibyte integer into given destination string, which
85  * must be large enough to handle the number (5 bytes is always sufficient)
86  * returns the total length of the written number, in bytes. Does not fail */
87 int write_variable_value(MultibyteInt value, Octet *dest);
88 
89 /* reverse the value of an octet */
90 Octet reverse_octet(Octet source);
91 
92 /* parse command line arguments and set options for the following
93  * global arguments:
94  *
95  * -v or --verbosity
96  * -D or --debug
97  * -F or --logfile
98  * -V or --fileverbosity
99  * -X or --panic-script
100  * -P or --parachute
101  * -d or --daemonize
102  * -p or --pid-file
103  * -u or --user
104  * -g or --generate
105  *
106  * Any other argument starting with '-' calls 'find_own' function,
107  * which is provided by the user. If set to NULL, these are ignored
108  * (but error message is put into stderr)
109  *
110  * Returns index of next argument after any parsing
111  *
112  * Function 'find_own' has following parameters:
113  * index is the current index in argv
114  * argc and argv are command line parameters, directly transfered
115  *
116  * the function returns any extra number of parameters needed to be
117  * skipped. It must personally deal with any malformed arguments.
118  * It return -1 if it cannot find match for the argument
119  *
120  * sample simple function is like:
121  * int find_is_there_X(int i, int argc, char **argv)
122  * { if (strcmp(argv[i], "-X")==0) return 0; else return -1; }
123  */
124 int get_and_set_debugs(int argc, char **argv,
125  int (*find_own) (int index, int argc, char **argv));
126 
127 
128 /*
129  * return 0 if 'ip' is denied by deny_ip and not allowed by allow_ip
130  * return 1 otherwise (deny_ip is NULL or 'ip' is in allow_ip or is not
131  * in deny_ip)
132  * return -1 on error ('ip' is NULL)
133  */
135 
136 /*
137  * Return 1 if 'ip' is not allowed to connect, when 'allow_ip' defines
138  * allowed hosts, and 0 if connect ok. If 'allow_ip' is NULL, check against
139  * "127.0.0.1" (localhost)
140  */
142 
143 /*
144  * Checks if a given prefix list seperated with semicolon does match
145  * a given number.
146  *
147  * This is mainly used for the allowed-prefix, denied-prefix configuration
148  * directives.
149  */
150 int does_prefix_match(Octstr *prefix, Octstr *number);
151 
152 /*
153  * Normalize 'number', like change number "040500" to "0035840500" if
154  * the dial-prefix is like "0035840,040;0035850,050"
155  *
156  * return -1 on error, 0 if no match in dial_prefixes and 1 if match found
157  * If the 'number' needs normalization, it is done.
158  */
159 int normalize_number(char *dial_prefixes, Octstr **number);
160 
161 /*
162  * Convert a standard "network long" (32 bits in 4 octets, most significant
163  * octet first) to the host representation.
164  */
165 long decode_network_long(unsigned char *data);
166 
167 
168 /*
169  * Convert a long to the standard network representation (32 bits in 4
170  * octets, most significant octet first).
171  */
172 void encode_network_long(unsigned char *data, unsigned long value);
173 
174 
175 /* kannel implementation of cfmakeraw, which is an extension in GNU libc */
176 void kannel_cfmakeraw (struct termios *tio);
177 
178 
179 /*
180  * Wrappers around the isdigit and isxdigit functions that are guaranteed
181  * to be functions, not macros. (The standard library functions are also
182  * guaranteed by the C standard to be functions, in addition to possibly
183  * also being macros, but not all implementations follow the standard.)
184  */
185 int gw_isdigit(int);
186 int gw_isxdigit(int);
187 
188 
189 /*
190  * Rounds up the result of a division
191  */
192 int roundup_div(int a, int b);
193 
194 
195 /*
196  * generate a unique id
197  * (not guarenteed to be unique, but it's extremly unlikely for it not to be)
198  */
199 unsigned long long gw_generate_id(void);
200 
205 void init_fatal_signals(void);
206 
207 /*
208  * Return an octet string with information about Kannel version,
209  * operating system, and libxml version. The caller must take care to
210  * destroy the string when done.
211  */
212 Octstr *version_report_string(const char *boxname);
213 
214 
215 /*
216  * Output the information returned by version_report_string to the log
217  * files.
218  */
219 void report_versions(const char *boxname);
220 
221 
222 #endif
int number
Definition: smsc_cimd2.c:213
int gw_isdigit(int)
Definition: utils.c:988
void report_versions(const char *boxname)
Definition: utils.c:539
int is_allowed_ip(Octstr *allow_ip, Octstr *deny_ip, Octstr *ip)
Definition: utils.c:815
int get_and_set_debugs(int argc, char **argv, int(*find_own)(int index, int argc, char **argv))
Definition: utils.c:626
int does_prefix_match(Octstr *prefix, Octstr *number)
Definition: utils.c:850
int normalize_number(char *dial_prefixes, Octstr **number)
Definition: utils.c:882
unsigned long MultibyteInt
Definition: utils.h:76
void init_fatal_signals(void)
Definition: utils.c:527
void encode_network_long(unsigned char *data, unsigned long value)
Definition: utils.c:940
MultibyteInt get_variable_value(Octet *source, int *len)
Definition: utils.c:477
void kannel_cfmakeraw(struct termios *tio)
Definition: utils.c:951
Octstr * version_report_string(const char *boxname)
Definition: utils.c:549
int write_variable_value(MultibyteInt value, Octet *dest)
Definition: utils.c:490
static Octstr * deny_ip
Definition: bb_udp.c:112
int connect_denied(Octstr *allow_ip, Octstr *ip)
Definition: utils.c:833
unsigned long long gw_generate_id(void)
Definition: utils.c:1013
static Octstr * allow_ip
Definition: bb_udp.c:111
Definition: octstr.c:118
int roundup_div(int a, int b)
Definition: utils.c:1001
int gw_isxdigit(int)
Definition: utils.c:994
Octet reverse_octet(Octet source)
Definition: utils.c:511
long decode_network_long(unsigned char *data)
Definition: utils.c:935
unsigned char Octet
Definition: utils.h:75
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.