select.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. /* next parameter is optional (use with CONSUME_NEXT_STR or CONSUME_NEXT_INT
  60. * resolution is accepted even if there is no other parameter
  61. * or the parameter is of wrong type
  62. */
  63. #define OPTIONAL 1<<12
  64. /* left function is noted to be called
  65. * rigth function continues in resolution
  66. * NOTE: the parameter is not consumed for PARENT,
  67. * so you can leave it as ..,SEL_PARAM_INT, 0,..
  68. *
  69. * run_select then calls all functions with PARENT flag
  70. * in the order of resolution until the final call or
  71. * the result is != 0 (<0 error, 1 null str)
  72. * the only one parameter passed between nested calls
  73. * is the result str*
  74. */
  75. #define NESTED 1<<13
  76. /* "fixup call" would be done, when the structure is resolved to this node
  77. * which means call with res and msg NULL
  78. *
  79. * if the fixup call return value <0, the select resolution will fail
  80. */
  81. #define FIXUP_CALL 1<<14
  82. /*
  83. * Selector call parameter
  84. */
  85. typedef enum {
  86. SEL_PARAM_INT, /* Integer parameter */
  87. SEL_PARAM_STR, /* String parameter */
  88. SEL_PARAM_DIV, /* Integer value got from parsing table */
  89. SEL_PARAM_PTR /* void* data got from e.g. fixup call */
  90. } select_param_type_t;
  91. typedef union {
  92. int i; /* Integer value */
  93. str s; /* String value */
  94. void* p;/* Any data ptr */
  95. } select_param_value_t;
  96. typedef struct sel_param {
  97. select_param_type_t type;
  98. select_param_value_t v;
  99. } select_param_t;
  100. struct select;
  101. typedef int (*select_f)(str* res, struct select* s, struct sip_msg* msg);
  102. typedef struct select {
  103. select_f f[MAX_NESTED_CALLS];
  104. int param_offset[MAX_NESTED_CALLS+1];
  105. /* contains broken down select string (@foo.bar[-2].foo -> 4 entries) */
  106. select_param_t params[MAX_SELECT_PARAMS];
  107. /* how many elements are used in 'params' */
  108. int n;
  109. } select_t;
  110. typedef struct {
  111. select_f curr_f;
  112. select_param_type_t type;
  113. str name;
  114. select_f new_f;
  115. int flags;
  116. } select_row_t;
  117. typedef struct select_table {
  118. select_row_t *table;
  119. struct select_table *next;
  120. } select_table_t;
  121. /* the level of the select call that is beeing evaluated
  122. * by the child process
  123. */
  124. extern int select_level;
  125. /*
  126. * Lookup corresponding select function based on
  127. * the select parameters
  128. */
  129. int resolve_select(select_t* s);
  130. /*
  131. * Run the select function
  132. */
  133. int run_select(str* res, select_t* s, struct sip_msg* msg);
  134. /*
  135. * Print select for debugging purposes
  136. */
  137. void print_select(select_t* s);
  138. /*
  139. * Register modules' own select parser table
  140. */
  141. int register_select_table(select_row_t *table);
  142. /*
  143. * Tries to parse string pointed by *p (up to first non alpha char) into select structure
  144. * if parsing succeeded, call resolve_select
  145. * if resolving passes, returns final structure
  146. * *p moves to first unused char
  147. * return 0
  148. *
  149. * if memory allocation fails, returns -1
  150. * if parsing or resolving fails, returns -2
  151. */
  152. int parse_select (char** p, select_t** s);
  153. /**
  154. * Frees the select obtained with parse_select().
  155. */
  156. void free_select(select_t *s);
  157. /*
  158. * Select parser, result is stored in SHARED memory
  159. *
  160. * If you call this, you must ensure, that the string which
  161. * is beeing parsed MUST be at the same place for all child
  162. * processes, e.g. allocated in the shared memory as well
  163. *
  164. * parameters and results same as parse_select
  165. */
  166. int shm_parse_select (char** p, select_t** s);
  167. #define SELECT_F(function) extern int function (str* res, select_t* s, struct sip_msg* msg);
  168. #define ABSTRACT_F(function) int function (str* res, select_t* s, struct sip_msg* msg) {return -1;}
  169. #endif /* _SELECT_H */