1c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt/*
2c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Copyright (C) 2012 The Android Open Source Project
3c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt *
4c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
5c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * you may not use this file except in compliance with the License.
6c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * You may obtain a copy of the License at
7c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt *
8c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
9c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt *
10c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
11c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
12c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * See the License for the specific language governing permissions and
14c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * limitations under the License.
15c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt */
16c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt
17aa1be2b3d24d99f3ccb98ff4fbb2a81b63587effDan Albert#include <ctype.h>
18aa1be2b3d24d99f3ccb98ff4fbb2a81b63587effDan Albert#include <errno.h>
1970afde6ad9b5fce63cca594ac9b230d2fc9b21eeLorenzo Colitti#include <fcntl.h>
20ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <netdb.h>
21aa1be2b3d24d99f3ccb98ff4fbb2a81b63587effDan Albert#include <net/if.h>
22ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <netinet/in.h>
23ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <stdlib.h>
248e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include <string.h>
25838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand#include <sys/wait.h>
268e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
27bec6d04e1a84823b89e5581ea8ebcf9f08915216Jeff Sharkey#define LOG_TAG "Netd"
28bec6d04e1a84823b89e5581ea8ebcf9f08915216Jeff Sharkey
298e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include <cutils/log.h>
30838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand#include <logwrap/logwrap.h>
318e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
32c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt#include "NetdConstants.h"
33c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt
34c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
35c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IPTABLES_PATH = "/system/bin/iptables";
360031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char * const IP6TABLES_PATH = "/system/bin/ip6tables";
3789faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitticonst char * const IPTABLES_RESTORE_PATH = "/system/bin/iptables-restore";
3889faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitticonst char * const IP6TABLES_RESTORE_PATH = "/system/bin/ip6tables-restore";
39c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const TC_PATH = "/system/bin/tc";
40c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IP_PATH = "/system/bin/ip";
41c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const ADD = "add";
42c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const DEL = "del";
438e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
44838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchandstatic void logExecError(const char* argv[], int res, int status) {
458e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char** argp = argv;
468e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::string args = "";
478e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    while (*argp) {
488e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        args += *argp;
498e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        args += ' ';
508e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argp++;
518e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
52838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
53838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand}
54838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand
55838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchandstatic int execIptablesCommand(int argc, const char *argv[], bool silent) {
56838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    int res;
57838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    int status;
58838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand
59838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    res = android_fork_execvp(argc, (char **)argv, &status, false,
60838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        !silent);
61838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
62838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        if (!silent) {
63838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand            logExecError(argv, res, status);
64838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        }
65838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        if (res)
66838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand            return res;
67838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        if (!WIFEXITED(status))
68838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand            return ECHILD;
69838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    }
70838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand    return WEXITSTATUS(status);
718e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
728e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
738e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic int execIptables(IptablesTarget target, bool silent, va_list args) {
748e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
758e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::list<const char*> argsList;
768e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    argsList.push_back(NULL);
778e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char* arg;
78390e4ea8106f9e741bc80fb962aaee94d5b28cbbAmith Yamasani
79390e4ea8106f9e741bc80fb962aaee94d5b28cbbAmith Yamasani    // Wait to avoid failure due to another process holding the lock
80390e4ea8106f9e741bc80fb962aaee94d5b28cbbAmith Yamasani    argsList.push_back("-w");
81390e4ea8106f9e741bc80fb962aaee94d5b28cbbAmith Yamasani
828e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    do {
838e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        arg = va_arg(args, const char *);
848e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argsList.push_back(arg);
858e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    } while (arg);
868e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
878e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int i = 0;
888e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char* argv[argsList.size()];
898e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::list<const char*>::iterator it;
908e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    for (it = argsList.begin(); it != argsList.end(); it++, i++) {
918e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[i] = *it;
928e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
938e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
948e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = 0;
958e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    if (target == V4 || target == V4V6) {
968e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[0] = IPTABLES_PATH;
97838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        res |= execIptablesCommand(argsList.size(), argv, silent);
988e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
998e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    if (target == V6 || target == V4V6) {
1008e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[0] = IP6TABLES_PATH;
101838ef6491794900731760aecc1d4a10bb2fdd2ecRom Lemarchand        res |= execIptablesCommand(argsList.size(), argv, silent);
1028e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
1038e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
1048e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
1058e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1068e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptables(IptablesTarget target, ...) {
1078e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_list args;
1088e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_start(args, target);
1098e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = execIptables(target, false, args);
1108e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_end(args);
1118e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
1128e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
1138e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
1148e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptablesSilently(IptablesTarget target, ...) {
1158e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_list args;
1168e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_start(args, target);
1178e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = execIptables(target, true, args);
1188e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_end(args);
1198e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
1208e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
12170afde6ad9b5fce63cca594ac9b230d2fc9b21eeLorenzo Colitti
12289faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colittistatic int execIptablesRestoreCommand(const char *cmd, const std::string& commands) {
12389faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    const char *argv[] = {
12489faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        cmd,
12589faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        "--noflush",  // Don't flush the whole table.
12689faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        "-w",         // Wait instead of failing if the lock is held.
12789faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    };
12889faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    AndroidForkExecvpOption opt[1] = {
12989faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        {
13089faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti            .opt_type = FORK_EXECVP_OPTION_INPUT,
13189faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti            .opt_input.input = reinterpret_cast<const uint8_t*>(commands.c_str()),
13289faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti            .opt_input.input_len = commands.size(),
13389faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        }
13489faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    };
13589faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti
13689faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    int status = 0;
13789faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    int res = android_fork_execvp_ext(
13889faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti            ARRAY_SIZE(argv), (char**)argv, &status, false /* ignore_int_quit */, LOG_NONE,
13989faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti            false /* abbreviated */, NULL /* file_path */, opt, ARRAY_SIZE(opt));
14089faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    if (res || status) {
14189faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        ALOGE("%s failed with res=%d, status=%d", argv[0], res, status);
14289faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        return -1;
14389faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    }
14489faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti
14589faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    return 0;
14689faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti}
14789faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti
14889faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colittiint execIptablesRestore(IptablesTarget target, const std::string& commands) {
14989faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    int res = 0;
15089faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    if (target == V4 || target == V4V6) {
15189faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        res |= execIptablesRestoreCommand(IPTABLES_RESTORE_PATH, commands);
15289faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    }
15389faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    if (target == V6 || target == V4V6) {
15489faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti        res |= execIptablesRestoreCommand(IP6TABLES_RESTORE_PATH, commands);
15589faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    }
15689faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti    return res;
15789faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti}
15889faa349525ad1110b6fa3f2149e6ef825c65662Lorenzo Colitti
15969261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall/*
16069261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall * Check an interface name for plausibility. This should e.g. help against
16169261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall * directory traversal.
16269261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall */
16369261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrallbool isIfaceName(const char *name) {
16469261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    size_t i;
16569261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    size_t name_len = strlen(name);
16669261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    if ((name_len == 0) || (name_len > IFNAMSIZ)) {
16769261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall        return false;
16869261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    }
16969261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall
17069261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    /* First character must be alphanumeric */
17169261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    if (!isalnum(name[0])) {
17269261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall        return false;
17369261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    }
17469261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall
17569261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    for (i = 1; i < name_len; i++) {
17669261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall        if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {
17769261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall            return false;
17869261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall        }
17969261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    }
18069261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall
18169261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall    return true;
18269261cb65186e27dfbdc1e3eec796437f9968ff9JP Abgrall}
183ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
184ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colittiint parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
185ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (!prefix || !family || !address || !prefixlen) {
186ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EFAULT;
187ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
188ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
189ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Find the '/' separating address from prefix length.
190ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    const char *slash = strchr(prefix, '/');
191ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    const char *prefixlenString = slash + 1;
192ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (!slash || !*prefixlenString)
193ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EINVAL;
194ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
195ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Convert the prefix length to a uint8_t.
196ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    char *endptr;
197ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    unsigned templen;
198ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    templen = strtoul(prefixlenString, &endptr, 10);
199ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (*endptr || templen > 255) {
200ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EINVAL;
201ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
202ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    *prefixlen = templen;
203ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
204ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Copy the address part of the prefix to a local buffer. We have to copy
205ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // because inet_pton and getaddrinfo operate on null-terminated address
206ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // strings, but prefix is const and has '/' after the address.
207ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    std::string addressString(prefix, slash - prefix);
208ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
209ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Parse the address.
210ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    addrinfo *res;
211ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    addrinfo hints = {
212ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        .ai_flags = AI_NUMERICHOST,
213ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    };
214ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    int ret = getaddrinfo(addressString.c_str(), NULL, &hints, &res);
215ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (ret || !res) {
216ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EINVAL;  // getaddrinfo return values are not errno values.
217ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
218ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
219ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Convert the address string to raw address bytes.
220ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    void *rawAddress;
221ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    int rawLength;
222ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    switch (res[0].ai_family) {
223ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        case AF_INET: {
224ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            if (*prefixlen > 32) {
225ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti                return -EINVAL;
226ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            }
227ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
228ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            rawAddress = &sin->sin_addr;
229ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            rawLength = 4;
230ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            break;
231ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        }
232ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        case AF_INET6: {
233ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            if (*prefixlen > 128) {
234ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti                return -EINVAL;
235ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            }
236ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
237ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            rawAddress = &sin6->sin6_addr;
238ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            rawLength = 16;
239ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            break;
240ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        }
241ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        default: {
242ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            freeaddrinfo(res);
243ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            return -EAFNOSUPPORT;
244ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        }
245ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
246ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
247ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (rawLength > size) {
248ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        freeaddrinfo(res);
249ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -ENOSPC;
250ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
251ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
252ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    *family = res[0].ai_family;
253ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    memcpy(address, rawAddress, rawLength);
254ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    freeaddrinfo(res);
255ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
256ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    return rawLength;
257ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti}
258