forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.go
161 lines (136 loc) · 3.52 KB
/
socket.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
159
160
161
/*
* Copyright (C) 2017 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package common
/*
#ifdef __linux__
#define _GNU_SOURCE
#define __USE_GNU
#include <sched.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/sched.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
int open_raw_socket(const char *name)
{
struct sockaddr_ll sll;
int fd;
fd = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC,
htons(ETH_P_ALL));
if (fd < 0)
return 0;
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = if_nametoindex(name);
sll.sll_protocol = htons(ETH_P_ALL);
if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
close(fd);
return 0;
}
return fd;
}
int open_raw_socket_in_netns(int curns, int newns, const char *name)
{
int errno = 0;
int fd = 0;
errno = setns(newns, CLONE_NEWNET);
if (errno) {
return 0;
}
fd = open_raw_socket(name);
errno = setns(curns, CLONE_NEWNET);
if (errno) {
if (fd) {
close(fd);
}
return 0;
}
return fd;
}
#endif
*/
import "C"
import (
"fmt"
"runtime"
"syscall"
"unsafe"
"github.com/vishvananda/netns"
)
// RawSocket describes a raw socket C implemenation
type RawSocket struct {
fd int
}
// GetFd return the file descriptor
func (s *RawSocket) GetFd() int {
return s.fd
}
// Close the file descriptor
func (s *RawSocket) Close() error {
if s.fd != 0 {
_, _, err := syscall.RawSyscall(syscall.SYS_CLOSE, uintptr(s.fd), 0, 0)
if err != 0 {
return syscall.Errno(err)
}
}
return nil
}
// NewRawSocket create a raw socket for the network interface ifName
func NewRawSocket(ifName string) (*RawSocket, error) {
li := unsafe.Pointer(C.CString(ifName))
defer C.free(li)
fd := C.open_raw_socket((*C.char)(li))
if fd == 0 {
return nil, fmt.Errorf("Failed to open raw socket for %s", ifName)
}
return &RawSocket{
fd: int(fd),
}, nil
}
// NewRawSocketInNs create/open a socket in the namespace nsPath for the network interface ifName
func NewRawSocketInNs(nsPath string, ifName string) (*RawSocket, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, err := netns.Get()
if err != nil {
return nil, fmt.Errorf("Error while getting current ns: %s", err.Error())
}
defer origns.Close()
newns, err := netns.GetFromPath(nsPath)
if err != nil {
return nil, fmt.Errorf("Error while opening %s: %s", nsPath, err.Error())
}
defer newns.Close()
pIfName := unsafe.Pointer(C.CString(ifName))
defer C.free(pIfName)
fd := C.open_raw_socket_in_netns(C.int(origns), C.int(newns), (*C.char)(pIfName))
if fd == 0 {
return nil, fmt.Errorf("Failed to open raw socket for %s", ifName)
}
return &RawSocket{
fd: int(fd),
}, nil
}