d914932403a313c84245c948026edd407fb19b86
[openafs.git] / src / libadmin / samples / cm_list_cells.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * This file contains sample code for the client admin interface 
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID
18     ("$Header$");
19
20 #ifdef AFS_NT40_ENV
21 #include <winsock2.h>
22 #include <pthread.h>
23 #endif
24 #include <afs/afs_Admin.h>
25 #include <afs/afs_clientAdmin.h>
26 #include <afs/afs_utilAdmin.h>
27
28 void
29 Usage()
30 {
31     fprintf(stderr, "Usage: cm_list_cells <host> <port>\n");
32     exit(1);
33 }
34
35 void
36 ParseArgs(int argc, char *argv[], char **srvrName, long *srvrPort)
37 {
38     char **argp = argv;
39
40     if (!*(++argp))
41         Usage();
42     *srvrName = *(argp++);
43     if (!*(argp))
44         Usage();
45     *srvrPort = strtol(*(argp++), NULL, 0);
46     if (*srvrPort <= 0 || *srvrPort >= 65536)
47         Usage();
48     if (*(argp))
49         Usage();
50 }
51
52 int
53 main(int argc, char *argv[])
54 {
55     int rc;
56     afs_status_t st = 0;
57     struct rx_connection *conn;
58     char *srvrName;
59     long srvrPort;
60     void *cellHandle;
61     afs_CMListCell_t cellInfo;
62     void *iterator;
63     afs_uint32 taddr;
64     int i;
65
66     ParseArgs(argc, argv, &srvrName, &srvrPort);
67
68     rc = afsclient_Init(&st);
69     if (!rc) {
70         fprintf(stderr, "afsclient_Init, status %d\n", st);
71         exit(1);
72     }
73
74     rc = afsclient_NullCellOpen(&cellHandle, &st);
75     if (!rc) {
76         fprintf(stderr, "afsclient_NullCellsOpen, status %d\n", st);
77         exit(1);
78     }
79
80     rc = afsclient_CMStatOpenPort(cellHandle, srvrName, srvrPort, &conn, &st);
81     if (!rc) {
82         fprintf(stderr, "afsclient_CMStatOpenPort, status %d\n", st);
83         exit(1);
84     }
85
86     rc = util_CMListCellsBegin(conn, &iterator, &st);
87     if (!rc) {
88         fprintf(stderr, "util_CMListCellsBegin, status %d\n", st);
89         exit(1);
90     }
91
92     printf("\n");
93     while (util_CMListCellsNext(iterator, &cellInfo, &st)) {
94         printf("Cell %s on hosts", cellInfo.cellname);
95         for (i = 0; i < UTIL_MAX_CELL_HOSTS && cellInfo.serverAddr[i]; i++) {
96             taddr = cellInfo.serverAddr[i];
97             printf(" %d.%d.%d.%d", (taddr >> 24) & 0xff, (taddr >> 16) & 0xff,
98                    (taddr >> 8) & 0xff, taddr & 0xff);
99         }
100         printf("\n");
101     }
102     if (st != ADMITERATORDONE) {
103         fprintf(stderr, "util_CMListCellsNext, status %d\n", st);
104         exit(1);
105     }
106     printf("\n");
107
108     rc = util_CMListCellsDone(iterator, &st);
109     if (!rc) {
110         fprintf(stderr, "util_CMListCellsDone, status %d\n", st);
111         exit(1);
112     }
113
114     rc = afsclient_CMStatClose(conn, &st);
115     if (!rc) {
116         fprintf(stderr, "afsclient_CMStatClose, status %d\n", st);
117         exit(1);
118     }
119
120     rc = afsclient_CellClose(cellHandle, &st);
121     if (!rc) {
122         fprintf(stderr, "afsclient_CellClose, status %d\n", st);
123         exit(1);
124     }
125
126     exit(0);
127 }