Commit 8a6e2b65 authored by Dan Williams's avatar Dan Williams
Browse files

ifnet: constify tons of stuff

Use 'const char *' where appropriate.
parent 090329d0
......@@ -24,20 +24,23 @@
#include <nm-connection.h>
#include "net_parser.h"
NMConnection *ifnet_update_connection_from_config_block (gchar * conn_name,
GError ** error);
NMConnection *ifnet_update_connection_from_config_block (const char *conn_name,
GError **error);
/* nm_conn_name is used to update nm_ifnet_connection's priv data */
gboolean ifnet_update_parsers_by_connection (NMConnection * connection,
gchar * conn_name,
gchar ** nm_conn_name,
gchar * config_file,
gchar * wpa_file, GError ** error);
gboolean ifnet_update_parsers_by_connection (NMConnection *connection,
const char *conn_name,
const char *config_file,
const char *wpa_file,
const char **out_new_name,
GError **error);
gboolean ifnet_delete_connection_in_parsers (gchar * conn_name,
gchar * config_file,
gchar * wpa_file);
gboolean ifnet_add_new_connection (NMConnection * connection,
gchar * config_file, gchar * wpa_file,
GError ** error);
gboolean ifnet_delete_connection_in_parsers (const char *conn_name,
const char *config_file,
const char *wpa_file);
gboolean ifnet_add_new_connection (NMConnection *connection,
const char *config_file,
const char *wpa_file,
GError ** error);
#endif
......@@ -58,24 +58,24 @@ add_new_connection_config (const gchar * type, const gchar * name)
}
gboolean
ifnet_add_connection (gchar * name, gchar * type)
ifnet_add_connection (const char *name, const char *type)
{
if (add_new_connection_config (type, name)) {
PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Adding network for %s", name);
net_parser_data_changed = TRUE;
return TRUE;
} else
return FALSE;
}
return FALSE;
}
gboolean
ifnet_has_connection (gchar * conn_name)
ifnet_has_connection (const char *conn_name)
{
return g_hash_table_lookup (conn_table, conn_name) != NULL;
}
static GHashTable *
get_connection_config (gchar * name)
get_connection_config (const char *name)
{
return g_hash_table_lookup (conn_table, name);
}
......@@ -87,7 +87,7 @@ static gchar *ignore_name[] = {
};
static gboolean
ignore_connection_name (gchar * name)
ignore_connection_name (const char *name)
{
gboolean result = FALSE;
guint i = 0;
......@@ -194,8 +194,8 @@ destroy_connection_config (GHashTable * conn)
}
// read settings from /etc/NetworkManager/nm-system-settings.conf
gchar *
ifnet_get_global_setting (gchar * group, gchar * key)
const char *
ifnet_get_global_setting (const char *group, const char *key)
{
GError *error = NULL;
GKeyFile *keyfile = g_key_file_new ();
......@@ -362,8 +362,8 @@ ifnet_init (gchar * config_file)
return TRUE;
}
gchar *
ifnet_get_data (gchar * conn_name, const gchar * key)
const char *
ifnet_get_data (const char *conn_name, const char *key)
{
GHashTable *conn = g_hash_table_lookup (conn_table, conn_name);
......@@ -373,7 +373,7 @@ ifnet_get_data (gchar * conn_name, const gchar * key)
}
void
ifnet_set_data (gchar * conn_name, gchar * key, gchar * value)
ifnet_set_data (const char *conn_name, const char *key, const char *value)
{
gpointer orin_key = NULL, orin_value = NULL;
GHashTable *conn = g_hash_table_lookup (conn_table, conn_name);
......@@ -396,17 +396,10 @@ ifnet_set_data (gchar * conn_name, gchar * key, gchar * value)
}
// Remember to free return value
gchar *
const char *
ifnet_get_global_data (const gchar * key)
{
gchar *result = g_hash_table_lookup (global_settings_table, key);
if (result)
result = g_strdup (result);
else
return NULL;
strip_string (result, '"');
return result;
return g_hash_table_lookup (global_settings_table, key);
}
// Return names of legal connections
......@@ -455,7 +448,7 @@ format_ips (gchar * value, gchar ** out_line, gchar * key, gchar * name)
}
gboolean
ifnet_flush_to_file (gchar * config_file)
ifnet_flush_to_file (const char *config_file)
{
GIOChannel *channel;
GError **error = NULL;
......@@ -584,7 +577,7 @@ ifnet_flush_to_file (gchar * config_file)
}
gboolean
ifnet_delete_network (gchar * conn_name)
ifnet_delete_network (const char *conn_name)
{
GHashTable *network = NULL;
......
......@@ -33,14 +33,14 @@ void ifnet_destroy (void);
/* Reader functions */
GList *ifnet_get_connection_names (void);
gchar *ifnet_get_data (gchar * conn_name, const gchar * key);
gchar *ifnet_get_global_data (const gchar * key);
gchar *ifnet_get_global_setting (gchar * group, gchar * key);
gboolean ifnet_has_connection (gchar * conn_name);
const char *ifnet_get_data (const char *conn_name, const char *key);
const char *ifnet_get_global_data (const char *key);
const char *ifnet_get_global_setting (const char *group, const char *key);
gboolean ifnet_has_connection (const char *conn_name);
/* Writer functions */
gboolean ifnet_flush_to_file (gchar * config_file);
void ifnet_set_data (gchar * conn_name, gchar * key, gchar * value);
gboolean ifnet_add_connection (gchar * name, gchar * type);
gboolean ifnet_delete_network (gchar * conn_name);
gboolean ifnet_flush_to_file (const char *config_file);
void ifnet_set_data (const char *conn_name, const char *key, const char *value);
gboolean ifnet_add_connection (const char *name, const char *type);
gboolean ifnet_delete_network (const char *conn_name);
#endif
......@@ -60,40 +60,34 @@ strip_string (gchar * str, gchar t)
}
gboolean
is_hex (gchar * value)
is_hex (const char *value)
{
gchar *p;
const char *p = value;
if (!value)
if (!p)
return FALSE;
p = value;
while (*p) {
if (!isxdigit (*p)) {
if (!isxdigit (*p++))
return FALSE;
}
p++;
}
return TRUE;
}
gboolean
is_ascii (gchar * value)
is_ascii (const char *value)
{
gchar *p;
const char *p = value;
p = value;
while (*p) {
if (!isascii (*p)) {
if (!isascii (*p++))
return FALSE;
}
p++;
}
return TRUE;
}
gboolean
is_true (char *str)
is_true (const char *str)
{
if (!g_ascii_strcasecmp (str, "yes")
|| !g_ascii_strcasecmp (str, "true"))
......@@ -195,7 +189,7 @@ ifnet_plugin_error_quark (void)
}
gboolean
reload_parsers ()
reload_parsers (void)
{
ifnet_destroy ();
wpa_parser_destroy ();
......@@ -206,7 +200,7 @@ reload_parsers ()
}
gchar *
read_hostname (gchar * path)
read_hostname (const char *path)
{
gchar *contents = NULL, *result = NULL, *tmp;
gchar **all_lines = NULL;
......@@ -235,21 +229,23 @@ read_hostname (gchar * path)
}
gboolean
write_hostname (const gchar * hostname, gchar * path)
write_hostname (const gchar *hostname, const char *path)
{
gchar *contents = g_strdup_printf ("#Generated by NetworkManager\n"
"hostname=\"%s\"\n", hostname);
gboolean result = g_file_set_contents (path, contents, -1, NULL);
gboolean result;
char *contents;
contents = g_strdup_printf ("#Generated by NetworkManager\n"
"hostname=\"%s\"\n", hostname);
result = g_file_set_contents (path, contents, -1, NULL);
g_free (contents);
return result;
}
gboolean
is_static_ip4 (gchar * conn_name)
is_static_ip4 (const char *conn_name)
{
gchar *data = ifnet_get_data (conn_name, "config");
gchar *dhcp6;
const char *data = ifnet_get_data (conn_name, "config");
const char *dhcp6;
if (!data)
return FALSE;
......@@ -270,9 +266,9 @@ is_static_ip4 (gchar * conn_name)
}
gboolean
is_static_ip6 (gchar * conn_name)
is_static_ip6 (const char *conn_name)
{
gchar *data = ifnet_get_data (conn_name, "config");
const char *data = ifnet_get_data (conn_name, "config");
if (!data)
return TRUE;
......@@ -280,9 +276,9 @@ is_static_ip6 (gchar * conn_name)
}
gboolean
is_ip4_address (gchar * in_address)
is_ip4_address (const char *in_address)
{
gchar *pattern =
const char *pattern =
"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.((\\{\\d{1,3}\\.\\.\\d{1,3}\\})|\\d{1,3})$";
gchar *address = g_strdup (in_address);
gboolean result = FALSE;
......@@ -308,17 +304,15 @@ is_ip4_address (gchar * in_address)
}
gboolean
is_ip6_address (gchar * in_address)
is_ip6_address (const char *in_address)
{
struct in6_addr tmp_ip6_addr;
gchar *tmp;
gchar *address = g_strdup (in_address);
gchar *tmp, *address;
gboolean result = FALSE;
if (!address) {
g_free (address);
if (!in_address)
return FALSE;
}
address = g_strdup (in_address);
g_strstrip (address);
if ((tmp = strchr (address, '/')) != NULL)
*tmp = '\0';
......@@ -330,7 +324,7 @@ is_ip6_address (gchar * in_address)
}
gboolean
has_ip6_address (gchar * conn_name)
has_ip6_address (const char *conn_name)
{
gchar **ipset;
guint length;
......@@ -353,30 +347,29 @@ has_ip6_address (gchar * conn_name)
}
gboolean
has_default_route (gchar * conn_name, gboolean (*check_fn) (gchar *))
has_default_route (const char *conn_name, gboolean (*check_fn) (const char *))
{
gchar *routes = NULL, *tmp, *end;
char *routes = NULL, *end, *tmp;
gboolean success = FALSE;
g_return_val_if_fail (conn_name != NULL, FALSE);
tmp = ifnet_get_data (conn_name, "routes");
if (!tmp)
routes = g_strdup (ifnet_get_data (conn_name, "routes"));
if (!routes)
return FALSE;
routes = g_strdup (tmp);
tmp = strstr (routes, "default via ");
if (!tmp) {
goto error;
}
tmp += strlen ("default via ");
g_strstrip (tmp);
if ((end = strstr (tmp, "\"")) != NULL)
*end = '\0';
if (check_fn (tmp)) {
g_free (routes);
return TRUE;
if (tmp) {
tmp += strlen ("default via ");
g_strstrip (tmp);
if ((end = strstr (tmp, "\"")) != NULL)
*end = '\0';
if (check_fn (tmp))
success = TRUE;
}
error:
g_free (routes);
return FALSE;
return success;
}
static ip_block *
......@@ -529,28 +522,28 @@ get_ip6_next_hop (gchar * next_hop)
}
ip_block *
convert_ip4_config_block (gchar * conn_name)
convert_ip4_config_block (const char *conn_name)
{
gchar **ipset;
guint length;
guint i;
gchar *ip;
guint32 def_gateway;
gchar *routes;
guint32 def_gateway = 0;
const char *routes;
gchar *pos;
ip_block *start = NULL, *current = NULL, *iblock = NULL;
gchar *pattern =
const char *pattern =
"((\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.)\\{(\\d{1,3})\\.\\.(\\d{1,3})\\}(/\\d{1,2}))";
GRegex *regex = g_regex_new (pattern, 0, 0, NULL);
g_return_val_if_fail (conn_name != NULL, NULL);
ipset = g_strsplit (ifnet_get_data (conn_name, "config"), "\" \"", 0);
length = g_strv_length (ipset);
routes = ifnet_get_data (conn_name, "routes");
if (routes)
def_gateway = get_ip4_gateway (strstr (routes, "default"));
else
def_gateway = 0;
for (i = 0; i < length; i++) {
ip = ipset[i];
ip = strip_string (ip, '"');
......@@ -559,9 +552,13 @@ convert_ip4_config_block (gchar * conn_name)
gchar *ip_start, *ip_prefix;
gchar *begin_str, *end_str;
int begin, end, j;
GRegex *regex;
GMatchInfo *match_info;
regex = g_regex_new (pattern, 0, 0, NULL);
g_regex_match (regex, ip, 0, &match_info);
g_regex_unref (regex);
if (!g_match_info_matches (match_info)) {
g_match_info_free (match_info);
continue;
......@@ -620,12 +617,11 @@ convert_ip4_config_block (gchar * conn_name)
}
}
g_strfreev (ipset);
g_regex_unref (regex);
return start;
}
ip6_block *
convert_ip6_config_block (gchar * conn_name)
convert_ip6_config_block (const char *conn_name)
{
gchar **ipset;
guint length;
......@@ -654,16 +650,17 @@ convert_ip6_config_block (gchar * conn_name)
}
ip_block *
convert_ip4_routes_block (gchar * conn_name)
convert_ip4_routes_block (const char *conn_name)
{
gchar **ipset;
guint length;
guint i;
gchar *ip;
gchar *routes;
const char *routes;
ip_block *start = NULL, *current = NULL, *iblock = NULL;
g_return_val_if_fail (conn_name != NULL, NULL);
routes = ifnet_get_data (conn_name, "routes");
if (!routes)
return NULL;
......@@ -691,13 +688,13 @@ convert_ip4_routes_block (gchar * conn_name)
}
ip6_block *
convert_ip6_routes_block (gchar * conn_name)
convert_ip6_routes_block (const char *conn_name)
{
gchar **ipset;
guint length;
guint i;
gchar *ip, *tmp_addr;
gchar *routes;
const char *routes;
ip6_block *start = NULL, *current = NULL, *iblock = NULL;
struct in6_addr *tmp_ip6_addr;
......@@ -766,18 +763,22 @@ destroy_ip6_block (ip6_block * iblock)
}
void
set_ip4_dns_servers (NMSettingIP4Config * s_ip4, gchar * conn_name)
set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name)
{
gchar *dns_servers = ifnet_get_data (conn_name, "dns_servers");
gchar **server_list;
const char *dns_servers;
gchar **server_list, *stripped;
guint length, i;
struct in_addr tmp_ip4_addr;
guint32 new_dns;
dns_servers = ifnet_get_data (conn_name, "dns_servers");
if (!dns_servers)
return;
strip_string (dns_servers, '"');
server_list = g_strsplit (dns_servers, " ", 0);
stripped = g_strdup (dns_servers);
strip_string (stripped, '"');
server_list = g_strsplit (stripped, " ", 0);
g_free (stripped);
length = g_strv_length (server_list);
if (length)
g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS,
......@@ -803,17 +804,22 @@ set_ip4_dns_servers (NMSettingIP4Config * s_ip4, gchar * conn_name)
}
void
set_ip6_dns_servers (NMSettingIP6Config * s_ip6, gchar * conn_name)
set_ip6_dns_servers (NMSettingIP6Config *s_ip6, const char *conn_name)
{
gchar *dns_servers = ifnet_get_data (conn_name, "dns_servers");
gchar **server_list;
const char *dns_servers;
gchar **server_list, *stripped;
guint length, i;
struct in6_addr tmp_ip6_addr;
dns_servers = ifnet_get_data (conn_name, "dns_servers");
if (!dns_servers)
return;
strip_string (dns_servers, '"');
server_list = g_strsplit (dns_servers, " ", 0);
stripped = g_strdup (dns_servers);
strip_string (stripped, '"');
server_list = g_strsplit (stripped, " ", 0);
g_free (stripped);
length = g_strv_length (server_list);
if (length)
g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS,
......@@ -839,7 +845,7 @@ set_ip6_dns_servers (NMSettingIP6Config * s_ip6, gchar * conn_name)
}
gboolean
is_managed (gchar * conn_name)
is_managed (const char *conn_name)
{
gchar *config;
......@@ -855,13 +861,14 @@ is_managed (gchar * conn_name)
void
get_dhcp_hostname_and_client_id (char **hostname, char **client_id)
{
gchar *dhcp_client = ifnet_get_global_setting ("main", "dhcp");
const char *dhcp_client;
const gchar *dhcpcd_conf = "/etc/dhcpcd.conf";
const gchar *dhclient_conf = "/etc/dhcp/dhclient.conf";
gchar *line = NULL, *tmp = NULL, *contents = NULL;
gchar **all_lines;
guint line_num, i;
dhcp_client = ifnet_get_global_setting ("main", "dhcp");
*hostname = NULL;
*client_id = NULL;
if (dhcp_client) {
......
......@@ -27,8 +27,8 @@
#include <nm-setting-ip6-config.h>
#include <nm-setting-ip4-config.h>
#include "net_parser.h"
#define has_default_ip4_route(conn_name) has_default_route((conn_name),&is_ip4_address)
#define has_default_ip6_route(conn_name) has_default_route((conn_name),&is_ip6_address)
#define has_default_ip4_route(conn_name) has_default_route((conn_name), &is_ip4_address)
#define has_default_ip6_route(conn_name) has_default_route((conn_name), &is_ip6_address)
typedef struct _ip_block {
guint32 ip;
......@@ -44,36 +44,36 @@ typedef struct _ip6_block {
struct _ip6_block *next;
} ip6_block;
gchar *read_hostname (gchar * path);
gboolean write_hostname (const gchar * hostname, gchar * path);
gboolean is_static_ip4 (gchar * conn_name);
gboolean is_static_ip6 (gchar * conn_name);
gboolean is_ip4_address (gchar * in_address);
gboolean is_ip6_address (gchar * in_address);
gboolean has_ip6_address (gchar * conn_name);
gboolean has_default_route (gchar * conn_name, gboolean (*check_fn) (gchar *));
gchar *read_hostname (const char *path);
gboolean write_hostname (const char *hostname, const char *path);
gboolean is_static_ip4 (const char *conn_name);
gboolean is_static_ip6 (const char *conn_name);
gboolean is_ip4_address (const char *in_address);
gboolean is_ip6_address (const char *in_address);
gboolean has_ip6_address (const char *conn_name);
gboolean has_default_route (const char *conn_name, gboolean (*check_fn) (const char *));
gboolean reload_parsers (void);
ip_block *convert_ip4_config_block (gchar * conn_name);
ip6_block *convert_ip6_config_block (gchar * conn_name);
ip_block *convert_ip4_routes_block (gchar * conn_name);
ip6_block *convert_ip6_routes_block (gchar * conn_name);
ip_block *convert_ip4_config_block (const char *conn_name);
ip6_block *convert_ip6_config_block (const char *conn_name);
ip_block *convert_ip4_routes_block (const char *conn_name);
ip6_block *convert_ip6_routes_block (const char *conn_name);
void destroy_ip_block (ip_block * iblock);
void destroy_ip6_block (ip6_block * iblock);
void set_ip4_dns_servers (NMSettingIP4Config * s_ip4, gchar * conn_name);
void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, gchar * conn_name);
void set_ip4_dns_servers (NMSettingIP4Config * s_ip4, const char *conn_name);
void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name);
gchar *strip_string (gchar * str, gchar t);
gboolean is_managed (gchar * conn_name);
gchar *strip_string (gchar *str, gchar t);
gboolean is_managed (const char *conn_name);
GQuark ifnet_plugin_error_quark (void);
gchar *utils_hexstr2bin (const gchar * hex, size_t len);
gchar *utils_bin2hexstr (const gchar * bytes, int len, int final_len);
gboolean is_hex (gchar * value);
gboolean is_ascii (gchar * value);
gboolean is_true (gchar * str);
gboolean is_hex (const char