sipcapture.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /*
  2. * $Id$
  3. *
  4. * sipcapture module - helper module to capture sip messages
  5. *
  6. * Copyright (C) 2011-2014 Alexandr Dubovikov (QSC AG) ([email protected])
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. /*! \file
  26. * sipcapture module - helper module to capture sip messages
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <net/if.h>
  38. #include <netdb.h>
  39. #include <arpa/inet.h>
  40. /* BPF structure */
  41. #ifdef __OS_linux
  42. #include <linux/filter.h>
  43. #endif
  44. #ifndef __USE_BSD
  45. #define __USE_BSD /* on linux use bsd version of iphdr (more portable) */
  46. #endif /* __USE_BSD */
  47. #include <netinet/ip.h>
  48. #define __FAVOR_BSD /* on linux use bsd version of udphdr (more portable) */
  49. #include <netinet/udp.h>
  50. #include "../../sr_module.h"
  51. #include "../../dprint.h"
  52. #include "../../events.h"
  53. #include "../../ut.h"
  54. #include "../../ip_addr.h"
  55. #include "../../mem/mem.h"
  56. #include "../../mem/shm_mem.h"
  57. #include "../../lib/kmi/mi.h"
  58. #include "../../rpc.h"
  59. #include "../../rpc_lookup.h"
  60. #include "../../lib/srdb1/db.h"
  61. #include "../../parser/contact/parse_contact.h"
  62. #include "../../parser/parse_content.h"
  63. #include "../../parser/parse_from.h"
  64. #include "../../parser/parse_uri.h"
  65. #include "../../parser/digest/digest.h"
  66. #include "../../parser/parse_ppi_pai.h"
  67. #include "../../pvar.h"
  68. #include "../../str.h"
  69. #include "../../onsend.h"
  70. #include "../../resolve.h"
  71. #include "../../receive.h"
  72. #include "../../mod_fix.h"
  73. #include "sipcapture.h"
  74. #include "hash_mode.h"
  75. #include "hep.h"
  76. #ifdef STATISTICS
  77. #include "../../lib/kcore/statistics.h"
  78. #endif
  79. MODULE_VERSION
  80. #define ETHHDR 14 /* sizeof of ethhdr structure */
  81. #define EMPTY_STR(val) val.s=""; val.len=0;
  82. #define TABLE_LEN 256
  83. #define NR_KEYS 37
  84. #define RTCP_NR_KEYS 12
  85. /*multiple table mode*/
  86. enum e_mt_mode{
  87. mode_random = 1,
  88. mode_hash,
  89. mode_round_robin,
  90. mode_error
  91. };
  92. typedef struct _capture_mode_data {
  93. unsigned int id;
  94. str name;
  95. str db_url;
  96. db1_con_t *db_con;
  97. db_func_t db_funcs;
  98. str * table_names;
  99. unsigned int no_tables;
  100. enum e_mt_mode mtmode;
  101. enum hash_source hash_source;
  102. unsigned int rr_idx;
  103. stat_var* sipcapture_req;
  104. stat_var* sipcapture_rpl;
  105. struct _capture_mode_data * next;
  106. }_capture_mode_data_t;
  107. _capture_mode_data_t * capture_modes_root = NULL;
  108. _capture_mode_data_t * capture_def = NULL;
  109. /* module function prototypes */
  110. static int mod_init(void);
  111. static int sipcapture_init_rpc(void);
  112. static int child_init(int rank);
  113. static void destroy(void);
  114. static int sipcapture_fixup(void** param, int param_no);
  115. static int sip_capture(struct sip_msg *msg, str *dtable, _capture_mode_data_t *cm_data);
  116. static int w_sip_capture(struct sip_msg* _m, char* _table, _capture_mode_data_t * _cm_data, char* s2);
  117. int init_rawsock_children(void);
  118. int extract_host_port(void);
  119. int raw_capture_socket(struct ip_addr* ip, str* iface, int port_start, int port_end, int proto);
  120. int raw_capture_rcv_loop(int rsock, int port1, int port2, int ipip);
  121. static struct mi_root* sip_capture_mi(struct mi_root* cmd, void* param );
  122. static str db_url = str_init(DEFAULT_DB_URL);
  123. static str table_name = str_init("sip_capture");
  124. static str hash_source = str_init("call_id");
  125. static str mt_mode = str_init("rand");
  126. static str date_column = str_init("date");
  127. static str micro_ts_column = str_init("micro_ts");
  128. static str method_column = str_init("method");
  129. static str reply_reason_column = str_init("reply_reason");
  130. static str correlation_column = str_init("correlation_id");
  131. static str ruri_column = str_init("ruri");
  132. static str ruri_user_column = str_init("ruri_user");
  133. static str from_user_column = str_init("from_user");
  134. static str from_tag_column = str_init("from_tag");
  135. static str to_user_column = str_init("to_user");
  136. static str to_tag_column = str_init("to_tag");
  137. static str pid_user_column = str_init("pid_user");
  138. static str contact_user_column = str_init("contact_user");
  139. static str auth_user_column = str_init("auth_user");
  140. static str callid_column = str_init("callid");
  141. static str callid_aleg_column = str_init("callid_aleg");
  142. static str via_1_column = str_init("via_1");
  143. static str via_1_branch_column = str_init("via_1_branch");
  144. static str cseq_column = str_init("cseq");
  145. static str diversion_column = str_init("diversion_user");
  146. static str reason_column = str_init("reason");
  147. static str content_type_column = str_init("content_type");
  148. static str authorization_column = str_init("auth");
  149. static str user_agent_column = str_init("user_agent");
  150. static str source_ip_column = str_init("source_ip");
  151. static str source_port_column = str_init("source_port");
  152. static str dest_ip_column = str_init("destination_ip");
  153. static str dest_port_column = str_init("destination_port");
  154. static str contact_ip_column = str_init("contact_ip");
  155. static str contact_port_column = str_init("contact_port");
  156. static str orig_ip_column = str_init("originator_ip");
  157. static str orig_port_column = str_init("originator_port");
  158. static str rtp_stat_column = str_init("rtp_stat");
  159. static str proto_column = str_init("proto");
  160. static str family_column = str_init("family");
  161. static str type_column = str_init("type");
  162. static str node_column = str_init("node");
  163. static str msg_column = str_init("msg");
  164. static str capture_node = str_init("homer01");
  165. static str star_contact = str_init("*");
  166. static str callid_aleg_header = str_init("X-CID");
  167. int raw_sock_desc = -1; /* raw socket used for ip packets */
  168. unsigned int raw_sock_children = 1;
  169. int capture_on = 0;
  170. int ipip_capture_on = 0;
  171. int moni_capture_on = 0;
  172. int moni_port_start = 0;
  173. int moni_port_end = 0;
  174. int *capture_on_flag = NULL;
  175. int db_insert_mode = 0;
  176. int promisc_on = 0;
  177. int bpf_on = 0;
  178. int hep_capture_on = 0;
  179. int insert_retries = 0;
  180. int insert_retry_timeout = 60;
  181. int hep_offset = 0;
  182. str raw_socket_listen = { 0, 0 };
  183. str raw_interface = { 0, 0 };
  184. char *authkey = NULL, *correlation_id = NULL;
  185. struct ifreq ifr; /* interface structure */
  186. #ifdef __OS_linux
  187. /* Linux socket filter */
  188. /* tcpdump -s 0 udp and portrange 5060-5090 -dd */
  189. static struct sock_filter BPF_code[] = { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 7, 0x000086dd },
  190. { 0x30, 0, 0, 0x00000014 }, { 0x15, 0, 18, 0x00000011 }, { 0x28, 0, 0, 0x00000036 },
  191. { 0x35, 0, 1, 0x000013c4 }, { 0x25, 0, 14, 0x000013e2 }, { 0x28, 0, 0, 0x00000038 },
  192. { 0x35, 11, 13, 0x000013c4 }, { 0x15, 0, 12, 0x00000800 }, { 0x30, 0, 0, 0x00000017 },
  193. { 0x15, 0, 10, 0x00000011 }, { 0x28, 0, 0, 0x00000014 }, { 0x45, 8, 0, 0x00001fff },
  194. { 0xb1, 0, 0, 0x0000000e }, { 0x48, 0, 0, 0x0000000e }, { 0x35, 0, 1, 0x000013c4 },
  195. { 0x25, 0, 3, 0x000013e2 }, { 0x48, 0, 0, 0x00000010 }, { 0x35, 0, 2, 0x000013c4 },
  196. { 0x25, 1, 0, 0x000013e2 }, { 0x6, 0, 0, 0x0000ffff }, { 0x6, 0, 0, 0x00000000 },
  197. };
  198. #endif
  199. //db1_con_t *db_con = NULL; /*!< database connection */
  200. //db_func_t db_funcs; /*!< Database functions */
  201. //str* table_names = NULL;
  202. unsigned int no_tables = 0;
  203. enum e_mt_mode mtmode = mode_random ;
  204. enum hash_source source = hs_error;
  205. //unsigned int rr_idx = 0;
  206. struct hep_timehdr* heptime;
  207. /*! \brief
  208. * Exported functions
  209. */
  210. static cmd_export_t cmds[] = {
  211. {"sip_capture", (cmd_function)w_sip_capture, 0, 0, 0, ANY_ROUTE},
  212. {"sip_capture", (cmd_function)w_sip_capture, 1, sipcapture_fixup, 0, ANY_ROUTE },
  213. {"sip_capture", (cmd_function)w_sip_capture, 2, sipcapture_fixup, 0, ANY_ROUTE },
  214. {0, 0, 0, 0, 0, 0}
  215. };
  216. int capture_mode_param(modparam_t type, void *val);
  217. /*! \brief
  218. * Exported parameters
  219. */
  220. static param_export_t params[] = {
  221. {"db_url", PARAM_STR, &db_url },
  222. {"table_name", PARAM_STR, &table_name },
  223. {"hash_source", PARAM_STR, &hash_source },
  224. {"mt_mode", PARAM_STR, &mt_mode },
  225. {"date_column", PARAM_STR, &date_column },
  226. {"micro_ts_column", PARAM_STR, &micro_ts_column },
  227. {"method_column", PARAM_STR, &method_column },
  228. {"correlation_column", PARAM_STR, &correlation_column.s },
  229. {"reply_reason_column", PARAM_STR, &reply_reason_column },
  230. {"ruri_column", PARAM_STR, &ruri_column },
  231. {"ruri_user_column", PARAM_STR, &ruri_user_column },
  232. {"from_user_column", PARAM_STR, &from_user_column },
  233. {"from_tag_column", PARAM_STR, &from_tag_column },
  234. {"to_user_column", PARAM_STR, &to_user_column },
  235. {"to_tag_column", PARAM_STR, &to_tag_column },
  236. {"pid_user_column", PARAM_STR, &pid_user_column },
  237. {"contact_user_column", PARAM_STR, &contact_user_column },
  238. {"auth_user_column", PARAM_STR, &auth_user_column },
  239. {"callid_column", PARAM_STR, &callid_column},
  240. {"callid_aleg_column", PARAM_STR, &callid_aleg_column},
  241. {"via_1_column", PARAM_STR, &via_1_column },
  242. {"via_1_branch_column", PARAM_STR, &via_1_branch_column },
  243. {"cseq_column", PARAM_STR, &cseq_column },
  244. {"diversion_column", PARAM_STR, &diversion_column },
  245. {"reason_column", PARAM_STR, &reason_column },
  246. {"content_type_column", PARAM_STR, &content_type_column },
  247. {"authorization_column", PARAM_STR, &authorization_column },
  248. {"user_agent_column", PARAM_STR, &user_agent_column },
  249. {"source_ip_column", PARAM_STR, &source_ip_column },
  250. {"source_port_column", PARAM_STR, &source_port_column},
  251. {"destination_ip_column", PARAM_STR, &dest_ip_column },
  252. {"destination_port_column", PARAM_STR, &dest_port_column },
  253. {"contact_ip_column", PARAM_STR, &contact_ip_column },
  254. {"contact_port_column", PARAM_STR, &contact_port_column},
  255. {"originator_ip_column", PARAM_STR, &orig_ip_column },
  256. {"originator_port_column", PARAM_STR, &orig_port_column },
  257. {"proto_column", PARAM_STR, &proto_column },
  258. {"family_column", PARAM_STR, &family_column },
  259. {"rtp_stat_column", PARAM_STR, &rtp_stat_column },
  260. {"type_column", PARAM_STR, &type_column },
  261. {"node_column", PARAM_STR, &node_column },
  262. {"msg_column", PARAM_STR, &msg_column },
  263. {"capture_on", INT_PARAM, &capture_on },
  264. {"capture_node", PARAM_STR, &capture_node },
  265. {"raw_sock_children", INT_PARAM, &raw_sock_children },
  266. {"hep_capture_on", INT_PARAM, &hep_capture_on },
  267. {"raw_socket_listen", PARAM_STR, &raw_socket_listen },
  268. {"raw_ipip_capture_on", INT_PARAM, &ipip_capture_on },
  269. {"raw_moni_capture_on", INT_PARAM, &moni_capture_on },
  270. {"db_insert_mode", INT_PARAM, &db_insert_mode },
  271. {"raw_interface", PARAM_STR, &raw_interface },
  272. {"promiscious_on", INT_PARAM, &promisc_on },
  273. {"raw_moni_bpf_on", INT_PARAM, &bpf_on },
  274. {"callid_aleg_header", PARAM_STR, &callid_aleg_header},
  275. {"capture_mode", PARAM_STRING|USE_FUNC_PARAM, (void *)capture_mode_param},
  276. {"insert_retries", INT_PARAM, &insert_retries },
  277. {"insert_retry_timeout",INT_PARAM, &insert_retry_timeout },
  278. {0, 0, 0}
  279. };
  280. /*! \brief
  281. * MI commands
  282. */
  283. static mi_export_t mi_cmds[] = {
  284. { "sip_capture", sip_capture_mi, 0, 0, 0 },
  285. { 0, 0, 0, 0, 0}
  286. };
  287. #ifdef STATISTICS
  288. /*stat_var* sipcapture_req;
  289. stat_var* sipcapture_rpl;
  290. stat_export_t sipcapture_stats[] = {
  291. {"captured_requests" , 0, &sipcapture_req },
  292. {"captured_replies" , 0, &sipcapture_rpl },
  293. {0,0,0}
  294. };
  295. */
  296. stat_export_t *sipcapture_stats = NULL;
  297. #endif
  298. /*! \brief module exports */
  299. struct module_exports exports = {
  300. "sipcapture",
  301. DEFAULT_DLFLAGS, /*!< dlopen flags */
  302. cmds, /*!< Exported functions */
  303. params, /*!< Exported parameters */
  304. #ifdef STATISTICS
  305. // sipcapture_stats, /*!< exported statistics */
  306. 0,
  307. #else
  308. 0, /*!< exported statistics */
  309. #endif
  310. mi_cmds, /*!< exported MI functions */
  311. 0, /*!< exported pseudo-variables */
  312. 0, /*!< extra processes */
  313. mod_init, /*!< module initialization function */
  314. 0, /*!< response function */
  315. destroy, /*!< destroy function */
  316. child_init /*!< child initialization function */
  317. };
  318. /* returns number of tables if successful
  319. * <0 if failed
  320. */
  321. int parse_table_names (str table_name, str ** table_names){
  322. char *p = NULL;
  323. unsigned int no_tables;
  324. char * table_name_cpy;
  325. unsigned int i;
  326. /*parse and save table names*/
  327. no_tables = 1;
  328. i = 0;
  329. str * names;
  330. table_name_cpy = (char *) pkg_malloc(sizeof(char) * table_name.len + 1 );
  331. if (table_name_cpy == NULL){
  332. LM_ERR("no more pkg memory left\n");
  333. return -1;
  334. }
  335. memcpy (table_name_cpy, table_name.s, table_name.len);
  336. table_name_cpy[table_name.len] = '\0';
  337. p = table_name_cpy;
  338. while (*p)
  339. {
  340. if (*p== '|')
  341. {
  342. no_tables++;
  343. }
  344. p++;
  345. }
  346. names = (str*)pkg_malloc(sizeof(str) * no_tables);
  347. if(names == NULL) {
  348. LM_ERR("no more pkg memory left\n");
  349. return -1;
  350. }
  351. p = strtok (table_name_cpy,"| \t");
  352. while (p != NULL)
  353. {
  354. LM_INFO ("INFO: table name:%s\n",p);
  355. names[i].len = strlen (p);
  356. names[i].s = (char *)pkg_malloc(sizeof(char) *names[i].len);
  357. memcpy(names[i].s, p, names[i].len);
  358. i++;
  359. p = strtok (NULL, "| \t");
  360. }
  361. pkg_free(table_name_cpy);
  362. *table_names = names;
  363. return no_tables;
  364. }
  365. /* checks for some missing fields*/
  366. int check_capture_mode ( _capture_mode_data_t * n) {
  367. if (!n->db_url.s || !n->db_url.len){
  368. LM_ERR("db_url not set\n");
  369. goto error;
  370. }
  371. if (!n->mtmode ){
  372. LM_ERR("mt_mode not set\n");
  373. goto error;
  374. }
  375. else if (!n->no_tables || !n->table_names){
  376. LM_ERR("table names not set\n");
  377. goto error;
  378. }
  379. return 0;
  380. error:
  381. LM_ERR("parsing capture_mode: not all needed parameters are set. Please check again\n");
  382. return -1;
  383. }
  384. int capture_mode_set_params (_capture_mode_data_t * n, str * params){
  385. param_t * params_list = NULL;
  386. param_hooks_t phooks;
  387. param_t *pit=NULL;
  388. db_func_t db_funcs;
  389. str s;
  390. LM_DBG("to tokenize: [%.*s]\n", params->len, params->s);
  391. if ( n == NULL || params == NULL)
  392. return -1;
  393. s = *params;
  394. if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0)
  395. return -1;
  396. for (pit = params_list; pit; pit=pit->next)
  397. {
  398. LM_DBG("parameter is [%.*s]\n",pit->name.len, pit->name.s );
  399. LM_DBG("parameter value is [%.*s]\n", pit->body.len, pit->body.s);
  400. if (pit->name.len == 6 && strncmp (pit->name.s, "db_url", pit->name.len)==0){
  401. n->db_url.len =pit->body.len;
  402. n->db_url.s = (char*)pkg_malloc(sizeof(char) * n->db_url.len);
  403. if (!n->db_url.s){
  404. LM_ERR("no more pkg memory\n");
  405. goto error;
  406. }
  407. memcpy(n->db_url.s, pit->body.s,n->db_url.len );
  408. if (db_bind_mod(&n->db_url, &db_funcs)){
  409. LM_ERR("parsing capture_mode: could not bind db funcs for url:[%.*s]\n", n->db_url.len, n->db_url.s);
  410. goto error;
  411. }
  412. n->db_funcs = db_funcs;
  413. if (!DB_CAPABILITY(n->db_funcs, DB_CAP_INSERT))
  414. {
  415. LM_ERR("parsing capture_mode: database modules does not provide all functions needed"
  416. " by module\n");
  417. goto error;
  418. }
  419. }
  420. else if (pit->name.len == 10 && strncmp (pit->name.s, "table_name", pit->name.len)==0){
  421. if ((int)(n->no_tables = parse_table_names(pit->body, &n->table_names))<0){
  422. LM_ERR("parsing capture_mode: table name parsing failed\n");
  423. goto error;
  424. }
  425. }
  426. else if (pit->name.len == 7 && strncmp (pit->name.s, "mt_mode", pit->name.len)==0){
  427. if (pit->body.len == 4 && strncmp(pit->body.s, "rand",pit->body.len ) ==0)
  428. {
  429. n->mtmode = mode_random;
  430. }
  431. else if (pit->body.len == 11 && strncmp(pit->body.s, "round_robin",pit->body.len ) ==0)
  432. {
  433. n->mtmode = mode_round_robin;
  434. }
  435. else if (pit->body.len == 4 && strncmp(pit->body.s, "hash", pit->body.len) ==0)
  436. {
  437. n->mtmode = mode_hash;
  438. }
  439. else {
  440. LM_ERR("parsing capture_mode: capture mode not recognized: [%.*s]\n", pit->body.len, pit->body.s);
  441. goto error;
  442. }
  443. }
  444. else if (pit->name.len == 11 && strncmp (pit->name.s, "hash_source", pit->name.len)==0){
  445. if ( (n->hash_source = get_hash_source (pit->body.s)) == hs_error)
  446. {
  447. LM_ERR("parsing capture_mode: hash source unrecognized: [%.*s]\n", pit->body.len, pit->body.s);
  448. goto error;
  449. }
  450. }
  451. }
  452. if (n->mtmode == mode_hash && ( n->hash_source == 0 || n->hash_source == hs_error )){
  453. LM_WARN("Hash mode set, but no hash source provided for [%.*s]. Will consider hashing by call id.\n", n->name.len, n->name.s);
  454. n->hash_source = hs_call_id;
  455. }
  456. if ( check_capture_mode(n)){
  457. goto error;
  458. }
  459. return 0;
  460. error:
  461. if (n->db_url.s){
  462. pkg_free(n->db_url.s);
  463. }
  464. return -1;
  465. }
  466. void * capture_mode_init(str *name, str * params) {
  467. _capture_mode_data_t * n = NULL;
  468. unsigned int id;
  469. if (!name || name->len == 0){
  470. LM_ERR("capture_mode name is empty\n");
  471. goto error;
  472. }
  473. if (!params || params->len == 0){
  474. LM_ERR("capture_mode params are empty\n");
  475. goto error;
  476. }
  477. id = core_case_hash(name, 0, 0);
  478. n = (_capture_mode_data_t *) pkg_malloc(sizeof(_capture_mode_data_t));
  479. if (!n){
  480. LM_ERR("no more pkg memory\n");
  481. goto error;
  482. }
  483. memset (n, 0,sizeof(_capture_mode_data_t) );
  484. n->id = id;
  485. n->name.len = name->len;
  486. n->name.s = (char *)pkg_malloc(sizeof(char) * n->name.len);
  487. if (!n->name.s){
  488. LM_ERR("no more pkg memory\n");
  489. goto error;
  490. }
  491. memcpy(n->name.s, name->s, n->name.len);
  492. n->table_names = (str *)pkg_malloc(sizeof(str));
  493. if (!n->table_names){
  494. LM_ERR("no more pkg memory\n");
  495. goto error;
  496. }
  497. if (capture_mode_set_params (n, params)<0){
  498. LM_ERR("capture mode parsing failed\n");
  499. goto error;
  500. }
  501. n->next = capture_modes_root;
  502. capture_modes_root = n;
  503. return n;
  504. error:
  505. if (n->name.s){
  506. pkg_free(n->name.s);
  507. }
  508. if (n->table_names){
  509. pkg_free(n->table_names);
  510. }
  511. if (n){
  512. pkg_free(n);
  513. }
  514. return 0;
  515. }
  516. /*parse name=>param1=>val1;param2=>val2;..*/
  517. int capture_mode_param(modparam_t type, void *val){
  518. str name;
  519. str in;
  520. str tok;
  521. char * p;
  522. in.s = val;
  523. in.len = strlen(in.s);
  524. p = in.s;
  525. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  526. p++;
  527. if(p>in.s+in.len || *p=='\0')
  528. goto error;
  529. name.s = p;
  530. while(p < in.s + in.len)
  531. {
  532. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  533. break;
  534. p++;
  535. }
  536. if(p>in.s+in.len || *p=='\0')
  537. goto error;
  538. name.len = p - name.s;
  539. if(*p!='=')
  540. {
  541. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  542. p++;
  543. if(p>in.s+in.len || *p=='\0' || *p!='=')
  544. goto error;
  545. }
  546. p++;
  547. if(*p!='>')
  548. goto error;
  549. p++;
  550. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  551. p++;
  552. tok.s = p;
  553. tok.len = in.len + (int)(in.s - p);
  554. LM_DBG("capture_mode name: [%.*s] data: [%.*s]\n", name.len, name.s, tok.len, tok.s);
  555. if (!capture_mode_init(&name, &tok)){
  556. return -1;
  557. }
  558. return 0;
  559. error:
  560. LM_ERR("invalid parameter [%.*s] at [%d]\n", in.len, in.s,
  561. (int)(p-in.s));
  562. return -1;
  563. }
  564. /*! \brief Initialize sipcapture module */
  565. static int mod_init(void) {
  566. struct ip_addr *ip = NULL;
  567. char * def_params = NULL;
  568. #ifdef STATISTICS
  569. int cnt = 0;
  570. int i = 0;
  571. char * stat_name = NULL;
  572. _capture_mode_data_t * c = NULL;
  573. int def;
  574. #endif
  575. if(register_mi_mod(exports.name, mi_cmds)!=0)
  576. {
  577. LM_ERR("failed to register MI commands\n");
  578. return -1;
  579. }
  580. if(sipcapture_init_rpc()!=0)
  581. {
  582. LM_ERR("failed to register RPC commands\n");
  583. return -1;
  584. }
  585. /*Check the table name - if table_name is empty and no capture modes are defined, then error*/
  586. if(!table_name.len && capture_modes_root == NULL) {
  587. LM_ERR("ERROR: sipcapture: mod_init: table_name is not defined or empty\n");
  588. return -1;
  589. }
  590. /*create a default capture mode using the default parameters*/
  591. def_params = (char *) pkg_malloc(snprintf(NULL, 0, "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",db_url.s, table_name.s, mt_mode.s,hash_source.s) + 1);
  592. sprintf(def_params, "db_url=%s;table_name=%s;mt_mode=%s;hash_source=%s",db_url.s, table_name.s, mt_mode.s,hash_source.s);
  593. str def_name, def_par;
  594. def_name.s= strdup("default");
  595. def_name.len = 7;
  596. def_par.s = def_params;
  597. def_par.len = strlen (def_params);
  598. LM_DBG("def_params is: %s\n", def_params);
  599. if ((capture_def =capture_mode_init(&def_name, &def_par)) == NULL){
  600. LM_WARN("Default capture mode configuration failed. Suppose sip_capture calls will use other defined capture modes.\n");
  601. }
  602. pkg_free(def_params);
  603. #ifdef STATISTICS
  604. c = capture_modes_root;
  605. while (c){
  606. cnt++;
  607. c=c->next;
  608. }
  609. /*requests and replies for each mode + 1 zero-filled stat_export */
  610. stat_export_t *stats = (stat_export_t *) shm_malloc(sizeof(stat_export_t) * cnt * 2 + 1 );
  611. c = capture_modes_root;
  612. while (c){
  613. /*for the default capture_mode, don't add it's name to the stat name*/
  614. def = (capture_def && c == capture_def)?1:0;
  615. stat_name = (char *)shm_malloc(sizeof (char) * (snprintf(NULL, 0 , (def)?"captured_requests%.*s":"captured_requests[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s) + 1));
  616. sprintf(stat_name, (def)?"captured_requests%.*s":"captured_requests[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s);
  617. stats[i].name = stat_name;
  618. stats[i].flags = 0;
  619. stats[i].stat_pointer = &c->sipcapture_req;
  620. i++;
  621. stat_name = (char *)shm_malloc(sizeof (char) * (snprintf(NULL, 0 , (def)?"captured_replies%.*s":"captured_replies[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s) + 1));
  622. sprintf(stat_name, (def)?"captured_replies%.*s":"captured_replies[%.*s]", (def)?0:c->name.len, (def)?"":c->name.s);
  623. stats[i].name = stat_name;
  624. stats[i].flags = 0;
  625. stats[i].stat_pointer = &c->sipcapture_rpl;
  626. i++;
  627. c=c->next;
  628. }
  629. stats[i].name = 0;
  630. stats[i].flags = 0;
  631. stats[i].stat_pointer = 0;
  632. sipcapture_stats = stats;
  633. /* register statistics */
  634. if (register_module_stats(exports.name, sipcapture_stats)!=0)
  635. {
  636. LM_ERR("failed to register core statistics\n");
  637. return -1;
  638. }
  639. #endif
  640. srand(time(NULL));
  641. if(db_insert_mode) {
  642. LM_INFO("INFO: sipcapture: mod_init: you have enabled INSERT DELAYED \
  643. Make sure your DB can support it\n");
  644. }
  645. capture_on_flag = (int*)shm_malloc(sizeof(int));
  646. if(capture_on_flag==NULL) {
  647. LM_ERR("no more shm memory left\n");
  648. return -1;
  649. }
  650. *capture_on_flag = capture_on;
  651. /* register DGRAM event */
  652. if(sr_event_register_cb(SREV_NET_DGRAM_IN, hep_msg_received) < 0) {
  653. LM_ERR("failed to register SREV_NET_DGRAM_IN event\n");
  654. return -1;
  655. }
  656. if(ipip_capture_on && moni_capture_on) {
  657. LM_ERR("only one RAW mode is supported. Please disable ipip_capture_on or moni_capture_on\n");
  658. return -1;
  659. }
  660. if ((insert_retries <0) || ( insert_retries > 500)) {
  661. LM_ERR("insert_retries should be a value between 0 and 500");
  662. return -1;
  663. }
  664. if (( 0 == insert_retries) && (insert_retry_timeout != 0)){
  665. LM_ERR("insert_retry_timeout has no meaning when insert_retries is not set");
  666. }
  667. if ((insert_retry_timeout <0) || ( insert_retry_timeout > 300)) {
  668. LM_ERR("insert_retry_timeout should be a value between 0 and 300");
  669. return -1;
  670. }
  671. /* raw processes for IPIP encapsulation */
  672. if (ipip_capture_on || moni_capture_on) {
  673. register_procs(raw_sock_children);
  674. if(extract_host_port() && (((ip=str2ip(&raw_socket_listen)) == NULL)
  675. && ((ip=str2ip6(&raw_socket_listen)) == NULL)
  676. ))
  677. {
  678. LM_ERR("sipcapture mod_init: bad RAW IP: %.*s\n", raw_socket_listen.len, raw_socket_listen.s);
  679. return -1;
  680. }
  681. if(moni_capture_on && !moni_port_start) {
  682. LM_ERR("ERROR:sipcapture:mod_init: Please define port/portrange in 'raw_socket_listen', before \
  683. activate monitoring capture\n");
  684. return -1;
  685. }
  686. raw_sock_desc = raw_capture_socket(raw_socket_listen.len ? ip : 0, raw_interface.len ? &raw_interface : 0,
  687. moni_port_start, moni_port_end , ipip_capture_on ? IPPROTO_IPIP : htons(0x0800));
  688. if(raw_sock_desc < 0) {
  689. LM_ERR("could not initialize raw udp socket:"
  690. " %s (%d)\n", strerror(errno), errno);
  691. if (errno == EPERM)
  692. LM_ERR("could not initialize raw socket on startup"
  693. " due to inadequate permissions, please"
  694. " restart as root or with CAP_NET_RAW\n");
  695. return -1;
  696. }
  697. if(promisc_on && raw_interface.len) {
  698. memset(&ifr, 0, sizeof(ifr));
  699. memcpy(ifr.ifr_name, raw_interface.s, raw_interface.len);
  700. #ifdef __OS_linux
  701. if(ioctl(raw_sock_desc, SIOCGIFFLAGS, &ifr) < 0) {
  702. LM_ERR("could not get flags from interface [%.*s]:"
  703. " %s (%d)\n", raw_interface.len, raw_interface.s, strerror(errno), errno);
  704. goto error;
  705. }
  706. ifr.ifr_flags |= IFF_PROMISC;
  707. if (ioctl(raw_sock_desc, SIOCSIFFLAGS, &ifr) < 0) {
  708. LM_ERR("could not set PROMISC flag to interface [%.*s]:"
  709. " %s (%d)\n", raw_interface.len, raw_interface.s, strerror(errno), errno);
  710. goto error;
  711. }
  712. #endif
  713. }
  714. }
  715. return 0;
  716. #ifdef __OS_linux
  717. error:
  718. if(raw_sock_desc) close(raw_sock_desc);
  719. return -1;
  720. #endif
  721. }
  722. static int sipcapture_fixup(void** param, int param_no)
  723. {
  724. _capture_mode_data_t *con;
  725. str val;
  726. unsigned int id;
  727. if (param_no == 1 ) {
  728. return fixup_var_pve_str_12(param, 1);
  729. }
  730. if (param_no == 2 ){
  731. val.s = (char *)*param;
  732. val.len = strlen((char *)*param);
  733. con = capture_modes_root;
  734. id = core_case_hash (&val, 0 , 0);
  735. while (con){
  736. if (id == con->id && con->name.len == val.len
  737. && strncmp(con->name.s, val.s, val.len) == 0){
  738. *param = (void *)con;
  739. LM_DBG("found capture mode :[%.*s]\n",con->name.len, con->name.s);
  740. return 0;
  741. }
  742. con = con->next;
  743. }
  744. LM_ERR("no capture mode found\n");
  745. return -1;
  746. }
  747. return 0;
  748. }
  749. static int w_sip_capture(struct sip_msg* _m, char* _table, _capture_mode_data_t * cm_data, char* s2)
  750. {
  751. str table = {0};
  752. if(_table!=NULL && (get_str_fparam(&table, _m, (fparam_t*)_table) < 0))
  753. {
  754. LM_ERR("invalid table parameter [%s] [%s]\n", _table, table.s);
  755. return -1;
  756. }
  757. return sip_capture(_m, (table.len>0)?&table:NULL, cm_data );
  758. }
  759. int extract_host_port(void)
  760. {
  761. if(raw_socket_listen.len) {
  762. char *p1,*p2;
  763. p1 = raw_socket_listen.s;
  764. if( (p1 = strrchr(p1, ':')) != 0 ) {
  765. *p1 = '\0';
  766. p1++;
  767. p2=p1;
  768. if((p2 = strrchr(p2, '-')) != 0 ) {
  769. p2++;
  770. moni_port_end = atoi(p2);
  771. p1[strlen(p1)-strlen(p2)-1]='\0';
  772. }
  773. moni_port_start = atoi(p1);
  774. raw_socket_listen.len = strlen(raw_socket_listen.s);
  775. }
  776. return 1;
  777. }
  778. return 0;
  779. }
  780. static int child_init(int rank)
  781. {
  782. _capture_mode_data_t * c;
  783. if (rank == PROC_MAIN && (ipip_capture_on || moni_capture_on)) {
  784. if (init_rawsock_children() < 0) return -1;
  785. }
  786. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  787. return 0; /* do nothing for the main process */
  788. c = capture_modes_root;
  789. while (c){
  790. if (!c->db_url.s || !c->db_url.len ){
  791. LM_ERR("DB URL not set for capture mode:[%.*s]", c->name.len, c->name.s);
  792. return -1;
  793. }
  794. c->db_con = c->db_funcs.init(&c->db_url);
  795. if (!c->db_con)
  796. {
  797. LM_ERR("unable to connect to database [%.*s] from capture_mode param.\n", c->db_url.len, c->db_url.s);
  798. return -1;
  799. }
  800. if (c->mtmode ==mode_round_robin && rank > 0)
  801. {
  802. c->rr_idx = rank % c->no_tables;
  803. }
  804. c = c->next;
  805. }
  806. heptime = (struct hep_timehdr*)pkg_malloc(sizeof(struct hep_timehdr));
  807. if(heptime==NULL) {
  808. LM_ERR("no more pkg memory left\n");
  809. return -1;
  810. }
  811. heptime->tv_sec = 0;
  812. return 0;
  813. }
  814. /*
  815. * RAW IPIP || Monitoring listeners
  816. */
  817. int init_rawsock_children(void)
  818. {
  819. int i;
  820. pid_t pid;
  821. for(i = 0; i < raw_sock_children; i++) {
  822. pid = fork_process(PROC_UNIXSOCK,"homer raw socket", 1);
  823. if (pid < 0) {
  824. ERR("Unable to fork: %s\n", strerror(errno));
  825. return -1;
  826. } else if (pid == 0) { /* child */
  827. raw_capture_rcv_loop(raw_sock_desc, moni_port_start, moni_port_end, moni_capture_on ? 0 : 1);
  828. }
  829. /* Parent */
  830. }
  831. DBG("Raw IPIP socket server successfully initialized\n");
  832. return 1;
  833. }
  834. static void destroy(void)
  835. {
  836. //if (capture_def->db_con!=NULL)
  837. // capture_def->db_funcs.close(capture_def->db_con);
  838. /*free content from the linked list*/
  839. _capture_mode_data_t * c;
  840. _capture_mode_data_t * c0;
  841. c = capture_modes_root;
  842. while (c){
  843. c0 = c;
  844. if (c->name.s){
  845. pkg_free(c->name.s);
  846. }
  847. if (c->db_url.s){
  848. pkg_free(c->db_url.s);
  849. }
  850. if (c->db_con){
  851. c->db_funcs.close(c->db_con);
  852. }
  853. if (c->table_names){
  854. pkg_free(c->table_names);
  855. }
  856. pkg_free(c);
  857. c = c0->next;
  858. }
  859. if (capture_on_flag)
  860. shm_free(capture_on_flag);
  861. if(heptime) pkg_free(heptime);
  862. if(raw_sock_desc > 0) {
  863. if(promisc_on && raw_interface.len) {
  864. #ifdef __OS_linux
  865. ifr.ifr_flags &= ~(IFF_PROMISC);
  866. if (ioctl(raw_sock_desc, SIOCSIFFLAGS, &ifr) < 0) {
  867. LM_ERR("destroy: could not remove PROMISC flag from interface [%.*s]:"
  868. " %s (%d)\n", raw_interface.len, raw_interface.s, strerror(errno), errno);
  869. }
  870. #endif
  871. }
  872. close(raw_sock_desc);
  873. }
  874. // if (table_names){
  875. // pkg_free(table_names);
  876. // }
  877. }
  878. static int sip_capture_prepare(sip_msg_t *msg)
  879. {
  880. /* We need parse all headers */
  881. if (parse_headers(msg, HDR_CALLID_F|HDR_EOH_F, 0) != 0) {
  882. LM_ERR("cannot parse headers\n");
  883. return 0;
  884. }
  885. return 0;
  886. }
  887. static int sip_capture_store(struct _sipcapture_object *sco, str *dtable, _capture_mode_data_t * cm_data)
  888. {
  889. db_key_t db_keys[NR_KEYS];
  890. db_val_t db_vals[NR_KEYS];
  891. str tmp, corrtmp;
  892. int ii = 0;
  893. int ret = 0;
  894. int counter = 0;
  895. db_insert_f insert;
  896. time_t retry_failed_time = 0;
  897. str *table = NULL;
  898. _capture_mode_data_t *c = NULL;
  899. c = (cm_data)? cm_data:capture_def;
  900. if (!c){
  901. LM_ERR("no connection mode available to store data\n");
  902. return -1;
  903. }
  904. if(sco==NULL)
  905. {
  906. LM_DBG("invalid parameter\n");
  907. return -1;
  908. }
  909. if(correlation_id) {
  910. corrtmp.s = correlation_id;
  911. corrtmp.len = strlen(correlation_id);
  912. }
  913. db_keys[0] = &date_column;
  914. db_vals[0].type = DB1_DATETIME;
  915. db_vals[0].nul = 0;
  916. db_vals[0].val.time_val = time(NULL);
  917. db_keys[1] = &micro_ts_column;
  918. db_vals[1].type = DB1_BIGINT;
  919. db_vals[1].nul = 0;
  920. db_vals[1].val.ll_val = sco->tmstamp;
  921. db_keys[2] = &method_column;
  922. db_vals[2].type = DB1_STR;
  923. db_vals[2].nul = 0;
  924. db_vals[2].val.str_val = sco->method;
  925. db_keys[3] = &reply_reason_column;
  926. db_vals[3].type = DB1_STR;
  927. db_vals[3].nul = 0;
  928. db_vals[3].val.str_val = sco->reply_reason;
  929. db_keys[4] = &ruri_column;
  930. db_vals[4].type = DB1_STR;
  931. db_vals[4].nul = 0;
  932. db_vals[4].val.str_val = sco->ruri;
  933. db_keys[5] = &ruri_user_column;
  934. db_vals[5].type = DB1_STR;
  935. db_vals[5].nul = 0;
  936. db_vals[5].val.str_val = sco->ruri_user;
  937. db_keys[6] = &from_user_column;
  938. db_vals[6].type = DB1_STR;
  939. db_vals[6].nul = 0;
  940. db_vals[6].val.str_val = sco->from_user;
  941. db_keys[7] = &from_tag_column;
  942. db_vals[7].type = DB1_STR;
  943. db_vals[7].nul = 0;
  944. db_vals[7].val.str_val = sco->from_tag;
  945. db_keys[8] = &to_user_column;
  946. db_vals[8].type = DB1_STR;
  947. db_vals[8].nul = 0;
  948. db_vals[8].val.str_val = sco->to_user;
  949. db_keys[9] = &to_tag_column;
  950. db_vals[9].type = DB1_STR;
  951. db_vals[9].nul = 0;
  952. db_vals[9].val.str_val = sco->to_tag;
  953. db_keys[10] = &pid_user_column;
  954. db_vals[10].type = DB1_STR;
  955. db_vals[10].nul = 0;
  956. db_vals[10].val.str_val = sco->pid_user;
  957. db_keys[11] = &contact_user_column;
  958. db_vals[11].type = DB1_STR;
  959. db_vals[11].nul = 0;
  960. db_vals[11].val.str_val = sco->contact_user;
  961. db_keys[12] = &auth_user_column;
  962. db_vals[12].type = DB1_STR;
  963. db_vals[12].nul = 0;
  964. db_vals[12].val.str_val = sco->auth_user;
  965. db_keys[13] = &callid_column;
  966. db_vals[13].type = DB1_STR;
  967. db_vals[13].nul = 0;
  968. db_vals[13].val.str_val = sco->callid;
  969. db_keys[14] = &callid_aleg_column;
  970. db_vals[14].type = DB1_STR;
  971. db_vals[14].nul = 0;
  972. db_vals[14].val.str_val = sco->callid_aleg;
  973. db_keys[15] = &via_1_column;
  974. db_vals[15].type = DB1_STR;
  975. db_vals[15].nul = 0;
  976. db_vals[15].val.str_val = sco->via_1;
  977. db_keys[16] = &via_1_branch_column;
  978. db_vals[16].type = DB1_STR;
  979. db_vals[16].nul = 0;
  980. db_vals[16].val.str_val = sco->via_1_branch;
  981. db_keys[17] = &cseq_column;
  982. db_vals[17].type = DB1_STR;
  983. db_vals[17].nul = 0;
  984. db_vals[17].val.str_val = sco->cseq;
  985. db_keys[18] = &reason_column;
  986. db_vals[18].type = DB1_STR;
  987. db_vals[18].nul = 0;
  988. db_vals[18].val.str_val = sco->reason;
  989. db_keys[19] = &content_type_column;
  990. db_vals[19].type = DB1_STR;
  991. db_vals[19].nul = 0;
  992. db_vals[19].val.str_val = sco->content_type;
  993. db_keys[20] = &authorization_column;
  994. db_vals[20].type = DB1_STR;
  995. db_vals[20].nul = 0;
  996. db_vals[20].val.str_val = sco->authorization;
  997. db_keys[21] = &user_agent_column;
  998. db_vals[21].type = DB1_STR;
  999. db_vals[21].nul = 0;
  1000. db_vals[21].val.str_val = sco->user_agent;
  1001. db_keys[22] = &source_ip_column;
  1002. db_vals[22].type = DB1_STR;
  1003. db_vals[22].nul = 0;
  1004. db_vals[22].val.str_val = sco->source_ip;
  1005. db_keys[23] = &source_port_column;
  1006. db_vals[23].type = DB1_INT;
  1007. db_vals[23].nul = 0;
  1008. db_vals[23].val.int_val = sco->source_port;
  1009. db_keys[24] = &dest_ip_column;
  1010. db_vals[24].type = DB1_STR;
  1011. db_vals[24].nul = 0;
  1012. db_vals[24].val.str_val = sco->destination_ip;
  1013. db_keys[25] = &dest_port_column;
  1014. db_vals[25].type = DB1_INT;
  1015. db_vals[25].nul = 0;
  1016. db_vals[25].val.int_val = sco->destination_port;
  1017. db_keys[26] = &contact_ip_column;
  1018. db_vals[26].type = DB1_STR;
  1019. db_vals[26].nul = 0;
  1020. db_vals[26].val.str_val = sco->contact_ip;
  1021. db_keys[27] = &contact_port_column;
  1022. db_vals[27].type = DB1_INT;
  1023. db_vals[27].nul = 0;
  1024. db_vals[27].val.int_val = sco->contact_port;
  1025. db_keys[28] = &orig_ip_column;
  1026. db_vals[28].type = DB1_STR;
  1027. db_vals[28].nul = 0;
  1028. db_vals[28].val.str_val = sco->originator_ip;
  1029. db_keys[29] = &orig_port_column;
  1030. db_vals[29].type = DB1_INT;
  1031. db_vals[29].nul = 0;
  1032. db_vals[29].val.int_val = sco->originator_port;
  1033. db_keys[30] = &proto_column;
  1034. db_vals[30].type = DB1_INT;
  1035. db_vals[30].nul = 0;
  1036. db_vals[30].val.int_val = sco->proto;
  1037. db_keys[31] = &family_column;
  1038. db_vals[31].type = DB1_INT;
  1039. db_vals[31].nul = 0;
  1040. db_vals[31].val.int_val = sco->family;
  1041. db_keys[32] = &rtp_stat_column;
  1042. db_vals[32].type = DB1_STR;
  1043. db_vals[32].nul = 0;
  1044. db_vals[32].val.str_val = sco->rtp_stat;
  1045. db_keys[33] = &type_column;
  1046. db_vals[33].type = DB1_INT;
  1047. db_vals[33].nul = 0;
  1048. db_vals[33].val.int_val = sco->type;
  1049. db_keys[34] = &node_column;
  1050. db_vals[34].type = DB1_STR;
  1051. db_vals[34].nul = 0;
  1052. db_vals[34].val.str_val = sco->node;
  1053. db_keys[35] = &correlation_column;
  1054. db_vals[35].type = DB1_STR;
  1055. db_vals[35].nul = 0;
  1056. db_vals[35].val.str_val = (correlation_id) ? corrtmp : sco->callid;
  1057. db_keys[36] = &msg_column;
  1058. db_vals[36].type = DB1_BLOB;
  1059. db_vals[36].nul = 0;
  1060. /*we don't have empty spaces now */
  1061. tmp.s = sco->msg.s;
  1062. tmp.len = sco->msg.len;
  1063. db_vals[36].val.blob_val = tmp;
  1064. if (dtable){
  1065. table = dtable;
  1066. }
  1067. else if (c->no_tables > 0 ){
  1068. if ( c->mtmode == mode_hash ){
  1069. ii = hash_func ( sco, c->hash_source , c->no_tables);
  1070. if (ii < 0){
  1071. LM_ERR("hashing failed\n");
  1072. return -1;
  1073. }
  1074. LM_DBG ("hash idx is:%d\n", ii);
  1075. }
  1076. else if (c->mtmode == mode_random )
  1077. {
  1078. ii = rand() % c->no_tables;
  1079. LM_DBG("rand idx is:%d\n", ii);
  1080. }
  1081. else if (c->mtmode == mode_round_robin)
  1082. {
  1083. ii = c->rr_idx;
  1084. c->rr_idx = (c->rr_idx +1) % c->no_tables;
  1085. LM_DBG("round robin idx is:%d\n", ii);
  1086. }
  1087. table = &c->table_names[ii];
  1088. }
  1089. /* check dynamic table */
  1090. LM_DBG("insert into homer table: [%.*s]\n", table->len, table->s);
  1091. c->db_funcs.use_table(c->db_con, table);
  1092. LM_DBG("storing info...\n");
  1093. if (db_insert_mode == 1 && c->db_funcs.insert_delayed != NULL)
  1094. insert = c->db_funcs.insert_delayed;
  1095. else
  1096. insert = c->db_funcs.insert;
  1097. ret = insert(c->db_con, db_keys, db_vals, NR_KEYS);
  1098. if (ret < 0) {
  1099. LM_DBG("failed to insert into database(first attempt)\n");
  1100. if (insert_retries != 0) {
  1101. counter = 0;
  1102. while ((ret = insert(c->db_con, db_keys, db_vals, NR_KEYS)) < 0) {
  1103. counter++;
  1104. if (1 == counter) //first failed retry
  1105. retry_failed_time = time(NULL);
  1106. if ((counter > insert_retries) || (time(NULL)
  1107. - retry_failed_time > insert_retry_timeout)) {
  1108. LM_ERR("failed to insert into database(second attempt)\n");
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. if (ret < 0)
  1115. goto error;
  1116. #ifdef STATISTICS
  1117. update_stat(sco->stat, 1);
  1118. #endif
  1119. return 1;
  1120. error: return -1;
  1121. }
  1122. static int sip_capture(struct sip_msg *msg, str *_table, _capture_mode_data_t * cm_data)
  1123. {
  1124. struct _sipcapture_object sco;
  1125. struct sip_uri from, to, contact;
  1126. struct hdr_field *hook1 = NULL;
  1127. hdr_field_t *tmphdr[4];
  1128. contact_body_t* cb=0;
  1129. char buf_ip[IP_ADDR_MAX_STR_SIZE+12];
  1130. char *port_str = NULL, *tmp = NULL;
  1131. struct timeval tvb;
  1132. struct timezone tz;
  1133. char tmp_node[100];
  1134. char rtpinfo[256];
  1135. unsigned int len = 0;
  1136. LM_DBG("CAPTURE DEBUG...\n");
  1137. gettimeofday( &tvb, &tz );
  1138. if(msg==NULL) {
  1139. LM_DBG("nothing to capture\n");
  1140. return -1;
  1141. }
  1142. memset(&sco, 0, sizeof(struct _sipcapture_object));
  1143. if(capture_on_flag==NULL || *capture_on_flag==0) {
  1144. LM_DBG("capture off...\n");
  1145. return -1;
  1146. }
  1147. if(sip_capture_prepare(msg)<0) return -1;
  1148. if(msg->first_line.type == SIP_REQUEST) {
  1149. if (parse_sip_msg_uri(msg)<0) return -1;
  1150. sco.method = msg->first_line.u.request.method;
  1151. EMPTY_STR(sco.reply_reason);
  1152. sco.ruri = msg->first_line.u.request.uri;
  1153. sco.ruri_user = msg->parsed_uri.user;
  1154. }
  1155. else if(msg->first_line.type == SIP_REPLY) {
  1156. sco.method = msg->first_line.u.reply.status;
  1157. sco.reply_reason = msg->first_line.u.reply.reason;
  1158. EMPTY_STR(sco.ruri);
  1159. EMPTY_STR(sco.ruri_user);
  1160. }
  1161. else {
  1162. LM_ERR("unknown type [%i]\n", msg->first_line.type);
  1163. EMPTY_STR(sco.method);
  1164. EMPTY_STR(sco.reply_reason);
  1165. EMPTY_STR(sco.ruri);
  1166. EMPTY_STR(sco.ruri_user);
  1167. }
  1168. if(heptime && heptime->tv_sec != 0) {
  1169. sco.tmstamp = (unsigned long long)heptime->tv_sec*1000000+heptime->tv_usec; /* micro ts */
  1170. snprintf(tmp_node, 100, "%.*s:%i", capture_node.len, capture_node.s, heptime->captid);
  1171. sco.node.s = tmp_node;
  1172. sco.node.len = strlen(tmp_node);
  1173. }
  1174. else {
  1175. sco.tmstamp = (unsigned long long)tvb.tv_sec*1000000+tvb.tv_usec; /* micro ts */
  1176. sco.node = capture_node;
  1177. }
  1178. /* Parse FROM */
  1179. if(msg->from) {
  1180. if (parse_from_header(msg)!=0){
  1181. LOG(L_ERR, "ERROR: eval_elem: bad or missing" " From: header\n");
  1182. return -1;
  1183. }
  1184. if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len, &from)<0){
  1185. LOG(L_ERR, "ERROR: do_action: bad from dropping"" packet\n");
  1186. return -1;
  1187. }
  1188. sco.from_user = from.user;
  1189. sco.from_tag = get_from(msg)->tag_value;
  1190. }
  1191. else {
  1192. EMPTY_STR(sco.from_user);
  1193. EMPTY_STR(sco.from_tag);
  1194. }
  1195. /* Parse TO */
  1196. if(msg->to) {
  1197. if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len, &to)<0){
  1198. LOG(L_ERR, "ERROR: do_action: bad to dropping"" packet\n");
  1199. return -1;
  1200. }
  1201. sco.to_user = to.user;
  1202. if(get_to(msg)->tag_value.len)
  1203. sco.to_tag = get_to(msg)->tag_value;
  1204. else { EMPTY_STR(sco.to_tag); }
  1205. }
  1206. else {
  1207. EMPTY_STR(sco.to_user);
  1208. EMPTY_STR(sco.to_tag);
  1209. }
  1210. /* Call-id */
  1211. if(msg->callid) sco.callid = msg->callid->body;
  1212. else { EMPTY_STR(sco.callid); }
  1213. /* P-Asserted-Id */
  1214. if((parse_pai_header(msg) == 0) && (msg->pai) && (msg->pai->parsed)) {
  1215. to_body_t *pai = get_pai(msg)->id; /* This returns the first entry */
  1216. if ((pai->parsed_uri.user.s == NULL) &&
  1217. (parse_uri(pai->uri.s, pai->uri.len, &pai->parsed_uri) < 0)){
  1218. LM_DBG("DEBUG: do_action: bad pai: method:[%.*s] CID: [%.*s]\n", sco.method.len, sco.method.s, sco.callid.len, sco.callid.s);
  1219. }
  1220. else {
  1221. LM_DBG("PARSE PAI: (%.*s)\n", pai->uri.len, pai->uri.s);
  1222. sco.pid_user = pai->parsed_uri.user;
  1223. }
  1224. }
  1225. else if((parse_ppi_header(msg) == 0) && (msg->ppi) && (msg->ppi->parsed)) {
  1226. to_body_t *ppi = get_ppi(msg)->id; /* This returns the first entry */
  1227. if ((ppi->parsed_uri.user.s == NULL) &&
  1228. (parse_uri(ppi->uri.s, ppi->uri.len, &ppi->parsed_uri) < 0)){
  1229. LM_DBG("DEBUG: do_action: bad ppi: method:[%.*s] CID: [%.*s]\n", sco.method.len, sco.method.s, sco.callid.len, sco.callid.s);
  1230. }
  1231. else {
  1232. LM_DBG("PARSE PPI: (%.*s)\n", ppi->uri.len, ppi->uri.s);
  1233. sco.pid_user = ppi->parsed_uri.user;
  1234. }
  1235. }
  1236. else { EMPTY_STR(sco.pid_user); }
  1237. /* Auth headers */
  1238. if(msg->proxy_auth != NULL) hook1 = msg->proxy_auth;
  1239. else if(msg->authorization != NULL) hook1 = msg->authorization;
  1240. if(hook1) {
  1241. if(parse_credentials(hook1) == 0) sco.auth_user = ((auth_body_t*)(hook1->parsed))->digest.username.user;
  1242. else { EMPTY_STR(sco.auth_user); }
  1243. }
  1244. else { EMPTY_STR(sco.auth_user);}
  1245. if(msg->contact) {
  1246. if (msg->contact->parsed == 0 && parse_contact(msg->contact) == -1) {
  1247. LOG(L_ERR,"assemble_msg: error while parsing <Contact:> header\n");
  1248. return -1;
  1249. }
  1250. cb = (contact_body_t*)msg->contact->parsed;
  1251. if(cb) {
  1252. if (cb->contacts) {
  1253. if(parse_uri( cb->contacts->uri.s, cb->contacts->uri.len, &contact)<0){
  1254. LOG(L_ERR, "ERROR: do_action: bad contact dropping"" packet\n");
  1255. return -1;
  1256. }
  1257. } else {
  1258. if(cb->star){ /* in the case Contact is "*" */
  1259. memset(&contact, 0, sizeof(contact));
  1260. contact.user.s = star_contact.s;
  1261. contact.user.len = star_contact.len;
  1262. } else {
  1263. LOG(L_NOTICE,"Invalid contact\n");
  1264. memset(&contact, 0, sizeof(contact));
  1265. }
  1266. }
  1267. }
  1268. }
  1269. /* callid_aleg - default is X-CID but configurable via modul params */
  1270. if((tmphdr[0] = get_hdr_by_name(msg, callid_aleg_header.s, callid_aleg_header.len)) != NULL) {
  1271. sco.callid_aleg = tmphdr[0]->body;
  1272. }
  1273. else { EMPTY_STR(sco.callid_aleg);}
  1274. /* VIA 1 */
  1275. sco.via_1 = msg->h_via1->body;
  1276. /* Via branch */
  1277. if(msg->via1->branch) sco.via_1_branch = msg->via1->branch->value;
  1278. else { EMPTY_STR(sco.via_1_branch); }
  1279. /* CSEQ */
  1280. if(msg->cseq) sco.cseq = msg->cseq->body;
  1281. else { EMPTY_STR(sco.cseq); }
  1282. /* Reason */
  1283. if((tmphdr[1] = get_hdr_by_name(msg,"Reason", 6)) != NULL) {
  1284. sco.reason = tmphdr[1]->body;
  1285. }
  1286. else { EMPTY_STR(sco.reason); }
  1287. /* Diversion */
  1288. if(msg->diversion) sco.diversion = msg->diversion->body;
  1289. else { EMPTY_STR(sco.diversion);}
  1290. /* Content-type */
  1291. if(msg->content_type) sco.content_type = msg->content_type->body;
  1292. else { EMPTY_STR(sco.content_type);}
  1293. /* User-Agent */
  1294. if(msg->user_agent) sco.user_agent = msg->user_agent->body;
  1295. else { EMPTY_STR(sco.user_agent);}
  1296. /* Contact */
  1297. if(msg->contact && cb) {
  1298. sco.contact_ip = contact.host;
  1299. str2int(&contact.port, (unsigned int*)&sco.contact_port);
  1300. sco.contact_user = contact.user;
  1301. }
  1302. else {
  1303. EMPTY_STR(sco.contact_ip);
  1304. sco.contact_port = 0;
  1305. EMPTY_STR(sco.contact_user);
  1306. }
  1307. /* X-OIP */
  1308. if((tmphdr[2] = get_hdr_by_name(msg,"X-OIP", 5)) != NULL) {
  1309. sco.originator_ip = tmphdr[2]->body;
  1310. /* Originator port. Should be parsed from XOIP header as ":" param */
  1311. tmp = strchr(tmphdr[2]->body.s, ':');
  1312. if (tmp) {
  1313. *tmp = '\0';
  1314. port_str = tmp + 1;
  1315. sco.originator_port = strtol(port_str, NULL, 10);
  1316. }
  1317. else sco.originator_port = 0;
  1318. }
  1319. else {
  1320. EMPTY_STR(sco.originator_ip);
  1321. sco.originator_port = 0;
  1322. }
  1323. /* X-RTP-Stat */
  1324. if((tmphdr[3] = get_hdr_by_name(msg,"X-RTP-Stat", 10)) != NULL) {
  1325. sco.rtp_stat = tmphdr[3]->body;
  1326. }
  1327. /* P-RTP-Stat */
  1328. else if((tmphdr[3] = get_hdr_by_name(msg,"P-RTP-Stat", 10)) != NULL) {
  1329. sco.rtp_stat = tmphdr[3]->body;
  1330. }
  1331. /* X-Siemens-RTP-stats */
  1332. else if((tmphdr[3] = get_hdr_by_name(msg,"X-Siemens-RTP-stats", 19)) != NULL) {
  1333. sco.rtp_stat = tmphdr[3]->body;
  1334. }
  1335. /* X-NG-RTP-STATS */
  1336. else if((tmphdr[3] = get_hdr_by_name(msg,"X-NG-RTP-STATS", 14)) != NULL) {
  1337. sco.rtp_stat = tmphdr[3]->body;
  1338. }
  1339. /* RTP-RxStat */
  1340. else if((tmphdr[3] = get_hdr_by_name(msg,"RTP-RxStat", 10)) != NULL) {
  1341. if(tmphdr[3]->body.len > 250) tmphdr[3]->body.len = 250;
  1342. memcpy(&rtpinfo, tmphdr[3]->body.s, tmphdr[3]->body.len);
  1343. len = tmphdr[3]->body.len;
  1344. if((tmphdr[3] = get_hdr_by_name(msg,"RTP-TxStat", 10)) != NULL) {
  1345. memcpy(&rtpinfo[len], ", ", 2);
  1346. if((len + 2 + tmphdr[3]->body.len) > 256) tmphdr[3]->body.len = 256 - (len+2);
  1347. memcpy(&rtpinfo[len+2], tmphdr[3]->body.s, tmphdr[3]->body.len);
  1348. }
  1349. sco.rtp_stat.s = rtpinfo;
  1350. sco.rtp_stat.len = strlen(rtpinfo);
  1351. }
  1352. else { EMPTY_STR(sco.rtp_stat); }
  1353. /* PROTO TYPE */
  1354. sco.proto = msg->rcv.proto;
  1355. /* FAMILY TYPE */
  1356. sco.family = msg->rcv.src_ip.af;
  1357. /* MESSAGE TYPE */
  1358. sco.type = msg->first_line.type;
  1359. /* MSG */
  1360. sco.msg.s = msg->buf;
  1361. sco.msg.len = msg->len;
  1362. //EMPTY_STR(sco.msg);
  1363. /* IP source and destination */
  1364. strcpy(buf_ip, ip_addr2a(&msg->rcv.src_ip));
  1365. sco.source_ip.s = buf_ip;
  1366. sco.source_ip.len = strlen(buf_ip);
  1367. sco.source_port = msg->rcv.src_port;
  1368. /*source ip*/
  1369. sco.destination_ip.s = ip_addr2a(&msg->rcv.dst_ip);
  1370. sco.destination_ip.len = strlen(sco.destination_ip.s);
  1371. sco.destination_port = msg->rcv.dst_port;
  1372. LM_DBG("src_ip: [%.*s]\n", sco.source_ip.len, sco.source_ip.s);
  1373. LM_DBG("dst_ip: [%.*s]\n", sco.destination_ip.len, sco.destination_ip.s);
  1374. LM_DBG("dst_port: [%d]\n", sco.destination_port);
  1375. LM_DBG("src_port: [%d]\n", sco.source_port);
  1376. #ifdef STATISTICS
  1377. if(msg->first_line.type==SIP_REPLY) {
  1378. sco.stat = (cm_data)?cm_data->sipcapture_rpl:capture_def->sipcapture_rpl;
  1379. } else {
  1380. sco.stat = (cm_data)?cm_data->sipcapture_req:capture_def->sipcapture_req;
  1381. }
  1382. #endif
  1383. //LM_DBG("DONE");
  1384. return sip_capture_store(&sco, _table, cm_data);
  1385. }
  1386. #define capture_is_off(_msg) \
  1387. (capture_on_flag==NULL || *capture_on_flag==0)
  1388. /*! \brief
  1389. * MI Sip_capture command
  1390. *
  1391. * MI command format:
  1392. * name: sip_capture
  1393. * attribute: name=none, value=[on|off]
  1394. */
  1395. static struct mi_root* sip_capture_mi(struct mi_root* cmd_tree, void* param )
  1396. {
  1397. struct mi_node* node;
  1398. struct mi_node *rpl;
  1399. struct mi_root *rpl_tree ;
  1400. node = cmd_tree->node.kids;
  1401. if(node == NULL) {
  1402. rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
  1403. if (rpl_tree == 0)
  1404. return 0;
  1405. rpl = &rpl_tree->node;
  1406. if (*capture_on_flag == 0 ) {
  1407. node = add_mi_node_child(rpl,0,0,0,MI_SSTR("off"));
  1408. } else if (*capture_on_flag == 1) {
  1409. node = add_mi_node_child(rpl,0,0,0,MI_SSTR("on"));
  1410. }
  1411. return rpl_tree ;
  1412. }
  1413. if(capture_on_flag==NULL)
  1414. return init_mi_tree( 500, MI_SSTR(MI_INTERNAL_ERR));
  1415. if ( node->value.len==2 && (node->value.s[0]=='o'
  1416. || node->value.s[0]=='O') &&
  1417. (node->value.s[1]=='n'|| node->value.s[1]=='N')) {
  1418. *capture_on_flag = 1;
  1419. return init_mi_tree( 200, MI_SSTR(MI_OK));
  1420. } else if ( node->value.len==3 && (node->value.s[0]=='o'
  1421. || node->value.s[0]=='O')
  1422. && (node->value.s[1]=='f'|| node->value.s[1]=='F')
  1423. && (node->value.s[2]=='f'|| node->value.s[2]=='F')) {
  1424. *capture_on_flag = 0;
  1425. return init_mi_tree( 200, MI_SSTR(MI_OK));
  1426. } else {
  1427. return init_mi_tree( 400, MI_SSTR(MI_BAD_PARM));
  1428. }
  1429. }
  1430. /* Local raw socket */
  1431. int raw_capture_socket(struct ip_addr* ip, str* iface, int port_start, int port_end, int proto)
  1432. {
  1433. int sock = -1;
  1434. union sockaddr_union su;
  1435. #ifdef __OS_linux
  1436. struct sock_fprog pf;
  1437. char short_ifname[sizeof(int)];
  1438. int ifname_len;
  1439. char* ifname;
  1440. #endif
  1441. //0x0003 - all packets
  1442. if(proto == IPPROTO_IPIP) {
  1443. sock = socket(PF_INET, SOCK_RAW, proto);
  1444. }
  1445. #ifdef __OS_linux
  1446. else if(proto == htons(0x800)) {
  1447. sock = socket(PF_PACKET, SOCK_RAW, proto);
  1448. }
  1449. #endif
  1450. else {
  1451. ERR("raw_capture_socket: LSF currently supported only on linux\n");
  1452. goto error;
  1453. }
  1454. if (sock==-1)
  1455. goto error;
  1456. #ifdef __OS_linux
  1457. /* set socket options */
  1458. if (iface && iface->s){
  1459. /* workaround for linux bug: arg to setsockopt must have at least
  1460. * sizeof(int) size or EINVAL would be returned */
  1461. if (iface->len<sizeof(int)){
  1462. memcpy(short_ifname, iface->s, iface->len);
  1463. short_ifname[iface->len]=0; /* make sure it's zero term */
  1464. ifname_len=sizeof(short_ifname);
  1465. ifname=short_ifname;
  1466. }else{
  1467. ifname_len=iface->len;
  1468. ifname=iface->s;
  1469. }
  1470. if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, ifname_len) <0){
  1471. ERR("raw_socket: could not bind to %.*s: %s [%d]\n",
  1472. iface->len, ZSW(iface->s), strerror(errno), errno);
  1473. goto error;
  1474. }
  1475. }
  1476. if(bpf_on) {
  1477. memset(&pf, 0, sizeof(pf));
  1478. pf.len = sizeof(BPF_code) / sizeof(BPF_code[0]);
  1479. pf.filter = (struct sock_filter *) BPF_code;
  1480. if(!port_end) port_end = port_start;
  1481. /* Start PORT */
  1482. BPF_code[5] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 1);
  1483. BPF_code[8] = (struct sock_filter)BPF_JUMP(0x35, port_start, 11, 13);
  1484. BPF_code[16] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 1);
  1485. BPF_code[19] = (struct sock_filter)BPF_JUMP(0x35, port_start, 0, 2);
  1486. /* Stop PORT */
  1487. BPF_code[6] = (struct sock_filter)BPF_JUMP(0x25, port_end, 0, 14);
  1488. BPF_code[17] = (struct sock_filter)BPF_JUMP(0x25, port_end, 0, 3);
  1489. BPF_code[20] = (struct sock_filter)BPF_JUMP(0x25, port_end, 1, 0);
  1490. /* Attach the filter to the socket */
  1491. if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) < 0 ) {
  1492. ERR(" setsockopt filter: [%s] [%d]\n", strerror(errno), errno);
  1493. }
  1494. }
  1495. #endif
  1496. if (ip && proto == IPPROTO_IPIP){
  1497. init_su(&su, ip, 0);
  1498. if (bind(sock, &su.s, sockaddru_len(su))==-1){
  1499. ERR("raw_capture_socket: bind(%s) failed: %s [%d]\n",
  1500. ip_addr2a(ip), strerror(errno), errno);
  1501. goto error;
  1502. }
  1503. }
  1504. return sock;
  1505. error:
  1506. if (sock!=-1) close(sock);
  1507. return -1;
  1508. }
  1509. /* Local raw receive loop */
  1510. int raw_capture_rcv_loop(int rsock, int port1, int port2, int ipip) {
  1511. static char buf [BUF_SIZE+1];
  1512. union sockaddr_union from;
  1513. union sockaddr_union to;
  1514. struct receive_info ri;
  1515. int len;
  1516. struct ip *iph;
  1517. struct udphdr *udph;
  1518. char* udph_start;
  1519. unsigned short udp_len;
  1520. int offset = 0;
  1521. char* end;
  1522. unsigned short dst_port;
  1523. unsigned short src_port;
  1524. struct ip_addr dst_ip, src_ip;
  1525. struct socket_info* si = 0;
  1526. int tmp_len;
  1527. for(;;){
  1528. len = recvfrom(rsock, buf, BUF_SIZE, 0x20, 0, 0);
  1529. if (len<0){
  1530. if (len==-1){
  1531. LOG(L_ERR, "ERROR: raw_moni_rcv_loop:recvfrom: %s [%d]\n",
  1532. strerror(errno), errno);
  1533. if ((errno==EINTR)||(errno==EWOULDBLOCK))
  1534. continue;
  1535. }else{
  1536. DBG("raw_moni_rcv_loop: recvfrom error: %d\n", len);
  1537. continue;
  1538. }
  1539. }
  1540. end=buf+len;
  1541. offset = ipip ? sizeof(struct ip) : ETHHDR;
  1542. if (unlikely(len<(sizeof(struct ip)+sizeof(struct udphdr) + offset))) {
  1543. DBG("received small packet: %d. Ignore it\n",len);
  1544. continue;
  1545. }
  1546. iph = (struct ip*) (buf + offset);
  1547. offset+=iph->ip_hl*4;
  1548. udph_start = buf+offset;
  1549. udph = (struct udphdr*) udph_start;
  1550. offset +=sizeof(struct udphdr);
  1551. if (unlikely((buf+offset)>end)){
  1552. continue;
  1553. }
  1554. udp_len=ntohs(udph->uh_ulen);
  1555. if (unlikely((udph_start+udp_len)!=end)){
  1556. if ((udph_start+udp_len)>end){
  1557. continue;
  1558. }else{
  1559. DBG("udp length too small: %d/%d\n", (int)udp_len, (int)(end-udph_start));
  1560. continue;
  1561. }
  1562. }
  1563. /* cut off the offset */
  1564. len -= offset;
  1565. if (len<MIN_UDP_PACKET){
  1566. DBG("raw_udp4_rcv_loop: probing packet received from\n");
  1567. continue;
  1568. }
  1569. /* fill dst_port && src_port */
  1570. dst_port=ntohs(udph->uh_dport);
  1571. src_port=ntohs(udph->uh_sport);
  1572. /* if the message has not alpha */
  1573. if(!isalnum((buf+offset)[0])) {
  1574. DBG("not alpha and not digit... skiping...\n");
  1575. continue;
  1576. }
  1577. DBG("PORT: [%d] and [%d]\n", port1, port2);
  1578. if((!port1 && !port2) || (src_port >= port1 && src_port <= port2)
  1579. || (dst_port >= port1 && dst_port <= port2)
  1580. || (!port2 && (src_port == port1 || dst_port == port1))) {
  1581. /*FIL IPs*/
  1582. dst_ip.af=AF_INET;
  1583. dst_ip.len=4;
  1584. dst_ip.u.addr32[0]=iph->ip_dst.s_addr;
  1585. /* fill dst_port */
  1586. ip_addr2su(&to, &dst_ip, dst_port);
  1587. /* fill src_port */
  1588. src_ip.af=AF_INET;
  1589. src_ip.len=4;
  1590. src_ip.u.addr32[0]=iph->ip_src.s_addr;
  1591. ip_addr2su(&from, &src_ip, src_port);
  1592. su_setport(&from, src_port);
  1593. ri.src_su=from;
  1594. su2ip_addr(&ri.src_ip, &from);
  1595. ri.src_port=src_port;
  1596. su2ip_addr(&ri.dst_ip, &to);
  1597. ri.dst_port=dst_port;
  1598. ri.proto=PROTO_UDP;
  1599. /* a little bit memory */
  1600. si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
  1601. if (si==0) {
  1602. LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
  1603. return 0;
  1604. }
  1605. memset(si, 0, sizeof(struct socket_info));
  1606. si->address = ri.dst_ip;
  1607. si->socket=-1;
  1608. /* set port & proto */
  1609. si->port_no = dst_port;
  1610. si->proto=PROTO_UDP;
  1611. si->flags=0;
  1612. si->addr_info_lst=0;
  1613. si->port_no_str.s = int2str(si->port_no, &tmp_len);
  1614. si->port_no_str.len = tmp_len;
  1615. si->address_str.s = ip_addr2a(&si->address);;
  1616. si->address_str.len = strlen(si->address_str.s);
  1617. si->name.len = si->address_str.len;
  1618. si->name.s = si->address_str.s;
  1619. ri.bind_address=si;
  1620. /* and now recieve message */
  1621. receive_msg(buf+offset, len, &ri);
  1622. if(si) pkg_free(si);
  1623. }
  1624. }
  1625. return 0;
  1626. }
  1627. static void sipcapture_rpc_status (rpc_t* rpc, void* c) {
  1628. str status = {0, 0};
  1629. if (rpc->scan(c, "S", &status) < 1) {
  1630. rpc->fault(c, 500, "Not enough parameters (on, off or check)");
  1631. return;
  1632. }
  1633. if(capture_on_flag==NULL) {
  1634. rpc->fault(c, 500, "Internal error");
  1635. return;
  1636. }
  1637. if (strncasecmp(status.s, "on", strlen("on")) == 0) {
  1638. *capture_on_flag = 1;
  1639. rpc->rpl_printf(c, "Enabled");
  1640. return;
  1641. }
  1642. if (strncasecmp(status.s, "off", strlen("off")) == 0) {
  1643. *capture_on_flag = 0;
  1644. rpc->rpl_printf(c, "Disabled");
  1645. return;
  1646. }
  1647. if (strncasecmp(status.s, "check", strlen("check")) == 0) {
  1648. rpc->rpl_printf(c, *capture_on_flag ? "Enabled" : "Disabled");
  1649. return;
  1650. }
  1651. rpc->fault(c, 500, "Bad parameter (on, off or check)");
  1652. return;
  1653. }
  1654. static const char* sipcapture_status_doc[2] = {
  1655. "Get status or turn on/off sipcapture.",
  1656. 0
  1657. };
  1658. rpc_export_t sipcapture_rpc[] = {
  1659. {"sipcapture.status", sipcapture_rpc_status, sipcapture_status_doc, 0},
  1660. {0, 0, 0, 0}
  1661. };
  1662. static int sipcapture_init_rpc(void)
  1663. {
  1664. if (rpc_register_array(sipcapture_rpc)!=0)
  1665. {
  1666. LM_ERR("failed to register RPC commands\n");
  1667. return -1;
  1668. }
  1669. return 0;
  1670. }
  1671. /* for rtcp and logging */
  1672. int receive_logging_json_msg(char * buf, unsigned int len, struct hep_generic_recv *hg, char *log_table) {
  1673. db_key_t db_keys[RTCP_NR_KEYS];
  1674. db_val_t db_vals[RTCP_NR_KEYS];
  1675. struct _sipcapture_object sco;
  1676. char ipstr_dst[INET6_ADDRSTRLEN], ipstr_src[INET6_ADDRSTRLEN];
  1677. char tmp_node[100];
  1678. struct timeval tvb;
  1679. struct timezone tz;
  1680. time_t epoch_time_as_time_t;
  1681. str tmp, corrtmp, table;
  1682. _capture_mode_data_t *c = NULL;
  1683. c = capture_def;
  1684. if (!c){
  1685. LM_ERR("no connection mode available to store data\n");
  1686. return -1;
  1687. }
  1688. memset(&sco, 0, sizeof(struct _sipcapture_object));
  1689. gettimeofday( &tvb, &tz );
  1690. /* PROTO TYPE */
  1691. if(hg->ip_proto->data == IPPROTO_TCP) sco.proto=PROTO_TCP;
  1692. else if(hg->ip_proto->data == IPPROTO_UDP) sco.proto=PROTO_UDP;
  1693. /* FAMILY TYPE */
  1694. sco.family = hg->ip_family->data;
  1695. /* IP source and destination */
  1696. if ( hg->ip_family->data == AF_INET6 ) {
  1697. inet_ntop(AF_INET6, &(hg->hep_dst_ip6->data), ipstr_dst, INET6_ADDRSTRLEN);
  1698. inet_ntop(AF_INET6, &(hg->hep_src_ip6->data), ipstr_src, INET6_ADDRSTRLEN);
  1699. }
  1700. else if ( hg->ip_family->data == AF_INET ) {
  1701. inet_ntop(AF_INET, &(hg->hep_src_ip4->data), ipstr_src, INET_ADDRSTRLEN);
  1702. inet_ntop(AF_INET, &(hg->hep_dst_ip4->data), ipstr_dst, INET_ADDRSTRLEN);
  1703. }
  1704. /*source ip*/
  1705. sco.source_ip.s = ipstr_src;
  1706. sco.source_ip.len = strlen(ipstr_src);
  1707. sco.source_port = hg->src_port->data;
  1708. sco.destination_ip.s = ipstr_dst;
  1709. sco.destination_ip.len = strlen(ipstr_dst);
  1710. sco.destination_port = hg->dst_port->data;
  1711. if(heptime && heptime->tv_sec != 0) {
  1712. sco.tmstamp = (unsigned long long)heptime->tv_sec*1000000+heptime->tv_usec; /* micro ts */
  1713. snprintf(tmp_node, 100, "%.*s:%i", capture_node.len, capture_node.s, heptime->captid);
  1714. sco.node.s = tmp_node;
  1715. sco.node.len = strlen(tmp_node);
  1716. epoch_time_as_time_t = heptime->tv_sec;;
  1717. }
  1718. else {
  1719. sco.tmstamp = (unsigned long long)tvb.tv_sec*1000000+tvb.tv_usec; /* micro ts */
  1720. sco.node = capture_node;
  1721. epoch_time_as_time_t = tvb.tv_sec;
  1722. }
  1723. if(correlation_id) {
  1724. corrtmp.s = correlation_id;
  1725. corrtmp.len = strlen(correlation_id);
  1726. if(!strncmp(log_table, "rtcp_capture",12)) corrtmp.len--;
  1727. }
  1728. db_keys[0] = &date_column;
  1729. db_vals[0].type = DB1_DATETIME;
  1730. db_vals[0].nul = 0;
  1731. db_vals[0].val.time_val = epoch_time_as_time_t;
  1732. db_keys[1] = &micro_ts_column;
  1733. db_vals[1].type = DB1_BIGINT;
  1734. db_vals[1].nul = 0;
  1735. db_vals[1].val.ll_val = sco.tmstamp;
  1736. db_keys[2] = &correlation_column;
  1737. db_vals[2].type = DB1_STR;
  1738. db_vals[2].nul = 0;
  1739. db_vals[2].val.str_val = corrtmp;
  1740. db_keys[3] = &source_ip_column;
  1741. db_vals[3].type = DB1_STR;
  1742. db_vals[3].nul = 0;
  1743. db_vals[3].val.str_val = sco.source_ip;
  1744. db_keys[4] = &source_port_column;
  1745. db_vals[4].type = DB1_INT;
  1746. db_vals[4].nul = 0;
  1747. db_vals[4].val.int_val = sco.source_port;
  1748. db_keys[5] = &dest_ip_column;
  1749. db_vals[5].type = DB1_STR;
  1750. db_vals[5].nul = 0;
  1751. db_vals[5].val.str_val = sco.destination_ip;
  1752. db_keys[6] = &dest_port_column;
  1753. db_vals[6].type = DB1_INT;
  1754. db_vals[6].nul = 0;
  1755. db_vals[6].val.int_val = sco.destination_port;
  1756. db_keys[7] = &proto_column;
  1757. db_vals[7].type = DB1_INT;
  1758. db_vals[7].nul = 0;
  1759. db_vals[7].val.int_val = sco.proto;
  1760. db_keys[8] = &family_column;
  1761. db_vals[8].type = DB1_INT;
  1762. db_vals[8].nul = 0;
  1763. db_vals[8].val.int_val = sco.family;
  1764. db_keys[9] = &type_column;
  1765. db_vals[9].type = DB1_INT;
  1766. db_vals[9].nul = 0;
  1767. db_vals[9].val.int_val = sco.type;
  1768. db_keys[10] = &node_column;
  1769. db_vals[10].type = DB1_STR;
  1770. db_vals[10].nul = 0;
  1771. db_vals[10].val.str_val = sco.node;
  1772. db_keys[11] = &msg_column;
  1773. db_vals[11].type = DB1_BLOB;
  1774. db_vals[11].nul = 0;
  1775. tmp.s = buf;
  1776. tmp.len = len;
  1777. db_vals[11].val.blob_val = tmp;
  1778. table.s = log_table;
  1779. table.len = strlen(log_table);
  1780. c->db_funcs.use_table(c->db_con, &table);
  1781. if(db_insert_mode==1 && c->db_funcs.insert_delayed!=NULL) {
  1782. if (c->db_funcs.insert_delayed(c->db_con, db_keys, db_vals, RTCP_NR_KEYS) < 0) {
  1783. LM_ERR("failed to insert delayed into database\n");
  1784. goto error;
  1785. }
  1786. } else if (c->db_funcs.insert(c->db_con, db_keys, db_vals, RTCP_NR_KEYS) < 0) {
  1787. LM_ERR("failed to insert into database\n");
  1788. goto error;
  1789. }
  1790. return 1;
  1791. error:
  1792. return -1;
  1793. }