* Simplified get_address_family() function.

* Use new BNetworkInterface::AutoConfigure() method.
* Automatic whitespace cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39594 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2010-11-23 21:05:32 +00:00
parent 2bd61a6f33
commit 01308f219b

View File

@ -167,21 +167,19 @@ media_parse_subtype(const char* string, int media, int* type)
} }
static bool int
get_address_family(const char* argument, int& family) get_address_family(const char* argument)
{ {
for (int32 i = 0; kFamilies[i].family >= 0; i++) { for (int32 i = 0; kFamilies[i].family >= 0; i++) {
for (int32 j = 0; kFamilies[i].identifiers[j]; j++) { for (int32 j = 0; kFamilies[i].identifiers[j]; j++) {
if (!strcmp(argument, kFamilies[i].identifiers[j])) { if (!strcmp(argument, kFamilies[i].identifiers[j])) {
// found a match // found a match
family = kFamilies[i].family; return kFamilies[i].family;
return true;
} }
} }
} }
family = AF_UNSPEC; return AF_UNSPEC;
return false;
} }
@ -216,7 +214,7 @@ parse_address(int& family, const char* argument, BNetworkAddress& address)
if (family == AF_UNSPEC) { if (family == AF_UNSPEC) {
// Test if we support the resulting address family // Test if we support the resulting address family
bool supported = false; bool supported = false;
for (int32 i = 0; kFamilies[i].family >= 0; i++) { for (int32 i = 0; kFamilies[i].family >= 0; i++) {
if (kFamilies[i].family == address.Family()) { if (kFamilies[i].family == address.Family()) {
supported = true; supported = true;
@ -440,8 +438,8 @@ delete_interface(const char* name, char* const* args, int32 argCount)
BNetworkInterface interface(name); BNetworkInterface interface(name);
for (int32 i = 0; i < argCount; i++) { for (int32 i = 0; i < argCount; i++) {
int family; int family = get_address_family(args[i]);
if (get_address_family(args[i], family)) if (family != AF_UNSPEC)
i++; i++;
BNetworkAddress address; BNetworkAddress address;
@ -461,7 +459,7 @@ delete_interface(const char* name, char* const* args, int32 argCount)
if (argCount == 0) { if (argCount == 0) {
// Delete interface // Delete interface
BNetworkRoster& roster = BNetworkRoster::Default(); BNetworkRoster& roster = BNetworkRoster::Default();
status_t status = roster.RemoveInterface(interface); status_t status = roster.RemoveInterface(interface);
if (status != B_OK) { if (status != B_OK) {
fprintf(stderr, "%s: Could not delete interface %s: %s\n", fprintf(stderr, "%s: Could not delete interface %s: %s\n",
@ -477,16 +475,16 @@ configure_interface(const char* name, char* const* args,
{ {
// try to parse address family // try to parse address family
int family;
int32 i = 0; int32 i = 0;
if (get_address_family(args[i], family)) int family = get_address_family(args[i]);
if (family != AF_UNSPEC)
i++; i++;
// try to parse address // try to parse address
BNetworkAddress address; BNetworkAddress address;
BNetworkAddress mask; BNetworkAddress mask;
if (parse_address(family, args[i], address)) { if (parse_address(family, args[i], address)) {
i++; i++;
@ -498,7 +496,7 @@ configure_interface(const char* name, char* const* args,
if (!interface.Exists()) { if (!interface.Exists()) {
// the interface does not exist yet, we have to add it first // the interface does not exist yet, we have to add it first
BNetworkRoster& roster = BNetworkRoster::Default(); BNetworkRoster& roster = BNetworkRoster::Default();
status_t status = roster.AddInterface(interface); status_t status = roster.AddInterface(interface);
if (status != B_OK) { if (status != B_OK) {
fprintf(stderr, "%s: Could not add interface: %s\n", kProgramName, fprintf(stderr, "%s: Could not add interface: %s\n", kProgramName,
@ -702,28 +700,13 @@ configure_interface(const char* name, char* const* args,
// start auto configuration, if asked for // start auto configuration, if asked for
if (doAutoConfig) { if (doAutoConfig) {
BMessage message(kMsgConfigureInterface); status_t status = interface.AutoConfigure(family);
message.AddString("device", name); if (status == B_BAD_PORT_ID) {
BMessage address;
address.AddString("family", address_family_for(family)->name);
address.AddBool("auto_config", true);
message.AddMessage("address", &address);
BMessenger networkServer(kNetServerSignature);
if (networkServer.IsValid()) {
BMessage reply;
status_t status = networkServer.SendMessage(&message, &reply);
if (status != B_OK) {
fprintf(stderr, "%s: Sending auto-config message failed: %s\n",
kProgramName, strerror(status));
} else if (reply.FindInt32("status", &status) == B_OK
&& status != B_OK) {
fprintf(stderr, "%s: Auto-configuring failed: %s\n",
kProgramName, strerror(status));
}
} else {
fprintf(stderr, "%s: The net_server needs to run for the auto " fprintf(stderr, "%s: The net_server needs to run for the auto "
"configuration!\n", kProgramName); "configuration!\n", kProgramName);
} else if (status != B_OK) {
fprintf(stderr, "%s: Auto-configuring failed: %s\n", kProgramName,
strerror(status));
} }
} }
} }