2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
24 #define STRSAFE_NO_DEPRECATE
29 osi_rwlock_t cm_cellLock;
31 /* function called as callback proc from cm_SearchCellFile. Return 0 to
32 * continue processing.
34 * At the present time the return value is ignored by the caller.
36 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short adminRank)
41 cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
44 cellp = cellrockp->cellp;
45 probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
47 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL,
48 probe ? 0 : CM_FLAG_NOPROBE);
51 tsp->adminRank = adminRank;
53 /* Insert the vlserver into a sorted list, sorted by server rank */
54 tsrp = cm_NewServerRef(tsp, 0);
55 cm_InsertServerList(&cellp->vlServersp, tsrp);
60 /* if it's from DNS, see if it has expired
61 * and check to make sure we have a valid set of volume servers
62 * this function must not be called with a lock on cm_cellLock
64 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
68 afs_uint32 mxheld = 0;
73 lock_ObtainMutex(&cp->mx);
76 #ifdef AFS_FREELANCE_CLIENT
77 if (cp->flags & CM_CELLFLAG_FREELANCE) {
78 lock_ReleaseMutex(&cp->mx);
83 if (cm_IsServerListEmpty(cp->vlServersp) ||
84 (time(0) > cp->timeout) ||
86 (cp->flags & CM_CELLFLAG_DNS) &&
87 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID))))
89 lock_ReleaseMutex(&cp->mx);
92 /* must empty cp->vlServersp */
94 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
98 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
99 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
100 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
102 lock_ObtainMutex(&cp->mx);
104 cp->timeout = time(0) + 7200;
110 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
111 if (code == 0) { /* got cell from DNS */
112 lock_ObtainMutex(&cp->mx);
114 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
115 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
116 cp->timeout = time(0) + ttl;
118 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
121 /* if we fail to find it this time, we'll just do nothing and leave the
122 * current entry alone
124 lock_ObtainMutex(&cp->mx);
126 _InterlockedOr(&cp->flags, CM_CELLFLAG_VLSERVER_INVALID);
133 cm_RandomizeServer(&cp->vlServersp);
136 lock_ReleaseMutex(&cp->mx);
138 return code ? NULL : cp;
141 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
142 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
144 return cm_GetCell_Gen(namep, NULL, flags);
147 void cm_FreeCell(cm_cell_t *cellp)
149 lock_AssertWrite(&cm_cellLock);
151 if (cellp->vlServersp)
152 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
153 cellp->name[0] = '\0';
155 cellp->freeNextp = cm_data.freeCellsp;
156 cm_data.freeCellsp = cellp;
159 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
163 char fullname[CELL_MAXNAMELEN]="";
164 char linkedName[CELL_MAXNAMELEN]="";
165 char name[CELL_MAXNAMELEN]="";
166 int hasWriteLock = 0;
174 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
178 * Strip off any trailing dots at the end of the cell name.
179 * Failure to do so results in an undesireable alias as the
180 * result of DNS AFSDB record lookups where a trailing dot
181 * has special meaning.
183 strncpy(name, namep, CELL_MAXNAMELEN);
184 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
190 namelen = strlen(namep);
192 hash = CM_CELL_NAME_HASH(namep);
194 lock_ObtainRead(&cm_cellLock);
195 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
196 if (cm_stricmp_utf8(namep, cp->name) == 0) {
197 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
198 fullname[CELL_MAXNAMELEN-1] = '\0';
204 /* the name wasn't found but it might be a matching prefix */
205 for (cp = cm_data.allCellsp, cp2 = NULL; cp; cp=cp->allNextp) {
206 if (strnicmp(namep, cp->name, namelen) == 0
207 && (namelen == strlen(cp->name) || cp->name[namelen] == '.')) {
210 "cm_GetCell_gen ambiguous prefix match: prefix %s matches (%u) %s and (%u) %s",
211 osi_LogSaveString(afsd_logp,namep),
213 osi_LogSaveString(afsd_logp,cp->name),
215 osi_LogSaveString(afsd_logp,cp2->name));
217 lock_ReleaseRead(&cm_cellLock);
225 /* an unambiguous prefix match was found */
227 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
228 fullname[CELL_MAXNAMELEN-1] = '\0';
233 lock_ReleaseRead(&cm_cellLock);
234 cm_UpdateCell(cp, flags);
235 } else if (flags & CM_FLAG_CREATE) {
236 lock_ConvertRToW(&cm_cellLock);
239 /* when we dropped the lock the cell could have been added
240 * to the list so check again while holding the write lock
242 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
243 if (cm_stricmp_utf8(namep, cp->name) == 0) {
244 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
245 fullname[CELL_MAXNAMELEN-1] = '\0';
250 /* the name wasn't found but it might be a matching prefix */
251 for (cp = cm_data.allCellsp, cp2 = NULL; cp; cp=cp->allNextp) {
252 if (strnicmp(namep, cp->name, namelen) == 0
253 && (namelen == strlen(cp->name) || cp->name[namelen] == '.')) {
256 "cm_GetCell_gen ambiguous prefix match: prefix %s matches (%u) %s and (%u) %s",
257 osi_LogSaveString(afsd_logp,namep),
259 osi_LogSaveString(afsd_logp,cp->name),
261 osi_LogSaveString(afsd_logp,cp2->name));
270 /* an unambiguous prefix match was found */
272 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
273 fullname[CELL_MAXNAMELEN-1] = '\0';
277 if ( cm_data.freeCellsp != NULL ) {
278 cp = cm_data.freeCellsp;
279 cm_data.freeCellsp = cp->freeNextp;
282 * The magic, cellID, and mx fields are already set.
285 if ( cm_data.currentCells >= cm_data.maxCells )
286 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
289 * the cellID cannot be 0.
290 * If there is a name collision, one of the entries
291 * will end up on cm_data.freeCellsp for reuse.
293 cellID = InterlockedIncrement(&cm_data.currentCells);
294 cp = &cm_data.cellBaseAddress[cellID - 1];
295 memset(cp, 0, sizeof(cm_cell_t));
296 cp->magic = CM_CELL_MAGIC;
299 /* otherwise we found the cell, and so we're nearly done */
300 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
303 lock_ReleaseWrite(&cm_cellLock);
308 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
309 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
310 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
312 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
313 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
314 osi_LogSaveString(afsd_logp,linkedName));
319 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
321 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
322 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
323 lock_ObtainMutex(&cp->mx);
324 lock_ObtainWrite(&cm_cellLock);
326 cm_RemoveCellFromIDHashTable(cp);
327 cm_RemoveCellFromNameHashTable(cp);
328 lock_ReleaseMutex(&cp->mx);
332 } else { /* got cell from DNS */
333 lock_ObtainMutex(&cp->mx);
335 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
336 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
337 cp->timeout = time(0) + ttl;
342 lock_ObtainMutex(&cp->mx);
343 lock_ObtainWrite(&cm_cellLock);
345 cm_RemoveCellFromIDHashTable(cp);
346 cm_RemoveCellFromNameHashTable(cp);
347 lock_ReleaseMutex(&cp->mx);
353 lock_ObtainMutex(&cp->mx);
355 cp->timeout = time(0) + 7200; /* two hour timeout */
358 /* we have now been given the fullname of the cell. It may
359 * be that we already have a cell with that name. If so,
360 * we should use it instead of completing the allocation
363 lock_ObtainRead(&cm_cellLock);
364 hash = CM_CELL_NAME_HASH(fullname);
365 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
366 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
375 lock_ObtainMutex(&cp->mx);
378 lock_ConvertRToW(&cm_cellLock);
380 cm_RemoveCellFromIDHashTable(cp);
381 cm_RemoveCellFromNameHashTable(cp);
382 lock_ReleaseMutex(&cp->mx);
385 /* Replace cells references in servers */
386 lock_ObtainRead(&cm_serverLock);
387 for (tsp = cm_serversAllFirstp;
389 tsp = (cm_server_t *)osi_QNext(&tsp->allq)) {
390 if (tsp->cellp == cp) {
391 osi_Log4(afsd_logp, "cm_GetCell_gen race: replacing cell (%u) %s with cell (%u) %s",
393 osi_LogSaveString(afsd_logp,cp->name),
395 osi_LogSaveString(afsd_logp,cp2->name));
399 lock_ReleaseRead(&cm_serverLock);
405 lock_ReleaseRead(&cm_cellLock);
407 /* randomise among those vlservers having the same rank*/
408 cm_RandomizeServer(&cp->vlServersp);
411 lock_ObtainMutex(&cp->mx);
414 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
415 cp->name[CELL_MAXNAMELEN-1] = '\0';
417 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
418 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
420 lock_ObtainWrite(&cm_cellLock);
422 cm_AddCellToNameHashTable(cp);
423 cm_AddCellToIDHashTable(cp);
424 lock_ReleaseMutex(&cp->mx);
427 /* append cell to global list */
428 if (cm_data.allCellsp == NULL) {
429 cm_data.allCellsp = cp;
431 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
438 lock_ReleaseRead(&cm_cellLock);
442 lock_ReleaseMutex(&cp->mx);
444 lock_ReleaseWrite(&cm_cellLock);
446 /* fullname is not valid if cp == NULL */
449 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
450 newnamep[CELL_MAXNAMELEN-1]='\0';
456 if (cp && cp->linkedName[0]) {
457 cm_cell_t * linkedCellp = NULL;
459 if (!strcmp(cp->name, cp->linkedName)) {
460 cp->linkedName[0] = '\0';
461 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
462 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
464 lock_ObtainWrite(&cm_cellLock);
466 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
467 cp->linkedName[0] = '\0';
469 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
470 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
472 lock_ReleaseWrite(&cm_cellLock);
478 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
483 lock_ObtainRead(&cm_cellLock);
485 hash = CM_CELL_ID_HASH(cellID);
487 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
488 if (cellID == cp->cellID)
491 lock_ReleaseRead(&cm_cellLock);
494 cm_UpdateCell(cp, flags);
500 cm_ValidateCell(void)
503 afs_uint32 count1, count2;
505 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
507 if ( cellp < (cm_cell_t *)cm_data.cellBaseAddress ||
508 cellp >= (cm_cell_t *)cm_data.aclBaseAddress) {
509 afsi_log("cm_ValidateCell failure: out of range cm_cell_t pointers");
510 fprintf(stderr, "cm_ValidateCell failure: out of range cm_cell_t pointers\n");
514 if ( cellp->magic != CM_CELL_MAGIC ) {
515 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
516 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
519 if ( count1 != 0 && cellp == cm_data.allCellsp ||
520 count1 > cm_data.maxCells ) {
521 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
522 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
527 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
529 if ( cellp < (cm_cell_t *)cm_data.cellBaseAddress ||
530 cellp >= (cm_cell_t *)cm_data.aclBaseAddress) {
531 afsi_log("cm_ValidateCell failure: out of range cm_cell_t pointers");
532 fprintf(stderr, "cm_ValidateCell failure: out of range cm_cell_t pointers\n");
536 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
537 count2 > cm_data.maxCells ) {
538 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
539 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
544 if ( (count1 + count2) != cm_data.currentCells ) {
545 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
546 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
555 cm_ShutdownCell(void)
559 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
560 lock_FinalizeMutex(&cellp->mx);
566 void cm_InitCell(int newFile, long maxCells)
568 static osi_once_t once;
570 if (osi_Once(&once)) {
573 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
576 cm_data.allCellsp = NULL;
577 cm_data.currentCells = 0;
578 cm_data.maxCells = maxCells;
579 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
580 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
582 #ifdef AFS_FREELANCE_CLIENT
583 /* Generate a dummy entry for the Freelance cell whether or not
584 * freelance mode is being used in this session
587 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
588 memset(cellp, 0, sizeof(cm_cell_t));
589 cellp->magic = CM_CELL_MAGIC;
591 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
593 lock_ObtainMutex(&cellp->mx);
594 lock_ObtainWrite(&cm_cellLock);
597 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
598 cellp->name[CELL_MAXNAMELEN-1] = '\0';
600 /* thread on global list */
601 cellp->allNextp = cm_data.allCellsp;
602 cm_data.allCellsp = cellp;
604 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
605 cellp->vlServersp = NULL;
606 _InterlockedOr(&cellp->flags, CM_CELLFLAG_FREELANCE);
608 cm_AddCellToNameHashTable(cellp);
609 cm_AddCellToIDHashTable(cellp);
610 lock_ReleaseWrite(&cm_cellLock);
611 lock_ReleaseMutex(&cellp->mx);
614 lock_ObtainRead(&cm_cellLock);
615 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
616 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
617 cellp->vlServersp = NULL;
618 _InterlockedOr(&cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
620 lock_ReleaseRead(&cm_cellLock);
627 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
632 cp = tsp->cellp; /* cell that this vlserver belongs to */
634 lock_ObtainMutex(&cp->mx);
635 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
637 if ( !code ) /* if the server list was rearranged */
638 cm_RandomizeServer(&cp->vlServersp);
640 lock_ReleaseMutex(&cp->mx);
644 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
651 lock_ObtainRead(&cm_cellLock);
653 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
654 cookie, cm_data.currentCells, cm_data.maxCells);
655 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
657 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
658 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
659 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
660 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
663 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
664 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
667 lock_ReleaseRead(&cm_cellLock);
672 /* call with volume write-locked and mutex held */
673 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
677 lock_AssertWrite(&cm_cellLock);
678 lock_AssertMutex(&cellp->mx);
680 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
683 i = CM_CELL_NAME_HASH(cellp->name);
685 cellp->nameNextp = cm_data.cellNameHashTablep[i];
686 cm_data.cellNameHashTablep[i] = cellp;
687 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_NAMEHASH);
690 /* call with cell write-locked and mutex held */
691 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
697 lock_AssertWrite(&cm_cellLock);
698 lock_AssertMutex(&cellp->mx);
700 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
701 /* hash it out first */
702 i = CM_CELL_NAME_HASH(cellp->name);
703 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
705 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
706 if (tcellp == cellp) {
707 *lcellpp = cellp->nameNextp;
708 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_NAMEHASH);
709 cellp->nameNextp = NULL;
716 /* call with cell write-locked and mutex held */
717 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
721 lock_AssertWrite(&cm_cellLock);
722 lock_AssertMutex(&cellp->mx);
724 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
727 i = CM_CELL_ID_HASH(cellp->cellID);
729 cellp->idNextp = cm_data.cellIDHashTablep[i];
730 cm_data.cellIDHashTablep[i] = cellp;
731 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_IDHASH);
734 /* call with cell write-locked and mutex held */
735 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
741 lock_AssertWrite(&cm_cellLock);
742 lock_AssertMutex(&cellp->mx);
744 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
745 /* hash it out first */
746 i = CM_CELL_ID_HASH(cellp->cellID);
747 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
749 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
750 if (tcellp == cellp) {
751 *lcellpp = cellp->idNextp;
752 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_IDHASH);
753 cellp->idNextp = NULL;
761 cm_CreateCellWithInfo( char * cellname,
762 char * linked_cellname,
763 unsigned short vlport,
764 afs_uint32 host_count,
771 struct sockaddr_in vlSockAddr;
774 rock.cellp = cm_GetCell(cellname, CM_FLAG_CREATE | CM_FLAG_NOPROBE);
777 if (!(flags & CM_CELLFLAG_DNS)) {
780 for (i = 0; i < host_count; i++) {
781 thp = gethostbyname(hostname[i]);
784 * If there is at least one resolved vlserver or an authoritative,
785 * host not found response, destroy the prior list.
787 if (thp != NULL || WSAGetLastError() == WSAHOST_NOT_FOUND) {
788 cm_FreeServerList(&rock.cellp->vlServersp, CM_FREESERVERLIST_DELETE);
794 if (thp->h_addrtype != AF_INET)
797 for (j=0 ; thp->h_addr_list[j]; j++) {
798 memcpy(&vlSockAddr.sin_addr.s_addr,
801 vlSockAddr.sin_port = htons(vlport ? vlport : 7003);
802 vlSockAddr.sin_family = AF_INET;
803 cm_AddCellProc(&rock, &vlSockAddr, hostname[i], CM_FLAG_NOPROBE);
807 lock_ObtainMutex(&rock.cellp->mx);
808 _InterlockedAnd(&rock.cellp->flags, ~CM_CELLFLAG_DNS);
809 } else if (cm_dnsEnabled) {
811 cm_serverRef_t * vlServersp = NULL;
813 lock_ObtainWrite(&cm_serverLock);
814 vlServersp = rock.cellp->vlServersp;
815 rock.cellp->vlServersp = NULL;
816 lock_ReleaseWrite(&cm_serverLock);
818 code = cm_SearchCellByDNS(rock.cellp->name, NULL, &ttl, cm_AddCellProc, &rock);
819 lock_ObtainMutex(&rock.cellp->mx);
820 if (code == 0) { /* got cell from DNS */
821 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_DNS);
822 rock.cellp->timeout = time(0) + ttl;
824 fprintf(stderr, "cell %s: ttl=%d\n", rock.cellp->name, ttl);
827 lock_ObtainWrite(&cm_serverLock);
828 if (rock.cellp->vlServersp == NULL) {
829 rock.cellp->vlServersp = vlServersp;
832 lock_ReleaseWrite(&cm_serverLock);
835 cm_FreeServerList(&vlServersp, CM_FREESERVERLIST_DELETE);
836 if (vlServersp != NULL) {
838 * We moved the vlServer list out of the way and
839 * in the meantime it was replaced. If the vlServerp
840 * list is non-Empty after cm_FreeServerList was called
841 * it means that there are deleted entries with active
842 * references. Must put them back onto the list to
843 * avoid leaking the memory.
845 cm_AppendServerList(rock.cellp->vlServersp, &vlServersp);
848 cm_FreeServerList(&rock.cellp->vlServersp, CM_FREESERVERLIST_DELETE);
849 lock_ObtainMutex(&rock.cellp->mx);
850 rock.cellp->flags &= ~CM_CELLFLAG_DNS;
852 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
853 StringCbCopy(rock.cellp->linkedName, CELL_MAXNAMELEN, linked_cellname);
854 lock_ReleaseMutex(&rock.cellp->mx);
856 if (rock.cellp->vlServersp)
857 cm_RandomizeServer(&rock.cellp->vlServersp);