NetworkManager/src/NetworkManagerUtils.c

174 lines
3.6 KiB
C
Raw Normal View History

/* NetworkManager -- Network link manager
*
* Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2004 Red Hat, Inc.
*/
#include <glib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include "NetworkManager.h"
#include "NetworkManagerUtils.h"
extern gboolean debug;
/*
* nm_try_acquire_mutex
*
* Tries to acquire a given mutex, sleeping a bit between tries.
*
* Returns: FALSE if mutex was not acquired
* TRUE if mutex was successfully acquired
*/
gboolean nm_try_acquire_mutex (GMutex *mutex, const char *func)
{
gint i = 5;
g_return_val_if_fail (mutex != NULL, FALSE);
while (i > 0)
{
if (g_mutex_trylock (mutex))
{
/*
if (func)
NM_DEBUG_PRINT_1 ("MUTEX: %s got mutex\n", func);
*/
return (TRUE);
}
usleep (500);
i++;
}
return (FALSE);
}
/*
* nm_unlock_mutex
*
* Simply unlocks a mutex, balances nm_try_acquire_mutex()
*
*/
void nm_unlock_mutex (GMutex *mutex, const char *func)
{
g_return_if_fail (mutex != NULL);
/*
if (func)
NM_DEBUG_PRINT_1 ("MUTEX: %s released mutex\n", func);
*/
g_mutex_unlock (mutex);
}
/*
* nm_null_safe_strcmp
*
* Doesn't freaking segfault if s1/s2 are NULL
*
*/
int nm_null_safe_strcmp (const char *s1, const char *s2)
{
if (!s1 && !s2)
return 0;
if (!s1 && s2)
return -1;
if (s1 && !s2)
return 1;
return (strcmp (s1, s2));
}
/*
* nm_get_network_control_socket
*
* Get a control socket for network operations.
*
*/
int nm_get_network_control_socket (void)
{
int fd;
/* Try to grab a control socket */
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
return (fd);
fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (fd >= 0)
return (fd);
fd = socket(PF_INET6, SOCK_DGRAM, 0);
if (fd >= 0)
return (fd);
NM_DEBUG_PRINT ("nm_get_network_control_socket() could not get network control socket.\n");
return (-1);
}
/*
* nm_ethernet_address_is_valid
*
* Compares an ethernet address against known invalid addresses.
*
*/
gboolean nm_ethernet_address_is_valid (struct ether_addr *test_addr)
{
gboolean valid = FALSE;
struct ether_addr invalid_addr1 = { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} };
struct ether_addr invalid_addr2 = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
struct ether_addr invalid_addr3 = { {0x44, 0x44, 0x44, 0x44, 0x44, 0x44} };
2004-07-15 Dan Williams <dcbw@redhat.com> * src/Makefile.am - Turn on warnings * src/NetworkManager.c - nm_create_device_and_add_to_list(): call nm_device_deactivate() rather that doing the deactivation ourselves - Cancel an pending actions on a device if its being removed - Break up link state checking a bit, make non-active wireless cards deactivated to save power - Remove unused variables * src/NetworkManager.h - Add support for "pending" device * src/NetworkManagerAP.h src/NetworkManagerAP.c - Add support for determining whether and AP has encryption enabled or not - AP address is now "struct ether_addr" rather than a string * src/NetworkManagerDbus.h src/NetworkManagerDbus.c - Add signal NeedKeyForNetwork, method SetKeyForNetwork (testing only) - Changes for AP address from struct ether_addr->string * src/NetworkManagerDevice.h src/NetworkManagerDevice.c - Remove unused variables, fix warnings - Add support for Pending Actions (things that block a device from being "active" until they are completed). - First pending action: Get a WEP key from the user - Add nm_device_is_wire[d|less](), rename nm_device_is_wireless() - Clean up explicit testing of dev->iface_type to use nm_device_is_wireless() - Update wireless link checking to try to determine if the AP we are associated with is correct, but the WEP key we are using is just wrong. If its wrong, trigger the GetUserKey pending action on the device - If dhclient can't get an IP address, it brings the device down. Bring it back up in that case, otherwise we can't scan or link-check on it - Add IP address change notifications at appropriate points (still needs some work) - Add nm_device_need_ap_switch(), checks whether we need to switch access points or not * src/NetworkManagerPolicy.h src/NetworkManagerPolicy.c - Split out "best" access point determiniation into separate function - Make device activation 2-stage: first the device is pending, then in the next iteration through it becomes "active" unless it has pending actions * src/NetworkManagerUtils.h src/NetworkManagerUtils.c - Clean up unused variables and warnings - Wrap our debug macros in {} to prevent possible confusion * src/NetworkManagerWireless.c - Forgot to return current best priority, which lead to last available AP always being chosen no matter what its priority was. Corrected. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@15 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-07-15 16:51:48 +00:00
g_return_val_if_fail (test_addr != NULL, FALSE);
/* Compare the AP address the card has with invalid ethernet MAC addresses. */
if ( (memcmp(test_addr, &invalid_addr1, sizeof(struct ether_addr)) != 0)
&& (memcmp(test_addr, &invalid_addr2, sizeof(struct ether_addr)) != 0)
&& (memcmp(test_addr, &invalid_addr3, sizeof(struct ether_addr)) != 0))
valid = TRUE;
return (valid);
}
/*
* nm_dispose_scan_results
*
* Free memory used by the wireless scan results structure
*
*/
void nm_dispose_scan_results (wireless_scan *result_list)
{
wireless_scan *tmp = result_list;
while (tmp)
{
wireless_scan *tmp2 = tmp;
tmp = tmp->next;
free (tmp2);
}
}