This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconn.go
158 lines (137 loc) · 3.52 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2014 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssdp
import (
"errors"
"net"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
type conn interface {
Close() error
JoinGroup(*net.Interface, net.Addr) error
LeaveGroup(*net.Interface, net.Addr) error
SetMulticastInterface(*net.Interface) error
SetMulticastLoopback(bool) error
setControlFlags() error
readFrom([]byte) (int, *path, error)
writeTo([]byte, *net.UDPAddr) (int, error)
writeToMulti([]byte, *net.UDPAddr, []net.Interface) (int, error)
}
// a path represents a reverse path.
type path struct {
src *net.UDPAddr
dst *net.UDPAddr
ifIndex int
}
type udp4Conn struct {
*ipv4.PacketConn
}
func (c *udp4Conn) setControlFlags() error {
return c.SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true)
}
func (c *udp4Conn) readFrom(b []byte) (int, *path, error) {
n, cm, src, err := c.ReadFrom(b)
if err != nil {
return 0, nil, err
}
return n, &path{src: src.(*net.UDPAddr), dst: &net.UDPAddr{IP: cm.Dst}, ifIndex: cm.IfIndex}, err
}
func (c *udp4Conn) writeTo(b []byte, peer *net.UDPAddr) (int, error) {
if len(b) == 0 { // to prevent writing malformed packets on some platforms
return 0, nil
}
return c.WriteTo(b, nil, peer)
}
func (c *udp4Conn) writeToMulti(b []byte, grp *net.UDPAddr, mifs []net.Interface) (int, error) {
if len(b) == 0 { // to prevent writing malformed packets on some platforms
return 0, nil
}
var n, oks int
var lastErr error
for _, ifi := range mifs {
c.SetMulticastInterface(&ifi)
nn, err := c.writeTo(b, grp)
if err != nil {
lastErr = err
continue
}
n = nn
oks++
}
if oks == 0 {
return 0, lastErr
}
return n, nil
}
func newUDP4Conn(c *ipv4.PacketConn) *udp4Conn {
return &udp4Conn{PacketConn: c}
}
type udp6Conn struct {
*ipv6.PacketConn
}
func (c *udp6Conn) setControlFlags() error {
return c.SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true)
}
func (c *udp6Conn) readFrom(b []byte) (int, *path, error) {
n, cm, src, err := c.ReadFrom(b)
if err != nil {
return 0, nil, err
}
return n, &path{src: src.(*net.UDPAddr), dst: &net.UDPAddr{IP: cm.Dst}, ifIndex: cm.IfIndex}, err
}
func (c *udp6Conn) writeTo(b []byte, peer *net.UDPAddr) (int, error) {
if len(b) == 0 { // to prevent writing malformed packets on some platforms
return 0, nil
}
return c.WriteTo(b, nil, peer)
}
func (c *udp6Conn) writeToMulti(b []byte, grp *net.UDPAddr, mifs []net.Interface) (int, error) {
if len(b) == 0 { // to prevent writing malformed packets on some platforms
return 0, nil
}
var n, oks int
var lastErr error
wrgrp := *grp
for _, ifi := range mifs {
c.SetMulticastInterface(&ifi)
if ipv6LinkLocal(wrgrp.IP) {
wrgrp.Zone = ifi.Name
}
nn, err := c.writeTo(b, &wrgrp)
if err != nil {
lastErr = err
continue
}
n = nn
oks++
}
if oks == 0 {
return 0, lastErr
}
return n, nil
}
func newUDP6Conn(c *ipv6.PacketConn) *udp6Conn {
return &udp6Conn{PacketConn: c}
}
func joinGroup(c conn, grp *net.UDPAddr, mifs []net.Interface, unicast func(net.IP) bool) ([]net.Interface, error) {
mifs, err := interfaces(mifs, unicast)
if err != nil {
return nil, err
}
var rmifs []net.Interface
for _, ifi := range mifs {
if err := c.JoinGroup(&ifi, grp); err != nil {
continue
}
rmifs = append(rmifs, ifi)
}
if len(rmifs) == 0 {
return nil, errors.New("no such multicast network interface")
}
if err := c.setControlFlags(); err != nil {
return nil, err
}
return rmifs, nil
}