utils.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package tun
  20. import (
  21. std_errors "errors"
  22. "fmt"
  23. "net"
  24. "strconv"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  26. )
  27. var errUnsupported = std_errors.New("operation unsupported on this platform")
  28. func splitIPMask(IPAddressCIDR string) (string, string, error) {
  29. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  30. if err != nil {
  31. return "", "", errors.Trace(err)
  32. }
  33. var netmask string
  34. IPv4Mask := net.IP(IPNet.Mask).To4()
  35. if IPv4Mask != nil {
  36. netmask = fmt.Sprintf(
  37. "%d.%d.%d.%d", IPv4Mask[0], IPv4Mask[1], IPv4Mask[2], IPv4Mask[3])
  38. } else {
  39. netmask = IPNet.Mask.String()
  40. }
  41. return IP.String(), netmask, nil
  42. }
  43. func splitIPPrefixLen(IPAddressCIDR string) (string, string, error) {
  44. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  45. if err != nil {
  46. return "", "", errors.Trace(err)
  47. }
  48. prefixLen, _ := IPNet.Mask.Size()
  49. return IP.String(), strconv.Itoa(prefixLen), nil
  50. }
  51. func getMTU(configMTU int) int {
  52. if configMTU <= 0 {
  53. return DEFAULT_MTU
  54. } else if configMTU > 65536 {
  55. return 65536
  56. }
  57. return configMTU
  58. }