pv_time.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * $Id: pv_shv.c 5291 2008-12-03 16:47:50Z miconda $
  3. *
  4. * Copyright (C) 2007 Elena-Ramona Modroiu
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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. * Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include "../../dprint.h"
  31. #include "../../globals.h"
  32. #include "../../pvar.h"
  33. #include "pv_time.h"
  34. int pv_parse_time_name(pv_spec_p sp, str *in)
  35. {
  36. if(sp==NULL || in==NULL || in->len<=0)
  37. return -1;
  38. switch(in->len)
  39. {
  40. case 3:
  41. if(strncmp(in->s, "sec", 3)==0)
  42. sp->pvp.pvn.u.isname.name.n = 0;
  43. else if(strncmp(in->s, "min", 3)==0)
  44. sp->pvp.pvn.u.isname.name.n = 1;
  45. else if(strncmp(in->s, "mon", 3)==0)
  46. sp->pvp.pvn.u.isname.name.n = 4;
  47. else goto error;
  48. break;
  49. case 4:
  50. if(strncmp(in->s, "hour", 4)==0)
  51. sp->pvp.pvn.u.isname.name.n = 2;
  52. else if(strncmp(in->s, "mday", 4)==0)
  53. sp->pvp.pvn.u.isname.name.n = 3;
  54. else if(strncmp(in->s, "year", 4)==0)
  55. sp->pvp.pvn.u.isname.name.n = 5;
  56. else if(strncmp(in->s, "wday", 4)==0)
  57. sp->pvp.pvn.u.isname.name.n = 6;
  58. else if(strncmp(in->s, "yday", 4)==0)
  59. sp->pvp.pvn.u.isname.name.n = 7;
  60. else goto error;
  61. break;
  62. case 5:
  63. if(strncmp(in->s, "isdst", 5)==0)
  64. sp->pvp.pvn.u.isname.name.n = 8;
  65. else goto error;
  66. break;
  67. default:
  68. goto error;
  69. }
  70. sp->pvp.pvn.type = PV_NAME_INTSTR;
  71. sp->pvp.pvn.u.isname.type = 0;
  72. return 0;
  73. error:
  74. LM_ERR("unknown PV time name %.*s\n", in->len, in->s);
  75. return -1;
  76. }
  77. int pv_parse_strftime_name(pv_spec_p sp, str *in)
  78. {
  79. if(sp==NULL || in==NULL || in->len<=0)
  80. return -1;
  81. sp->pvp.pvn.u.isname.name.s.s = as_asciiz(in);
  82. if(sp->pvp.pvn.u.isname.name.s.s==NULL)
  83. {
  84. LM_ERR("no more pkg\n");
  85. return -1;
  86. }
  87. sp->pvp.pvn.u.isname.name.s.len = in->len;
  88. #if 0
  89. /* to-do: free function for pv name structure */
  90. sp->pvp.pvn.nfree = pkg_free;
  91. #endif
  92. return 0;
  93. }
  94. static struct tm _cfgutils_ts;
  95. static msg_ctx_id_t _cfgutils_msgid = { 0 };
  96. /**
  97. * return broken-down time attributes
  98. */
  99. int pv_get_time(struct sip_msg *msg, pv_param_t *param,
  100. pv_value_t *res)
  101. {
  102. if(msg==NULL || param==NULL)
  103. return -1;
  104. if(msg_ctx_id_match(msg, &_cfgutils_msgid)!=1)
  105. {
  106. msg_set_time(msg);
  107. msg_ctx_id_set(msg, &_cfgutils_msgid);
  108. if(localtime_r(&msg->tval.tv_sec, &_cfgutils_ts) == NULL)
  109. {
  110. LM_ERR("unable to break time to attributes\n");
  111. return -1;
  112. }
  113. }
  114. switch(param->pvn.u.isname.name.n)
  115. {
  116. case 1:
  117. return pv_get_uintval(msg, param, res, (unsigned int)_cfgutils_ts.tm_min);
  118. case 2:
  119. return pv_get_uintval(msg, param, res, (unsigned int)_cfgutils_ts.tm_hour);
  120. case 3:
  121. return pv_get_uintval(msg, param, res, (unsigned int)_cfgutils_ts.tm_mday);
  122. case 4:
  123. return pv_get_uintval(msg, param, res,
  124. (unsigned int)(_cfgutils_ts.tm_mon+1));
  125. case 5:
  126. return pv_get_uintval(msg, param, res,
  127. (unsigned int)(_cfgutils_ts.tm_year+1900));
  128. case 6:
  129. return pv_get_uintval(msg, param, res,
  130. (unsigned int)(_cfgutils_ts.tm_wday+1));
  131. case 7:
  132. return pv_get_uintval(msg, param, res,
  133. (unsigned int)(_cfgutils_ts.tm_yday+1));
  134. case 8:
  135. return pv_get_sintval(msg, param, res, _cfgutils_ts.tm_isdst);
  136. default:
  137. return pv_get_uintval(msg, param, res, (unsigned int)_cfgutils_ts.tm_sec);
  138. }
  139. }
  140. /**
  141. * return strftime() formatted time
  142. */
  143. int pv_get_strftime(struct sip_msg *msg, pv_param_t *param,
  144. pv_value_t *res)
  145. {
  146. str s;
  147. #define PV_STRFTIME_BUF_SIZE 64
  148. static char _pv_strftime_buf[PV_STRFTIME_BUF_SIZE];
  149. if(msg==NULL || param==NULL)
  150. return -1;
  151. if(msg_ctx_id_match(msg, &_cfgutils_msgid)!=1)
  152. {
  153. msg_set_time(msg);
  154. msg_ctx_id_set(msg, &_cfgutils_msgid);
  155. if(localtime_r(&msg->tval.tv_sec, &_cfgutils_ts) == NULL)
  156. {
  157. LM_ERR("unable to break time to attributes\n");
  158. return -1;
  159. }
  160. }
  161. s.len = strftime(_pv_strftime_buf, PV_STRFTIME_BUF_SIZE,
  162. param->pvn.u.isname.name.s.s, &_cfgutils_ts);
  163. if(s.len<=0)
  164. return pv_get_null(msg, param, res);
  165. s.s = _pv_strftime_buf;
  166. return pv_get_strval(msg, param, res, &s);
  167. }
  168. int pv_get_timenows(struct sip_msg *msg, pv_param_t *param,
  169. pv_value_t *res)
  170. {
  171. time_t t;
  172. t = time(NULL);
  173. return pv_get_uintval(msg, param, res, (unsigned int)t);
  174. }
  175. int pv_get_timenowf(struct sip_msg *msg, pv_param_t *param,
  176. pv_value_t *res)
  177. {
  178. str s;
  179. time_t t;
  180. t = time(NULL);
  181. s.s = ctime(&t);
  182. s.len = strlen(s.s)-1;
  183. return pv_get_strintval(msg, param, res, &s, (int)t);
  184. }
  185. int pv_get_times(struct sip_msg *msg, pv_param_t *param,
  186. pv_value_t *res)
  187. {
  188. if(msg==NULL)
  189. return -1;
  190. msg_set_time(msg);
  191. return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_sec);
  192. }
  193. int pv_get_timef(struct sip_msg *msg, pv_param_t *param,
  194. pv_value_t *res)
  195. {
  196. str s;
  197. if(msg==NULL)
  198. return -1;
  199. msg_set_time(msg);
  200. s.s = ctime(&msg->tval.tv_sec);
  201. s.len = strlen(s.s)-1;
  202. return pv_get_strintval(msg, param, res, &s, (int)msg->tval.tv_sec);
  203. }
  204. int pv_get_timeb(struct sip_msg *msg, pv_param_t *param,
  205. pv_value_t *res)
  206. {
  207. if(msg==NULL)
  208. return -1;
  209. return pv_get_uintval(msg, param, res, (unsigned int)up_since);
  210. }
  211. static struct timeval _timeval_ts = {0};
  212. static char _timeval_ts_buf[32];
  213. int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
  214. pv_value_t *res)
  215. {
  216. struct timeval tv;
  217. str s;
  218. if(msg==NULL || param==NULL)
  219. return -1;
  220. switch(param->pvn.u.isname.name.n)
  221. {
  222. case 1:
  223. msg_set_time(msg);
  224. return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_usec);
  225. case 2:
  226. if(gettimeofday(&_timeval_ts, NULL)!=0)
  227. {
  228. LM_ERR("unable to get time val attributes\n");
  229. return pv_get_null(msg, param, res);
  230. }
  231. return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_sec);
  232. case 3:
  233. return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_usec);
  234. case 4:
  235. if(gettimeofday(&tv, NULL)!=0)
  236. {
  237. LM_ERR("unable to get time val attributes\n");
  238. return pv_get_null(msg, param, res);
  239. }
  240. s.len = snprintf(_timeval_ts_buf, 32, "%u.%06u",
  241. (unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec);
  242. if(s.len<0)
  243. return pv_get_null(msg, param, res);
  244. s.s = _timeval_ts_buf;
  245. return pv_get_strval(msg, param, res, &s);
  246. default:
  247. msg_set_time(msg);
  248. return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_sec);
  249. }
  250. }
  251. int pv_parse_timeval_name(pv_spec_p sp, str *in)
  252. {
  253. if(sp==NULL || in==NULL || in->len<=0)
  254. return -1;
  255. switch(in->len)
  256. {
  257. case 1:
  258. if(strncmp(in->s, "s", 1)==0)
  259. sp->pvp.pvn.u.isname.name.n = 0;
  260. else if(strncmp(in->s, "u", 1)==0)
  261. sp->pvp.pvn.u.isname.name.n = 1;
  262. else goto error;
  263. break;
  264. case 2:
  265. if(strncmp(in->s, "sn", 2)==0)
  266. sp->pvp.pvn.u.isname.name.n = 2;
  267. else if(strncmp(in->s, "un", 2)==0)
  268. sp->pvp.pvn.u.isname.name.n = 3;
  269. else if(strncmp(in->s, "Sn", 2)==0)
  270. sp->pvp.pvn.u.isname.name.n = 4;
  271. else goto error;
  272. break;
  273. default:
  274. goto error;
  275. }
  276. sp->pvp.pvn.type = PV_NAME_INTSTR;
  277. sp->pvp.pvn.u.isname.type = 0;
  278. return 0;
  279. error:
  280. LM_ERR("unknown PV timeval name %.*s\n", in->len, in->s);
  281. return -1;
  282. }