~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to lib/guestInfo/guestInfoServer.c

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-15 21:21:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080815212140-05fhxj8wroosysmj
Tags: 2008.08.08-109361-1ubuntu1
* Merge from Debian unstable (LP: #258393), remaining Ubuntu change:
  - add ubuntu_toolchain_FTBFS.dpatch patch, fix FTBFS
* Update ubuntu_toolchain_FTBFS.dpatch patch for the new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "vmware.h"
37
37
#include "eventManager.h"
38
38
#include "debug.h"
 
39
#include "dynxdr.h"
39
40
#include "str.h"
40
41
#include "util.h"
41
42
#include "rpcout.h"
48
49
#include "system.h"
49
50
#include "wiper.h" // for WiperPartition functions
50
51
#include "guest_msg_def.h" // For GUESTMSG_MAX_IN_SIZE
 
52
#include "xdrutil.h"
51
53
 
52
54
#define GUESTINFO_DEFAULT_DELIMITER ' '
53
55
 
57
59
 
58
60
typedef struct _GuestInfoCache{
59
61
   char value[INFO_MAX][MAX_VALUE_LEN]; /* Stores values of all key-value pairs. */
60
 
   GuestNicInfo  nicInfo;
 
62
   GuestNicList  nicInfo;
61
63
   GuestDiskInfo diskInfo;
62
64
} GuestInfoCache;
63
65
 
79
81
 
80
82
static Bool vmResumed;
81
83
 
82
 
/*
83
 
 * The Windows Guest Info Server runs in a separate thread,
84
 
 * so we have to synchronize access to 'vmResumed' variable.
85
 
 * Non-windows guest info server does not run in a separate
86
 
 * thread, so no locking is needed.
87
 
 */
88
 
 
89
 
#if defined(_WIN32)
90
 
typedef CRITICAL_SECTION vmResumedLockType;
91
 
#define GUESTINFO_DELETE_LOCK(lockPtr) DeleteCriticalSection(lockPtr)
92
 
#define GUESTINFO_ENTER_LOCK(lockPtr) EnterCriticalSection(lockPtr)
93
 
#define GUESTINFO_LEAVE_LOCK(lockPtr) LeaveCriticalSection(lockPtr)
94
 
#define GUESTINFO_INIT_LOCK(lockPtr) InitializeCriticalSection(lockPtr)
95
 
 
96
 
vmResumedLockType vmResumedLock;
97
 
 
98
 
#else // #if LINUX
99
 
 
100
 
typedef int vmResumedLockType;
101
 
#define GUESTINFO_DELETE_LOCK(lockPtr)
102
 
#define GUESTINFO_ENTER_LOCK(lockPtr)
103
 
#define GUESTINFO_LEAVE_LOCK(lockPtr)
104
 
#define GUESTINFO_INIT_LOCK(lockPtr)
105
 
 
106
 
#endif // #if LINUX
107
 
 
108
84
static Bool GuestInfoGather(void * clientData);
109
85
static Bool GuestInfoUpdateVmdb(GuestInfoType infoType, void* info);
110
86
static Bool SetGuestInfo(GuestInfoType key, const char* value, char delimiter);
111
 
static Bool NicInfoChanged(GuestNicInfo *nicInfo);
 
87
static Bool NicInfoChanged(GuestNicList *nicInfo);
112
88
static Bool DiskInfoChanged(PGuestDiskInfo diskInfo);
113
89
static void GuestInfoClearCache(void);
114
 
static Bool GuestInfoSerializeNicInfo(GuestNicInfo *nicInfo,
115
 
                                      char buffer[GUESTMSG_MAX_IN_SIZE],
116
 
                                      size_t *bufferLen);
117
 
static int PrintNicInfo(GuestNicInfo *nicInfo, int (*PrintFunc)(const char *, ...));
118
 
 
119
 
#ifdef _WIN32
120
 
static Bool GuestInfoConvertNicInfoToNicInfoV1(GuestNicInfo *info, GuestNicInfoV1 *infoV1);
121
 
 
122
 
 
123
 
/*
124
 
 *-----------------------------------------------------------------------------
125
 
 *
126
 
 * GuestInfoServer_Main --
127
 
 *
128
 
 *    The main event loop for the guest info server.
129
 
 *    GuestInfoServer_Init() much be called prior to calling this function.
130
 
 *
131
 
 * Result
132
 
 *    None
133
 
 *
134
 
 * Side-effects
135
 
 *    Events are processed, information gathered and updates sent.
136
 
 *
137
 
 *-----------------------------------------------------------------------------
138
 
 */
139
 
 
140
 
void
141
 
GuestInfoServer_Main(void *data) // IN
142
 
{
143
 
   int retVal;
144
 
   uint64 sleepUsecs;
145
 
   HANDLE *events = (HANDLE *) data;
146
 
   HANDLE quitEvent;
147
 
   HANDLE finishedEvent;
148
 
 
149
 
   ASSERT(data);
150
 
   ASSERT(events[0]);
151
 
   ASSERT(events[1]);
152
 
 
153
 
   quitEvent = events[0];
154
 
   finishedEvent = events[1];
155
 
 
156
 
   Debug("Starting GuestInfoServer for Windows.\n");
157
 
   for(;;) {
158
 
      DWORD dwError;
159
 
 
160
 
      retVal = EventManager_ProcessNext(gGuestInfoEventQueue, &sleepUsecs);
161
 
      if (retVal != 1) {
162
 
         Debug("Unexpected end of the guest info loop.\n");
163
 
         break;
164
 
      }
165
 
 
166
 
      /*
167
 
       * The number of micro seconds to sleep should not overflow a long. This
168
 
       * corresponds to a maximum sleep time of around 4295 seconds (~ 71 minutes)
169
 
       * which should be more than enough.
170
 
       */
171
 
 
172
 
      Debug("Sleeping for %"FMT64"u msecs...\n", sleepUsecs / 1000);
173
 
      dwError = WaitForSingleObject(quitEvent, sleepUsecs / 1000);
174
 
      if (dwError == WAIT_OBJECT_0) {
175
 
         GuestApp_Log("GuestInfoServer received quit event.\n");
176
 
         Debug("GuestInfoServer received quit event.\n");
177
 
         break;
178
 
      } else if (dwError == WAIT_TIMEOUT) {
179
 
         Debug("GuestInfoServer woke up.\n");
180
 
      } else if (dwError == WAIT_FAILED) {
181
 
         Debug("GuestInfoServer error waiting on exit event: %d %d\n",
182
 
               dwError, GetLastError());
183
 
         break;
184
 
      }
185
 
   }
186
 
   SetEvent(finishedEvent);
187
 
   GuestApp_Log("GuestInfoServer exiting.\n");
188
 
}
189
 
#endif
 
90
static int PrintNicInfo(GuestNicList *nicInfo, int (*PrintFunc)(const char *, ...));
190
91
 
191
92
 
192
93
/*
325
226
   char name[255];
326
227
   char osNameFull[MAX_VALUE_LEN];
327
228
   char osName[MAX_VALUE_LEN];
328
 
   GuestNicInfo nicInfo;
 
229
   GuestNicList nicInfo;
329
230
   GuestDiskInfo diskInfo;
330
231
#if defined(_WIN32) || defined(linux)
331
232
   GuestMemInfo vmStats = {0};
443
344
 */
444
345
 
445
346
Bool
446
 
GuestInfoConvertNicInfoToNicInfoV1(GuestNicInfo *info,             // IN
447
 
                                   GuestNicInfoV1  *infoV1)        // OUT
 
347
GuestInfoConvertNicInfoToNicInfoV1(GuestNicList *info,        // IN
 
348
                                   GuestNicInfoV1 *infoV1)    // OUT
448
349
{
449
 
   NicEntry *nicEntryCur;
450
350
   uint32 maxNics;
451
 
   uint32 nicIndex = 0;
452
 
   DblLnkLst_Links *nicEntryLink;
 
351
   u_int i;
453
352
 
454
353
   if ((NULL == info) ||
455
354
       (NULL == infoV1)) {
456
355
      return FALSE;
457
356
   }
458
357
 
459
 
   maxNics = info->nicInfoProto.numNicEntries > MAX_NICS ?
460
 
                                   MAX_NICS : info->nicInfoProto.numNicEntries;
 
358
   maxNics = MIN(info->nics.nics_len, MAX_NICS);
461
359
   infoV1->numNicEntries = maxNics;
462
 
   if (maxNics < info->nicInfoProto.numNicEntries) {
463
 
      Debug("Truncating NICs.\n");
 
360
   if (maxNics < info->nics.nics_len) {
 
361
      Debug("GuestInfo: truncating NIC list for backwards compatibility.\n");
464
362
   }
465
363
 
466
 
   DblLnkLst_ForEach(nicEntryLink, &info->nicList) {
467
 
      uint32 ipIndex = 0;
 
364
   XDRUTIL_FOREACH(i, info, nics) {
 
365
      u_int j;
468
366
      uint32 maxIPs;
469
 
      VmIpAddressEntry *ipAddressCur;
470
 
      DblLnkLst_Links *ipAddrLink;
471
 
 
472
 
      if (nicIndex >= maxNics) {
 
367
      GuestNic *nic = XDRUTIL_GETITEM(info, nics, i);
 
368
      
 
369
      Str_Strcpy(infoV1->nicList[i].macAddress,
 
370
                 nic->macAddress,
 
371
                 sizeof infoV1->nicList[i].macAddress);
 
372
 
 
373
      maxIPs = MIN(nic->ips.ips_len, MAX_IPS);
 
374
      infoV1->nicList[i].numIPs = 0;
 
375
 
 
376
      XDRUTIL_FOREACH(j, nic, ips) {
 
377
         VmIpAddress *ip = XDRUTIL_GETITEM(nic, ips, j);
 
378
         
 
379
         if (strlen(ip->ipAddress) < sizeof infoV1->nicList[i].ipAddress[j]) {
 
380
            Str_Strcpy(infoV1->nicList[i].ipAddress[j],
 
381
                       ip->ipAddress,
 
382
                       sizeof infoV1->nicList[i].ipAddress[j]);
 
383
            infoV1->nicList[i].numIPs++;
 
384
            if (infoV1->nicList[i].numIPs == maxIPs) {
 
385
               break;
 
386
            }
 
387
         } else {
 
388
            Debug("GuestInfo: ignoring IPV6 address for compatibility.\n");
 
389
         }
 
390
      }
 
391
 
 
392
      if (infoV1->nicList[i].numIPs != nic->ips.ips_len) {
 
393
         Debug("GuestInfo: some IP addresses were ignored for compatibility.\n");
 
394
      }
 
395
      if (i == maxNics) {
473
396
         break;
474
397
      }
475
 
 
476
 
      nicEntryCur = DblLnkLst_Container(nicEntryLink,
477
 
                                        NicEntry,
478
 
                                        links);
479
 
      if (NULL == nicEntryCur) {
480
 
         return FALSE;
481
 
      }
482
 
 
483
 
      strcpy(infoV1->nicList[nicIndex].macAddress, nicEntryCur->nicEntryProto.macAddress);
484
 
 
485
 
      maxIPs = nicEntryCur->nicEntryProto.numIPs > MAX_IPS ?
486
 
                                          MAX_IPS : nicEntryCur->nicEntryProto.numIPs;
487
 
      nicEntryCur -> nicEntryProto.numIPs = maxIPs;
488
 
      if (maxIPs < nicEntryCur->nicEntryProto.numIPs) {
489
 
         Debug("Truncating IP addresses for NIC %d.\n", nicIndex);
490
 
      }
491
 
 
492
 
      DblLnkLst_ForEach(ipAddrLink, &nicEntryCur->ipAddressList) {
493
 
 
494
 
         if (ipIndex >= maxIPs) {
495
 
            break;
496
 
         }
497
 
 
498
 
         ipAddressCur = DblLnkLst_Container(ipAddrLink,
499
 
                                          VmIpAddressEntry,
500
 
                                          links);
501
 
         if (NULL == ipAddressCur) {
502
 
            return FALSE;
503
 
         }
504
 
         strcpy(infoV1->nicList[nicIndex].ipAddress[ipIndex],
505
 
                ipAddressCur->ipEntryProto.ipAddress);
506
 
 
507
 
         ipIndex++;
508
 
         infoV1->nicList[nicIndex].numIPs = ipIndex;
509
 
      }
510
 
 
511
 
      nicIndex++;
512
398
   }
513
399
 
514
400
   return TRUE;
574
460
      break;
575
461
 
576
462
   case INFO_IPADDRESS:
577
 
      if (NicInfoChanged((GuestNicInfo *)info)) {
 
463
      if (NicInfoChanged((GuestNicList *)info)) {
578
464
         static Bool isCmdV1 = FALSE;
579
 
         char request[GUESTMSG_MAX_IN_SIZE];
580
 
         size_t requestLength = 0;
581
465
         char *reply = NULL;
582
466
         size_t replyLen;
583
467
         Bool status;
584
468
 
585
469
         if (FALSE == isCmdV1) {
586
 
            Debug("Creating nic info message.\n");
587
 
            Str_Sprintf(request,
588
 
                        sizeof request,
589
 
                        "%s  %d ",
590
 
                        GUEST_INFO_COMMAND_TWO,
591
 
                        INFO_IPADDRESS);
592
 
 
593
 
            if (GuestInfoSerializeNicInfo((GuestNicInfo *)info,
594
 
                                          request + strlen(request),
595
 
                                          &requestLength)) {
596
 
               requestLength += strlen(request);
 
470
            /* 13 = max size of string representation of an int + 3 spaces. */
 
471
            char request[sizeof GUEST_INFO_COMMAND + 13];
 
472
            GuestNicProto message;
 
473
            XDR xdrs;
 
474
 
 
475
            if (DynXdr_Create(&xdrs) == NULL) {
 
476
               return FALSE;
 
477
            }
 
478
 
 
479
            /* Add the RPC preamble: message name, and type. */
 
480
            Str_Sprintf(request, sizeof request, "%s  %d ",
 
481
                        GUEST_INFO_COMMAND, INFO_IPADDRESS_V2);
 
482
 
 
483
            message.ver = NIC_INFO_V2;
 
484
            message.GuestNicProto_u.nicsV2 = info;
 
485
 
 
486
            /* Write preamble and serialized nic info to XDR stream. */
 
487
            if (!DynXdr_AppendRaw(&xdrs, request, strlen(request)) ||
 
488
                !xdr_GuestNicProto(&xdrs, &message)) {
 
489
               Debug("GuestInfo: Error serializing nic info v2 data.");
 
490
               DynXdr_Destroy(&xdrs, TRUE);
 
491
               return FALSE;
 
492
            }
 
493
 
 
494
            status = RpcOut_SendOneRaw(DynXdr_Get(&xdrs),
 
495
                                       xdr_getpos(&xdrs),
 
496
                                       &reply,
 
497
                                       &replyLen);
 
498
            DynXdr_Destroy(&xdrs, TRUE);
 
499
            if (status) {
 
500
               Debug("GuestInfo: sent nic info message.\n");
597
501
            } else {
598
 
               return FALSE;
599
 
            }
600
 
 
601
 
            Debug("GuestInfo: Sending nic info message.\n");
602
 
            /* Send all the information in the message. */
603
 
            status = RpcOut_SendOneRaw(request, requestLength, &reply, &replyLen);
604
 
 
605
 
            Debug("GuestInfo: Just sent nic info message.\n");
 
502
               Debug("GuestInfo: failed to send V2 nic info message.\n");
 
503
            }
 
504
 
 
505
            if (RpcVMX_ConfigGetBool(FALSE, "printNicInfo")) {
 
506
               PrintNicInfo((GuestNicList *) info,
 
507
                            (int (*)(const char *fmt, ...)) RpcVMX_Log);
 
508
            }
606
509
         } else {
607
510
            status = FALSE;
608
511
         }
648
551
            }
649
552
         }
650
553
 
651
 
         if (RpcVMX_ConfigGetBool(FALSE, "printNicInfo")) {
652
 
            PrintNicInfo((GuestNicInfo *) info, (int (*)(const char *fmt, ...)) RpcVMX_Log);
653
 
         }
654
 
 
655
554
         Debug("GuestInfo: Updated new NIC information\n");
656
555
         free(reply);
657
556
         reply = NULL;
658
557
 
659
558
         /*
660
 
          * Update the cache.  Assign info to gInfoCache.nicInfo. First free dynamic
661
 
          * memory allocated in gInfoCache.nicInfo. Then unlink those in nicInfo and
662
 
          * link them back to gInfoCache.nicInfo this is sort of hacking.  However, it
663
 
          * works in this case, since nicInfo is not going to be used after this.  NOTE,
664
 
          * nicInfo CAN NOT BE USED AFTER THIS POINT.
 
559
          * Update the cache. Release the memory previously used by the cache,
 
560
          * and copy the new information into the cache.
665
561
          */
666
 
         GuestInfo_FreeDynamicMemoryInNicInfo(&gInfoCache.nicInfo);
667
 
         /* assign the fixed memory part */
668
 
         gInfoCache.nicInfo = *(GuestNicInfo *)info;
669
 
         /* assign the dynamic memory part */
670
 
         DblLnkLst_Init(&gInfoCache.nicInfo.nicList);
671
 
         DblLnkLst_Link(&gInfoCache.nicInfo.nicList, ((GuestNicInfo *)info)->nicList.next);
672
 
         DblLnkLst_Unlink1(&((GuestNicInfo *)info)->nicList);
 
562
         VMX_XDR_FREE(xdr_GuestNicList, &gInfoCache.nicInfo);
 
563
         gInfoCache.nicInfo = *(GuestNicList *)info;
673
564
      } else {
674
565
         Debug("GuestInfo: Nic info not changed.\n");
675
566
      }
858
749
 *
859
750
 * GuestInfoFindMacAddress --
860
751
 *
861
 
 *      Locates a MAC address in the NIC info structure.
 
752
 *      Locates a NIC with the given MAC address in the NIC list.
862
753
 *
863
754
 * Return value:
864
755
 *      If there is an entry in nicInfo which corresponds to this MAC address,
865
 
 *      its index is returned. If not -1 is returned.
 
756
 *      it is returned. If not NULL is returned.
866
757
 *
867
758
 * Side effects:
868
759
 *      None
870
761
 *-----------------------------------------------------------------------------
871
762
 */
872
763
 
873
 
NicEntry *
874
 
GuestInfoFindMacAddress(GuestNicInfo *nicInfo, const char *macAddress)
 
764
GuestNic *
 
765
GuestInfoFindMacAddress(GuestNicList *nicInfo,  // IN/OUT
 
766
                        const char *macAddress) // IN
875
767
{
876
 
   NicEntry *nicEntry;
877
 
   DblLnkLst_Links *sCurrent;
878
 
 
879
 
   if (0 == nicInfo->nicInfoProto.numNicEntries) {
880
 
      return NULL;
881
 
   }
882
 
 
883
 
   DblLnkLst_ForEach(sCurrent, &nicInfo->nicList) {
884
 
      nicEntry = DblLnkLst_Container(sCurrent, NicEntry, links);
885
 
      if (Str_Strcasecmp(macAddress, nicEntry->nicEntryProto.macAddress) == 0) {
886
 
         return nicEntry;
 
768
   u_int i;
 
769
 
 
770
   for (i = 0; i < nicInfo->nics.nics_len; i++) {
 
771
      GuestNic *nic = &nicInfo->nics.nics_val[i];
 
772
      if (strncmp(nic->macAddress, macAddress, NICINFO_MAC_LEN) == 0) {
 
773
         return nic;
887
774
      }
888
775
   }
889
776
 
911
798
 */
912
799
 
913
800
Bool
914
 
NicInfoChanged(GuestNicInfo *nicInfo)     // IN:
 
801
NicInfoChanged(GuestNicList *nicInfo)  // IN
915
802
{
916
 
   char *currentMac;
917
 
   GuestNicInfo *cachedNicInfo;
918
 
   NicEntry *cachedNic;
919
 
   DblLnkLst_Links *cachedNicLink;
920
 
 
921
 
   cachedNicInfo = &gInfoCache.nicInfo;
922
 
   cachedNic = DblLnkLst_Container(cachedNicInfo->nicList.next,
923
 
                                   NicEntry,
924
 
                                   links);
925
 
 
926
 
   if (cachedNicInfo->nicInfoProto.numNicEntries !=
927
 
       nicInfo->nicInfoProto.numNicEntries) {
 
803
   u_int i;
 
804
   GuestNicList *cachedNicInfo = &gInfoCache.nicInfo;
 
805
 
 
806
   if (cachedNicInfo->nics.nics_len != nicInfo->nics.nics_len) {
928
807
      Debug("GuestInfo: number of nics has changed\n");
929
808
      return TRUE;
930
809
   }
931
810
 
932
 
   /* Have any MAC or IP addresses been modified? */
933
 
   DblLnkLst_ForEach(cachedNicLink, &cachedNicInfo->nicList) {
934
 
      NicEntry *matchedNIC;
935
 
      DblLnkLst_Links *curCachedIpLink;
936
 
 
937
 
      cachedNic = DblLnkLst_Container(cachedNicLink, NicEntry, links);
938
 
      currentMac = cachedNic->nicEntryProto.macAddress;
 
811
   for (i = 0; i < cachedNicInfo->nics.nics_len; i++) {
 
812
      u_int j;
 
813
      GuestNic *cachedNic = &cachedNicInfo->nics.nics_val[i];
 
814
      GuestNic *matchedNic;
939
815
 
940
816
      /* Find the corresponding nic in the new nic info. */
941
 
      matchedNIC = GuestInfoFindMacAddress(nicInfo, currentMac);
 
817
      matchedNic = GuestInfoFindMacAddress(nicInfo, cachedNic->macAddress);
942
818
 
943
 
      if (NULL == matchedNIC) {
 
819
      if (NULL == matchedNic) {
944
820
         /* This mac address has been deleted. */
945
821
         return TRUE;
946
822
      }
947
823
 
948
 
      if (matchedNIC->nicEntryProto.numIPs != cachedNic->nicEntryProto.numIPs) {
 
824
      if (matchedNic->ips.ips_len != cachedNic->ips.ips_len) {
949
825
         Debug("GuestInfo: count of ip addresses for mac %d\n",
950
 
                                                matchedNIC->nicEntryProto.numIPs);
 
826
                                                matchedNic->ips.ips_len);
951
827
         return TRUE;
952
828
      }
953
829
 
954
830
      /* Which IP addresses have been modified for this NIC? */
955
 
      DblLnkLst_ForEach(curCachedIpLink, &cachedNic->ipAddressList) {
956
 
         char *currentCachedIp;
957
 
         VmIpAddressEntry *cachedIpAddress;
958
 
         DblLnkLst_Links * matchedIpAddressLink;
 
831
      for (j = 0; j < cachedNic->ips.ips_len; j++) {
 
832
         VmIpAddress *cachedIp = &cachedNic->ips.ips_val[j];
959
833
         Bool foundIP = FALSE;
960
 
 
961
 
         cachedIpAddress = DblLnkLst_Container(curCachedIpLink,
962
 
                                               VmIpAddressEntry,
963
 
                                               links);
964
 
 
965
 
         if (cachedIpAddress) {
966
 
            currentCachedIp = cachedIpAddress->ipEntryProto.ipAddress;
967
 
         } else {
968
 
            break;
969
 
         }
970
 
 
971
 
         DblLnkLst_ForEach(matchedIpAddressLink, &matchedNIC->ipAddressList) {
972
 
            VmIpAddressEntry *matchedIpAddressEntry =
973
 
                                  DblLnkLst_Container(matchedIpAddressLink,
974
 
                                                      VmIpAddressEntry,
975
 
                                                      links);
976
 
 
977
 
            if (matchedIpAddressEntry) {
978
 
               if (strncmp(matchedIpAddressEntry->ipEntryProto.ipAddress,
979
 
                           currentCachedIp,
980
 
                           IP_ADDR_SIZE_V2) == 0) {
981
 
                  foundIP = TRUE;
982
 
                  break;
983
 
               }
984
 
            } else {
 
834
         u_int k;
 
835
 
 
836
         for (k = 0; k < matchedNic->ips.ips_len; k++) {
 
837
            VmIpAddress *matchedIp = &matchedNic->ips.ips_val[k];
 
838
            if (0 == strncmp(cachedIp->ipAddress,
 
839
                             matchedIp->ipAddress,
 
840
                             NICINFO_MAX_IP_LEN)) {
 
841
               foundIP = TRUE;
985
842
               break;
986
843
            }
987
844
         }
988
845
 
989
 
 
990
846
         if (FALSE == foundIP) {
991
847
            /* This ip address couldn't be found and has been modified. */
992
 
            Debug("GuestInfo: mac address %s, ipaddress %s deleted\n", currentMac,
993
 
                  currentCachedIp);
 
848
            Debug("GuestInfo: mac address %s, ipaddress %s deleted\n",
 
849
                  cachedNic->macAddress,
 
850
                  cachedIp->ipAddress);
994
851
            return TRUE;
995
852
         }
996
853
 
1003
860
 
1004
861
 
1005
862
/*
1006
 
 *----------------------------------------------------------------------
1007
 
 *
1008
 
 * GuestInfoSerializeNicInfo --
1009
 
 *
1010
 
 *      Now that GuestNicInfo is not fixed size, serialize nicInfo into a
1011
 
 *      buffer, in order to send it over wire.
1012
 
 *
1013
 
 * Results:
1014
 
 *
1015
 
 *      TRUE if successful, FALSE otherwise.
1016
 
 *
1017
 
 * Side effects:
1018
 
 *
1019
 
 *      None.
1020
 
 *
1021
 
 *----------------------------------------------------------------------
1022
 
 */
1023
 
 
1024
 
Bool
1025
 
GuestInfoSerializeNicInfo(GuestNicInfo *nicInfo,                      // IN
1026
 
                          char buffer[GUESTMSG_MAX_IN_SIZE],          // OUT
1027
 
                          size_t *bufferLen)                          // OUT
1028
 
{
1029
 
   char *buf;
1030
 
   char *info;
1031
 
   size_t entrySize;
1032
 
   DblLnkLst_Links *nicEntryLink;
1033
 
 
1034
 
   ASSERT_ON_COMPILE(sizeof nicInfo->nicInfoProto.version == 4);
1035
 
   ASSERT_ON_COMPILE(offsetof(NicInfoProtocol, nicEntrySizeOnWire) == 4);
1036
 
   ASSERT_ON_COMPILE(sizeof nicInfo->nicInfoProto.nicEntrySizeOnWire == 4);
1037
 
   ASSERT_ON_COMPILE(offsetof(NicInfoProtocol, numNicEntries) == 8);
1038
 
   ASSERT_ON_COMPILE(sizeof nicInfo->nicInfoProto.numNicEntries == 4);
1039
 
   ASSERT_ON_COMPILE(offsetof(NicInfoProtocol, totalInfoSizeOnWire) == 12);
1040
 
   ASSERT_ON_COMPILE(sizeof nicInfo->nicInfoProto.totalInfoSizeOnWire == 4);
1041
 
 
1042
 
   if ((NULL == nicInfo) ||
1043
 
       (NULL == buffer ) ||
1044
 
       (NULL == bufferLen)) {
1045
 
      return FALSE;
1046
 
   }
1047
 
 
1048
 
   if (0 == nicInfo->nicInfoProto.numNicEntries) {
1049
 
      return FALSE;
1050
 
   }
1051
 
 
1052
 
   nicInfo->nicInfoProto.totalInfoSizeOnWire = 0;
1053
 
   nicInfo->nicInfoProto.nicEntrySizeOnWire = sizeof(NicEntryProtocol);
1054
 
 
1055
 
   buf = buffer;
1056
 
   info = (char *)(&nicInfo->nicInfoProto);
1057
 
   entrySize = sizeof nicInfo->nicInfoProto;
1058
 
 
1059
 
   memcpy(buf, info, entrySize);
1060
 
   nicInfo->nicInfoProto.totalInfoSizeOnWire += entrySize;
1061
 
 
1062
 
   buf += entrySize;
1063
 
 
1064
 
   DblLnkLst_ForEach(nicEntryLink, &nicInfo->nicList) {
1065
 
      NicEntry *nicEntry;
1066
 
      DblLnkLst_Links *ipAddrLink;
1067
 
      VmIpAddressEntry *ipAddressCur;
1068
 
      char *nicEntryBuf = buf;
1069
 
 
1070
 
      nicEntry = DblLnkLst_Container(nicEntryLink, NicEntry, links);
1071
 
      nicEntry->nicEntryProto.totalNicEntrySizeOnWire = 0;
1072
 
      nicEntry->nicEntryProto.ipAddressSizeOnWire = sizeof(VmIpAddressEntryProtocol);
1073
 
 
1074
 
      info = (char *)(&nicEntry->nicEntryProto);
1075
 
 
1076
 
      entrySize = sizeof nicEntry->nicEntryProto;
1077
 
 
1078
 
       /* to prevent buffer overflow */
1079
 
      if (buf + entrySize - buffer < GUESTMSG_MAX_IN_SIZE) {
1080
 
         memcpy(buf, info, entrySize);
1081
 
         nicEntry->nicEntryProto.totalNicEntrySizeOnWire += entrySize;
1082
 
         nicInfo->nicInfoProto.totalInfoSizeOnWire += entrySize;
1083
 
      } else {
1084
 
         return FALSE;
1085
 
      }
1086
 
 
1087
 
      buf += entrySize;
1088
 
 
1089
 
      entrySize = sizeof ipAddressCur->ipEntryProto;
1090
 
 
1091
 
      DblLnkLst_ForEach(ipAddrLink, &nicEntry->ipAddressList) {
1092
 
         ipAddressCur = DblLnkLst_Container(ipAddrLink, VmIpAddressEntry, links);
1093
 
         ipAddressCur->ipEntryProto.totalIpEntrySizeOnWire = 0;
1094
 
         info = (char *)(&ipAddressCur->ipEntryProto);
1095
 
 
1096
 
         if (info) {
1097
 
            /* to prevent buffer overflow */
1098
 
            if (buf + entrySize - buffer < GUESTMSG_MAX_IN_SIZE) {
1099
 
               memcpy(buf, info, entrySize);
1100
 
               ipAddressCur->ipEntryProto.totalIpEntrySizeOnWire +=
1101
 
                  entrySize;
1102
 
               nicEntry->nicEntryProto.totalNicEntrySizeOnWire +=
1103
 
                  entrySize;
1104
 
               nicInfo->nicInfoProto.totalInfoSizeOnWire +=
1105
 
                  entrySize;
1106
 
            } else {
1107
 
               return FALSE;
1108
 
            }
1109
 
         }
1110
 
 
1111
 
         /*
1112
 
          * Update total size portion that was just calculated.
1113
 
          */
1114
 
         memcpy(buf + offsetof(VmIpAddressEntryProtocol,
1115
 
                               totalIpEntrySizeOnWire),
1116
 
                &ipAddressCur->ipEntryProto.totalIpEntrySizeOnWire,
1117
 
                sizeof ipAddressCur->ipEntryProto.totalIpEntrySizeOnWire);
1118
 
         buf += entrySize;
1119
 
      }
1120
 
      /*
1121
 
       * Update total size portion that was just calculated.
1122
 
       */
1123
 
      memcpy(nicEntryBuf + offsetof(NicEntryProtocol ,
1124
 
                                    totalNicEntrySizeOnWire),
1125
 
             &nicEntry->nicEntryProto.totalNicEntrySizeOnWire,
1126
 
             sizeof nicEntry->nicEntryProto.totalNicEntrySizeOnWire);
1127
 
   }
1128
 
 
1129
 
   *bufferLen = buf + entrySize - buffer;
1130
 
 
1131
 
   /*
1132
 
    * Update total size portion that was just calculated.
1133
 
    */
1134
 
   memcpy(buffer + offsetof(NicInfoProtocol, totalInfoSizeOnWire),
1135
 
          &nicInfo->nicInfoProto.totalInfoSizeOnWire,
1136
 
          sizeof nicInfo->nicInfoProto.totalInfoSizeOnWire);
1137
 
 
1138
 
   return TRUE;
1139
 
}
1140
 
 
1141
 
 
1142
 
/*
1143
863
 *----------------------------------------------------------------------------
1144
864
 *
1145
865
 * PrintNicInfo --
1157
877
 */
1158
878
 
1159
879
int
1160
 
PrintNicInfo(GuestNicInfo *nicInfo,                    // IN
1161
 
             int (*PrintFunc)(const char *, ...))      // IN
 
880
PrintNicInfo(GuestNicList *nicInfo,               // IN
 
881
             int (*PrintFunc)(const char *, ...)) // IN
1162
882
{
1163
883
   int ret = 0;
1164
 
   uint32 i = 0;
1165
 
   DblLnkLst_Links *nicEntryLink;
1166
 
 
1167
 
 
1168
 
   ret += PrintFunc("GuestNicInfo: count: %d\n", nicInfo->nicInfoProto.numNicEntries);
1169
 
   DblLnkLst_ForEach(nicEntryLink, &nicInfo->nicList) {
1170
 
      uint32 j = 0;
1171
 
      DblLnkLst_Links *ipAddrLink;
1172
 
      NicEntry *nicEntry = DblLnkLst_Container(nicEntryLink,
1173
 
                                               NicEntry,
1174
 
                                               links);
1175
 
      if (nicEntry) {
1176
 
         ret += PrintFunc("GuestNicInfo: nic [%d/%d] mac:      %s",
 
884
   u_int i = 0;
 
885
 
 
886
   ret += PrintFunc("NicInfo: count: %ud\n", nicInfo->nics.nics_len);
 
887
   for (i = 0; i < nicInfo->nics.nics_len; i++) {
 
888
      u_int j;
 
889
      GuestNic *nic = &nicInfo->nics.nics_val[i];
 
890
 
 
891
      ret += PrintFunc("NicInfo: nic [%d/%d] mac:      %s",
 
892
                       i+1,
 
893
                       nicInfo->nics.nics_len,
 
894
                       nic->macAddress);
 
895
 
 
896
      for (j = 0; j < nic->ips.ips_len; j++) {
 
897
         VmIpAddress *ipAddress = &nic->ips.ips_val[j];
 
898
 
 
899
         ret += PrintFunc("NicInfo: nic [%d/%d] IP [%d/%d]: %s",
1177
900
                          i+1,
1178
 
                          nicInfo->nicInfoProto.numNicEntries,
1179
 
                          nicEntry->nicEntryProto.macAddress);
1180
 
      } else {
1181
 
         break;
1182
 
      }
1183
 
 
1184
 
      DblLnkLst_ForEach(ipAddrLink, &nicEntry->ipAddressList) {
1185
 
         VmIpAddressEntry *ipAddress = DblLnkLst_Container(
1186
 
                                                       ipAddrLink,
1187
 
                                                       VmIpAddressEntry,
1188
 
                                                       links);
1189
 
 
1190
 
         if (ipAddress) {
1191
 
            ret += PrintFunc("GuestNicInfo: nic [%d/%d] IP [%d/%d]: %s",
1192
 
                             i+1,
1193
 
                             nicInfo->nicInfoProto.numNicEntries,
1194
 
                             j+1,
1195
 
                             nicEntry->nicEntryProto.numIPs,
1196
 
                             ipAddress->ipEntryProto.ipAddress);
1197
 
         } else {
1198
 
            break;
1199
 
         }
1200
 
         j++;
1201
 
      }
1202
 
      i++;
 
901
                          nicInfo->nics.nics_len,
 
902
                          j+1,
 
903
                          nic->ips.ips_len,
 
904
                          ipAddress->ipAddress);
 
905
      }
1203
906
   }
1204
907
 
1205
908
   return ret;
1389
1092
      gInfoCache.value[i][0] = 0;
1390
1093
   }
1391
1094
 
1392
 
   GuestInfo_FreeDynamicMemoryInNicInfo(&gInfoCache.nicInfo);
1393
 
 
1394
 
   gInfoCache.nicInfo.nicInfoProto.numNicEntries = 0;
1395
 
   gInfoCache.diskInfo.numEntries = 0;
1396
 
 
1397
 
   if (gInfoCache.diskInfo.partitionList != NULL) {
1398
 
      free(gInfoCache.diskInfo.partitionList);
1399
 
      gInfoCache.diskInfo.partitionList = NULL;
1400
 
   }
 
1095
   VMX_XDR_FREE(xdr_GuestNicList, &gInfoCache.nicInfo);
 
1096
   memset(&gInfoCache.nicInfo, 0, sizeof gInfoCache.nicInfo);
1401
1097
}
1402
1098
 
1403
1099
 
1485
1181
 *      initialized with the input parameter
1486
1182
 *
1487
1183
 * Results:
1488
 
 *      newly allocated NicEntry
 
1184
 *      Newly allocated GuestNic, NULL on failure.
1489
1185
 *
1490
1186
 * Side effects:
1491
 
 *           All linked list in the new entry is initialized. Number of Nic
1492
 
 *      entries is bumped up by 1.
 
1187
 *           Number of Nic entries is bumped up by 1.
1493
1188
 *----------------------------------------------------------------------
1494
1189
 */
1495
1190
 
1496
 
NicEntry *
1497
 
GuestInfoAddNicEntry(GuestNicInfo *nicInfo,                       // IN/OUT
1498
 
                     const char macAddress[MAC_ADDR_SIZE])        // IN
 
1191
GuestNic *
 
1192
GuestInfoAddNicEntry(GuestNicList *nicInfo,                    // IN/OUT
 
1193
                     const char macAddress[NICINFO_MAC_LEN])   // IN
1499
1194
{
1500
 
   NicEntry   *nicEntryCur = NULL;
1501
 
 
1502
 
   nicEntryCur = Util_SafeCalloc(1, sizeof(*nicEntryCur));
1503
 
   DblLnkLst_Init(&nicEntryCur->ipAddressList);
1504
 
   DblLnkLst_Init(&nicEntryCur->links);
1505
 
   DblLnkLst_LinkLast(&nicInfo->nicList, &nicEntryCur->links);
1506
 
 
1507
 
   Str_Strcpy(nicEntryCur->nicEntryProto.macAddress, macAddress, MAC_ADDR_SIZE);
1508
 
   nicInfo->nicInfoProto.numNicEntries++;
1509
 
 
1510
 
   return nicEntryCur;
 
1195
   GuestNic *newNic;
 
1196
 
 
1197
   newNic = XDRUTIL_ARRAYAPPEND(nicInfo, nics, 1);
 
1198
   if (newNic != NULL) {
 
1199
      Str_Strcpy(newNic->macAddress, macAddress, sizeof newNic->macAddress);
 
1200
   }
 
1201
 
 
1202
   return newNic;
1511
1203
}
1512
1204
 
1513
1205
 
1516
1208
 *
1517
1209
 * GuestInfoAddIpAddress --
1518
1210
 *
1519
 
 *      Add an IP address entry into NicEntry
 
1211
 *      Add an IP address entry into the GuestNic.
1520
1212
 *
1521
1213
 * Results:
1522
 
 *      Newly allocated IP address Entry
 
1214
 *      Newly allocated IP address struct.
1523
1215
 *
1524
1216
 * Side effects:
1525
 
 *           Linked list in the new IP address entry is initialized.Number
1526
 
 *      of IP addresses on the NIC is bumped up by 1
 
1217
 *      Number of IP addresses on the NIC is bumped up by 1.
1527
1218
 *
1528
1219
 *----------------------------------------------------------------------
1529
1220
 */
1530
1221
 
1531
 
VmIpAddressEntry *
1532
 
GuestInfoAddIpAddress(NicEntry *nicEntry,               // IN/OUT
 
1222
VmIpAddress *
 
1223
GuestInfoAddIpAddress(GuestNic *nic,                    // IN/OUT
1533
1224
                      const char *ipAddr,               // IN
1534
1225
                      const uint32 af_type)             // IN
1535
1226
{
1536
 
   VmIpAddressEntry *ipAddressCur;
1537
 
 
1538
 
   ipAddressCur = Util_SafeCalloc(1, sizeof *ipAddressCur);
1539
 
   DblLnkLst_Init(&ipAddressCur->links);
1540
 
   DblLnkLst_LinkLast(&nicEntry->ipAddressList, &ipAddressCur->links);
1541
 
   memcpy(ipAddressCur->ipEntryProto.ipAddress, ipAddr, IP_ADDR_SIZE_V2);
1542
 
   ipAddressCur->ipEntryProto.addressFamily = af_type;
1543
 
 
1544
 
   nicEntry->nicEntryProto.numIPs++;
1545
 
 
1546
 
   return ipAddressCur;
 
1227
   VmIpAddress *ip;
 
1228
 
 
1229
   ip = XDRUTIL_ARRAYAPPEND(nic, ips, 1);
 
1230
   if (ip != NULL) {
 
1231
      Str_Strcpy(ip->ipAddress, ipAddr, sizeof ip->ipAddress);
 
1232
      ip->addressFamily = af_type;
 
1233
   }
 
1234
 
 
1235
   return ip;
1547
1236
}
1548
1237
 
1549
1238
 
1552
1241
 *
1553
1242
 * GuestInfoAddSubnetMask --
1554
1243
 *
1555
 
 *      Add an IPV4 subnet mask to the IpAddressEntry in ASCII form
 
1244
 *      Add an IPV4 subnet mask to the IpAddress in ASCII form.
1556
1245
 *
1557
1246
 * Results:
1558
1247
 *      The 'n' bits subnet mask is converted to an ASCII string as a
1565
1254
 */
1566
1255
 
1567
1256
void
1568
 
GuestInfoAddSubnetMask(VmIpAddressEntry *ipAddressEntry,       // IN/OUT
 
1257
GuestInfoAddSubnetMask(VmIpAddress *ipAddressEntry,            // IN/OUT
1569
1258
                       const uint32 subnetMaskBits)            // IN
1570
1259
{
1571
1260
   int i;
1583
1272
   }
1584
1273
 
1585
1274
   // Convert the hexadecimal value to a string and add to the IpAddress Entry
1586
 
   Str_Sprintf(ipAddressEntry->ipEntryProto.subnetMask,
1587
 
               sizeof ipAddressEntry->ipEntryProto.subnetMask,
 
1275
   Str_Sprintf(ipAddressEntry->subnetMask,
 
1276
               sizeof ipAddressEntry->subnetMask,
1588
1277
               "0x%x", subnetMask);
1589
1278
 
1590
1279
   return;