1    	/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2    	/* This program is free software; you can redistribute it and/or modify
3    	 * it under the terms of the GNU General Public License as published by
4    	 * the Free Software Foundation; either version 2, or (at your option)
5    	 * any later version.
6    	 *
7    	 * This program is distributed in the hope that it will be useful,
8    	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9    	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10   	 * GNU General Public License for more details.
11   	 *
12   	 * You should have received a copy of the GNU General Public License along
13   	 * with this program; if not, write to the Free Software Foundation, Inc.,
14   	 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15   	 *
16   	 * Copyright (C) 2010 Red Hat, Inc.
17   	 *
18   	 */
19   	
20   	#include <arpa/inet.h>
21   	#include <string.h>
22   	
23   	#include "nm-dns-utils.h"
24   	#include "nm-utils.h"
25   	
26   	static void
27   	add_ip4_to_rdns_array (guint32 ip, GPtrArray *domains) /* network byte order */
28   	{
29   		guint32 defprefix;
30   		guchar *p;
(1) Event assign_zero: Assigning: "str" = "NULL".
Also see events: [var_deref_model]
31   		char *str = NULL;
32   		int i;
33   	
34   		defprefix = nm_utils_ip4_get_default_prefix (ip);
35   	
36   		/* Convert to host byte order, mask the host bits, and convert back */
37   		ip = ntohl (ip);
38   		ip &= 0xFFFFFFFF << (32 - defprefix);
39   		ip = htonl (ip);
40   		p = (guchar *) &ip;
41   	
(2) Event cond_false: Condition "defprefix == 8", taking false branch
42   		if (defprefix == 8)
43   			str = g_strdup_printf ("%u.in-addr.arpa", p[0] & 0xFF);
(3) Event else_branch: Reached else branch
(4) Event cond_false: Condition "defprefix == 16", taking false branch
44   		else if (defprefix == 16)
45   			str = g_strdup_printf ("%u.%u.in-addr.arpa", p[1] & 0xFF, p[0] & 0xFF);
(5) Event else_branch: Reached else branch
(6) Event cond_false: Condition "defprefix == 24", taking false branch
46   		else if (defprefix == 24)
(7) Event if_end: End of if statement
47   			str = g_strdup_printf ("%u.%u.%u.in-addr.arpa", p[2] & 0xFF, p[1] & 0xFF, p[0] & 0xFF);
48   	
(8) Event cond_false: Condition "str != NULL", taking false branch
(9) Event else_branch: Reached else branch
(10) Event cond_true: Condition "({...})", taking true branch
(11) Event if_fallthrough: Falling through to end of if statement
(12) Event if_end: End of if statement
49   		g_return_if_fail (str != NULL);
50   	
51   		/* Suppress duplicates */
(13) Event cond_true: Condition "i < domains->len", taking true branch
52   		for (i = 0; i < domains->len; i++) {
(14) Event var_deref_model: Passing null pointer "str" to function "__coverity_strcmp(char const *, char const *)", which dereferences it.
Also see events: [assign_zero]
53   			if (strcmp (str, g_ptr_array_index (domains, i)) == 0)
54   				break;
55   		}
56   	
57   		if (i == domains->len)
58   			g_ptr_array_add (domains, str);
59   		else
60   			g_free (str);
61   	}
62   	
63   	char **
64   	nm_dns_utils_get_ip4_rdns_domains (NMIP4Config *ip4)
65   	{
66   		GPtrArray *domains = NULL;
67   		int i;
68   	
69   		g_return_val_if_fail (ip4 != NULL, NULL);
70   	
71   		domains = g_ptr_array_sized_new (5);
72   	
73   		/* To calculate the reverse DNS domains for this IP4 config, we take
74   		 * all the IP addresses and routes in the config, calculate the network
75   		 * portion, and convert that to classful, and use the network bits for
76   		 * the final domain.  FIXME: better handle classless routing, which might
77   		 * require us to add multiple domains for each actual network prefix to
78   		 * cover all the separate networks in that block.
79   		 */
80   	
81   		for (i = 0; i < nm_ip4_config_get_num_addresses (ip4); i++) {
82   			const NMPlatformIP4Address *address = nm_ip4_config_get_address (ip4, i);
83   	
84   			add_ip4_to_rdns_array (address->address, domains);
85   		}
86   	
87   		for (i = 0; i < nm_ip4_config_get_num_routes (ip4); i++) {
88   			const NMPlatformIP4Route *route = nm_ip4_config_get_route (ip4, i);
89   	
90   			add_ip4_to_rdns_array (route->network, domains);
91   		}
92   	
93   		/* Terminating NULL so we can use g_strfreev() to free it */
94   		g_ptr_array_add (domains, NULL);
95   	
96   		/* Free the array and return NULL if the only element was the ending NULL */
97   		return (char **) g_ptr_array_free (domains, (domains->len == 1));
98   	}
99   	
100