local.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. Copyright (c) 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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <sys/time.h>
  25. #include <arpa/inet.h>
  26. #include "babeld.h"
  27. #include "interface.h"
  28. #include "source.h"
  29. #include "neighbour.h"
  30. #include "kernel.h"
  31. #include "xroute.h"
  32. #include "route.h"
  33. #include "util.h"
  34. #include "configuration.h"
  35. #include "local.h"
  36. #include "version.h"
  37. int local_server_socket = -1;
  38. struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
  39. int num_local_sockets = 0;
  40. int local_server_port = -1;
  41. char *local_server_path;
  42. int local_server_write = 0;
  43. static int
  44. write_timeout(int fd, const void *buf, int len)
  45. {
  46. int n = 0, rc = 0;
  47. const char *b = buf;
  48. while(n < len) {
  49. rc = write(fd, b + n, len - n);
  50. if(rc < 0) {
  51. if(errno == EAGAIN || errno == EINTR) {
  52. rc = wait_for_fd(1, fd, 100);
  53. if(rc > 0) {
  54. rc = write(fd, b + n, len - n);
  55. }
  56. }
  57. }
  58. if(rc > 0)
  59. n += rc;
  60. else
  61. break;
  62. }
  63. if(n >= len)
  64. return 1;
  65. else {
  66. if(rc >= 0)
  67. errno = EAGAIN;
  68. return -1;
  69. }
  70. }
  71. static const char *
  72. local_kind(int kind)
  73. {
  74. switch(kind) {
  75. case LOCAL_FLUSH: return "flush";
  76. case LOCAL_CHANGE: return "change";
  77. case LOCAL_ADD: return "add";
  78. default: return "???";
  79. }
  80. }
  81. static void
  82. local_notify_interface_1(struct local_socket *s,
  83. struct interface *ifp, int kind)
  84. {
  85. char buf[512], v4[INET_ADDRSTRLEN];
  86. int rc;
  87. int up;
  88. up = if_up(ifp);
  89. if(up && ifp->ipv4)
  90. inet_ntop(AF_INET, ifp->ipv4, v4, INET_ADDRSTRLEN);
  91. else
  92. v4[0] = '\0';
  93. if(up)
  94. rc = snprintf(buf, 512,
  95. "%s interface %s up true%s%s%s%s\n",
  96. local_kind(kind), ifp->name,
  97. ifp->ll ? " ipv6 " : "",
  98. ifp->ll ? format_address(*ifp->ll) : "",
  99. v4[0] ? " ipv4 " : "", v4);
  100. else
  101. rc = snprintf(buf, 512, "%s interface %s up false\n",
  102. local_kind(kind), ifp->name);
  103. if(rc < 0 || rc >= 512)
  104. goto fail;
  105. rc = write_timeout(s->fd, buf, rc);
  106. if(rc < 0)
  107. goto fail;
  108. return;
  109. fail:
  110. shutdown(s->fd, 1);
  111. return;
  112. }
  113. void
  114. local_notify_interface(struct interface *ifp, int kind)
  115. {
  116. int i;
  117. for(i = 0; i < num_local_sockets; i++) {
  118. if(local_sockets[i].monitor)
  119. local_notify_interface_1(&local_sockets[i], ifp, kind);
  120. }
  121. }
  122. static void
  123. local_notify_neighbour_1(struct local_socket *s,
  124. struct neighbour *neigh, int kind)
  125. {
  126. char buf[512], rttbuf[64];
  127. int rc;
  128. rttbuf[0] = '\0';
  129. if(valid_rtt(neigh)) {
  130. rc = snprintf(rttbuf, 64, " rtt %s rttcost %d",
  131. format_thousands(neigh->rtt), neighbour_rttcost(neigh));
  132. if(rc < 0 || rc >= 64)
  133. rttbuf[0] = '\0';
  134. }
  135. rc = snprintf(buf, 512,
  136. "%s neighbour %lx address %s "
  137. "if %s reach %04x rxcost %d txcost %d%s cost %d\n",
  138. local_kind(kind),
  139. /* Neighbours never move around in memory , so we can use the
  140. address as a unique identifier. */
  141. (unsigned long int)neigh,
  142. format_address(neigh->address),
  143. neigh->ifp->name,
  144. neigh->reach,
  145. neighbour_rxcost(neigh),
  146. neighbour_txcost(neigh),
  147. rttbuf,
  148. neighbour_cost(neigh));
  149. if(rc < 0 || rc >= 512)
  150. goto fail;
  151. rc = write_timeout(s->fd, buf, rc);
  152. if(rc < 0)
  153. goto fail;
  154. return;
  155. fail:
  156. shutdown(s->fd, 1);
  157. return;
  158. }
  159. void
  160. local_notify_neighbour(struct neighbour *neigh, int kind)
  161. {
  162. int i;
  163. for(i = 0; i < num_local_sockets; i++) {
  164. if(local_sockets[i].monitor)
  165. local_notify_neighbour_1(&local_sockets[i], neigh, kind);
  166. }
  167. }
  168. static void
  169. local_notify_xroute_1(struct local_socket *s, struct xroute *xroute, int kind)
  170. {
  171. char buf[512];
  172. int rc;
  173. const char *dst_prefix = format_prefix(xroute->prefix,
  174. xroute->plen);
  175. const char *src_prefix = format_prefix(xroute->src_prefix,
  176. xroute->src_plen);
  177. rc = snprintf(buf, 512, "%s xroute %s-%s prefix %s from %s metric %d\n",
  178. local_kind(kind), dst_prefix, src_prefix,
  179. dst_prefix, src_prefix, xroute->metric);
  180. if(rc < 0 || rc >= 512)
  181. goto fail;
  182. rc = write_timeout(s->fd, buf, rc);
  183. if(rc < 0)
  184. goto fail;
  185. return;
  186. fail:
  187. shutdown(s->fd, 1);
  188. return;
  189. }
  190. void
  191. local_notify_xroute(struct xroute *xroute, int kind)
  192. {
  193. int i;
  194. for(i = 0; i < num_local_sockets; i++) {
  195. if(local_sockets[i].monitor)
  196. local_notify_xroute_1(&local_sockets[i], xroute, kind);
  197. }
  198. }
  199. static void
  200. local_notify_route_1(struct local_socket *s, struct babel_route *route, int kind)
  201. {
  202. char buf[512];
  203. int rc;
  204. const char *dst_prefix = format_prefix(route->src->prefix,
  205. route->src->plen);
  206. const char *src_prefix = format_prefix(route->src->src_prefix,
  207. route->src->src_plen);
  208. rc = snprintf(buf, 512,
  209. "%s route %lx prefix %s from %s installed %s "
  210. "id %s metric %d refmetric %d via %s if %s\n",
  211. local_kind(kind),
  212. (unsigned long)route,
  213. dst_prefix, src_prefix,
  214. route->installed ? "yes" : "no",
  215. format_eui64(route->src->id),
  216. route_metric(route), route->refmetric,
  217. format_address(route->neigh->address),
  218. route->neigh->ifp->name);
  219. if(rc < 0 || rc >= 512)
  220. goto fail;
  221. rc = write_timeout(s->fd, buf, rc);
  222. if(rc < 0)
  223. goto fail;
  224. return;
  225. fail:
  226. shutdown(s->fd, 1);
  227. return;
  228. }
  229. void
  230. local_notify_route(struct babel_route *route, int kind)
  231. {
  232. int i;
  233. for(i = 0; i < num_local_sockets; i++) {
  234. if(local_sockets[i].monitor)
  235. local_notify_route_1(&local_sockets[i], route, kind);
  236. }
  237. }
  238. static void
  239. local_notify_all_1(struct local_socket *s)
  240. {
  241. struct interface *ifp;
  242. struct neighbour *neigh;
  243. struct xroute_stream *xroutes;
  244. struct route_stream *routes;
  245. FOR_ALL_INTERFACES(ifp) {
  246. local_notify_interface_1(s, ifp, LOCAL_ADD);
  247. }
  248. FOR_ALL_NEIGHBOURS(neigh) {
  249. local_notify_neighbour_1(s, neigh, LOCAL_ADD);
  250. }
  251. xroutes = xroute_stream();
  252. if(xroutes) {
  253. while(1) {
  254. struct xroute *xroute = xroute_stream_next(xroutes);
  255. if(xroute == NULL)
  256. break;
  257. local_notify_xroute_1(s, xroute, LOCAL_ADD);
  258. }
  259. xroute_stream_done(xroutes);
  260. }
  261. routes = route_stream(ROUTE_ALL);
  262. if(routes) {
  263. while(1) {
  264. struct babel_route *route = route_stream_next(routes);
  265. if(route == NULL)
  266. break;
  267. local_notify_route_1(s, route, LOCAL_ADD);
  268. }
  269. route_stream_done(routes);
  270. }
  271. return;
  272. }
  273. int
  274. local_read(struct local_socket *s)
  275. {
  276. int rc;
  277. char *eol;
  278. char reply[100] = "ok\n";
  279. const char *message = NULL;
  280. if(s->buf == NULL)
  281. s->buf = malloc(LOCAL_BUFSIZE);
  282. if(s->buf == NULL)
  283. return -1;
  284. if(s->n >= LOCAL_BUFSIZE) {
  285. errno = ENOSPC;
  286. goto fail;
  287. }
  288. rc = read(s->fd, s->buf + s->n, LOCAL_BUFSIZE - s->n);
  289. if(rc <= 0)
  290. return rc;
  291. s->n += rc;
  292. eol = memchr(s->buf, '\n', s->n);
  293. if(eol == NULL)
  294. return 1;
  295. rc = parse_config_from_string(s->buf, eol + 1 - s->buf, &message);
  296. switch(rc) {
  297. case CONFIG_ACTION_DONE:
  298. break;
  299. case CONFIG_ACTION_QUIT:
  300. shutdown(s->fd, 1);
  301. reply[0] = '\0';
  302. break;
  303. case CONFIG_ACTION_DUMP:
  304. local_notify_all_1(s);
  305. break;
  306. case CONFIG_ACTION_MONITOR:
  307. local_notify_all_1(s);
  308. s->monitor = 1;
  309. break;
  310. case CONFIG_ACTION_UNMONITOR:
  311. s->monitor = 0;
  312. break;
  313. case CONFIG_ACTION_NO:
  314. snprintf(reply, sizeof(reply), "no%s%s\n",
  315. message ? " " : "", message ? message : "");
  316. break;
  317. default:
  318. snprintf(reply, sizeof(reply), "bad\n");
  319. }
  320. if(reply[0] != '\0') {
  321. rc = write_timeout(s->fd, reply, strlen(reply));
  322. if(rc < 0)
  323. goto fail;
  324. }
  325. if(s->n > eol + 1 - s->buf) {
  326. memmove(s->buf, eol + 1, s->n - (eol + 1 - s->buf));
  327. s->n -= (eol + 1 - s->buf);
  328. } else {
  329. s->n = 0;
  330. free(s->buf);
  331. s->buf = NULL;
  332. }
  333. return 1;
  334. fail:
  335. shutdown(s->fd, 1);
  336. return -1;
  337. }
  338. int
  339. local_header(struct local_socket *s)
  340. {
  341. char buf[512], host[64];
  342. int rc;
  343. rc = gethostname(host, 64);
  344. if(rc < 0)
  345. strncpy(host, "alamakota", 64);
  346. rc = snprintf(buf, 512, "BABEL 1.0\nversion %s\nhost %s\nmy-id %s\nok\n",
  347. BABELD_VERSION, host, format_eui64(myid));
  348. if(rc < 0 || rc >= 512)
  349. goto fail;
  350. rc = write_timeout(s->fd, buf, rc);
  351. if(rc < 0)
  352. goto fail;
  353. return 1;
  354. fail:
  355. shutdown(s->fd, 1);
  356. return -1;
  357. }
  358. struct local_socket *
  359. local_socket_create(int fd)
  360. {
  361. if(num_local_sockets >= MAX_LOCAL_SOCKETS)
  362. return NULL;
  363. memset(&local_sockets[num_local_sockets], 0, sizeof(struct local_socket));
  364. local_sockets[num_local_sockets].fd = fd;
  365. num_local_sockets++;
  366. return &local_sockets[num_local_sockets - 1];
  367. }
  368. void
  369. local_socket_destroy(int i)
  370. {
  371. if(i < 0 || i >= num_local_sockets) {
  372. fprintf(stderr, "Internal error: closing unknown local socket.\n");
  373. return;
  374. }
  375. free(local_sockets[i].buf);
  376. close(local_sockets[i].fd);
  377. local_sockets[i] = local_sockets[--num_local_sockets];
  378. VALGRIND_MAKE_MEM_UNDEFINED(local_sockets + num_local_sockets,
  379. sizeof(struct local_socket));
  380. }