blob: e02b51793d7dadf42a04c50a64cbebc74c24ccac [file] [log] [blame]
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07001/*
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 *
Sasha Levitskiy329c3b42012-07-30 16:11:23 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07009 *
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
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070022#include <dlfcn.h>
23
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024#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
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070039char 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();
Sasha Levitskiy87482aa2012-09-11 11:04:29 -070049 ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown");
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070050 } 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 }
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070071}
72
73InterfaceController::~InterfaceController() {
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070074 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 }
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070086}
87
88/*
89 * Arguments:
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070090 * argv[2] - wlan interface
91 * argv[3] - command
92 * argv[4] - argument
93 * rbuf - returned buffer
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070094 */
95int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) {
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070096 int ret = -ENOSYS;
97 if (sendCommand_)
98 ret = sendCommand_(argc, argv, rbuf);
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070099
Sasha Levitskiy329c3b42012-07-30 16:11:23 -0700100 return ret;
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700101}