discover: Record IP address of network interfaces
[petitboot] / discover / sysinfo.c
1
2 #include <string.h>
3
4 #include <talloc/talloc.h>
5 #include <process/process.h>
6 #include <log/log.h>
7
8 #include "discover-server.h"
9 #include "platform.h"
10 #include "sysinfo.h"
11
12 static struct system_info *sysinfo;
13 static struct discover_server *server;
14
15 const struct system_info *system_info_get(void)
16 {
17         return sysinfo;
18 }
19
20
21 void system_info_set_interface_address(unsigned int hwaddr_size,
22                 uint8_t *hwaddr, const char *address)
23 {
24         struct interface_info *if_info;
25         unsigned int i;
26
27         for (i = 0; i < sysinfo->n_interfaces; i++) {
28                 if_info = sysinfo->interfaces[i];
29
30                 if (if_info->hwaddr_size != hwaddr_size)
31                         continue;
32
33                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
34                         continue;
35
36                 /* Found an existing interface. Notify clients if a new address
37                  * is set */
38                 if (!if_info->address || strcmp(if_info->address, address)) {
39                         talloc_free(if_info->address);
40                         if_info->address = talloc_strdup(if_info, address);
41                         discover_server_notify_system_info(server, sysinfo);
42                         return;
43                 }
44         }
45
46         pb_log("Couldn't find interface matching %s\n", "foo");
47 }
48
49 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
50                 const char *name, bool link)
51 {
52         struct interface_info *if_info;
53         unsigned int i;
54
55         for (i = 0; i < sysinfo->n_interfaces; i++) {
56                 bool changed = false;
57
58                 if_info = sysinfo->interfaces[i];
59
60                 if (if_info->hwaddr_size != hwaddr_size)
61                         continue;
62
63                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
64                         continue;
65
66                 /* Found an existing interface. Notify clients on any name or
67                  * link changes */
68                 if (strcmp(if_info->name, name)) {
69                         talloc_free(if_info->name);
70                         if_info->name = talloc_strdup(if_info, name);
71                         changed = true;
72                 }
73
74                 if (if_info->link != link) {
75                         if_info->link = link;
76                         changed = true;
77                 }
78
79                 if (changed)
80                         discover_server_notify_system_info(server, sysinfo);
81
82                 return;
83         }
84
85         if_info = talloc_zero(sysinfo, struct interface_info);
86         if_info->hwaddr_size = hwaddr_size;
87         if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
88         if_info->name = talloc_strdup(if_info, name);
89         if_info->link = link;
90
91         sysinfo->n_interfaces++;
92         sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
93                                                 struct interface_info *,
94                                                 sysinfo->n_interfaces);
95         sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
96
97         discover_server_notify_system_info(server, sysinfo);
98 }
99
100 void system_info_register_blockdev(const char *name, const char *uuid,
101                 const char *mountpoint)
102 {
103         struct blockdev_info *bd_info;
104         unsigned int i;
105
106         for (i = 0; i < sysinfo->n_blockdevs; i++) {
107                 bd_info = sysinfo->blockdevs[i];
108
109                 if (strcmp(bd_info->name, name))
110                         continue;
111
112                 /* update the mountpoint and UUID, and we're done */
113                 talloc_free(bd_info->mountpoint);
114                 bd_info->uuid = talloc_strdup(bd_info, uuid);
115                 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
116                 discover_server_notify_system_info(server, sysinfo);
117                 return;
118         }
119
120         bd_info = talloc_zero(sysinfo, struct blockdev_info);
121         bd_info->name = talloc_strdup(bd_info, name);
122         bd_info->uuid = talloc_strdup(bd_info, uuid);
123         bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
124
125         sysinfo->n_blockdevs++;
126         sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
127                                                 struct blockdev_info *,
128                                                 sysinfo->n_blockdevs);
129         sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
130
131         discover_server_notify_system_info(server, sysinfo);
132 }
133
134 void system_info_init(struct discover_server *s)
135 {
136         server = s;
137         sysinfo = talloc_zero(server, struct system_info);
138         platform_get_sysinfo(sysinfo);
139 }