2008-06-26 Dan Williams <dcbw@redhat.com>

Patch from David Cantrell <dcantrell@redhat.com>
	
	* Use inet_ntop() and inet_pton() everwhere and check for errors



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3777 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Dan Williams 2008-06-26 19:33:13 +00:00
parent 518095511e
commit dd0109ee28
17 changed files with 162 additions and 46 deletions

View file

@ -1,3 +1,9 @@
2008-06-26 Dan Williams <dcbw@redhat.com>
Patch from David Cantrell <dcantrell@redhat.com>
* Use inet_ntop() and inet_pton() everwhere and check for errors
2008-06-26 Dan Williams <dcbw@redhat.com>
* Update FSF address in license headers (Michael Biebl <biebl@debian.org>)

View file

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -47,13 +48,19 @@ test_get_state (NMClient *client)
static gchar *
ip4_address_as_string (guint32 ip)
{
char buf[INET_ADDRSTRLEN+1];
struct in_addr tmp_addr;
gchar *ip_string;
memset (&buf, '\0', sizeof (buf));
tmp_addr.s_addr = ip;
ip_string = inet_ntoa (tmp_addr);
return g_strdup (ip_string);
if (inet_ntop (AF_INET, &tmp_addr, buf, INET_ADDRSTRLEN)) {
return g_strdup (buf);
} else {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (tmp_addr.s_addr));
return NULL;
}
}
static void

View file

@ -490,7 +490,9 @@ nm_utils_convert_uint_array_to_string (const GValue *src_value, GValue *dest_val
memset (buf, 0, sizeof (buf));
addr.s_addr = g_array_index (array, guint32, i++);
inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN);
if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
g_string_append_printf (printable, "%u (%s)", addr.s_addr, buf);
}
g_string_append_c (printable, ']');
@ -528,13 +530,17 @@ nm_utils_convert_ip4_addr_struct_array_to_string (const GValue *src_value, GValu
memset (buf, 0, sizeof (buf));
addr.s_addr = g_array_index (array, guint32, 0);
inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN);
if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
g_string_append_printf (printable, "ip = %s", buf);
g_string_append (printable, ", ");
memset (buf, 0, sizeof (buf));
addr.s_addr = g_array_index (array, guint32, 1);
inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN);
if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
g_string_append_printf (printable, "mask = %s", buf);
if (array->len > 2) {
@ -542,7 +548,9 @@ nm_utils_convert_ip4_addr_struct_array_to_string (const GValue *src_value, GValu
memset (buf, 0, sizeof (buf));
addr.s_addr = g_array_index (array, guint32, 2);
inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN);
if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
g_string_append_printf (printable, "gw = %s", buf);
}

View file

@ -214,6 +214,9 @@ gboolean get_autoip (NMDevice *dev, struct in_addr *out_ip)
int nannounce = 0;
gboolean success = FALSE;
const char *iface;
char buf[INET_ADDRSTRLEN+1];
memset(&buf, '\0', sizeof (buf));
g_return_val_if_fail (dev != NULL, FALSE);
g_return_val_if_fail (out_ip != NULL, FALSE);
@ -262,7 +265,12 @@ gboolean get_autoip (NMDevice *dev, struct in_addr *out_ip)
if (nprobes < PROBE_NUM)
{
nm_info ("autoip: Sending probe #%d for IP address %s.", nprobes, inet_ntoa (ip));
if (!inet_ntop (AF_INET, &ip, buf, INET_ADDRSTRLEN)) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (ip.s_addr));
continue;
}
nm_info ("autoip: Sending probe #%d for IP address %s.", nprobes, buf);
arp (fd, &saddr, ARPOP_REQUEST, &addr, null_ip, &null_addr, ip);
nprobes++;
gettimeofday (&timeout, NULL);
@ -284,7 +292,12 @@ gboolean get_autoip (NMDevice *dev, struct in_addr *out_ip)
}
else if (nannounce < ANNOUNCE_NUM)
{
nm_info ("autoip: Sending announce #%d for IP address %s.", nannounce, inet_ntoa (ip));
if (!inet_ntop (AF_INET, &ip, buf, INET_ADDRSTRLEN)) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (ip.s_addr));
continue;
}
nm_info ("autoip: Sending announce #%d for IP address %s.", nannounce, buf);
arp (fd, &saddr, ARPOP_REQUEST, &addr, ip, &addr, ip);
nannounce++;
gettimeofday (&timeout, NULL);
@ -319,10 +332,20 @@ gboolean get_autoip (NMDevice *dev, struct in_addr *out_ip)
{
struct in_addr a;
memcpy (&(a.s_addr), &(p.sInaddr), sizeof (a.s_addr));
nm_warning (" source = %s %02X:%02X:%02X:%02X:%02X:%02X, ", inet_ntoa (a),
if (!inet_ntop (AF_INET, &a, buf, INET_ADDRSTRLEN)) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (a.s_addr));
continue;
}
nm_warning (" source = %s %02X:%02X:%02X:%02X:%02X:%02X, ", buf,
p.sHaddr[0], p.sHaddr[1], p.sHaddr[2], p.sHaddr[3], p.sHaddr[4], p.sHaddr[5]);
memcpy (&(a.s_addr), &(p.tInaddr), sizeof (a.s_addr));
nm_warning (" target = %s %02X:%02X:%02X:%02X:%02X:%02X\n", inet_ntoa (a),
if (!inet_ntop (AF_INET, &a, buf, INET_ADDRSTRLEN)) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (a.s_addr));
continue;
}
nm_warning (" target = %s %02X:%02X:%02X:%02X:%02X:%02X\n", buf,
p.tHaddr[0], p.tHaddr[1], p.tHaddr[2], p.tHaddr[3], p.tHaddr[4], p.tHaddr[5]);
}
#endif
@ -333,7 +356,12 @@ gboolean get_autoip (NMDevice *dev, struct in_addr *out_ip)
&& (memcmp (&addr, &p.tHaddr, ETH_ALEN) != 0))
{
#ifdef ARP_DEBUG
nm_warning ("autoip: (%s) ARP conflict for IP address %s.\n", iface, inet_ntoa(ip));
if (!inet_ntop (AF_INET, &ip, buf, INET_ADDRSTRLEN)) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (ip.s_addr));
continue;
}
nm_warning ("autoip: (%s) ARP conflict for IP address %s.\n", iface, buf);
#endif
/* Ok, start all over again */

View file

@ -106,6 +106,9 @@ void nm_system_activate_nis (NMIP4Config *config)
struct in_addr temp_addr;
int i;
FILE *ypconf = NULL;
char buf[INET_ADDRSTRLEN+1];
memset (&buf, '\0', sizeof (buf));
g_return_if_fail (config != NULL);
@ -145,7 +148,12 @@ void nm_system_activate_nis (NMIP4Config *config)
fprintf (ypconf, "# generated by NetworkManager, do not edit!\n\n");
for (i = 0; i < num_nis_servers; i++) {
temp_addr.s_addr = nm_ip4_config_get_nis_server (config, i);
fprintf (ypconf, "domain %s server %s\n", nis_domain, inet_ntoa (temp_addr));
if (!inet_ntop (AF_INET, &temp_addr, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (tempaddr.s_addr));
else
fprintf (ypconf, "domain %s server %s\n", nis_domain, buf);
}
fprintf (ypconf, "\n");
fclose (ypconf);

View file

@ -889,20 +889,20 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
}
str = g_hash_table_lookup (device->options, "new_ip_address");
if (str && inet_aton (str, &tmp_addr)) {
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
addr->address = tmp_addr.s_addr;
nm_info (" address %s", str);
} else
goto error;
str = g_hash_table_lookup (device->options, "new_subnet_mask");
if (str && inet_aton (str, &tmp_addr)) {
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
addr->netmask = tmp_addr.s_addr;
nm_info (" netmask %s", str);
}
str = g_hash_table_lookup (device->options, "new_routers");
if (str && inet_aton (str, &tmp_addr)) {
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
addr->gateway = tmp_addr.s_addr;
nm_info(" gateway %s", str);
}
@ -922,7 +922,7 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
char **s;
for (s = searches; *s; s++) {
if (inet_aton (*s, &tmp_addr)) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nameserver (ip4_config, tmp_addr.s_addr);
nm_info (" nameserver '%s'", *s);
} else
@ -967,7 +967,7 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
char **s;
for (s = searches; *s; s++) {
if (inet_aton (*s, &tmp_addr)) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nis_server (ip4_config, tmp_addr.s_addr);
nm_info (" nis server '%s'", *s);
} else
@ -987,11 +987,11 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
struct in_addr rt_addr;
struct in_addr rt_route;
if (inet_aton (*s, &rt_addr) == 0) {
if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
nm_warning ("DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_aton (*(s + 1), &rt_route) == 0) {
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
nm_warning ("DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}

View file

@ -108,7 +108,9 @@ compute_nameservers (NMIP4Config *config)
if (!buf)
continue;
inet_ntop (AF_INET, &addr, buf, ADDR_BUF_LEN);
if (!inet_ntop (AF_INET, &addr, buf, ADDR_BUF_LEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
if (i == 3) {
g_string_append (str, "\n# ");

View file

@ -261,9 +261,18 @@ static const char *
ip_address_to_string (guint32 numeric)
{
struct in_addr temp_addr;
char buf[INET_ADDRSTRLEN+1];
memset (&buf, '\0', sizeof (buf));
temp_addr.s_addr = numeric;
return inet_ntoa (temp_addr);
if (inet_ntop (AF_INET, &temp_addr, buf, INET_ADDRSTRLEN)) {
return g_strdup (buf);
} else {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (temp_addr.s_addr));
return NULL;
}
}
static void

View file

@ -137,7 +137,7 @@ get_one_ip4_addr (shvarFile *ifcfg,
if (!value)
return;
if (inet_pton (AF_INET, value, &ip4_addr))
if (inet_pton (AF_INET, value, &ip4_addr) > 0)
*out_addr = ip4_addr.s_addr;
else {
g_set_error (error, ifcfg_plugin_error_quark (), 0,

View file

@ -141,7 +141,7 @@ make_ip4_setting (shvarFile *ifcfg)
pieces = g_strsplit (str, "/", 2);
if (inet_pton (AF_INET, pieces[0], &ip4_addr)) {
if (inet_pton (AF_INET, pieces[0], &ip4_addr) > 0) {
tmp.address = ip4_addr.s_addr;
if (g_strv_length (pieces) == 2)
@ -166,7 +166,7 @@ make_ip4_setting (shvarFile *ifcfg)
if (str) {
struct in_addr mask_addr;
if (inet_pton (AF_INET, str, &mask_addr))
if (inet_pton (AF_INET, str, &mask_addr) > 0)
tmp.netmask = mask_addr.s_addr;
else {
g_warning ("Ignoring invalid IP4 addres: invalid netmask: '%s'", str);

View file

@ -7,6 +7,7 @@
#include <nm-setting.h>
#include <nm-setting-connection.h>
#include <nm-setting-ip4-config.h>
#include <nm-utils.h>
#include <string.h>
#include <arpa/inet.h>
@ -36,7 +37,13 @@ write_array_of_uint (GKeyFile *file,
struct in_addr addr;
addr.s_addr = g_array_index (array, guint32, i);
list[i] = g_strdup (inet_ntop (AF_INET, &addr, buf, sizeof (buf)));
if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
list[i] = NULL;
} else {
list[i] = g_strdup (buf);
}
}
g_key_file_set_string_list (file, setting->name, key, (const char **) list, array->len);
@ -81,14 +88,33 @@ write_array_of_array_of_uint (GKeyFile *file,
char *key_name;
addr.s_addr = g_array_index (tuple, guint32, 0);
list[0] = g_strdup (inet_ntop (AF_INET, &addr, buf, sizeof (buf)));
if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
list[0] = NULL;
} else {
list[0] = g_strdup (buf);
}
addr.s_addr = g_array_index (tuple, guint32, 1);
list[1] = g_strdup (inet_ntop (AF_INET, &addr, buf, sizeof (buf)));
if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
list[1] = NULL;
} else {
list[1] = g_strdup (buf);
}
addr.s_addr = g_array_index (tuple, guint32, 2);
if (addr.s_addr)
list[2] = g_strdup (inet_ntop (AF_INET, &addr, buf, sizeof (buf)));
if (addr.s_addr) {
if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (addr.s_addr));
list[2] = NULL;
} else {
list[2] = g_strdup (buf);
}
}
key_name = g_strdup_printf ("address%d", j + 1);
g_key_file_set_string_list (file, setting->name, key_name, (const char **) list, list[2] ? 3 : 2);

View file

@ -155,6 +155,9 @@ void print_array (DBusConnection *connection, int opt)
const char *name = NULL;
int opt_type = -1;
unsigned int foo;
char buf[INET_ADDRSTRLEN+1];
memset (&buf, '\0', sizeof (buf));
ret = call_nm_method (connection, "getName", opt, FALSE, DBUS_TYPE_STRING, (void *)(&name), NULL);
if (ret != RETURN_SUCCESS)
@ -212,7 +215,11 @@ void print_array (DBusConnection *connection, int opt)
break;
case DBUS_TYPE_UINT32:
in.s_addr = uint32[i];
fprintf (stderr, "%u (%s)%s", uint32[i], inet_ntoa(in), last ? "" : ", ");
if (!inet_ntop (AF_INET, &in, buf, INET_ADDRSTRLEN))
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (in.s_addr));
else
fprintf (stderr, "%u (%s)%s", uint32[i], buf, last ? "" : ", ");
break;
case DBUS_TYPE_STRING:
fprintf (stderr, "'%s'%s", string[i], last ? "" : ", ");

View file

@ -162,12 +162,18 @@ static gchar *
ip4_address_as_string (guint32 ip)
{
struct in_addr tmp_addr;
gchar *ip_string;
char buf[INET_ADDRSTRLEN+1];
memset (&buf, '\0', sizeof (buf));
tmp_addr.s_addr = ip;
ip_string = inet_ntoa (tmp_addr);
return g_strdup (ip_string);
if (inet_ntop (AF_INET, &tmp_addr, buf, INET_ADDRSTRLEN)) {
return g_strdup (buf);
} else {
nm_warning ("%s: error converting IP4 address 0x%X",
__func__, ntohl (tmp_addr.s_addr));
return NULL;
}
}
static const char *

View file

@ -135,7 +135,7 @@ addr_to_gvalue (const char *str)
if (!str || strlen (str) < 1)
return NULL;
if (!inet_aton (str, &temp_addr))
if (inet_pton (AF_INET, str, &temp_addr) <= 0)
return NULL;
val = g_slice_new0 (GValue);
@ -164,7 +164,7 @@ parse_addr_list (GValue *value_array, const char *str)
split = g_strsplit (str, " ", -1);
for (i = 0; split[i]; i++) {
if (inet_aton (split[i], &temp_addr))
if (inet_pton (AF_INET, split[i], &temp_addr) > 0)
g_array_append_val (array, temp_addr.s_addr);
}
@ -203,21 +203,21 @@ get_routes (void)
if (!tmp || strlen (tmp) < 1)
break;
if (inet_aton (tmp, &network) != 0) {
if (inet_pton (AF_INET, tmp, &network) <= 0) {
nm_warning ("Ignoring invalid static route address '%s'", tmp ? tmp : "NULL");
continue;
}
snprintf (buf, BUFLEN, "route_netmask_%d", i);
tmp = getenv (buf);
if (!tmp || inet_aton (tmp, &netmask) != 0) {
if (!tmp || inet_pton (AF_INET, tmp, &netmask) <= 0) {
nm_warning ("Ignoring invalid static route netmask '%s'", tmp ? tmp : "NULL");
continue;
}
snprintf (buf, BUFLEN, "route_gateway_%d", i);
tmp = getenv (buf);
if (!tmp || inet_aton (tmp, &gateway) != 0) {
if (!tmp || inet_pton (AF_INET, tmp, &gateway) <= 0) {
nm_warning ("Ignoring invalid static route gateway '%s'", tmp ? tmp : "NULL");
continue;
}

View file

@ -244,7 +244,8 @@ nm_openvpn_connect_timer_cb (gpointer data)
return FALSE;
serv_addr.sin_family = AF_INET;
inet_aton ("127.0.0.1", &(serv_addr.sin_addr));
if (inet_pton (AF_INET, "127.0.0.1", &(serv_addr.sin_addr)) <= 0)
nm_warning ("%s: could not convert 127.0.0.1", __func__);
serv_addr.sin_port = htons (1194);
connected = (connect (socket_fd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == 0);

View file

@ -442,7 +442,9 @@ static gint nm_ppp_get_cmdline_pptp (NmPPPData *data, char **data_items, const i
int i = 0;
struct hostent *hostinfo = NULL;
char * pppd_pty = NULL;
char buf[INET_ADDRSTRLEN+1];
memset (&buf, '\0', sizeof (buf));
/* Find pptp */
pptp_binary = pptp_binary_paths;
@ -466,7 +468,10 @@ static gint nm_ppp_get_cmdline_pptp (NmPPPData *data, char **data_items, const i
return -1;
}
data -> ip4_vpn_gateway = *(struct in_addr*)(hostinfo->h_addr_list[0]);
data -> str_ip4_vpn_gateway = g_strdup( inet_ntoa( data -> ip4_vpn_gateway ) );
if ( !inet_ntop (AF_INET, &data -> ip4_vpn_gateway, buf, INET_ADDRSTRLEN))
data -> str_ip4_vpn_gateway = NULL;
data -> str_ip4_vpn_gateway = g_strdup( buf );
pppd_pty = g_strdup_printf ("%s %s --nolaunchpppd", (*pptp_binary), data->str_ip4_vpn_gateway);
@ -510,7 +515,10 @@ static gint nm_ppp_get_cmdline_dialup (NmPPPData *data, char **data_items, const
// return -1;
// }
// data -> ip4_vpn_gateway = *(struct in_addr*)(hostinfo->h_addr_list[0]);
// data -> str_ip4_vpn_gateway = g_strdup( inet_ntoa( data -> ip4_vpn_gateway ) );
//
// if ( !inet_ntop (AF_INET, &data -> ip4_vpn_gateway, buf, INET_ADDRSTRLEN))
// data -> str_ip4_gateway = NULL;
// data -> str_ip4_vpn_gateway = g_strdup( buf );
//
// pppd_pty = g_strdup_printf ("%s %s --nolaunchpppd", (*pptp_binary), data->str_ip4_vpn_gateway);
//

View file

@ -141,7 +141,7 @@ addr_to_gvalue (const char *str)
if (!str || strlen (str) < 1)
return NULL;
if (!inet_aton (str, &temp_addr))
if (inet_pton (AF_INET, str, &temp_addr) <= 0)
return NULL;
return uint_to_gvalue (temp_addr.s_addr);
@ -167,7 +167,7 @@ addr_list_to_gvalue (const char *str)
for (i = 0; split[i]; i++) {
struct in_addr addr;
if (inet_aton (split[i], &addr)) {
if (inet_pton (AF_INET, split[i], &addr) > 0) {
g_array_append_val (array, addr.s_addr);
} else {
g_strfreev (split);
@ -215,14 +215,14 @@ get_routes (void)
snprintf (buf, BUFLEN, "CISCO_SPLIT_INC_%d_ADDR", i);
tmp = getenv (buf);
if (!tmp || inet_aton (tmp, &network) != 0) {
if (!tmp || inet_pton (AF_INET, tmp, &network) <= 0) {
nm_warning ("Ignoring invalid static route address '%s'", tmp ? tmp : "NULL");
continue;
}
snprintf (buf, BUFLEN, "CISCO_SPLIT_INC_%d_MASK", i);
tmp = getenv (buf);
if (!tmp || inet_aton (tmp, &netmask) != 0) {
if (!tmp || inet_pton (AF_INET, tmp, &netmask) <= 0) {
nm_warning ("Ignoring invalid static route netmask '%s'", tmp ? tmp : "NULL");
continue;
}