net_server: Support joining networks by address.

* Until now, only the SSID name was being used to select a certain network.
* Now, you can also specify an address.
* Unfortunately, I could not test these changes, but they are part of ticket
  #10001.
This commit is contained in:
Axel Dörfler
2013-09-27 01:02:00 +02:00
parent a3c0790695
commit e3beee6b66
2 changed files with 37 additions and 19 deletions
+30 -17
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006-2012, Haiku, Inc. All Rights Reserved. * Copyright 2006-2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -90,6 +90,7 @@ private:
status_t _AutoJoinNetwork(const BMessage& message); status_t _AutoJoinNetwork(const BMessage& message);
status_t _JoinNetwork(const BMessage& message, status_t _JoinNetwork(const BMessage& message,
const BNetworkAddress* address = NULL,
const char* name = NULL); const char* name = NULL);
status_t _LeaveNetwork(const BMessage& message); status_t _LeaveNetwork(const BMessage& message);
@@ -661,12 +662,20 @@ NetServer::_ConfigureInterface(BMessage& message)
} }
} }
const char* networkName; // Join the specified networks
if (message.FindString("network", &networkName) == B_OK) { BMessage networkMessage;
// We want to join a specific network. for (int32 index = 0; message.FindMessage("network", index,
&networkMessage) == B_OK; index++) {
const char* networkName = message.GetString("name", NULL);
const char* addressString = message.GetString("mac", NULL);
BNetworkAddress address;
status_t addressStatus = address.SetTo(AF_LINK, addressString);
BNetworkDevice device(name); BNetworkDevice device(name);
if (device.IsWireless() && !device.HasLink()) { if (device.IsWireless() && !device.HasLink()) {
status_t status = _JoinNetwork(message, networkName); status_t status = _JoinNetwork(message,
addressStatus == B_OK ? &address : NULL, networkName);
if (status != B_OK) { if (status != B_OK) {
fprintf(stderr, "%s: joining network \"%s\" failed: %s\n", fprintf(stderr, "%s: joining network \"%s\" failed: %s\n",
interface.Name(), networkName, strerror(status)); interface.Name(), networkName, strerror(status));
@@ -1008,7 +1017,7 @@ NetServer::_AutoJoinNetwork(const BMessage& message)
const char* networkName; const char* networkName;
BNetworkAddress link; BNetworkAddress link;
const char* mac; const char* mac = NULL;
if (networkMessage.FindString("mac", &mac) == B_OK) { if (networkMessage.FindString("mac", &mac) == B_OK) {
link.SetTo(AF_LINK, mac); link.SetTo(AF_LINK, mac);
status = device.GetNetwork(link, network); status = device.GetNetwork(link, network);
@@ -1016,7 +1025,8 @@ NetServer::_AutoJoinNetwork(const BMessage& message)
status = device.GetNetwork(networkName, network); status = device.GetNetwork(networkName, network);
if (status == B_OK) { if (status == B_OK) {
status = _JoinNetwork(message, network.name); status = _JoinNetwork(message, mac != NULL ? &link : NULL,
network.name);
printf("auto join network \"%s\": %s\n", network.name, printf("auto join network \"%s\": %s\n", network.name,
strerror(status)); strerror(status));
if (status == B_OK) if (status == B_OK)
@@ -1029,20 +1039,23 @@ NetServer::_AutoJoinNetwork(const BMessage& message)
status_t status_t
NetServer::_JoinNetwork(const BMessage& message, const char* name) NetServer::_JoinNetwork(const BMessage& message, const BNetworkAddress* address,
const char* name)
{ {
const char* deviceName; const char* deviceName;
if (message.FindString("device", &deviceName) != B_OK) if (message.FindString("device", &deviceName) != B_OK)
return B_BAD_VALUE; return B_BAD_VALUE;
BNetworkAddress address; BNetworkAddress deviceAddress;
message.FindFlat("address", &address); message.FindFlat("address", &deviceAddress);
if (address == NULL)
address = &deviceAddress;
if (name == NULL) if (name == NULL)
message.FindString("name", &name); message.FindString("name", &name);
if (name == NULL) { if (name == NULL) {
// No name specified, we need a network address // No name specified, we need a network address
if (address.Family() != AF_LINK) if (address->Family() != AF_LINK)
return B_BAD_VALUE; return B_BAD_VALUE;
} }
@@ -1054,7 +1067,7 @@ NetServer::_JoinNetwork(const BMessage& message, const char* name)
while (fSettings.GetNextNetwork(cookie, networkMessage) == B_OK) { while (fSettings.GetNextNetwork(cookie, networkMessage) == B_OK) {
const char* networkName; const char* networkName;
if (networkMessage.FindString("name", &networkName) == B_OK if (networkMessage.FindString("name", &networkName) == B_OK
&& name != NULL && address.Family() != AF_LINK && name != NULL && address->Family() != AF_LINK
&& !strcmp(name, networkName)) { && !strcmp(name, networkName)) {
found = true; found = true;
break; break;
@@ -1062,9 +1075,9 @@ NetServer::_JoinNetwork(const BMessage& message, const char* name)
const char* mac; const char* mac;
if (networkMessage.FindString("mac", &mac) == B_OK if (networkMessage.FindString("mac", &mac) == B_OK
&& address.Family() == AF_LINK) { && address->Family() == AF_LINK) {
BNetworkAddress link(AF_LINK, mac); BNetworkAddress link(AF_LINK, mac);
if (link == address) { if (link == *address) {
found = true; found = true;
break; break;
} }
@@ -1080,13 +1093,13 @@ NetServer::_JoinNetwork(const BMessage& message, const char* name)
wireless_network network; wireless_network network;
bool askForConfig = false; bool askForConfig = false;
if ((address.Family() != AF_LINK if ((address->Family() != AF_LINK
|| device.GetNetwork(address, network) != B_OK) || device.GetNetwork(*address, network) != B_OK)
&& device.GetNetwork(name, network) != B_OK) { && device.GetNetwork(name, network) != B_OK) {
// We did not find a network - just ignore that, and continue // We did not find a network - just ignore that, and continue
// with some defaults // with some defaults
strlcpy(network.name, name, sizeof(network.name)); strlcpy(network.name, name, sizeof(network.name));
network.address = address; network.address = *address;
network.authentication_mode = B_NETWORK_AUTHENTICATION_NONE; network.authentication_mode = B_NETWORK_AUTHENTICATION_NONE;
network.cipher = 0; network.cipher = 0;
network.group_cipher = 0; network.group_cipher = 0;
+7 -2
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006-2010, Haiku, Inc. All Rights Reserved. * Copyright 2006-2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -42,11 +42,16 @@ const static settings_template kInterfaceAddressTemplate[] = {
{0, NULL, NULL} {0, NULL, NULL}
}; };
const static settings_template kInterfaceNetworkTemplate[] = {
{B_STRING_TYPE, "name", NULL, true},
{B_STRING_TYPE, "mac", NULL},
};
const static settings_template kInterfaceTemplate[] = { const static settings_template kInterfaceTemplate[] = {
{B_STRING_TYPE, "device", NULL, true}, {B_STRING_TYPE, "device", NULL, true},
{B_BOOL_TYPE, "disabled", NULL}, {B_BOOL_TYPE, "disabled", NULL},
{B_MESSAGE_TYPE, "address", kInterfaceAddressTemplate}, {B_MESSAGE_TYPE, "address", kInterfaceAddressTemplate},
{B_STRING_TYPE, "network", NULL}, {B_MESSAGE_TYPE, "network", kInterfaceNetworkTemplate},
{B_INT32_TYPE, "flags", NULL}, {B_INT32_TYPE, "flags", NULL},
{B_INT32_TYPE, "metric", NULL}, {B_INT32_TYPE, "metric", NULL},
{B_INT32_TYPE, "mtu", NULL}, {B_INT32_TYPE, "mtu", NULL},