dnnotify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. 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. syslog(0, logfile, "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. continue;
  87. if(repmsg.id == reqno && (repmsg.flags & Omask) == Onotify)
  88. break;
  89. }
  90. close(fd);
  91. }
  92. /* send notifies for any updated areas */
  93. static void
  94. notify_areas(Area *a, Request *req)
  95. {
  96. Server *s;
  97. for(; a != nil; a = a->next){
  98. if(!a->neednotify)
  99. continue;
  100. /* send notifies to all slaves */
  101. for(s = a->soarr->soa->slaves; s != nil; s = s->next)
  102. send_notify(s->name, a->soarr, req);
  103. a->neednotify = 0;
  104. }
  105. }
  106. /*
  107. * process to notify other servers of changes
  108. * (also reads in new databases)
  109. */
  110. void
  111. notifyproc(void)
  112. {
  113. Request req;
  114. static int already;
  115. if(already)
  116. return;
  117. switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
  118. case -1:
  119. return;
  120. case 0:
  121. break;
  122. default:
  123. return;
  124. }
  125. procsetname("notify slaves");
  126. memset(&req, 0, sizeof req);
  127. req.isslave = 1; /* don't fork off subprocesses */
  128. for(;;){
  129. getactivity(&req, 0);
  130. notify_areas(owned, &req);
  131. putactivity(0);
  132. sleep(60*1000);
  133. }
  134. }