parse_identityinfo.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (c) 2007 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. * ser is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*! \file
  23. * \brief Parser :: Parse Identity-info header field
  24. *
  25. * \ingroup parser
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include "../mem/mem.h"
  31. #include "parse_def.h"
  32. #include "parse_identityinfo.h"
  33. #include "parser_f.h" /* eat_space_end and so on */
  34. /*! \brief Parse Identity-info header field */
  35. void parse_identityinfo(char *buffer, char *end, struct identityinfo_body *ii_b)
  36. {
  37. int status = II_START;
  38. int mainstatus = II_M_START;
  39. char *p;
  40. if (!buffer || !end || !ii_b) return ;
  41. ii_b->error = PARSE_ERROR;
  42. for(p = buffer; p < end; p++) {
  43. switch(*p) {
  44. case '<':
  45. if (status == II_START) {
  46. status=II_URI_BEGIN;
  47. mainstatus = II_M_URI_BEGIN;
  48. ii_b->uri.s = p + 1;
  49. } else
  50. goto parseerror;
  51. break;
  52. case 'h':
  53. case 'H': /* "http://" or "https://" part */
  54. switch (status) {
  55. case II_URI_BEGIN:
  56. if (end - p <= 8 || strncasecmp(p,"http",strlen("http")))
  57. goto parseerror;
  58. p+=4;
  59. if (*p == 's' || *p == 'S') p++;
  60. if (memcmp(p,"://",strlen("://")))
  61. goto parseerror;
  62. p+=2;
  63. status = II_URI_DOMAIN;
  64. break;
  65. case II_URI_DOMAIN:
  66. status = II_URI_IPV4;
  67. case II_URI_IPV4:
  68. case II_URI_IPV6:
  69. case II_URI_PATH:
  70. case II_TOKEN:
  71. case II_TAG:
  72. break;
  73. case II_EQUAL:
  74. status = II_TOKEN;
  75. mainstatus = II_M_TOKEN;
  76. ii_b->alg.s = p;
  77. break;
  78. case II_LWSCRLF:
  79. ii_b->error=PARSE_OK;
  80. return ;
  81. default:
  82. goto parseerror;
  83. }
  84. break;
  85. case '/':
  86. switch(status){
  87. case II_URI_IPV4:
  88. ii_b->domain.len = p - ii_b->domain.s;
  89. status = II_URI_PATH;
  90. break;
  91. case II_URI_PATH:
  92. break;
  93. case II_URI_IPV6:
  94. default:
  95. goto parseerror;
  96. }
  97. break;
  98. case '>':
  99. if (status == II_URI_PATH) {
  100. ii_b->uri.len = p - ii_b->uri.s;
  101. status = II_URI_END;
  102. mainstatus = II_M_URI_END;
  103. } else
  104. goto parseerror;
  105. break;
  106. case ' ':
  107. case '\t':
  108. switch (status) {
  109. case II_EQUAL:
  110. case II_TAG:
  111. case II_SEMIC:
  112. case II_URI_END:
  113. status = II_LWS;
  114. break;
  115. case II_LWS:
  116. case II_LWSCRLFSP:
  117. break;
  118. case II_LWSCRLF:
  119. status = II_LWSCRLFSP;
  120. break;
  121. default:
  122. goto parseerror;
  123. }
  124. break;
  125. case '\r':
  126. switch (status) {
  127. case II_TOKEN:
  128. ii_b->alg.len = p - ii_b->alg.s;
  129. status = II_ENDHEADER;
  130. break;
  131. case II_EQUAL:
  132. case II_TAG:
  133. case II_SEMIC:
  134. case II_URI_END:
  135. case II_LWS:
  136. status = II_LWSCR;
  137. break;
  138. case II_LWSCRLF:
  139. ii_b->error=PARSE_OK;
  140. return ;
  141. default:
  142. goto parseerror;
  143. }
  144. break;
  145. case '\n':
  146. switch (status) {
  147. case II_LWSCRLF:
  148. ii_b->error=PARSE_OK;
  149. return ;
  150. case II_EQUAL:
  151. case II_TAG:
  152. case II_SEMIC:
  153. case II_URI_END:
  154. case II_LWS:
  155. case II_LWSCR:
  156. status = II_LWSCRLF;
  157. break;
  158. case II_TOKEN: /* if there was not '\r' */
  159. ii_b->alg.len = p - ii_b->alg.s;
  160. case II_ENDHEADER:
  161. p=eat_lws_end(p, end);
  162. /*check if the header ends here*/
  163. if (p>=end) {
  164. LOG(L_ERR, "ERROR: parse_identityinfo: strange EoHF\n");
  165. goto parseerror;
  166. }
  167. ii_b->error=PARSE_OK;
  168. return ;
  169. default:
  170. goto parseerror;
  171. }
  172. break;
  173. case ';':
  174. switch (status) {
  175. case II_URI_END:
  176. case II_LWS:
  177. case II_LWSCRLFSP:
  178. if (mainstatus == II_M_URI_END) {
  179. status = II_SEMIC;
  180. mainstatus = II_M_SEMIC;
  181. } else
  182. goto parseerror;
  183. break;
  184. default:
  185. goto parseerror;
  186. }
  187. break;
  188. case 'a': /* tag part of 'alg' parameter */
  189. case 'A':
  190. switch (status) {
  191. case II_LWS:
  192. case II_LWSCRLFSP:
  193. case II_SEMIC:
  194. if (mainstatus == II_M_SEMIC) {
  195. mainstatus = II_M_TAG;
  196. status = II_TAG;
  197. if (end - p <= 3 || strncasecmp(p,"alg",strlen("alg")))
  198. goto parseerror;
  199. p+=2;
  200. } else
  201. goto parseerror;
  202. break;
  203. case II_URI_DOMAIN:
  204. status = II_URI_IPV4;
  205. case II_URI_IPV4:
  206. case II_URI_IPV6:
  207. case II_URI_PATH:
  208. case II_TOKEN:
  209. break;
  210. case II_EQUAL:
  211. status = II_TOKEN;
  212. mainstatus = II_M_TOKEN;
  213. ii_b->alg.s = p;
  214. break;
  215. case II_LWSCRLF:
  216. ii_b->error=PARSE_OK;
  217. return ;
  218. default:
  219. goto parseerror;
  220. }
  221. break;
  222. case '=':
  223. switch (status) {
  224. case II_TAG:
  225. case II_LWS:
  226. case II_LWSCRLFSP:
  227. if (mainstatus == II_M_TAG) {
  228. status = II_EQUAL;
  229. mainstatus = II_M_EQUAL;
  230. } else
  231. goto parseerror;
  232. break;
  233. case II_URI_PATH:
  234. break;
  235. default:
  236. goto parseerror;
  237. }
  238. break;
  239. case '[':
  240. switch (status) {
  241. case II_URI_DOMAIN:
  242. status = II_URI_IPV6;
  243. ii_b->domain.s = p + 1;
  244. break;
  245. default:
  246. goto parseerror;
  247. }
  248. break;
  249. case ']':
  250. switch (status) {
  251. case II_URI_IPV6:
  252. ii_b->domain.len = p - ii_b->domain.s;
  253. status = II_URI_PATH;
  254. break;
  255. case II_URI_IPV4:
  256. case II_URI_PATH:
  257. goto parseerror;
  258. }
  259. break;
  260. case ':':
  261. if (status == II_URI_IPV4) {
  262. ii_b->domain.len = p - ii_b->domain.s;
  263. status = II_URI_PATH;
  264. }
  265. break;
  266. default:
  267. switch (status) {
  268. case II_EQUAL:
  269. case II_LWS:
  270. case II_LWSCRLFSP:
  271. if (mainstatus == II_M_EQUAL) {
  272. status = II_TOKEN;
  273. mainstatus = II_M_TOKEN;
  274. ii_b->alg.s = p;
  275. } else
  276. goto parseerror;
  277. break;
  278. case II_TOKEN:
  279. break;
  280. case II_LWSCRLF:
  281. ii_b->error=PARSE_OK;
  282. return ;
  283. case II_URI_DOMAIN:
  284. ii_b->domain.s = p;
  285. status = II_URI_IPV4;
  286. case II_URI_IPV4:
  287. case II_URI_IPV6:
  288. if (isalnum(*p)
  289. || *p == '-'
  290. || *p == '.'
  291. || *p == ':' )
  292. break;
  293. case II_START:
  294. goto parseerror;
  295. }
  296. break;
  297. }
  298. }
  299. /* we successfully parse the header */
  300. ii_b->error=PARSE_OK;
  301. return ;
  302. parseerror:
  303. LOG( L_ERR , "ERROR: parse_identityinfo: "
  304. "unexpected char [%c] in status %d: <<%.*s>> .\n",
  305. *p,status, (int)(p-buffer), ZSW(p));
  306. return ;
  307. }
  308. int parse_identityinfo_header(struct sip_msg *msg)
  309. {
  310. struct identityinfo_body* identityinfo_b;
  311. if ( !msg->identity_info
  312. && (parse_headers(msg,HDR_IDENTITY_INFO_F,0)==-1
  313. || !msg->identity_info) ) {
  314. LOG(L_ERR,"ERROR:parse_identityinfo_header: bad msg or missing IDENTITY-INFO header\n");
  315. goto error;
  316. }
  317. /* maybe the header is already parsed! */
  318. if (msg->identity_info->parsed)
  319. return 0;
  320. identityinfo_b=pkg_malloc(sizeof(*identityinfo_b));
  321. if (identityinfo_b==0){
  322. LOG(L_ERR, "ERROR:parse_identityinfo_header: out of memory\n");
  323. goto error;
  324. }
  325. memset(identityinfo_b, 0, sizeof(*identityinfo_b));
  326. parse_identityinfo(msg->identity_info->body.s,
  327. msg->identity_info->body.s + msg->identity_info->body.len+1,
  328. identityinfo_b);
  329. if (identityinfo_b->error==PARSE_ERROR){
  330. free_identityinfo(identityinfo_b);
  331. goto error;
  332. }
  333. msg->identity_info->parsed=(void*)identityinfo_b;
  334. return 0;
  335. error:
  336. return -1;
  337. }
  338. void free_identityinfo(struct identityinfo_body *ii_b)
  339. {
  340. pkg_free(ii_b);
  341. }