Exemple #1
0
func setRoute(ctl *os.File, dst ip.IP4Net, nextHopIP ip.IP4, nextHopPort int) {
	cmd := C.command{
		cmd:           C.CMD_SET_ROUTE,
		dest_net:      C.in_addr_t(dst.IP.NetworkOrder()),
		dest_net_len:  C.int(dst.PrefixLen),
		next_hop_ip:   C.in_addr_t(nextHopIP.NetworkOrder()),
		next_hop_port: C.short(nextHopPort),
	}

	writeCommand(ctl, &cmd)
}
Exemple #2
0
func removeRoute(ctl *os.File, dst ip.IP4Net) {
	cmd := C.command{
		cmd:          C.CMD_DEL_ROUTE,
		dest_net:     C.in_addr_t(dst.IP.NetworkOrder()),
		dest_net_len: C.int(dst.PrefixLen),
	}

	writeCommand(ctl, &cmd)
}
Exemple #3
0
// Initialize a natpmp_t object.
//
// The gateway is not detected automaticaly and the passed gateway address is
// used unless forcedgw == 0
func NewNATPMPWithGW(forcedgw uint32) (natpmp NATPMP, err error) {
	var force C.int = 1
	if forcedgw == 0 {
		force = 0
	}
	natpmp.ctype = new(C.natpmp_t)
	ret, err := C.initnatpmp(natpmp.ctype, force, C.in_addr_t(forcedgw))
	if err == nil && ret != 0 {
		err = strNATPMPerr(ret)
	}
	return
}
Exemple #4
0
func runCProxy(tun *os.File, conn *os.File, ctl *os.File, tunIP ip.IP4, tunMTU uint) {
	var log_errors int
	if log.V(1) {
		log_errors = 1
	}

	C.run_proxy(
		C.int(tun.Fd()),
		C.int(conn.Fd()),
		C.int(ctl.Fd()),
		C.in_addr_t(tunIP.NetworkOrder()),
		C.size_t(tunMTU),
		C.int(log_errors),
	)
}
Exemple #5
0
func runCProxy(tun *os.File, conn *net.UDPConn, ctl *os.File, tunIP ip.IP4, tunMTU int) {
	var log_errors int
	if log.V(1) {
		log_errors = 1
	}

	c, err := conn.File()
	if err != nil {
		log.Error("Converting UDPConn to File failed: ", err)
		return
	}
	defer c.Close()

	C.run_proxy(
		C.int(tun.Fd()),
		C.int(c.Fd()),
		C.int(ctl.Fd()),
		C.in_addr_t(tunIP.NetworkOrder()),
		C.size_t(tunMTU),
		C.int(log_errors),
	)
}
Exemple #6
0
func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP ip.IP4, tunMTU uint, port int) {
	log.Info("Running fast proxy loop")

	c, err := conn.File()
	if err != nil {
		log.Error("Converting UDPConn to File failed: ", err)
		return
	}
	defer c.Close()

	ctl, peerCtl, err := newCtlSockets()
	if err != nil {
		log.Error("Failed to create control socket: ", err)
		return
	}
	defer ctl.Close()
	defer peerCtl.Close()

	go runCProxy(tun, c, peerCtl, tunIP, tunMTU)

	log.Info("Watching for new subnet leases")
	evts := make(chan subnet.EventBatch)
	sm.Start(evts)

	for evtBatch := range evts {
		for _, evt := range evtBatch {
			if evt.Type == subnet.SubnetAdded {
				log.Info("Subnet added: ", evt.Lease.Network)
				var attrs subnet.BaseAttrs
				if err := json.Unmarshal([]byte(evt.Lease.Data), &attrs); err != nil {
					log.Error("Error decoding subnet lease JSON: ", err)
					continue
				}

				cmd := C.command{
					cmd:           C.CMD_SET_ROUTE,
					dest_net:      C.in_addr_t(evt.Lease.Network.IP.NetworkOrder()),
					dest_net_len:  C.int(evt.Lease.Network.PrefixLen),
					next_hop_ip:   C.in_addr_t(attrs.PublicIP.NetworkOrder()),
					next_hop_port: C.short(port),
				}

				writeCommand(ctl, &cmd)

			} else if evt.Type == subnet.SubnetRemoved {
				log.Info("Subnet removed: ", evt.Lease.Network)

				cmd := C.command{
					cmd:          C.CMD_DEL_ROUTE,
					dest_net:     C.in_addr_t(evt.Lease.Network.IP.NetworkOrder()),
					dest_net_len: C.int(evt.Lease.Network.PrefixLen),
				}

				writeCommand(ctl, &cmd)

			} else {
				log.Error("Internal error: unknown event type: ", int(evt.Type))
			}
		}
	}
}