Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

utils.h File Reference

#include <stddef.h>
#include <stdio.h>
#include "octstr.h"
#include <termios.h>

Include dependency graph for utils.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Typedefs

typedef unsigned char Octet
typedef unsigned long MultibyteInt

Functions

MultibyteInt get_variable_value (Octet *source, int *len)
int write_variable_value (MultibyteInt value, Octet *dest)
Octet reverse_octet (Octet source)
int get_and_set_debugs (int argc, char **argv, int(*find_own)(int index, int argc, char **argv))
int is_allowed_ip (Octstr *allow_ip, Octstr *deny_ip, Octstr *ip)
int connect_denied (Octstr *allow_ip, Octstr *ip)
int does_prefix_match (Octstr *prefix, Octstr *number)
int normalize_number (char *dial_prefixes, Octstr **number)
long decode_network_long (unsigned char *data)
void encode_network_long (unsigned char *data, unsigned long value)
void kannel_cfmakeraw (struct termios *tio)
int gw_isdigit (int)
int gw_isxdigit (int)
int roundup_div (int a, int b)
unsigned long long gw_generate_id (void)


Typedef Documentation

typedef unsigned long MultibyteInt
 

Definition at line 76 of file utils.h.

Referenced by get_variable_value().

typedef unsigned char Octet
 

Definition at line 75 of file utils.h.

Referenced by OTAbitmap_create(), OTAbitmap_create_stream(), reverse_octet(), wbmp_create(), wbmp_create_stream(), and write_variable_value().


Function Documentation

int connect_denied Octstr allow_ip,
Octstr ip
 

Definition at line 633 of file utils.c.

References octstr_imm(), and pattern_list_matches_ip().

Referenced by httpsmsc_receiver().

00634 {
00635     if (ip == NULL)
00636     return 1;
00637 
00638     /* If IP not set, allow from Localhost */
00639     if (allow_ip == NULL) { 
00640     if (pattern_list_matches_ip(octstr_imm("127.0.0.1"), ip))
00641         return 0;
00642     } else {
00643     if (pattern_list_matches_ip(allow_ip, ip))
00644         return 0;
00645     }
00646     return 1;
00647 }

Here is the call graph for this function:

long decode_network_long unsigned char *  data  ) 
 

Definition at line 735 of file utils.c.

References data.

Referenced by conn_read_withlen(), parse_integer(), read_msg(), and smpp_pdu_read_len().

00735                                               {
00736         return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
00737 }

int does_prefix_match Octstr prefix,
Octstr number
 

Definition at line 650 of file utils.c.

References gw_assert, number, and octstr_get_cstr.

Referenced by check_allowed_translation(), and smscconn_usable().

00651 {
00652     /* XXX modify to use just octstr operations
00653      */
00654     char *b, *p, *n;
00655 
00656     gw_assert(prefix != NULL);
00657     gw_assert(number != NULL);
00658 
00659     p = octstr_get_cstr(prefix);
00660     n = octstr_get_cstr(number);
00661     
00662 
00663     while (*p != '\0') {
00664         b = n;
00665         for (b = n; *b != '\0'; b++, p++) {
00666             if (*p == ';' || *p == '\0') {
00667                 return 1;
00668             }
00669             if (*p != *b) break;
00670         }
00671         if (*p == ';' || *p == '\0') {
00672             return 1;
00673         }
00674         while (*p != '\0' && *p != ';')
00675             p++;
00676         while (*p == ';') p++;
00677     }
00678     return 0;
00679 }

void encode_network_long unsigned char *  data,
unsigned long  value
 

Definition at line 740 of file utils.c.

References data.

Referenced by append_integer(), conn_write_withlen(), and write_msg().

00740                                                                    {
00741         data[0] = (value >> 24) & 0xff;
00742         data[1] = (value >> 16) & 0xff;
00743         data[2] = (value >> 8) & 0xff;
00744         data[3] = value & 0xff;
00745 }

int get_and_set_debugs int  argc,
char **  argv,
int(*)(int index, int argc, char **argv)  find_own
 

Definition at line 434 of file utils.c.

References become_daemon(), cfg_dump_all(), change_user(), GW_NON_EXCL, info(), log_close_all(), log_open(), log_set_debug_places(), log_set_output_level(), log_set_syslog(), panic, parachute_start(), pid_file, remove_pid_file(), and write_pid_file().

Referenced by main().

00436 {
00437     int i, ret = -1;
00438     int debug_lvl = -1;
00439     int file_lvl = GW_DEBUG;
00440     char *log_file = NULL;
00441     char *debug_places = NULL;
00442     char *panic_script = NULL, *user = NULL;
00443     int parachute = 0, daemonize = 0;
00444 
00445     for (i=1; i < argc; i++) {
00446         if (strcmp(argv[i],"-v")==0 || strcmp(argv[i],"--verbosity")==0) {
00447             if (i+1 < argc) {
00448                 debug_lvl = atoi(argv[i+1]);
00449                 i++;
00450             } else
00451                 panic(0, "Missing argument for option %s\n", argv[i]); 
00452         } else if (strcmp(argv[i],"-F")==0 || strcmp(argv[i],"--logfile")==0) {
00453             if (i+1 < argc && *(argv[i+1]) != '-') {
00454                 log_file = argv[i+1];
00455                 i++;
00456             } else
00457                 panic(0, "Missing argument for option %s\n", argv[i]); 
00458         } else if (strcmp(argv[i],"-V")==0 || strcmp(argv[i],"--fileverbosity")==0) {
00459             if (i+1 < argc) {
00460                 file_lvl = atoi(argv[i+1]);
00461                 i++;
00462             } else
00463                 panic(0, "Missing argument for option %s\n", argv[i]); 
00464         } else if (strcmp(argv[i],"-D")==0 || strcmp(argv[i],"--debug")==0) {
00465             if (i+1 < argc) {
00466                 debug_places = argv[i+1];
00467                 i++;
00468             } else
00469                 panic(0, "Missing argument for option %s\n", argv[i]); 
00470         } else if (strcmp(argv[i], "-X")==0 || strcmp(argv[i], "--panic-script")==0) {
00471             if (i+1 < argc) {
00472                 panic_script = argv[i+1];
00473                 i++;
00474             } else
00475                 panic(0, "Missing argument for option %s\n", argv[i]);
00476         } else if (strcmp(argv[i], "-P")==0 || strcmp(argv[i], "--parachute")==0) {
00477             parachute = 1;
00478         } else if (strcmp(argv[i], "-d")==0 || strcmp(argv[i], "--daemonize")==0) {
00479             daemonize = 1;
00480         } else if (strcmp(argv[i], "-p")==0 || strcmp(argv[i], "--pid-file")==0) {
00481             if (i+1 < argc) {
00482                 pid_file = argv[i+1];
00483                 i++;
00484             } else
00485                 panic(0, "Missing argument for option %s\n", argv[i]);
00486         } else if (strcmp(argv[i], "-u")==0 || strcmp(argv[i], "--user")==0) {
00487             if (i+1 < argc) {
00488                 user = argv[i+1];
00489                 i++;
00490             } else
00491                 panic(0, "Missing argument for option %s\n", argv[i]);
00492         } else if (strcmp(argv[i], "-g")==0 || strcmp(argv[i], "--generate")==0) {
00493             cfg_dump_all();
00494             exit(0);
00495         } else if (strcmp(argv[i],"--")==0) {
00496             i++;
00497             break;
00498         } else if (*argv[i] != '-') {
00499             break;
00500         } else {
00501             if (find_own != NULL) {
00502             ret = find_own(i, argc, argv);
00503         }
00504         if (ret < 0) {
00505             fprintf(stderr, "Unknown option %s, exiting.\n", argv[i]);
00506             panic(0, "Option parsing failed");
00507         } else
00508             i += ret;   /* advance additional args */
00509         }
00510     }
00511 
00512     if (user && -1 == change_user(user))
00513         panic(0, "Could not change to user `%s'.", user);
00514 
00515     /* deamonize */
00516     if (daemonize && !become_daemon())
00517        exit(0);
00518 
00519     if (pid_file) {
00520         write_pid_file();
00521         atexit(remove_pid_file);
00522     }
00523 
00524     if (parachute) {
00525         /*
00526          * if we are running as daemon so open syslog
00527          * in order not to deal with i.e. log rotate.
00528          */
00529         if (daemonize) {
00530             char *ident = strrchr(argv[0], '/');
00531             if (!ident)
00532                 ident = argv[0];
00533             else
00534                 ident++;
00535             log_set_syslog(ident, (debug_lvl > -1 ? debug_lvl : 0));
00536         }
00537         parachute_start(argv[0], panic_script);
00538         /* now we are in child process so close syslog */
00539         if (daemonize)
00540             log_close_all();
00541     }
00542 
00543     if (debug_lvl > -1)
00544         log_set_output_level(debug_lvl);
00545     if (debug_places != NULL)
00546         log_set_debug_places(debug_places);
00547     if (log_file != NULL)
00548         log_open(log_file, file_lvl, GW_NON_EXCL);
00549 
00550     info(0, "Debug_lvl = %d, log_file = %s, log_lvl = %d",
00551          debug_lvl, log_file ? log_file : "<none>", file_lvl);
00552     if (debug_places != NULL)
00553         info(0, "Debug places: `%s'", debug_places);
00554 
00555     return i;
00556 }

Here is the call graph for this function:

MultibyteInt get_variable_value Octet source,
int *  len
 

Definition at line 385 of file utils.c.

References MultibyteInt.

Referenced by sema_decode_msg().

00386 {
00387     MultibyteInt retval = 0;
00388     
00389     for(*len=1;; (*len)++, source++) {
00390     retval = retval * 0x80 + (*source & 0x7F);
00391     if (*source < 0x80)  /* if the continue-bit (high bit) is not set */
00392         break;
00393     }
00394     return retval;
00395 }

unsigned long long gw_generate_id void   ) 
 

Definition at line 813 of file utils.c.

References gw_rand().

00814 {
00815     /* create a 64 bit unique Id by putting a 32 bit epoch time value
00816      * and a 32 bit random value together */
00817     unsigned long random, timer;
00818      
00819     random = gw_rand();
00820     timer = (unsigned long)time(NULL);
00821     
00822     return ((unsigned long long)timer << 32) + random;
00823 }

Here is the call graph for this function:

int gw_isdigit int   ) 
 

Definition at line 788 of file utils.c.

Referenced by convert_addr_from_pdu(), date_parse_iso(), get_originator_type(), handle_dlr(), handle_pdu(), msg_to_cgwop(), msg_to_emimsg(), msg_to_pdu(), pack_parameter(), packet_check_header(), packet_check_parameter(), and run_batch().

00789 {
00790     return isdigit(c);
00791 }

int gw_isxdigit int   ) 
 

Definition at line 794 of file utils.c.

Referenced by octstr_hex_to_binary(), octstr_is_all_hex(), octstr_symbolize(), packet_check(), and packet_check_parameter().

00795 {
00796     return isxdigit(c);
00797 }

int is_allowed_ip Octstr allow_ip,
Octstr deny_ip,
Octstr ip
 

Definition at line 615 of file utils.c.

References octstr_len(), and pattern_list_matches_ip().

Referenced by accept_boxc(), authorise_username(), cgw_listener(), emi2_listener(), fake_listener(), get_data(), httpadmin_run(), main(), and udp_receiver().

00616 {
00617     if (ip == NULL)
00618     return 0;
00619 
00620     if (octstr_len(deny_ip) == 0)
00621     return 1;
00622 
00623     if (allow_ip != NULL && pattern_list_matches_ip(allow_ip, ip))
00624     return 1;
00625 
00626     if (pattern_list_matches_ip(deny_ip, ip))
00627         return 0;
00628 
00629     return 1;
00630 }

Here is the call graph for this function:

void kannel_cfmakeraw struct termios *  tio  ) 
 

Definition at line 751 of file utils.c.

Referenced by at2_open_device(), at_dial(), and X28_open_data_link().

00751                                            {
00752     /* Block until a charactor is available, but it only needs to be one*/
00753     tio->c_cc[VMIN]    = 1;
00754     tio->c_cc[VTIME]   = 0;
00755 
00756     /* GNU cfmakeraw sets these flags so we had better too...*/
00757 
00758     /* Control modes */
00759     /* Mask out character size (CSIZE), then set it to 8 bits (CS8).
00760      * Enable parity bit generation in both directions (PARENB).
00761      */
00762     tio->c_cflag      &= ~(CSIZE|PARENB);
00763     tio->c_cflag      |= CS8;
00764 
00765     /* Input Flags,*/
00766     /* Turn off all input flags that interfere with the byte stream:
00767      * BRKINT - generate SIGINT when receiving BREAK, ICRNL - translate
00768      * NL to CR, IGNCR - ignore CR, IGNBRK - ignore BREAK,
00769      * INLCR - translate NL to CR, IXON - use XON/XOFF flow control,
00770      * ISTRIP - strip off eighth bit.
00771      */
00772     tio->c_iflag &= ~(BRKINT|ICRNL|IGNCR|IGNBRK|INLCR|IXON|ISTRIP);
00773 
00774     /* Other flags,*/
00775     /* Turn off all local flags that interpret the byte stream:
00776      * ECHO - echo input chars, ECHONL - always echo NL even if ECHO is off,
00777      * ICANON - enable canonical mode (basically line-oriented mode),
00778      * IEXTEN - enable implementation-defined input processing,
00779      * ISIG - generate signals when certain characters are received. */
00780     tio->c_lflag      &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
00781 
00782     /* Output flags,*/
00783     /* Disable implementation defined processing on the output stream*/
00784     tio->c_oflag      &= ~OPOST;
00785 }

int normalize_number char *  dial_prefixes,
Octstr **  number
 

Definition at line 682 of file utils.c.

References gw_assert, number, octstr_create_from_data, octstr_destroy(), octstr_get_cstr, octstr_insert_data(), and octstr_len().

Referenced by bb_smscconn_receive(), radius_acct_get_msisdn(), smsc2_rout(), and smscconn_send().

00683 {
00684     char *t, *p, *official, *start;
00685     int len, official_len;
00686     
00687     if (dial_prefixes == NULL || dial_prefixes[0] == '\0')
00688         return 0;
00689 
00690     t = official = dial_prefixes;
00691     official_len = 0;
00692 
00693     gw_assert(number != NULL);
00694     
00695     while(1) {
00696 
00697         p = octstr_get_cstr(*number);
00698         for(start = t, len = 0; ; t++, p++, len++)
00699     {
00700             if (*t == ',' || *t == ';' || *t == '\0') {
00701                 if (start != official) {
00702                     Octstr *nstr;
00703             long n;
00704             
00705             if ( official[0] == '-' ) official_len=0;
00706             n = official_len;
00707             if (strlen(official) < (size_t) n)
00708                 n = strlen(official);
00709                     nstr = octstr_create_from_data(official, n);
00710                     octstr_insert_data(nstr, official_len,
00711                                            octstr_get_cstr(*number) + len,
00712                                            octstr_len(*number) - len);
00713                     octstr_destroy(*number);
00714                     *number = nstr;
00715                 }
00716                 return 1;
00717             }
00718             if (*p == '\0' || *t != *p)
00719                 break;          /* not matching */
00720         }
00721         for(; *t != ',' && *t != ';' && *t != '\0'; t++, len++)
00722             ;
00723         if (*t == '\0') break;
00724         if (start == official) official_len = len;
00725         if (*t == ';') official = t+1;
00726         t++;
00727     }
00728     return 0;
00729 }

Here is the call graph for this function:

Octet reverse_octet Octet  source  ) 
 

Definition at line 418 of file utils.c.

References Octet.

Referenced by OTAbitmap_create(), and wbmp_create().

00419 {
00420     Octet   dest;
00421     dest = (source & 1) <<7;
00422     dest += (source & 2) <<5;
00423     dest += (source & 4) <<3;
00424     dest += (source & 8) <<1;
00425     dest += (source & 16) >>1;
00426     dest += (source & 32) >>3;
00427     dest += (source & 64) >>5;
00428     dest += (source & 128) >>7;
00429     
00430     return dest;
00431 }

int roundup_div int  a,
int  b
 

Definition at line 801 of file utils.c.

00802 {
00803     int t;
00804 
00805     t = a / b;
00806     if (t * b != a)
00807         t += 1;
00808 
00809     return t;
00810 }

int write_variable_value MultibyteInt  value,
Octet dest
 

Definition at line 398 of file utils.c.

References Octet.

Referenced by sema_encode_msg(), sema_submit_result(), and wbmp_create_stream().

00399 {
00400     int i, loc = 0;
00401     Octet revbuffer[20];    /* we write it backwards */
00402     
00403     for (;;) {
00404     revbuffer[loc++] = (value & 0x7F) + 0x80;   
00405     if (value >= 0x80)
00406         value = value >> 7;
00407     else
00408         break;
00409     }
00410     for(i=0; i < loc; i++)      /* reverse the buffer */
00411     dest[i] = revbuffer[loc-i-1];
00412     
00413     dest[loc-1] &= 0x7F;    /* remove trailer-bit from last */
00414 
00415     return loc;
00416 }

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.