GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — go-netfilter-queue/netfilter.go at master · jimbertools/go-netfilter-queue Expand file tree/ Copy pathnetfilter.goMore file actions 226 lines (184 loc) · 5.87 KB / Copy pathnetfilter.goFile metadata and controls226 lines (184 loc) · 5.87 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 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 //go:build amd64 /* Copyright 2014 Krishna Raman <[email protected]> Licensed 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. */ /* Go bindings for libnetfilter_queue This library provides access to packets in the IPTables netfilter queue (NFQUEUE). The libnetfilter_queue library is part of the http://netfilter.org/projects/libnetfilter_queue/ project. */ package netfilter /* #cgo pkg-config: libnetfilter_queue #cgo CFLAGS: -Wall -I/usr/include #cgo LDFLAGS: -L/usr/lib64/ #include "netfilter.h" */ import "C" import ( "fmt" "os" "sync" "time" "unsafe" "github.com/google/gopacket" "github.com/google/gopacket/layers" ) // Verdict for a packet type Verdict C.uint type NFPacket struct { Packet gopacket.Packet Mark uint32 qh *C.struct_nfq_q_handle id C.uint32_t } // Set the verdict for the packet func (p *NFPacket) SetVerdict(v Verdict) { C.nfq_set_verdict(p.qh, p.id, C.uint(v), 0, nil) } // Set the verdict for the packet with a mark func (p *NFPacket) SetVerdictMark(v Verdict, mark uint32) { C.nfq_set_verdict2(p.qh, p.id, C.uint(v), C.uint32_t(mark), 0, nil) } // Set the verdict for the packet (in the case of requeue) func (p *NFPacket) SetRequeueVerdict(newQueueId uint16) { v := uint(NF_QUEUE) q := (uint(newQueueId) << 16) v = v | q C.nfq_set_verdict(p.qh, p.id, C.uint(v), 0, nil) } // Set the verdict for the packet AND provide new packet content for injection func (p *NFPacket) SetVerdictWithPacket(v Verdict, packet []byte) { C.nfq_set_verdict( p.qh, p.id, C.uint(v), C.uint(len(packet)), (*C.uchar)(unsafe.Pointer(&packet[0])), ) } type NFQueue struct { h *C.struct_nfq_handle qh *C.struct_nfq_q_handle fd C.int packets chan NFPacket idx uint32 } const ( AF_INET = 2 AF_INET6 = 10 NF_DROP Verdict = 0 NF_ACCEPT Verdict = 1 NF_STOLEN Verdict = 2 NF_QUEUE Verdict = 3 NF_REPEAT Verdict = 4 NF_STOP Verdict = 5 NF_DEFAULT_PACKET_SIZE uint32 = 0xffff ipv4version = 0x40 ) var theTable = make(map[uint32]*chan NFPacket, 0) var theTabeLock sync.RWMutex // Create and bind to queue specified by queueId func NewNFQueue(queueId uint16, maxPacketsInQueue uint32, packetSize uint32) (*NFQueue, error) { var nfq = NFQueue{} var err error var ret C.int if nfq.h, err = C.nfq_open(); err != nil { return nil, fmt.Errorf("Error opening NFQueue handle: %v\n", err) } if ret, err = C.nfq_unbind_pf(nfq.h, AF_INET); err != nil || ret < 0 { return nil, fmt.Errorf("Error unbinding existing NFQ handler from AF_INET protocol family: %v\n", err) } if ret, err = C.nfq_unbind_pf(nfq.h, AF_INET6); err != nil || ret < 0 { return nil, fmt.Errorf("Error unbinding existing NFQ handler from AF_INET6 protocol family: %v\n", err) } if ret, err := C.nfq_bind_pf(nfq.h, AF_INET); err != nil || ret < 0 { return nil, fmt.Errorf("Error binding to AF_INET protocol family: %v\n", err) } if ret, err := C.nfq_bind_pf(nfq.h, AF_INET6); err != nil || ret < 0 { return nil, fmt.Errorf("Error binding to AF_INET6 protocol family: %v\n", err) } nfq.packets = make(chan NFPacket, maxPacketsInQueue) nfq.idx = uint32(time.Now().UnixNano()) theTabeLock.Lock() theTable[nfq.idx] = &nfq.packets theTabeLock.Unlock() if nfq.qh, err = C.CreateQueue(nfq.h, C.u_int16_t(queueId), C.u_int32_t(nfq.idx)); err != nil || nfq.qh == nil { C.nfq_close(nfq.h) return nil, fmt.Errorf("Error binding to queue: %v\n", err) } if ret, err = C.nfq_set_queue_maxlen(nfq.qh, C.u_int32_t(maxPacketsInQueue)); err != nil || ret < 0 { C.nfq_destroy_queue(nfq.qh) C.nfq_close(nfq.h) return nil, fmt.Errorf("Unable to set max packets in queue: %v\n", err) } if C.nfq_set_mode(nfq.qh, C.u_int8_t(2), C.uint(packetSize)) < 0 { C.nfq_destroy_queue(nfq.qh) C.nfq_close(nfq.h) return nil, fmt.Errorf("Unable to set packets copy mode: %v\n", err) } if nfq.fd, err = C.nfq_fd(nfq.h); err != nil { C.nfq_destroy_queue(nfq.qh) C.nfq_close(nfq.h) return nil, fmt.Errorf("Unable to get queue file-descriptor. %v\n", err) } go nfq.run() return &nfq, nil } // Unbind and close the queue func (nfq *NFQueue) Close() { C.nfq_destroy_queue(nfq.qh) C.nfq_close(nfq.h) theTabeLock.Lock() close(nfq.packets) delete(theTable, nfq.idx) theTabeLock.Unlock() } // Get the channel for packets func (nfq *NFQueue) GetPackets() <-chan NFPacket { return nfq.packets } func (nfq *NFQueue) run() { if errno := C.Run(nfq.h, nfq.fd); errno != 0 { fmt.Fprintf(os.Stderr, "Terminating, unable to receive packet due to errno=%d\n", errno) } } //export go_callback func go_callback(packetId C.uint32_t, data *C.uchar, length C.int, mark C.uint32_t, idx uint32, qh *C.struct_nfq_q_handle) { xdata := C.GoBytes(unsafe.Pointer(data), length) var packet gopacket.Packet if xdata[0]&0xf0 == ipv4version { packet = gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{Lazy: true, NoCopy: true}) } else { packet = gopacket.NewPacket(xdata, layers.LayerTypeIPv6, gopacket.DecodeOptions{Lazy: true, NoCopy: true}) } p := NFPacket{ Packet: packet, qh: qh, id: packetId, Mark: uint32(mark), } theTabeLock.RLock() cb, ok := theTable[idx] theTabeLock.RUnlock() if !ok { fmt.Fprintf(os.Stderr, "Dropping, unexpectedly due to bad idx=%d\n", idx) p.SetVerdict(NF_DROP) } // Nonblocking write of packet to queue channel select { case *cb <- p: default: fmt.Fprintf(os.Stderr, "Dropping, unexpectedly due to no recv, idx=%d\n", idx) p.SetVerdict(NF_DROP) } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |