00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * utils.c - generally useful, non-application specific functions for Gateway 00059 * 00060 * Kalle Marjola <rpr@wapit.com> 00061 */ 00062 00063 #ifndef GW_UTILS_H 00064 #define GW_UTILS_H 00065 00066 #include <stddef.h> 00067 #include <stdio.h> 00068 #include "octstr.h" 00069 #include <termios.h> 00070 00071 /* 00072 * Octet and MultibyteInteger (variable length) functions 00073 */ 00074 00075 typedef unsigned char Octet; /* 8-bit basic data */ 00076 typedef unsigned long MultibyteInt; /* limited to 32 bits, not 35 */ 00077 00078 /* get value of a multibyte ineteger. Note that it MUST be a valid 00079 * numbers, otherwise an overflow may occur as the function keeps 00080 * on reading the number until continue-bit (high bit) is not set. 00081 * Does not fail, always returns some number, but may overflow. */ 00082 MultibyteInt get_variable_value(Octet *source, int *len); 00083 00084 /* write given multibyte integer into given destination string, which 00085 * must be large enough to handle the number (5 bytes is always sufficient) 00086 * returns the total length of the written number, in bytes. Does not fail */ 00087 int write_variable_value(MultibyteInt value, Octet *dest); 00088 00089 /* reverse the value of an octet */ 00090 Octet reverse_octet(Octet source); 00091 00092 /* parse command line arguments and set options for the following 00093 * global arguments: 00094 * 00095 * -v or --verbosity 00096 * -D or --debug 00097 * -F or --logfile 00098 * -V or --fileverbosity 00099 * -X or --panic-script 00100 * -P or --parachute 00101 * -d or --daemonize 00102 * -p or --pid-file 00103 * -u or --user 00104 * -g or --generate 00105 * 00106 * Any other argument starting with '-' calls 'find_own' function, 00107 * which is provided by the user. If set to NULL, these are ignored 00108 * (but error message is put into stderr) 00109 * 00110 * Returns index of next argument after any parsing 00111 * 00112 * Function 'find_own' has following parameters: 00113 * index is the current index in argv 00114 * argc and argv are command line parameters, directly transfered 00115 * 00116 * the function returns any extra number of parameters needed to be 00117 * skipped. It must personally deal with any malformed arguments. 00118 * It return -1 if it cannot find match for the argument 00119 * 00120 * sample simple function is like: 00121 * int find_is_there_X(int i, int argc, char **argv) 00122 * { if (strcmp(argv[i], "-X")==0) return 0; else return -1; } 00123 */ 00124 int get_and_set_debugs(int argc, char **argv, 00125 int (*find_own) (int index, int argc, char **argv)); 00126 00127 00128 /* 00129 * return 0 if 'ip' is denied by deny_ip and not allowed by allow_ip 00130 * return 1 otherwise (deny_ip is NULL or 'ip' is in allow_ip or is not 00131 * in deny_ip) 00132 * return -1 on error ('ip' is NULL) 00133 */ 00134 int is_allowed_ip(Octstr *allow_ip, Octstr *deny_ip, Octstr *ip); 00135 00136 /* 00137 * Return 1 if 'ip' is not allowed to connect, when 'allow_ip' defines 00138 * allowed hosts, and 0 if connect ok. If 'allow_ip' is NULL, check against 00139 * "127.0.0.1" (localhost) 00140 */ 00141 int connect_denied(Octstr *allow_ip, Octstr *ip); 00142 00143 /* 00144 * Checks if a given prefix list seperated with semicolon does match 00145 * a given number. 00146 * 00147 * This is mainly used for the allowed-prefix, denied-prefix configuration 00148 * directives. 00149 */ 00150 int does_prefix_match(Octstr *prefix, Octstr *number); 00151 00152 /* 00153 * Normalize 'number', like change number "040500" to "0035840500" if 00154 * the dial-prefix is like "0035840,040;0035850,050" 00155 * 00156 * return -1 on error, 0 if no match in dial_prefixes and 1 if match found 00157 * If the 'number' needs normalization, it is done. 00158 */ 00159 int normalize_number(char *dial_prefixes, Octstr **number); 00160 00161 /* 00162 * Convert a standard "network long" (32 bits in 4 octets, most significant 00163 * octet first) to the host representation. 00164 */ 00165 long decode_network_long(unsigned char *data); 00166 00167 00168 /* 00169 * Convert a long to the standard network representation (32 bits in 4 00170 * octets, most significant octet first). 00171 */ 00172 void encode_network_long(unsigned char *data, unsigned long value); 00173 00174 00175 /* kannel implementation of cfmakeraw, which is an extension in GNU libc */ 00176 void kannel_cfmakeraw (struct termios *tio); 00177 00178 00179 /* 00180 * Wrappers around the isdigit and isxdigit functions that are guaranteed 00181 * to be functions, not macros. (The standard library functions are also 00182 * guaranteed by the C standard to be functions, in addition to possibly 00183 * also being macros, but not all implementations follow the standard.) 00184 */ 00185 int gw_isdigit(int); 00186 int gw_isxdigit(int); 00187 00188 00189 /* 00190 * Rounds up the result of a division 00191 */ 00192 int roundup_div(int a, int b); 00193 00194 00195 /* 00196 * generate a unique id 00197 * (not guarenteed to be unique, but it's extremly unlikely for it not to be) 00198 */ 00199 unsigned long long gw_generate_id(void); 00200 00201 00202 #endif