dnnotify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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,
  76. up->raddr, nhgets(up->rport), soa->owner->name);
  77. if(write(fd, obuf, n) != n)
  78. break;
  79. alarm(2*1000);
  80. len = read(fd, ibuf, sizeof ibuf);
  81. alarm(0);
  82. if(len <= OUdphdrsize)
  83. continue;
  84. memset(&repmsg, 0, sizeof repmsg);
  85. err = convM2DNS(&ibuf[OUdphdrsize], len, &repmsg, nil);
  86. if(err != nil) {
  87. free(err);
  88. continue;
  89. }
  90. if(repmsg.id == reqno && (repmsg.flags & Omask) == Onotify)
  91. break;
  92. }
  93. close(fd);
  94. }
  95. /* send notifies for any updated areas */
  96. static void
  97. notify_areas(Area *a, Request *req)
  98. {
  99. Server *s;
  100. for(; a != nil; a = a->next){
  101. if(!a->neednotify)
  102. continue;
  103. /* send notifies to all slaves */
  104. for(s = a->soarr->soa->slaves; s != nil; s = s->next)
  105. send_notify(s->name, a->soarr, req);
  106. a->neednotify = 0;
  107. }
  108. }
  109. /*
  110. * process to notify other servers of changes
  111. * (also reads in new databases)
  112. */
  113. void
  114. notifyproc(void)
  115. {
  116. Request req;
  117. static int already;
  118. if(already)
  119. return;
  120. switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
  121. case -1:
  122. return;
  123. case 0:
  124. break;
  125. default:
  126. return;
  127. }
  128. procsetname("notify slaves");
  129. memset(&req, 0, sizeof req);
  130. req.isslave = 1; /* don't fork off subprocesses */
  131. for(;;){
  132. getactivity(&req, 0);
  133. notify_areas(owned, &req);
  134. putactivity(0);
  135. sleep(60*1000);
  136. }
  137. }