broadcast.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package beacon
  16. import "net"
  17. type Broadcast struct {
  18. conn *net.UDPConn
  19. port int
  20. inbox chan []byte
  21. outbox chan recv
  22. }
  23. func NewBroadcast(port int) (*Broadcast, error) {
  24. conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port})
  25. if err != nil {
  26. return nil, err
  27. }
  28. b := &Broadcast{
  29. conn: conn,
  30. port: port,
  31. inbox: make(chan []byte),
  32. outbox: make(chan recv, 16),
  33. }
  34. go genericReader(b.conn, b.outbox)
  35. go b.writer()
  36. return b, nil
  37. }
  38. func (b *Broadcast) Send(data []byte) {
  39. b.inbox <- data
  40. }
  41. func (b *Broadcast) Recv() ([]byte, net.Addr) {
  42. recv := <-b.outbox
  43. return recv.data, recv.src
  44. }
  45. func (b *Broadcast) writer() {
  46. for bs := range b.inbox {
  47. addrs, err := net.InterfaceAddrs()
  48. if err != nil {
  49. l.Warnln("Broadcast: interface addresses:", err)
  50. continue
  51. }
  52. var dsts []net.IP
  53. for _, addr := range addrs {
  54. if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
  55. baddr := bcast(iaddr)
  56. dsts = append(dsts, baddr.IP)
  57. }
  58. }
  59. if len(dsts) == 0 {
  60. // Fall back to the general IPv4 broadcast address
  61. dsts = append(dsts, net.IP{0xff, 0xff, 0xff, 0xff})
  62. }
  63. if debug {
  64. l.Debugln("addresses:", dsts)
  65. }
  66. for _, ip := range dsts {
  67. dst := &net.UDPAddr{IP: ip, Port: b.port}
  68. _, err := b.conn.WriteTo(bs, dst)
  69. if err != nil {
  70. if debug {
  71. l.Debugln(err)
  72. }
  73. } else if debug {
  74. l.Debugf("sent %d bytes to %s", len(bs), dst)
  75. }
  76. }
  77. }
  78. }
  79. func bcast(ip *net.IPNet) *net.IPNet {
  80. var bc = &net.IPNet{}
  81. bc.IP = make([]byte, len(ip.IP))
  82. copy(bc.IP, ip.IP)
  83. bc.Mask = ip.Mask
  84. offset := len(bc.IP) - len(bc.Mask)
  85. for i := range bc.IP {
  86. if i-offset >= 0 {
  87. bc.IP[i] = ip.IP[i] | ^ip.Mask[i-offset]
  88. }
  89. }
  90. return bc
  91. }