resend.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <sys/time.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "babeld.h"
  24. #include "util.h"
  25. #include "neighbour.h"
  26. #include "resend.h"
  27. #include "message.h"
  28. #include "interface.h"
  29. #include "configuration.h"
  30. #include "lorauth.h"
  31. struct timeval resend_time = {0, 0};
  32. struct resend *to_resend = NULL;
  33. static int
  34. resend_match(struct resend *resend,
  35. int kind, const unsigned char *prefix, unsigned char plen,
  36. const unsigned char *src_prefix, unsigned char src_plen)
  37. {
  38. return (resend->kind == kind &&
  39. resend->plen == plen && memcmp(resend->prefix, prefix, 16) == 0 &&
  40. resend->src_plen == src_plen &&
  41. memcmp(resend->src_prefix, src_prefix, 16) == 0);
  42. }
  43. /* This is called by neigh.c when a neighbour is flushed */
  44. void
  45. flush_resends(struct neighbour *neigh)
  46. {
  47. /* Nothing for now */
  48. }
  49. static struct resend *
  50. find_resend(int kind, const unsigned char *prefix, unsigned char plen,
  51. const unsigned char *src_prefix, unsigned char src_plen,
  52. struct resend **previous_return)
  53. {
  54. struct resend *current, *previous;
  55. previous = NULL;
  56. current = to_resend;
  57. while(current) {
  58. if(resend_match(current, kind, prefix, plen, src_prefix, src_plen)) {
  59. if(previous_return)
  60. *previous_return = previous;
  61. return current;
  62. }
  63. previous = current;
  64. current = current->next;
  65. }
  66. return NULL;
  67. }
  68. struct resend *
  69. find_request(const unsigned char *prefix, unsigned char plen,
  70. const unsigned char *src_prefix, unsigned char src_plen,
  71. struct resend **previous_return)
  72. {
  73. return find_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
  74. previous_return);
  75. }
  76. int
  77. record_resend(int kind, const unsigned char *prefix, unsigned char plen,
  78. const unsigned char *src_prefix, unsigned char src_plen,
  79. const unsigned char *cipher, unsigned short clen,
  80. unsigned short seqno, const unsigned char *id,
  81. struct interface *ifp, int delay)
  82. {
  83. struct resend *resend;
  84. unsigned int ifindex = ifp ? ifp->ifindex : 0;
  85. if((kind == RESEND_REQUEST &&
  86. input_filter(NULL, prefix, plen, src_prefix, src_plen, NULL,
  87. ifindex) >=
  88. INFINITY) ||
  89. (kind == RESEND_UPDATE &&
  90. output_filter(NULL, prefix, plen, src_prefix, src_plen, ifindex) >=
  91. INFINITY))
  92. return 0;
  93. if(delay >= 0xFFFF)
  94. delay = 0xFFFF;
  95. resend = find_resend(kind, prefix, plen, src_prefix, src_plen, NULL);
  96. if(resend) {
  97. if(resend->delay && delay)
  98. resend->delay = MIN(resend->delay, delay);
  99. else if(delay)
  100. resend->delay = delay;
  101. resend->time = now;
  102. resend->max = RESEND_MAX;
  103. if(id && memcmp(resend->id, id, 8) == 0 &&
  104. seqno_compare(resend->seqno, seqno) > 0) {
  105. return 0;
  106. }
  107. if(id)
  108. memcpy(resend->id, id, 8);
  109. else
  110. memset(resend->id, 0, 8);
  111. resend->seqno = seqno;
  112. if(resend->ifp != ifp)
  113. resend->ifp = NULL;
  114. // -- lorauth --
  115. if(clen>0) {
  116. memcpy(resend->cipher, cipher, LORAUTH_CIPHER_LEN);
  117. resend->clen = clen;
  118. }
  119. // ----
  120. } else {
  121. resend = calloc(1, sizeof(struct resend));
  122. if(resend == NULL)
  123. return -1;
  124. resend->kind = kind;
  125. resend->max = RESEND_MAX;
  126. resend->delay = delay;
  127. memcpy(resend->prefix, prefix, 16);
  128. resend->plen = plen;
  129. memcpy(resend->src_prefix, src_prefix, 16);
  130. resend->src_plen = src_plen;
  131. resend->seqno = seqno;
  132. if(id)
  133. memcpy(resend->id, id, 8);
  134. resend->ifp = ifp;
  135. resend->time = now;
  136. resend->next = to_resend;
  137. // -- lorauth --
  138. resend->clen = clen;
  139. if(clen > 0)
  140. memcpy(resend->cipher, cipher, LORAUTH_CIPHER_LEN);
  141. else
  142. clean_cipher(resend->cipher);
  143. // ----
  144. to_resend = resend;
  145. }
  146. if(resend->delay) {
  147. struct timeval timeout;
  148. timeval_add_msec(&timeout, &resend->time, resend->delay);
  149. timeval_min(&resend_time, &timeout);
  150. }
  151. return 1;
  152. }
  153. static int
  154. resend_expired(struct resend *resend)
  155. {
  156. switch(resend->kind) {
  157. case RESEND_REQUEST:
  158. return timeval_minus_msec(&now, &resend->time) >= REQUEST_TIMEOUT;
  159. default:
  160. return resend->max <= 0;
  161. }
  162. }
  163. int
  164. unsatisfied_request(const unsigned char *prefix, unsigned char plen,
  165. const unsigned char *src_prefix, unsigned char src_plen,
  166. unsigned short seqno, const unsigned char *id)
  167. {
  168. struct resend *request;
  169. request = find_request(prefix, plen, src_prefix, src_plen, NULL);
  170. if(request == NULL || resend_expired(request))
  171. return 0;
  172. if(memcmp(request->id, id, 8) != 0 ||
  173. seqno_compare(request->seqno, seqno) <= 0)
  174. return 1;
  175. return 0;
  176. }
  177. /* Determine whether a given request should be forwarded. */
  178. int
  179. request_redundant(struct interface *ifp,
  180. const unsigned char *prefix, unsigned char plen,
  181. const unsigned char *src_prefix, unsigned char src_plen,
  182. unsigned short seqno, const unsigned char *id)
  183. {
  184. struct resend *request;
  185. request = find_request(prefix, plen, src_prefix, src_plen, NULL);
  186. if(request == NULL || resend_expired(request))
  187. return 0;
  188. if(memcmp(request->id, id, 8) == 0 &&
  189. seqno_compare(request->seqno, seqno) > 0)
  190. return 0;
  191. if(request->ifp != NULL && request->ifp != ifp)
  192. return 0;
  193. if(request->max > 0)
  194. /* Will be resent. */
  195. return 1;
  196. if(timeval_minus_msec(&now, &request->time) <
  197. (ifp ? MIN(ifp->hello_interval, 1000) : 1000))
  198. /* Fairly recent. */
  199. return 1;
  200. return 0;
  201. }
  202. int
  203. satisfy_request(const unsigned char *prefix, unsigned char plen,
  204. const unsigned char *src_prefix, unsigned char src_plen,
  205. unsigned short seqno, const unsigned char *id,
  206. struct interface *ifp)
  207. {
  208. struct resend *request, *previous;
  209. request = find_request(prefix, plen, src_prefix, src_plen, &previous);
  210. if(request == NULL)
  211. return 0;
  212. if(ifp != NULL && request->ifp != ifp)
  213. return 0;
  214. if(memcmp(request->id, id, 8) != 0 ||
  215. seqno_compare(request->seqno, seqno) <= 0) {
  216. /* We cannot remove the request, as we may be walking the list right
  217. now. Mark it as expired, so that expire_resend will remove it. */
  218. request->max = 0;
  219. request->time.tv_sec = 0;
  220. recompute_resend_time();
  221. return 1;
  222. }
  223. return 0;
  224. }
  225. void
  226. expire_resend()
  227. {
  228. struct resend *current, *previous;
  229. int recompute = 0;
  230. previous = NULL;
  231. current = to_resend;
  232. while(current) {
  233. if(resend_expired(current)) {
  234. if(previous == NULL) {
  235. to_resend = current->next;
  236. free(current);
  237. current = to_resend;
  238. } else {
  239. previous->next = current->next;
  240. free(current);
  241. current = previous->next;
  242. }
  243. recompute = 1;
  244. } else {
  245. previous = current;
  246. current = current->next;
  247. }
  248. }
  249. if(recompute)
  250. recompute_resend_time();
  251. }
  252. void
  253. recompute_resend_time()
  254. {
  255. struct resend *request;
  256. struct timeval resend = {0, 0};
  257. request = to_resend;
  258. while(request) {
  259. if(!resend_expired(request) && request->delay > 0 && request->max > 0) {
  260. struct timeval timeout;
  261. timeval_add_msec(&timeout, &request->time, request->delay);
  262. timeval_min(&resend, &timeout);
  263. }
  264. request = request->next;
  265. }
  266. resend_time = resend;
  267. }
  268. void
  269. do_resend()
  270. {
  271. struct resend *resend;
  272. resend = to_resend;
  273. while(resend) {
  274. if(!resend_expired(resend) && resend->delay > 0 && resend->max > 0) {
  275. struct timeval timeout;
  276. timeval_add_msec(&timeout, &resend->time, resend->delay);
  277. if(timeval_compare(&now, &timeout) >= 0) {
  278. switch(resend->kind) {
  279. case RESEND_REQUEST:
  280. send_multihop_request(resend->ifp,
  281. resend->prefix, resend->plen,
  282. resend->src_prefix, resend->src_plen,
  283. resend->seqno, resend->id, 127);
  284. break;
  285. case RESEND_UPDATE:
  286. send_update(resend->ifp, 1,
  287. resend->prefix, resend->plen,
  288. resend->src_prefix, resend->src_plen,
  289. // -- lorauth --
  290. resend->cipher, resend->clen
  291. // ----
  292. );
  293. break;
  294. default: abort();
  295. }
  296. resend->delay = MIN(0xFFFF, resend->delay * 2);
  297. resend->max--;
  298. }
  299. }
  300. resend = resend->next;
  301. }
  302. recompute_resend_time();
  303. }