dnnotify.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /* notify a slave that an area has changed. */
  39. static void
  40. send_notify(char *slave, RR *soa, Request *req)
  41. {
  42. int i, len, n, reqno, status, fd;
  43. char *err;
  44. uchar ibuf[Maxudp+Udphdrsize], obuf[Maxudp+Udphdrsize];
  45. RR *rp;
  46. Udphdr *up = (Udphdr*)obuf;
  47. DNSmsg repmsg;
  48. /* create the request */
  49. reqno = rand();
  50. n = mkreq(soa->owner, Cin, obuf, Fauth | Onotify, reqno);
  51. /* get an address */
  52. if(strcmp(ipattr(slave), "ip") == 0)
  53. parseip(up->raddr, slave);
  54. else {
  55. rp = dnresolve(slave, Cin, Ta, req, nil, 0, 1, 1, &status);
  56. if(rp == nil)
  57. return;
  58. parseip(up->raddr, rp->ip->name);
  59. rrfreelist(rp); /* was rrfree */
  60. }
  61. fd = udpport(nil);
  62. if(fd < 0)
  63. return;
  64. /* send 3 times or until we get anything back */
  65. n += Udphdrsize;
  66. for(i = 0; i < 3; i++, freeanswers(&repmsg)){
  67. dnslog("sending %d byte notify to %s/%I.%d about %s", n, slave,
  68. up->raddr, nhgets(up->rport), soa->owner->name);
  69. memset(&repmsg, 0, sizeof repmsg);
  70. if(write(fd, obuf, n) != n)
  71. break;
  72. alarm(2*1000);
  73. len = read(fd, ibuf, sizeof ibuf);
  74. alarm(0);
  75. if(len <= Udphdrsize)
  76. continue;
  77. err = convM2DNS(&ibuf[Udphdrsize], len, &repmsg, nil);
  78. if(err != nil) {
  79. free(err);
  80. continue;
  81. }
  82. if(repmsg.id == reqno && (repmsg.flags & Omask) == Onotify)
  83. break;
  84. }
  85. if (i < 3)
  86. freeanswers(&repmsg);
  87. close(fd);
  88. }
  89. /* send notifies for any updated areas */
  90. static void
  91. notify_areas(Area *a, Request *req)
  92. {
  93. Server *s;
  94. for(; a != nil; a = a->next){
  95. if(!a->neednotify)
  96. continue;
  97. /* send notifies to all slaves */
  98. for(s = a->soarr->soa->slaves; s != nil; s = s->next)
  99. send_notify(s->name, a->soarr, req);
  100. a->neednotify = 0;
  101. }
  102. }
  103. /*
  104. * process to notify other servers of changes
  105. * (also reads in new databases)
  106. */
  107. void
  108. notifyproc(void)
  109. {
  110. Request req;
  111. switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
  112. case -1:
  113. return;
  114. case 0:
  115. break;
  116. default:
  117. return;
  118. }
  119. procsetname("notify slaves");
  120. memset(&req, 0, sizeof req);
  121. req.isslave = 1; /* don't fork off subprocesses */
  122. for(;;){
  123. getactivity(&req, 0);
  124. notify_areas(owned, &req);
  125. putactivity(0);
  126. sleep(60*1000);
  127. }
  128. }