cproxy_amd64.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !windows
  15. package udp
  16. //#include "proxy_amd64.h"
  17. import "C"
  18. import (
  19. "net"
  20. "os"
  21. "reflect"
  22. "unsafe"
  23. log "github.com/golang/glog"
  24. "github.com/coreos/flannel/pkg/ip"
  25. )
  26. func runCProxy(tun *os.File, conn *net.UDPConn, ctl *os.File, tunIP ip.IP4, tunMTU int) {
  27. var log_errors int
  28. if log.V(1) {
  29. log_errors = 1
  30. }
  31. c, err := conn.File()
  32. if err != nil {
  33. log.Error("Converting UDPConn to File failed: ", err)
  34. return
  35. }
  36. defer c.Close()
  37. C.run_proxy(
  38. C.int(tun.Fd()),
  39. C.int(c.Fd()),
  40. C.int(ctl.Fd()),
  41. C.in_addr_t(tunIP.NetworkOrder()),
  42. C.size_t(tunMTU),
  43. C.int(log_errors),
  44. )
  45. }
  46. func writeCommand(f *os.File, cmd *C.command) {
  47. hdr := reflect.SliceHeader{
  48. Data: uintptr(unsafe.Pointer(cmd)),
  49. Len: int(unsafe.Sizeof(*cmd)),
  50. Cap: int(unsafe.Sizeof(*cmd)),
  51. }
  52. buf := *(*[]byte)(unsafe.Pointer(&hdr))
  53. f.Write(buf)
  54. }
  55. func setRoute(ctl *os.File, dst ip.IP4Net, nextHopIP ip.IP4, nextHopPort int) {
  56. cmd := C.command{
  57. cmd: C.CMD_SET_ROUTE,
  58. dest_net: C.in_addr_t(dst.IP.NetworkOrder()),
  59. dest_net_len: C.int(dst.PrefixLen),
  60. next_hop_ip: C.in_addr_t(nextHopIP.NetworkOrder()),
  61. next_hop_port: C.short(nextHopPort),
  62. }
  63. writeCommand(ctl, &cmd)
  64. }
  65. func removeRoute(ctl *os.File, dst ip.IP4Net) {
  66. cmd := C.command{
  67. cmd: C.CMD_DEL_ROUTE,
  68. dest_net: C.in_addr_t(dst.IP.NetworkOrder()),
  69. dest_net_len: C.int(dst.PrefixLen),
  70. }
  71. writeCommand(ctl, &cmd)
  72. }
  73. func stopProxy(ctl *os.File) {
  74. cmd := C.command{
  75. cmd: C.CMD_STOP,
  76. }
  77. writeCommand(ctl, &cmd)
  78. }