dnnotify.c 3.1 KB

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