cproxy.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package udp
  2. //#include "proxy.h"
  3. import "C"
  4. import (
  5. "net"
  6. "os"
  7. "reflect"
  8. "unsafe"
  9. log "github.com/coreos/rudder/Godeps/_workspace/src/github.com/golang/glog"
  10. "github.com/coreos/rudder/pkg/ip"
  11. )
  12. func runCProxy(tun *os.File, conn *net.UDPConn, ctl *os.File, tunIP ip.IP4, tunMTU int) {
  13. var log_errors int
  14. if log.V(1) {
  15. log_errors = 1
  16. }
  17. c, err := conn.File()
  18. if err != nil {
  19. log.Error("Converting UDPConn to File failed: ", err)
  20. return
  21. }
  22. defer c.Close()
  23. C.run_proxy(
  24. C.int(tun.Fd()),
  25. C.int(c.Fd()),
  26. C.int(ctl.Fd()),
  27. C.in_addr_t(tunIP.NetworkOrder()),
  28. C.size_t(tunMTU),
  29. C.int(log_errors),
  30. )
  31. }
  32. func writeCommand(f *os.File, cmd *C.command) {
  33. hdr := reflect.SliceHeader{
  34. Data: uintptr(unsafe.Pointer(cmd)),
  35. Len: int(unsafe.Sizeof(*cmd)),
  36. Cap: int(unsafe.Sizeof(*cmd)),
  37. }
  38. buf := *(*[]byte)(unsafe.Pointer(&hdr))
  39. f.Write(buf)
  40. }
  41. func setRoute(ctl *os.File, dst ip.IP4Net, nextHopIP ip.IP4, nextHopPort int) {
  42. cmd := C.command{
  43. cmd: C.CMD_SET_ROUTE,
  44. dest_net: C.in_addr_t(dst.IP.NetworkOrder()),
  45. dest_net_len: C.int(dst.PrefixLen),
  46. next_hop_ip: C.in_addr_t(nextHopIP.NetworkOrder()),
  47. next_hop_port: C.short(nextHopPort),
  48. }
  49. writeCommand(ctl, &cmd)
  50. }
  51. func removeRoute(ctl *os.File, dst ip.IP4Net) {
  52. cmd := C.command{
  53. cmd: C.CMD_DEL_ROUTE,
  54. dest_net: C.in_addr_t(dst.IP.NetworkOrder()),
  55. dest_net_len: C.int(dst.PrefixLen),
  56. }
  57. writeCommand(ctl, &cmd)
  58. }
  59. func stopProxy(ctl *os.File) {
  60. cmd := C.command{
  61. cmd: C.CMD_STOP,
  62. }
  63. writeCommand(ctl, &cmd)
  64. }