1 /*
2  * GNetwork Library: tests/testinterfaces.c
3  *
4  * Copyright (C) 2003 James M. Cape
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; version 2.1 of the
9  * License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21 
22 #include <libgnetwork/gnetwork.h>
23 #include <string.h>
24 
25 
26 static gchar *
protocols_to_string(GNetworkProtocols protocols)27 protocols_to_string (GNetworkProtocols protocols)
28 {
29   gchar *retval, *tmp;
30 
31   retval = NULL;
32   if (protocols & GNETWORK_PROTOCOL_PACKET)
33     {
34       retval = g_strdup ("Ethernet");
35     }
36 
37   if (protocols & GNETWORK_PROTOCOL_IPv4)
38     {
39       tmp = retval;
40       retval = g_strconcat ((tmp != NULL ? "IPv4 " : "IPv4"), tmp, NULL);
41       g_free (tmp);
42     }
43 
44   if (protocols & GNETWORK_PROTOCOL_IPv6)
45     {
46       tmp = retval;
47       retval = g_strconcat ((tmp != NULL ? "IPv6 " : "IPv6"), tmp, NULL);
48       g_free (tmp);
49     }
50 
51   return retval;
52 }
53 
54 
55 static gchar *
flags_to_string(GNetworkInterfaceFlags flags)56 flags_to_string (GNetworkInterfaceFlags flags)
57 {
58   gchar *ptr;
59   gchar data[1024] = { 0 };
60 
61   ptr = data;
62   if (flags & GNETWORK_INTERFACE_IS_UP)
63     {
64       memcpy (ptr, "Up ", 3);
65       ptr += 3;
66     }
67 
68   if (flags & GNETWORK_INTERFACE_IS_RUNNING)
69     {
70       memcpy (ptr, "Running ", 8);
71       ptr += 8;
72     }
73 
74   if (flags & GNETWORK_INTERFACE_IS_DEBUGGING)
75     {
76       memcpy (ptr, "Debugging ", 10);
77       ptr += 10;
78     }
79 
80   if (flags & GNETWORK_INTERFACE_IS_LOOPBACK)
81     {
82       memcpy (ptr, "Loopback ", 9);
83       ptr += 9;
84     }
85 
86   if (flags & GNETWORK_INTERFACE_IS_POINT_TO_POINT)
87     {
88       memcpy (ptr, "Point-to-point ", 15);
89       ptr += 15;
90     }
91 
92   if (flags & GNETWORK_INTERFACE_IS_LOAD_MASTER)
93     {
94       memcpy (ptr, "Load-master ", 12);
95       ptr += 12;
96     }
97 
98   if (flags & GNETWORK_INTERFACE_IS_LOAD_SLAVE)
99     {
100       memcpy (ptr, "Load-slave ", 11);
101       ptr += 11;
102     }
103 
104   if (flags & GNETWORK_INTERFACE_NO_TRAILERS)
105     {
106       memcpy (ptr, "No-trailers ", 12);
107       ptr += 12;
108     }
109 
110   if (flags & GNETWORK_INTERFACE_NO_ARP)
111     {
112       memcpy (ptr, "No-ARP ", 7);
113       ptr += 7;
114     }
115 
116   if (flags & GNETWORK_INTERFACE_RECV_ALL_PACKETS)
117     {
118       memcpy (ptr, "All-packets ", 12);
119       ptr += 12;
120     }
121 
122   if (flags & GNETWORK_INTERFACE_RECV_ALL_MULTICAST)
123     {
124       memcpy (ptr, "All-multicast ", 14);
125       ptr += 14;
126     }
127 
128   if (flags & GNETWORK_INTERFACE_CAN_BROADCAST)
129     {
130       memcpy (ptr, "Broadcast ", 10);
131       ptr += 10;
132     }
133 
134   if (flags & GNETWORK_INTERFACE_CAN_MULTICAST)
135     {
136       memcpy (ptr, "Multicast ", 10);
137       ptr += 10;
138     }
139 
140   if (flags & GNETWORK_INTERFACE_CAN_SET_MEDIA)
141     {
142       memcpy (ptr, "Media-select ", 13);
143       ptr += 13;
144     }
145 
146   if (flags & GNETWORK_INTERFACE_AUTOSELECTED_MEDIA)
147     {
148       memcpy (ptr, "Media-autoselect ", 17);
149       ptr += 17;
150     }
151 
152   return g_strdup (data);
153 }
154 
155 
156 int
main(int argc,char * argv[])157 main (int argc, char *argv[])
158 {
159   GSList *interfaces;
160   guint i;
161 
162   g_type_init ();
163 
164   g_print ("Configured Interfaces:\n");
165   for (interfaces = gnetwork_interface_get_all_interfaces (), i = 0; interfaces != NULL;
166        interfaces = g_slist_remove_link (interfaces, interfaces), i++)
167     {
168       gchar *str = NULL;
169       G_CONST_RETURN GSList *multicasts;
170 
171       g_print ("\nInterface %u:\n", i);
172       str = gnetwork_interface_info_get_name (interfaces->data);
173       g_print ("  Name\t\t\t= %s\n", str);
174       g_free (str);
175 
176       str = protocols_to_string (gnetwork_interface_info_get_protocols (interfaces->data));
177       g_print ("  Protocols\t\t= %s\n", (str != NULL ? str : "None"));
178       g_free (str);
179 
180       str = flags_to_string (gnetwork_interface_info_get_flags (interfaces->data));
181       g_print ("  Flags\t\t\t= %s\n", str);
182       g_free (str);
183 
184       /* HW */
185       g_print ("  Hardware Address\t= %s\n",
186 	       (gchar *) gnetwork_interface_info_get_address (interfaces->data,
187 							      GNETWORK_PROTOCOL_PACKET));
188 
189       g_print ("  Hardware Broadcast\t= %s\n",
190 	       (gchar *) gnetwork_interface_info_get_broadcast_address (interfaces->data,
191 									GNETWORK_PROTOCOL_PACKET));
192 
193       /* IPv4 */
194       str = gnetwork_ip_address_to_string (gnetwork_interface_info_get_address (interfaces->data,
195 										GNETWORK_PROTOCOL_IPv4));
196       g_print ("  IPv4 Address\t\t= %s\n", str);
197       g_free (str);
198 
199       str = gnetwork_ip_address_to_string (gnetwork_interface_info_get_broadcast_address (interfaces->data,
200 										GNETWORK_PROTOCOL_IPv4));
201       g_print ("  IPv4 Broadcast\t= %s\n", str);
202       g_free (str);
203 
204       str = gnetwork_ip_address_to_string (gnetwork_interface_info_get_netmask (interfaces->data,
205 										GNETWORK_PROTOCOL_IPv4));
206       g_print ("  IPv4 Netmask\t\t= %s\n", str);
207       g_free (str);
208 
209       multicasts = gnetwork_interface_info_get_multicasts (interfaces->data,
210 							   GNETWORK_PROTOCOL_IPv4);
211       if (multicasts != NULL)
212 	{
213 	  g_print ("  IPv4 Multicasts\t=");
214 	  for (; multicasts != NULL; multicasts = multicasts->next)
215 	    {
216 	      str = gnetwork_ip_address_to_string (multicasts->data);
217 	      g_print (" %s", str);
218 	      g_free (str);
219 	    }
220 	  g_print ("\n");
221 	}
222 
223       /* IPv6 */
224       str = gnetwork_ip_address_to_string (gnetwork_interface_info_get_address (interfaces->data,
225 										GNETWORK_PROTOCOL_IPv6));
226       g_print ("  IPv6 Address\t\t= %s\n", str);
227       g_free (str);
228 
229       str = gnetwork_ip_address_to_string (gnetwork_interface_info_get_netmask (interfaces->data,
230 										GNETWORK_PROTOCOL_IPv6));
231       g_print ("  IPv6 Netmask\t\t= %s\n", str);
232       g_free (str);
233 
234       multicasts = gnetwork_interface_info_get_multicasts (interfaces->data,
235 							   GNETWORK_PROTOCOL_IPv6);
236       if (multicasts != NULL)
237 	{
238 	  g_print ("  IPv6 Multicasts\t= ");
239 	  for (; multicasts != NULL; multicasts = multicasts->next)
240 	    {
241 	      str = gnetwork_ip_address_to_string (multicasts->data);
242 	      g_print ("%s%s", str, (multicasts->next != NULL ? ", " : "\n"));
243 	      g_free (str);
244 	    }
245 	}
246 
247       gnetwork_interface_info_unref (interfaces->data);
248     }
249 
250   return 0;
251 }
252