dnnotify.c 3.0 KB

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