Debian Codegraph @

COSMIC/EXE/spmtable.c

raw
1 /* Spmtable: this program prints out a list of servers currently on the
2 * requested PortMaster
3 *
4 * spmtable machine machine machine ...
5 * spmtable
6 * spmtable -ENVVARNAME
7 */
8 #include <stdio.h>
9 #include "xtdio.h"
10 #include "sockets.h"
11
12 /* ----------------------------------------------------------------------
13 * Definitions:
14 */
15 #define BUFSIZE 256
16 #define MACHINE_SEP ':'
17 #define MAXTIME 5L /* max time for PM_TABLE response, in sec */
18
19 /* ----------------------------------------------------------------------
20 * Prototypes:
21 */
22 #ifdef __PROTOTYPE__
23 int main(int,char **); /* Spmtable.c */
24 void printPMtable(char *); /* Spmtable.c */
25 #else
26 extern main(); /* spmtable.c */
27 extern void printPMtable(); /* spmtable.c */
28 #endif
29
30
31 /* ---------------------------------------------------------------------- */
32
33 /* main: */
34 #ifdef __PROTOTYPE__
35 int main(int argc,char **argv)
36 #else
37 int main(argc,argv)
38 int argc;
39 char **argv;
40 #endif
41 {
42 char localhost[BUFSIZE];
43
44 rdcolor();
45
46 Sinit();
47
48 if(argc > 1) {
49 for(++argv; argc > 1; --argc, ++argv) { /* for every machine mentioned on the cmd line */
50 if(argv[0][0] == '-') { /* report on SKTPATH machines */
51 char *envvar= argv[0] + 1;
52 char *machines;
53 char *mend;
54
55 /* look up the SKTPATH */
56 machines= getenv(*envvar? envvar : "SKTPATH");
57 if(!machines) {
58 error(XTDIO_WARNING,"unable to getenv(%s%s%s)\n",
59 YELLOW,sprt(*envvar? envvar : "SKTPATH"),GREEN);
60 continue;
61 }
62
63 /* make a multiple machine report */
64 printf(" %sMultiple Machine PortMaster Tables\n",WHITE);
65 printf(" %s%s%s<%s%s%s>%s\n\n",
66 CYAN,*envvar? envvar : "SKTPATH",YELLOW,
67 GREEN,machines,YELLOW,NRML);
68 mend= strchr(machines,MACHINE_SEP);
69 while(mend) {
70 *mend = '\0';
71 printPMtable(machines);
72 *mend = MACHINE_SEP;
73 machines= mend + 1;
74 mend = strchr(machines,MACHINE_SEP);
75 }
76 if(machines) printPMtable(machines);
77 printf("%s\n",NRML);
78 }
79 else printPMtable(*argv); /* report on mentioned machine */
80 }
81 }
82 else { /* report on local machine */
83 char *pmshare= NULL;
84 pmshare= getenv("PMSHARE");
85 if(pmshare) printPMtable(pmshare); /* if PMSHARE, default to its PortMaster */
86 else {
87 gethostname(localhost,BUFSIZE); /* otherwise, use localhost's PortMaster */
88 printPMtable(localhost);
89 }
90 }
91
92 #ifdef vms
93 return 1;
94 #else
95 return 0;
96 #endif
97 }
98
99 /* ---------------------------------------------------------------------- */
100
101 /* printPMtable: this function queries the PortMaster for a table, and then
102 * prints it out
103 */
104 #ifdef __PROTOTYPE__
105 void printPMtable(char *buf)
106 #else /* __PROTOTYPE__ */
107 void printPMtable(buf)
108 char *buf;
109 #endif /* __PROTOTYPE__ */
110 {
111 char locbuf[BUFSIZE];
112 int icnt;
113 int itry;
114 Socket *pmskt;
115 SKTEVENT mesg;
116 SKTEVENT cnt;
117
118
119 /* open a client socket to the PortMaster */
120 pmskt= Sopen_clientport(buf,"PMClient",PORTMASTER);
121 if(!pmskt) {
122 error(XTDIO_WARNING,"unable to connect to <%s%s%s>'s PortMaster\n",
123 RVCYAN,buf,GREEN);
124 return;
125 }
126
127 /* send a PM_TABLE request */
128 for(itry= 0; itry < PM_MAXTRY; ++itry) {
129 mesg= PM_TABLE;
130 mesg= htons(mesg);
131 Swrite(pmskt,(char *) &mesg,sizeof(mesg));
132
133 /* wait for response */
134 if(Stimeoutwait(pmskt,MAXTIME,0L) < 0) {
135 Sclose(pmskt);
136 error(XTDIO_WARNING,"%s's PortMaster is not responding\n",sprt(buf));
137 return;
138 }
139 Sread(pmskt,(char *) &mesg,sizeof(mesg));
140
141 /* translate reponse */
142 mesg= ntohs(mesg);
143 if (mesg == PM_OK) break;
144 else if(mesg == PM_SORRY) {
145 error(XTDIO_WARNING,"%s's PortMaster is refusing connections\n",sprt(buf));
146 return;
147 }
148 }
149
150 /* find out how many entries in table there are */
151 if(Stimeoutwait(pmskt,MAXTIME,0L) < 0) {
152 Sclose(pmskt);
153 error(XTDIO_WARNING,"%s's PortMaster is not responding with entry count\n",sprt(buf));
154 return;
155 }
156 Sread(pmskt,(char *) &cnt,sizeof(cnt));
157 cnt= ntohs(cnt);
158
159 if(cnt) {
160 printf("\n%sCurrent %s%s%s Portmaster Servers\n",
161 UWHITE,
162 RVCYAN, buf, UWHITE);
163
164 /* begin to receive table */
165 for(icnt= 1; cnt > 0; --cnt, ++icnt) {
166 if(Stimeoutwait(pmskt,MAXTIME,0L) < 0) {
167 error(XTDIO_WARNING,"%s abruptly ceased sending server names\n",sprt(buf));
168 break;
169 }
170 if(!Sgets(locbuf,BUFSIZE,pmskt)) {
171 error(XTDIO_WARNING,"%s disconnected unexpectedly!\n",sprt(buf));
172 break;
173 }
174 printf("%s%2d: %s%s\n",CYAN,icnt,GREEN,locbuf);
175 }
176 puts(NRML);
177 }
178 else printf("%s%s%s's PortMaster currently has no servers%s\n",RVCYAN,buf,GREEN,NRML);
179
180 /* close socket to PortMaster */
181 Sclose(pmskt);
182
183 }
184
185 /* --------------------------------------------------------------------- */