1/*
2 * Copyright (C) 2012 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 <stdlib.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <string.h>
21
22#include <dlfcn.h>
23
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/ioctl.h>
27#include <sys/types.h>
28
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#define LOG_TAG "InterfaceController"
33#include <cutils/log.h>
34#include <netutils/ifc.h>
35#include <private/android_filesystem_config.h>
36
37#include "InterfaceController.h"
38
39char if_cmd_lib_file_name[] = "/system/lib/libnetcmdiface.so";
40char set_cmd_func_name[] = "net_iface_send_command";
41char set_cmd_init_func_name[] = "net_iface_send_command_init";
42char set_cmd_fini_func_name[] = "net_iface_send_command_fini";
43
44InterfaceController::InterfaceController()
45	: sendCommand_(NULL) {
46	libh_ = dlopen(if_cmd_lib_file_name, RTLD_NOW | RTLD_LOCAL);
47	if (libh_ == NULL) {
48		const char *err_str = dlerror();
49		ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown");
50	} else {
51		sendCommandInit_ = (int (*)(void))dlsym(libh_, set_cmd_init_func_name);
52		if (sendCommandInit_ == NULL) {
53			const char *err_str = dlerror();
54			ALOGW("Error (%s) while searching for the interface command init function", err_str ? err_str : "unknown");
55		} else if (sendCommandInit_()) {
56			ALOGE("Can't init the interface command API");
57			return;
58		}
59		sendCommandFini_ = (int (*)(void))dlsym(libh_, set_cmd_fini_func_name);
60		if (sendCommandFini_ == NULL) {
61			const char *err_str = dlerror();
62			ALOGW("Error (%s) while searching for the interface command fini function", err_str ? err_str : "unknown");
63		}
64		sendCommand_ = (int (*)(int, char **, char **))dlsym(libh_, set_cmd_func_name);
65		if (sendCommand_ == NULL) {
66			const char *err_str = dlerror();
67			ALOGE("Error (%s) while searching for the interface command function", err_str ? err_str : "unknown");
68			return;
69		}
70	}
71}
72
73InterfaceController::~InterfaceController() {
74	if (sendCommandFini_) {
75		if (sendCommandFini_()) {
76			ALOGE("Can't shutdown the interface command API");
77		}
78	}
79	if (libh_) {
80		int err = dlclose(libh_);
81		if (err) {
82			const char *err_str = dlerror();
83			ALOGE("Error (%s) while closing the net interface command library", err_str ? err_str : "unknown");
84		}
85	}
86}
87
88/*
89 * Arguments:
90 *	  argv[2] - wlan interface
91 *	  argv[3] - command
92 *	  argv[4] - argument
93 *	  rbuf	- returned buffer
94 */
95int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) {
96	int ret = -ENOSYS;
97	if (sendCommand_)
98		ret = sendCommand_(argc, argv, rbuf);
99
100	return ret;
101}
102