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
20package tun
21
22import (
23	std_errors "errors"
24	"fmt"
25	"net"
26	"strconv"
27
28	"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
29)
30
31var errUnsupported = std_errors.New("operation unsupported on this platform")
32
33func splitIPMask(IPAddressCIDR string) (string, string, error) {
34
35	IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
36	if err != nil {
37		return "", "", errors.Trace(err)
38	}
39
40	var netmask string
41	IPv4Mask := net.IP(IPNet.Mask).To4()
42	if IPv4Mask != nil {
43		netmask = fmt.Sprintf(
44			"%d.%d.%d.%d", IPv4Mask[0], IPv4Mask[1], IPv4Mask[2], IPv4Mask[3])
45	} else {
46		netmask = IPNet.Mask.String()
47	}
48
49	return IP.String(), netmask, nil
50}
51
52func splitIPPrefixLen(IPAddressCIDR string) (string, string, error) {
53
54	IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
55	if err != nil {
56		return "", "", errors.Trace(err)
57	}
58
59	prefixLen, _ := IPNet.Mask.Size()
60
61	return IP.String(), strconv.Itoa(prefixLen), nil
62}
63
64func getMTU(configMTU int) int {
65	if configMTU <= 0 {
66		return DEFAULT_MTU
67	} else if configMTU > 65536 {
68		return 65536
69	}
70	return configMTU
71}
72