blob: 93350217146e92362f78f1710a1dde87b9b4e2c5 [file] [log] [blame]
Tomasz Wasilczyka5c83a52020-06-16 15:58:19 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ifreqs.h"
18
19#include "common.h"
20
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23
24namespace android::netdevice::ifreqs {
25
26bool send(unsigned long request, struct ifreq& ifr) {
27 base::unique_fd sock(socket(socketparams::current.domain, socketparams::current.type,
28 socketparams::current.protocol));
29 if (!sock.ok()) {
30 LOG(ERROR) << "Can't create socket";
31 return false;
32 }
33
34 if (ioctl(sock.get(), request, &ifr) < 0) {
35 PLOG(ERROR) << "ioctl(" << std::hex << request << std::dec << ") failed";
36 return false;
37 }
38
39 return true;
40}
41
42struct ifreq fromName(const std::string& ifname) {
43 struct ifreq ifr = {};
44 strlcpy(ifr.ifr_name, ifname.c_str(), IF_NAMESIZE);
45 return ifr;
46}
47
48} // namespace android::netdevice::ifreqs