source.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include <assert.h>
  24. #include "babeld.h"
  25. #include "util.h"
  26. #include "lorauth.h"
  27. #include "source.h"
  28. #include "interface.h"
  29. #include "route.h"
  30. static struct source **sources = NULL;
  31. static int source_slots = 0, max_source_slots = 0;
  32. static int
  33. source_compare(const unsigned char *id,
  34. const unsigned char *prefix, unsigned char plen,
  35. const unsigned char *src_prefix, unsigned char src_plen,
  36. const unsigned char *cipher, unsigned short clen,
  37. const struct source *src)
  38. {
  39. int rc;
  40. rc = memcmp(id, src->id, 8);
  41. if(rc != 0)
  42. return rc;
  43. if(plen < src->plen)
  44. return -1;
  45. if(plen > src->plen)
  46. return 1;
  47. if(clen > src->clen)
  48. return 1;
  49. rc = memcmp(prefix, src->prefix, 16);
  50. if(rc != 0)
  51. return rc;
  52. rc = memcmp(src_prefix, src->src_prefix, 16);
  53. if(rc != 0)
  54. return rc;
  55. if(clen > 0) {
  56. rc = memcmp(cipher, src->cipher, clen);
  57. if(rc != 0)
  58. return rc;
  59. }
  60. return 0;
  61. }
  62. static int
  63. find_source_slot(const unsigned char *id,
  64. const unsigned char *prefix, unsigned char plen,
  65. const unsigned char *src_prefix, unsigned char src_plen,
  66. const unsigned char *cipher, unsigned short clen,
  67. int *new_return)
  68. {
  69. int p, m, g, c;
  70. debugf("\n find_source_slot: \n source_slots:%d, id: %s prefix:%s, clen:%d\n",
  71. source_slots,
  72. format_eui64(id), format_prefix(prefix, plen), clen);
  73. if(source_slots < 1) {
  74. if(new_return)
  75. *new_return = 0;
  76. return -1;
  77. }
  78. p = 0; g = source_slots - 1;
  79. debugf(" searching slots\n");
  80. do {
  81. m = (p + g) / 2;
  82. c = source_compare(id, prefix, plen, src_prefix, src_plen, cipher, clen, sources[m]);
  83. debugf("m:%d, c:%d ",m,c);
  84. if(c == 0)
  85. return m;
  86. else if(c < 0)
  87. g = m - 1;
  88. else
  89. p = m + 1;
  90. } while(p <= g);
  91. if(new_return)
  92. *new_return = p;
  93. return -1;
  94. }
  95. static int
  96. resize_source_table(int new_slots)
  97. {
  98. struct source **new_sources;
  99. assert(new_slots >= source_slots);
  100. if(new_slots == 0) {
  101. new_sources = NULL;
  102. free(sources);
  103. } else {
  104. new_sources = realloc(sources, new_slots * sizeof(struct source*));
  105. if(new_sources == NULL)
  106. return -1;
  107. }
  108. max_source_slots = new_slots;
  109. sources = new_sources;
  110. return 1;
  111. }
  112. struct source*
  113. find_source(const unsigned char *id,
  114. const unsigned char *prefix, unsigned char plen,
  115. const unsigned char *src_prefix, unsigned char src_plen,
  116. const unsigned char *cipher, unsigned short clen,
  117. int create, unsigned short seqno)
  118. {
  119. int n = -1;
  120. int i = find_source_slot(id, prefix, plen, src_prefix, src_plen, cipher, clen, &n);
  121. struct source *src;
  122. if(i >= 0)
  123. return sources[i];
  124. if(!create)
  125. return NULL;
  126. src = calloc(1, sizeof(struct source));
  127. if(src == NULL) {
  128. perror("malloc(source)");
  129. return NULL;
  130. }
  131. memcpy(src->id, id, 8);
  132. memcpy(src->prefix, prefix, 16);
  133. src->plen = plen;
  134. memcpy(src->src_prefix, src_prefix, 16);
  135. // -- lorauth --
  136. src->clen = clen;
  137. memcpy(src->cipher, cipher, clen);
  138. // ----
  139. src->src_plen = src_plen;
  140. src->seqno = seqno;
  141. src->metric = INFINITY;
  142. src->time = now.tv_sec;
  143. if(source_slots >= max_source_slots)
  144. resize_source_table(max_source_slots < 1 ? 8 : 2 * max_source_slots);
  145. if(source_slots >= max_source_slots) {
  146. free(src);
  147. return NULL;
  148. }
  149. if(n < source_slots)
  150. memmove(sources + n + 1, sources + n,
  151. (source_slots - n) * sizeof(struct source*));
  152. source_slots++;
  153. sources[n] = src;
  154. return src;
  155. }
  156. struct source *
  157. retain_source(struct source *src)
  158. {
  159. assert(src->route_count < 0xffff);
  160. src->route_count++;
  161. return src;
  162. }
  163. void
  164. release_source(struct source *src)
  165. {
  166. assert(src->route_count > 0);
  167. src->route_count--;
  168. }
  169. void
  170. update_source(struct source *src,
  171. unsigned short seqno, unsigned short metric)
  172. {
  173. if(metric >= INFINITY)
  174. return;
  175. /* If a source is expired, pretend that it doesn't exist and update
  176. it unconditionally. This makes ensures that old data will
  177. eventually be overridden, and prevents us from getting stuck if
  178. a router loses its sequence number. */
  179. if(src->time < now.tv_sec - SOURCE_GC_TIME ||
  180. seqno_compare(src->seqno, seqno) < 0 ||
  181. (src->seqno == seqno && src->metric > metric)) {
  182. src->seqno = seqno;
  183. src->metric = metric;
  184. // -- lorauth --
  185. src->clen = LORAUTH_CIPHER_LEN;
  186. debugf("---Updating source->cipher\n");
  187. //lorauth_token(src->cipher, src->id, src->seqno);
  188. debugf(" source->cipher: %s\n", src->cipher);
  189. // ----
  190. }
  191. src->time = now.tv_sec;
  192. }
  193. void
  194. expire_sources()
  195. {
  196. int i = 0, j = 0;
  197. while(i < source_slots) {
  198. struct source *src = sources[i];
  199. if(src->time > now.tv_sec)
  200. /* clock stepped */
  201. src->time = now.tv_sec;
  202. if(src->route_count == 0 && src->time < now.tv_sec - SOURCE_GC_TIME) {
  203. free(src);
  204. sources[i] = NULL;
  205. i++;
  206. } else {
  207. if(j < i) {
  208. sources[j] = sources[i];
  209. sources[i] = NULL;
  210. }
  211. i++;
  212. j++;
  213. }
  214. }
  215. source_slots = j;
  216. }
  217. void
  218. check_sources_released(void)
  219. {
  220. int i;
  221. for(i = 0; i < source_slots; i++) {
  222. struct source *src = sources[i];
  223. if(src->route_count != 0)
  224. fprintf(stderr, "Warning: source %s %s has refcount %d.\n",
  225. format_eui64(src->id),
  226. format_prefix(src->prefix, src->plen),
  227. (int)src->route_count);
  228. }
  229. }