frameworks\base\core\java\android\net\EthernetManager.java
public boolean setEthernetEnabled(boolean enabled) {
Log.d(TAG,enabled ? "turn on Ethernet" : "turn off Ethernet");
try {
return mService.setEthernetEnabled(enabled);
} catch (RemoteException e) {
return false;
}
}
frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetService.java
EthernetServiceImpl.java
public boolean setEthernetEnabled(boolean enable) {
//enforceChangePermission();
Log.i(TAG,"setEthernetEnabled() : enable="+enable);
if ( enable ) {
return mTracker.setInterfaceUp();
} else {
return mTracker.setInterfaceDown();
}
}
EthernetNetworkFactory.java
public boolean setInterfaceUp() {
try {
if(!TextUtils.isEmpty(mIface)) {
mNMService.setInterfaceUp(mIface);
sendEthIfaceStateChangedBroadcast(EthernetManager.ETHER_IFACE_STATE_UP);
return true;
}
else
Log.e(TAG,"mIface is null");
}catch (Exception e) {
Log.e(TAG, "Error downing interface " + mIface + ": " + e);
}
return false;
}
\frameworks\base\services\core\java\com\android\server\NetworkManagementService.java
public void setInterfaceUp(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
final InterfaceConfiguration ifcg = getInterfaceConfig(iface);
ifcg.setInterfaceUp();
setInterfaceConfig(iface, ifcg);
}
public void setInterfaceConfig(String iface, InterfaceConfiguration cfg) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
LinkAddress linkAddr = cfg.getLinkAddress();
if (linkAddr == null || linkAddr.getAddress() == null) {
throw new IllegalStateException("Null LinkAddress given");
}
final Command cmd = new Command("interface", "setcfg", iface,
linkAddr.getAddress().getHostAddress(),
linkAddr.getPrefixLength());
for (String flag : cfg.getFlags()) {
cmd.appendArg(flag);
}
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
system\netd\server\CommandListener.cpp
CommandListener::InterfaceCmd::InterfaceCmd() :
NetdCommand("interface") {
}
int CommandListener::InterfaceCmd::runCommand(SocketClient *cli,
int argc, char **argv) {
else if (!strcmp(argv[1], "setcfg")) {
// arglist: iface [addr prefixLength] flags
if (argc < 4) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
return 0;
}
ALOGD("Setting iface cfg");
struct in_addr addr;
int index = 5;
ifc_init();
if (!inet_aton(argv[3], &addr)) {
// Handle flags only case
index = 3;
} else {
if (ifc_set_addr(argv[2], addr.s_addr)) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to set address", true);
ifc_close();
return 0;
}
// Set prefix length on a non zero address
if (addr.s_addr != 0 && ifc_set_prefixLength(argv[2], atoi(argv[4]))) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to set prefixLength", true);
ifc_close();
return 0;
}
}
/* Process flags */
for (int i = index; i < argc; i++) {
char *flag = argv[i];
if (!strcmp(flag, "up")) {
ALOGD("Trying to bring up %s", argv[2]);
if (ifc_up(argv[2])) {
ALOGE("Error upping interface");
cli->sendMsg(ResponseCode::OperationFailed, "Failed to up interface", true);
ifc_close();
return 0;
}
} else if (!strcmp(flag, "down")) {
ALOGD("Trying to bring down %s", argv[2]);
if (ifc_down(argv[2])) {
ALOGE("Error downing interface");
cli->sendMsg(ResponseCode::OperationFailed, "Failed to down interface", true);
ifc_close();
return 0;
}
} else if (!strcmp(flag, "broadcast")) {
// currently ignored
} else if (!strcmp(flag, "multicast")) {
// currently ignored
} else if (!strcmp(flag, "running")) {
// currently ignored
} else if (!strcmp(flag, "loopback")) {
// currently ignored
} else if (!strcmp(flag, "point-to-point")) {
// currently ignored
} else {
cli->sendMsg(ResponseCode::CommandParameterError, "Flag unsupported", false);
ifc_close();
return 0;
}
}
cli->sendMsg(ResponseCode::CommandOkay, "Interface configuration set", false);
ifc_close();
return 0;
}
return 0;
}
system\core\libnetutils\ifc_utils.c
发送命令到内核;
int ifc_up(const char *name)
{
int ret = ifc_set_flags(name, IFF_UP, 0);
if (DBG) printerr("ifc_up(%s) = %d", name, ret);
return ret;
}
static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
{
struct ifreq ifr;
ifc_init_ifr(name, &ifr);
if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
}