File: guestInfoPosix.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (255 lines) | stat: -rw-r--r-- 7,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*********************************************************
 * Copyright (C) 1998 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/


/*
 * guestInfoPosix.c --
 *
 *    Routines to get guest information. These are invoked by guestInfoServer
 *    which writes this information into Vmdb.
 *
 */

#ifndef VMX86_DEVEL

#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifdef sun
# include <sys/systeminfo.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>
#if defined(__FreeBSD__) || defined(__APPLE__)
# include <sys/sysctl.h>
#endif
#ifndef NO_DNET
# ifdef DNET_IS_DUMBNET
#  include <dumbnet.h>
# else
#  include <dnet.h>
# endif
#endif
#include "util.h"
#include "sys/utsname.h"
#include "sys/ioctl.h"
#include "vmware.h"
#include "hostinfo.h"
#include "guestInfoInt.h"
#include "debug.h"
#include "str.h"
#include "guest_os.h"
#include "guestApp.h"
#include "guestInfo.h"


/*
 *-----------------------------------------------------------------------------
 *
 * GuestInfoGetFqdn--
 *
 *      Return the guest's fully qualified domain name.
 *      This is just a thin wrapper around gethostname.
 *
 * Return value:
 *      Returns TRUE on success and FALSE on failure.
 *      Returns the guest's fully qualified domain name.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Bool
GuestInfoGetFqdn(int outBufLen,    // IN: length of output buffer
                 char fqdn[])      // OUT: fully qualified domain name
{
   ASSERT(fqdn);
   if (gethostname(fqdn, outBufLen) < 0) {
      Debug("Error, gethostname failed\n");
      return FALSE;
   }

   return TRUE;
}


#ifndef NO_DNET
/*
 *-----------------------------------------------------------------------------
 *
 * RecordNetworkAddress --
 *
 *      Massages a dnet(3)-style interface address (IPv4 or IPv6) and stores it
 *      as part of a GuestNic structure.
 *
 * Results:
 *      If addr is IPv4 or IPv6, it will be appended to the GuestNic's list of
 *      IP addresses.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static void
RecordNetworkAddress(GuestNic *nic,             // IN: operand NIC
                     const struct addr *addr)   // IN: dnet(3) address to process
{
   char ipAddress[NICINFO_MAX_IP_LEN];
   VmIpAddress *ip = NULL;

   switch (addr->addr_type) {
   case ADDR_TYPE_IP:
      /*
       * GuestNicInfo clients expect IPv4 addresses and netmasks to be stored
       * as strings in separate fields.  As such, we'll use ip_ntop instead of
       * addr_ntop to get a string without the netmask bits.
       */
      ip_ntop(&addr->addr_ip, ipAddress, sizeof ipAddress);
      ip = GuestInfoAddIpAddress(nic, ipAddress, INFO_IP_ADDRESS_FAMILY_IPV4);
      if (ip) {
         GuestInfoAddSubnetMask(ip, addr->addr_bits, TRUE);
      }
      break;
   case ADDR_TYPE_IP6:
      ip6_ntop(&addr->addr_ip6, ipAddress, sizeof ipAddress);
      ip = GuestInfoAddIpAddress(nic, ipAddress, INFO_IP_ADDRESS_FAMILY_IPV6);
      if (ip) {
         GuestInfoAddSubnetMask(ip, addr->addr_bits, FALSE);
      }
      break;
   default:
      Debug("%s: Unknown address type: %hu\n", __func__, addr->addr_type);
      break;
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * ReadInterfaceDetails --
 *
 *      Callback function called by libdnet when iterating over all the
 *      NICs on the host.
 *
 * Return value:
 *      Returns 0 on success and -1 on failure.
 *      Adds the MAC addresses of all NICs and their corresponding IPs.
 *
 * Side effects:
 *      Memory is allocated for each NIC, as well as IP addresses of all NICs
 *      on successful return.
 *
 *-----------------------------------------------------------------------------
 */

int
ReadInterfaceDetails(const struct intf_entry *entry,  // IN: current interface entry
                     void *arg)                       // IN: Pointer to the GuestNicList
{
   int i;
   GuestNicList *nicInfo = arg;

   if ((entry->intf_type & INTF_TYPE_ETH) == INTF_TYPE_ETH) {
      GuestNic *nic;
      char macAddress[NICINFO_MAC_LEN];

      Str_Sprintf(macAddress, sizeof macAddress, "%s",
                  addr_ntoa(&entry->intf_link_addr));
      nic = GuestInfoAddNicEntry(nicInfo, macAddress);

      if (nic == NULL) {
         return -1;
      }

      /* Record the "primary" address. */
      if (entry->intf_addr.addr_type == ADDR_TYPE_IP ||
          entry->intf_addr.addr_type == ADDR_TYPE_IP6) {
         RecordNetworkAddress(nic, &entry->intf_addr);
      }

      /* Walk the list of alias's and add those that are IPV4 or IPV6 */
      for (i = 0; i < entry->intf_alias_num; i++) {
         const struct addr *alias = &entry->intf_alias_addrs[i];
         if (alias->addr_type == ADDR_TYPE_IP ||
             alias->addr_type == ADDR_TYPE_IP6) {
            RecordNetworkAddress(nic, alias);
         }
      }
   }

   return 0;
}
#endif


/*
 *-----------------------------------------------------------------------------
 *
 * GuestInfoGetNicInfo --
 *
 *      Return MAC addresses of all the NICs in the guest and their
 *      corresponding IP addresses.
 *
 * Return value:
 *      Returns TRUE on success and FALSE on failure.
 *      Return MAC addresses of all NICs and their corresponding IPs.
 *
 * Side effects:
 *      Memory is allocated for each NIC, as well as IP addresses of all NICs
 *      on successful return.
 *
 *-----------------------------------------------------------------------------
 */

Bool
GuestInfoGetNicInfo(GuestNicList *nicInfo)   // OUT
{
#ifndef NO_DNET
   intf_t *intf;

   memset(nicInfo, 0, sizeof *nicInfo);

   /* Get a handle to read the network interface configuration details. */
   if ((intf = intf_open()) == NULL) {
      Debug("GuestInfo: Error, failed NULL result from intf_open()\n");
      return FALSE;
   }

   if (intf_loop(intf, ReadInterfaceDetails, nicInfo) < 0) {
      intf_close(intf);
      Debug("GuestInfo: Error, negative result from intf_loop\n");
      return FALSE;
   }

   intf_close(intf);
   return TRUE;
#else
   return FALSE;
#endif
}