[docs] Enable documentation for utils.
[mono.git] / mono / utils / mono-networkinterfaces.c
1 /**
2  * \file
3  */
4
5 #include "config.h"
6 #include "utils/mono-networkinterfaces.h"
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 /* FIXME: bsds untested */
13
14 /**
15  * mono_networkinterface_list:
16  * @size: a pointer to a location where the size of the returned array is stored
17  *
18  * Return an array of names for the interfaces currently on the system.
19  * The size of the array is stored in @size.
20  */
21 gpointer*
22 mono_networkinterface_list (int *size)
23 {
24         int i = 0, count = 0;
25         void **nilist = NULL;
26         char buf [512];
27         FILE *f;
28         char name [256];
29
30         f = fopen ("/proc/net/dev", "r");
31         if (!f) 
32                 return NULL;
33
34         if (!fgets (buf, sizeof (buf) / sizeof (char), f))
35                 goto out;
36
37         if (!fgets (buf, sizeof (buf) / sizeof (char), f))
38                 goto out;
39
40         while (fgets (buf, sizeof (buf), f) != NULL) {
41                 char *ptr;
42                 buf [sizeof(buf) - 1] = 0;
43                 if ((ptr = strchr (buf, ':')) == NULL || (*ptr++ = 0, sscanf (buf, "%s", name) != 1))
44                         goto out;
45
46                 if (i >= count) {
47                         if (!count)
48                                 count = 16;
49                         else
50                                 count *= 2;
51                 }
52
53                 nilist = (void **) g_realloc (nilist, count * sizeof (void*));
54                 nilist [i++] = g_strdup (name);
55         }
56
57  out:
58         if (f) fclose(f);
59         if (size)
60                 *size = i;
61
62         if (!nilist)
63                 nilist = (void **) g_malloc (sizeof (void*));
64         nilist [i] = NULL;
65         return nilist;
66 }
67
68 /**
69  * mono_network_get_data:
70  * @name: name of the interface
71  * @data: description of data to return
72  *
73  * Return a data item of a network adapter like bytes sent per sec, etc
74  * according to the @data argumet.
75  */
76 gint64
77 mono_network_get_data (char* name, MonoNetworkData data, MonoNetworkError *error)
78 {
79         gint64 val = 0;
80         char buf [512];
81         char cname [256];
82         FILE *f;
83
84         unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
85                 rx_fifo, rx_frame, tx_bytes, tx_packets, tx_errs, tx_drops,
86                 tx_fifo, tx_colls, tx_carrier, rx_multi;
87
88         *error = MONO_NETWORK_ERROR_OTHER;
89
90         f = fopen ("/proc/net/dev", "r");
91         if (!f) 
92                 return -1;
93
94         if (!fgets (buf, sizeof (buf) / sizeof (char), f))
95                 goto out;
96
97         if (!fgets (buf, sizeof (buf) / sizeof (char), f))
98                 goto out;
99
100         while (fgets (buf, sizeof (buf), f) != NULL) {
101
102                 char *ptr;
103                 buf [sizeof (buf) - 1] = 0;
104                 if ((ptr = strchr (buf, ':')) == NULL ||
105                                 (*ptr++ = 0, sscanf (buf, "%250s", cname) != 1))
106                         goto out;
107
108                 if (strcmp (name, cname) != 0) continue;
109
110                 if (sscanf (ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
111                                                          &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
112                                                          &rx_fifo, &rx_frame, &rx_multi,
113                                                          &tx_bytes, &tx_packets, &tx_errs, &tx_drops,
114                                                          &tx_fifo, &tx_colls, &tx_carrier) != 14) 
115                         goto out;
116
117                 switch (data) {
118                 case MONO_NETWORK_BYTESSENT:
119                         val = tx_bytes;
120                         *error = MONO_NETWORK_ERROR_NONE;
121                         goto out;
122                 case MONO_NETWORK_BYTESREC:
123                         val = rx_bytes;
124                         *error = MONO_NETWORK_ERROR_NONE;
125                         goto out;
126                 case MONO_NETWORK_BYTESTOTAL:
127                         val = rx_bytes + tx_bytes;
128                         *error = MONO_NETWORK_ERROR_NONE;
129                         goto out;
130                 }
131         }
132
133  out:
134         if (f) fclose (f);
135         return val;
136 }
137