select.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005-2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2005-12-19 select framework (mma)
  30. * 2006-01-19 multiple nested calls, IS_ALIAS -> NESTED flag renamed (mma)
  31. */
  32. #ifndef _SELECT_H
  33. #define _SELECT_H
  34. #include "str.h"
  35. #include "parser/msg_parser.h"
  36. #define MAX_SELECT_PARAMS 32
  37. #define MAX_NESTED_CALLS 4
  38. /* Flags for parser table FLAG bitfiels
  39. */
  40. #define DIVERSION_MASK 0x00FF
  41. /* if DIVERSION is set and the function is accepted
  42. * the param is changed into SEL_PARAM_DIV and the value is set to (flags & DIVERSION_MASK)
  43. * - it is valuable for STR params (saves parsing time)
  44. * - does not release the memory occupied by the parameter
  45. */
  46. #define DIVERSION 1<<8
  47. /* set if any parameter is expected at this stage
  48. * (the function must be resolved further)
  49. */
  50. #define SEL_PARAM_EXPECTED 1<<9
  51. /* accept if following parameter is STR (any)
  52. * consume that extra parameter in one step
  53. */
  54. #define CONSUME_NEXT_STR 1<<10
  55. /* accept if following parameter is INT
  56. * consume that extra parameter in one ste
  57. */
  58. #define CONSUME_NEXT_INT 1<<11
  59. /* accept all the following parameters
  60. * without checking them
  61. */
  62. #define CONSUME_ALL 1<<12
  63. /* next parameter is optional (use with CONSUME_NEXT_STR or CONSUME_NEXT_INT
  64. * resolution is accepted even if there is no other parameter
  65. * or the parameter is of wrong type
  66. */
  67. #define OPTIONAL 1<<13
  68. /* left function is noted to be called
  69. * rigth function continues in resolution
  70. * NOTE: the parameter is not consumed for PARENT,
  71. * so you can leave it as ..,SEL_PARAM_INT, 0,..
  72. *
  73. * run_select then calls all functions with PARENT flag
  74. * in the order of resolution until the final call or
  75. * the result is != 0 (<0 error, 1 null str)
  76. * the only one parameter passed between nested calls
  77. * is the result str*
  78. */
  79. #define NESTED 1<<14
  80. /* "fixup call" would be done, when the structure is resolved to this node
  81. * which means call with res and msg NULL
  82. *
  83. * if the fixup call return value <0, the select resolution will fail
  84. */
  85. #define FIXUP_CALL 1<<15
  86. /*
  87. * Selector call parameter
  88. */
  89. typedef enum {
  90. SEL_PARAM_INT, /* Integer parameter */
  91. SEL_PARAM_STR, /* String parameter */
  92. SEL_PARAM_DIV, /* Integer value got from parsing table */
  93. SEL_PARAM_PTR /* void* data got from e.g. fixup call */
  94. } select_param_type_t;
  95. typedef union {
  96. int i; /* Integer value */
  97. str s; /* String value */
  98. void* p;/* Any data ptr */
  99. } select_param_value_t;
  100. typedef struct sel_param {
  101. select_param_type_t type;
  102. select_param_value_t v;
  103. } select_param_t;
  104. struct select;
  105. typedef int (*select_f)(str* res, struct select* s, struct sip_msg* msg);
  106. typedef struct select {
  107. select_f f[MAX_NESTED_CALLS];
  108. int param_offset[MAX_NESTED_CALLS+1];
  109. /* contains broken down select string (@foo.bar[-2].foo -> 4 entries) */
  110. select_param_t params[MAX_SELECT_PARAMS];
  111. /* how many elements are used in 'params' */
  112. int n;
  113. } select_t;
  114. typedef struct {
  115. select_f curr_f;
  116. select_param_type_t type;
  117. str name;
  118. select_f new_f;
  119. int flags;
  120. } select_row_t;
  121. typedef struct select_table {
  122. select_row_t *table;
  123. struct select_table *next;
  124. } select_table_t;
  125. /* the level of the select call that is beeing evaluated
  126. * by the child process
  127. */
  128. extern int select_level;
  129. /* pointer to the SIP uri beeing processed.
  130. * Nested function calls can pass information to each
  131. * other using this pointer. Only for performace reasons.
  132. * (Miklos)
  133. */
  134. extern struct sip_uri *select_uri_p;
  135. /*
  136. * Lookup corresponding select function based on
  137. * the select parameters
  138. */
  139. int resolve_select(select_t* s);
  140. /*
  141. * Run the select function
  142. */
  143. int run_select(str* res, select_t* s, struct sip_msg* msg);
  144. /*
  145. * Print select for debugging purposes
  146. */
  147. void print_select(select_t* s);
  148. /*
  149. * Register modules' own select parser table
  150. */
  151. int register_select_table(select_row_t *table);
  152. /*
  153. * Tries to parse string pointed by *p (up to first non alpha char) into select structure
  154. * if parsing succeeded, call resolve_select
  155. * if resolving passes, returns final structure
  156. * *p moves to first unused char
  157. * return 0
  158. *
  159. * if memory allocation fails, returns -1
  160. * if parsing or resolving fails, returns -2
  161. */
  162. int parse_select (char** p, select_t** s);
  163. /**
  164. * Frees the select obtained with parse_select().
  165. */
  166. void free_select(select_t *s);
  167. /*
  168. * Select parser, result is stored in SHARED memory
  169. *
  170. * If you call this, you must ensure, that the string which
  171. * is beeing parsed MUST be at the same place for all child
  172. * processes, e.g. allocated in the shared memory as well
  173. *
  174. * parameters and results same as parse_select
  175. */
  176. int shm_parse_select (char** p, select_t** s);
  177. /**
  178. * Frees the select obtained with shm_parse_select().
  179. */
  180. void shm_free_select(select_t *s);
  181. #define SELECT_F(function) extern int function (str* res, select_t* s, struct sip_msg* msg);
  182. #define ABSTRACT_F(function) int function (str* res, select_t* s, struct sip_msg* msg) {return -1;}
  183. #endif /* _SELECT_H */