-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.go
113 lines (99 loc) · 3.14 KB
/
listen.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
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2022
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
package patty
// #cgo linux LDFLAGS: -lpatty
//
// #include <patty/ax25.h>
import "C"
import (
"fmt"
"net"
"os"
)
// Listen will listen for incoming requests to our station, handshake with
// them, and return the connection back to our consuming Go code.
//
// 'network' must be "ax25", and 'address' is your callsign (e.g., K3XEC-10)
//
// This returns a net.Listener, which can be used in the Go stdlib to make
// connections using AX.25.
func (c *Client) Listen(network, address string) (net.Listener, error) {
switch network {
case "ax25":
break
default:
return nil, fmt.Errorf("patty: client.Listen: unknown network")
}
addr, err := ParseAddr(address)
if err != nil {
return nil, err
}
local, err := C.patty_client_socket(c.client, C.PATTY_AX25_PROTO_NONE, C.PATTY_AX25_SOCK_STREAM)
if err != nil {
return nil, err
}
// check local for errno
_, err = C.patty_client_bind(c.client, local, addr.addr())
if err != nil {
return nil, err
}
_, err = C.patty_client_listen(c.client, local)
if err != nil {
return nil, err
}
return &Listener{
client: c,
a: *addr,
fd: int(local),
}, nil
}
// Listener is the type returned by Client.Listen; and implements the
// net.Listener interface.
type Listener struct {
client *Client
a Addr
fd int
}
// Accept will accept the next connection, and return the active net.Conn.
func (l Listener) Accept() (net.Conn, error) {
peer := &Addr{}
fd, err := C.patty_client_accept(l.client.client, C.int(l.fd), peer.addr())
if err != nil {
return nil, err
}
return &Conn{
client: l.client,
fd: int(fd),
laddr: l.a,
raddr: *peer,
file: os.NewFile(uintptr(fd), fmt.Sprintf("ax25: %s<->%s", l.a, peer)),
}, nil
}
// Close will release all resources held by this Listener
func (l Listener) Close() error {
_, err := C.patty_client_close(l.client.client, C.int(l.fd))
return err
}
// Addr will return the AX.25 callsign and SSID that we are listening to
// requests to.
func (l Listener) Addr() net.Addr {
return l.a
}
// vim: foldmethod=marker