dnnotify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include <bio.h>
  5. #include <ndb.h>
  6. #include "dns.h"
  7. /* get a notification from another system of a changed zone */
  8. void
  9. dnnotify(DNSmsg *reqp, DNSmsg *repp, Request *)
  10. {
  11. RR *tp;
  12. Area *a;
  13. /* move one question from reqp to repp */
  14. memset(repp, 0, sizeof(*repp));
  15. tp = reqp->qd;
  16. reqp->qd = tp->next;
  17. tp->next = 0;
  18. repp->qd = tp;
  19. repp->id = reqp->id;
  20. repp->flags = Fresp | Onotify | Fauth;
  21. /* anything to do? */
  22. if(zonerefreshprogram == nil)
  23. return;
  24. /* make sure its the right type */
  25. if(repp->qd->type != Tsoa)
  26. return;
  27. dnslog("notification for %s", repp->qd->owner->name);
  28. /* is it something we care about? */
  29. a = inmyarea(repp->qd->owner->name);
  30. if(a == nil)
  31. return;
  32. dnslog("serial old %lud new %lud", a->soarr->soa->serial,
  33. repp->qd->soa->serial);
  34. /* do nothing if it didn't change */
  35. if(a->soarr->soa->serial != repp->qd->soa->serial)
  36. a->needrefresh = 1;
  37. }
  38. static void
  39. ding(void*, char *msg)
  40. {
  41. if(strstr(msg, "alarm") != nil)
  42. noted(NCONT);
  43. else
  44. noted(NDFLT);
  45. }
  46. /* notify a slave that an area has changed. */
  47. static void
  48. send_notify(char *slave, RR *soa, Request *req)
  49. {
  50. int i, len, n, reqno, status, fd;
  51. char *err;
  52. uchar ibuf[Maxudp+Udphdrsize], obuf[Maxudp+Udphdrsize];
  53. RR *rp;
  54. Udphdr *up = (Udphdr*)obuf;
  55. DNSmsg repmsg;
  56. /* create the request */
  57. reqno = rand();
  58. n = mkreq(soa->owner, Cin, obuf, Fauth | Onotify, reqno);
  59. /* get an address */
  60. if(strcmp(ipattr(slave), "ip") == 0)
  61. parseip(up->raddr, slave);
  62. else {
  63. rp = dnresolve(slave, Cin, Ta, req, nil, 0, 1, 1, &status);
  64. if(rp == nil)
  65. return;
  66. parseip(up->raddr, rp->ip->name);
  67. rrfreelist(rp); /* was rrfree */
  68. }
  69. fd = udpport(nil);
  70. if(fd < 0)
  71. return;
  72. notify(ding);
  73. /* send 3 times or until we get anything back */
  74. n += Udphdrsize;
  75. for(i = 0; i < 3; i++, freeanswers(&repmsg)){
  76. dnslog("sending %d byte notify to %s/%I.%d about %s", n, slave,
  77. up->raddr, nhgets(up->rport), soa->owner->name);
  78. memset(&repmsg, 0, sizeof repmsg);
  79. if(write(fd, obuf, n) != n)
  80. break;
  81. alarm(2*1000);
  82. len = read(fd, ibuf, sizeof ibuf);
  83. alarm(0);
  84. if(len <= Udphdrsize)
  85. continue;
  86. err = convM2DNS(&ibuf[Udphdrsize], len, &repmsg, nil);
  87. if(err != nil) {
  88. free(err);
  89. continue;
  90. }
  91. if(repmsg.id == reqno && (repmsg.flags & Omask) == Onotify)
  92. break;
  93. }
  94. freeanswers(&repmsg);
  95. close(fd);
  96. }
  97. /* send notifies for any updated areas */
  98. static void
  99. notify_areas(Area *a, Request *req)
  100. {
  101. Server *s;
  102. for(; a != nil; a = a->next){
  103. if(!a->neednotify)
  104. continue;
  105. /* send notifies to all slaves */
  106. for(s = a->soarr->soa->slaves; s != nil; s = s->next)
  107. send_notify(s->name, a->soarr, req);
  108. a->neednotify = 0;
  109. }
  110. }
  111. /*
  112. * process to notify other servers of changes
  113. * (also reads in new databases)
  114. */
  115. void
  116. notifyproc(void)
  117. {
  118. Request req;
  119. // static int already;
  120. // if(already)
  121. // return;
  122. switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
  123. case -1:
  124. return;
  125. case 0:
  126. break;
  127. default:
  128. return;
  129. }
  130. procsetname("notify slaves");
  131. memset(&req, 0, sizeof req);
  132. req.isslave = 1; /* don't fork off subprocesses */
  133. for(;;){
  134. getactivity(&req, 0);
  135. notify_areas(owned, &req);
  136. putactivity(0);
  137. sleep(60*1000);
  138. }
  139. }