This repository was archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_test.go
More file actions
76 lines (69 loc) · 1.64 KB
/
example_test.go
File metadata and controls
76 lines (69 loc) · 1.64 KB
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
// Copyright 2015 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 stun_test
import (
"bytes"
"fmt"
"log"
"net"
"time"
"github.com/mikioh/stun"
)
func ExampleControl_clientUDP() {
dst, err := net.ResolveUDPAddr("udp", "stun.l.google.com:19302")
if err != nil {
log.Fatal(err)
}
c, err := net.ListenPacket("udp", ":0")
if err != nil {
log.Fatal(err)
}
defer c.Close()
tid, err := stun.TransactionID()
if err != nil {
log.Fatal(err)
}
wm := stun.Control{
Type: stun.MessageType(stun.ClassRequest, stun.MethodBinding),
TID: tid,
Attrs: []stun.Attribute{
stun.Software("github.com/mikioh/stun"),
stun.ICEControlling(1),
&stun.UseCandidate{},
stun.Priority(1),
stun.Fingerprint(0),
},
}
wb := make([]byte, wm.Len())
n, err := wm.Marshal(wb, nil)
if err != nil {
log.Fatal(err)
}
if _, err := c.WriteTo(wb[:n], dst); err != nil {
log.Fatal(err)
}
rb := make([]byte, 1500)
if err := c.SetReadDeadline(time.Now().Add(2 * time.Second)); err != nil {
log.Fatal(err)
}
n, _, err = c.ReadFrom(rb)
if err != nil {
log.Fatal(err)
}
_, rm, err := stun.ParseMessage(rb[:n], nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(rm.(*stun.Control).Type.Class())
fmt.Println(rm.(*stun.Control).Type.Method())
if !bytes.Equal(rm.(*stun.Control).Cookie, stun.MagicCookie) {
log.Fatalf("got %#v; want %#v", rm.(*stun.Control).Cookie, stun.MagicCookie)
}
if !bytes.Equal(rm.(*stun.Control).TID, wm.TID) {
log.Fatalf("got %#v; want %#v", rm.(*stun.Control).TID, wm.TID)
}
// Output:
// success response
// binding
}