Skip to content

Commit 3e28e6d

Browse files
frei-0xffaboch
authored andcommitted
Added SocketDestroy function.
1 parent aafe841 commit 3e28e6d

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

netlink_unspecified.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,7 @@ func NeighDeserialize(m []byte) (*Neigh, error) {
283283
func SocketGet(local, remote net.Addr) (*Socket, error) {
284284
return nil, ErrNotImplemented
285285
}
286+
287+
func SocketDestroy(local, remote net.Addr) (*Socket, error) {
288+
return nil, ErrNotImplemented
289+
}

nl/syscall.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
// socket diags related
4747
const (
4848
SOCK_DIAG_BY_FAMILY = 20 /* linux.sock_diag.h */
49+
SOCK_DESTROY = 21
4950
TCPDIAG_NOCOOKIE = 0xFFFFFFFF /* TCPDIAG_NOCOOKIE in net/ipv4/tcp_diag.h*/
5051
)
5152

socket_linux.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,45 @@ func SocketGet(local, remote net.Addr) (*Socket, error) {
215215
return sock, nil
216216
}
217217

218+
// SocketDestroy kills the Socket identified by its local and remote addresses.
219+
func SocketDestroy(local, remote net.Addr) error {
220+
localTCP, ok := local.(*net.TCPAddr)
221+
if !ok {
222+
return ErrNotImplemented
223+
}
224+
remoteTCP, ok := remote.(*net.TCPAddr)
225+
if !ok {
226+
return ErrNotImplemented
227+
}
228+
localIP := localTCP.IP.To4()
229+
if localIP == nil {
230+
return ErrNotImplemented
231+
}
232+
remoteIP := remoteTCP.IP.To4()
233+
if remoteIP == nil {
234+
return ErrNotImplemented
235+
}
236+
237+
s, err := nl.Subscribe(unix.NETLINK_INET_DIAG)
238+
if err != nil {
239+
return err
240+
}
241+
defer s.Close()
242+
req := nl.NewNetlinkRequest(nl.SOCK_DESTROY, unix.NLM_F_ACK)
243+
req.AddData(&socketRequest{
244+
Family: unix.AF_INET,
245+
Protocol: unix.IPPROTO_TCP,
246+
ID: SocketID{
247+
SourcePort: uint16(localTCP.Port),
248+
DestinationPort: uint16(remoteTCP.Port),
249+
Source: localIP,
250+
Destination: remoteIP,
251+
Cookie: [2]uint32{nl.TCPDIAG_NOCOOKIE, nl.TCPDIAG_NOCOOKIE},
252+
},
253+
})
254+
return s.Send(req)
255+
}
256+
218257
// SocketDiagTCPInfo requests INET_DIAG_INFO for TCP protocol for specified family type and return with extension TCP info.
219258
func SocketDiagTCPInfo(family uint8) ([]*InetDiagTCPInfoResp, error) {
220259
// Construct the request

socket_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ func TestSocketGet(t *testing.T) {
6060
}
6161
}
6262

63+
func TestSocketDestroy(t *testing.T) {
64+
defer setUpNetlinkTestWithLoopback(t)()
65+
66+
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
l, err := net.ListenTCP("tcp", addr)
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
defer l.Close()
75+
76+
conn, err := net.Dial(l.Addr().Network(), l.Addr().String())
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
defer conn.Close()
81+
82+
localAddr := conn.LocalAddr().(*net.TCPAddr)
83+
remoteAddr := conn.RemoteAddr().(*net.TCPAddr)
84+
err = SocketDestroy(localAddr, remoteAddr)
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
}
89+
6390
func TestSocketDiagTCPInfo(t *testing.T) {
6491
Family4 := uint8(syscall.AF_INET)
6592
Family6 := uint8(syscall.AF_INET6)

0 commit comments

Comments
 (0)