| 1 |
/* sktdbg.c: this program supports testing the Sockets library |
| 2 |
* sktdbg {s|c} sktname |
| 3 |
*/ |
| 4 |
#include <stdio.h> |
| 5 |
#include "xtdio.h" |
| 6 |
#include "sockets.h" |
| 7 |
|
| 8 |
/* ------------------------------------------------------------------------- |
| 9 |
* Local Definitions: |
| 10 |
*/ |
| 11 |
/* #define ansi_escapes -- experimental, doesn't work yet */ |
| 12 |
|
| 13 |
#define BIGBUFSIZE 8192 |
| 14 |
#define BUFSIZE 128 |
| 15 |
#define PLAINPROMPT 0 |
| 16 |
#define BYTESPROMPT 1 |
| 17 |
#define DYNAMICPROMPT 2 |
| 18 |
|
| 19 |
#define TIMEOUT 30L |
| 20 |
|
| 21 |
/* ------------------------------------------------------------------------- |
| 22 |
* Prototypes: |
| 23 |
*/ |
| 24 |
#ifdef __PROTOTYPE__ |
| 25 |
void Taccept(char *buf); |
| 26 |
void Tclose(char *buf); |
| 27 |
void Tmenu(char *buf); |
| 28 |
void Tdebug(char *buf); |
| 29 |
void Tpeek(char *buf); |
| 30 |
void Ttest(char *buf); |
| 31 |
void Tfwinit(char *buf); |
| 32 |
void Tfput(char *buf); |
| 33 |
void Tgets(char *buf); |
| 34 |
void Tprintf(char *buf); |
| 35 |
void Tputs(char *buf); |
| 36 |
void Tshut(char *buf); |
| 37 |
void Tquit(char *buf); |
| 38 |
void Tread(char *buf); |
| 39 |
void Trmsrvr(char *buf); |
| 40 |
void Tscanf(char *buf); |
| 41 |
void Ttable(char *buf); |
| 42 |
void Twait(char *buf); |
| 43 |
void Twrite(char *buf); |
| 44 |
void Tisset(char *buf); |
| 45 |
|
| 46 |
#else |
| 47 |
|
| 48 |
extern main(); /* sktdbg.c */ |
| 49 |
extern void Taccept(); /* sktdbg.c */ |
| 50 |
extern void Tclose(); /* sktdbg.c */ |
| 51 |
extern void Tmenu(); /* sktdbg.c */ |
| 52 |
extern void Tdebug(); /* sktdbg.c */ |
| 53 |
extern void Tpeek(); /* sktdbg.c */ |
| 54 |
extern void Ttest(); /* sktdbg.c */ |
| 55 |
extern void Tfwinit(); /* sktdbg.c */ |
| 56 |
extern void Tfput(); /* sktdbg.c */ |
| 57 |
extern void Tgets(); /* sktdbg.c */ |
| 58 |
extern void Tputs(); /* sktdbg.c */ |
| 59 |
extern void Tshut(); /* sktdbg.c */ |
| 60 |
extern void Tquit(); /* sktdbg.c */ |
| 61 |
extern void Tread(); /* sktdbg.c */ |
| 62 |
extern void Twrite(); /* sktdbg.c */ |
| 63 |
extern void Twait(); /* sktdbg.c */ |
| 64 |
extern void Tscanf(); /* sktdbg.c */ |
| 65 |
extern void Tprintf(); /* sktdbg.c */ |
| 66 |
extern void Ttable(); /* sktdbg.c */ |
| 67 |
extern void Trmsrvr(); /* sktdbg.c */ |
| 68 |
extern void Tisset(); /* sktdbg.c */ |
| 69 |
|
| 70 |
#endif |
| 71 |
|
| 72 |
/* ------------------------------------------------------------------------- |
| 73 |
* Global Data |
| 74 |
*/ |
| 75 |
#ifdef __PROTOTYPE__ |
| 76 |
struct menu_str{ |
| 77 |
char *cmd; |
| 78 |
void (*test)(char *); |
| 79 |
} menu[]= |
| 80 |
#else |
| 81 |
struct menu_str{ |
| 82 |
char *cmd; |
| 83 |
void (*test)(); |
| 84 |
} menu[]= |
| 85 |
#endif |
| 86 |
{{"accept" , Taccept}, |
| 87 |
{"close" , Tclose}, |
| 88 |
{"fput" , Tfput}, |
| 89 |
{"fwinit" , Tfwinit}, |
| 90 |
{"get" , Tgets}, |
| 91 |
{"isset" , Tisset}, |
| 92 |
{"menu" , Tmenu}, |
| 93 |
{"peek" , Tpeek}, |
| 94 |
{"printf" , Tprintf}, |
| 95 |
{"put" , Tputs}, |
| 96 |
{"q" , Tquit}, |
| 97 |
{"quit" , Tquit}, |
| 98 |
{"read" , Tread}, |
| 99 |
{"rmsrvr" , Trmsrvr}, |
| 100 |
{"scanf" , Tscanf}, |
| 101 |
{"shutdown", Tshut}, |
| 102 |
{"table" , Ttable}, |
| 103 |
{"test" , Ttest}, |
| 104 |
{"wait" , Twait}, |
| 105 |
{"write" , Twrite}, |
| 106 |
#ifdef DEBUG |
| 107 |
{"D" , Tdebug}, |
| 108 |
#endif |
| 109 |
{"?" , Tmenu} |
| 110 |
}; |
| 111 |
int nmenu = sizeof(menu)/sizeof(struct menu_str); |
| 112 |
Socket *skt = NULL; |
| 113 |
Socket *server = NULL; |
| 114 |
|
| 115 |
#ifdef ansi_escapes |
| 116 |
int pflag= DYNAMICPROMPT; |
| 117 |
#else |
| 118 |
int pflag= BYTESPROMPT; |
| 119 |
#endif |
| 120 |
|
| 121 |
/* ------------------------------------------------------------------------- |
| 122 |
* Source Code: |
| 123 |
*/ |
| 124 |
|
| 125 |
/* main: */ |
| 126 |
#ifdef __PROTOTYPE__ |
| 127 |
int main( |
| 128 |
int argc, |
| 129 |
char **argv) |
| 130 |
#else /* __PROTOTYPE__ */ |
| 131 |
int main(argc,argv) |
| 132 |
int argc; |
| 133 |
char **argv; |
| 134 |
#endif /* __PROTOTYPE__ */ |
| 135 |
{ |
| 136 |
char *b = NULL; |
| 137 |
char buf[BUFSIZE]; |
| 138 |
char cmdbuf[BUFSIZE]; |
| 139 |
int icmd; |
| 140 |
#ifdef ansi_escapes |
| 141 |
int fd; |
| 142 |
Smask fdmask; |
| 143 |
int newcnt = 0; |
| 144 |
int oldcnt = 0; |
| 145 |
Smask sktmask; |
| 146 |
#endif |
| 147 |
|
| 148 |
rdcolor(); /* initialize ANSI escape sequences */ |
| 149 |
|
| 150 |
if(argc < 3) { /* give usage */ |
| 151 |
static char *usage[]={ |
| 152 |
XYELLOW,"sktdbg ", |
| 153 |
XCYAN,"sktname ",XGREEN, |
| 154 |
"{",XCYAN,"sScb",XGREEN,"}\n\n", |
| 155 |
|
| 156 |
XUWHITE,"Author:\n",XGREEN,"Dr. Charles E. Campbell\n\n", |
| 157 |
|
| 158 |
XUWHITE,"Explanation:\n",XGREEN, |
| 159 |
"This program is designed to test out the Sockets Library.\n", |
| 160 |
"The ",XCYAN,"sktname",XGREEN," is the name of the socket.\n", |
| 161 |
"It can be sktname@hostname or just sktname (current host is then used)\n", |
| 162 |
"The ",XCYAN,"s",XGREEN," yields a server/accept socket.\n", |
| 163 |
"The ",XCYAN,"c",XGREEN," yields a client socket.\n", |
| 164 |
"The ",XCYAN,"b",XGREEN," yields a client-wait socket.\n\n", |
| 165 |
|
| 166 |
"The Sockets Library was written by Dr. Charles E. Campbell, Jr. and\n", |
| 167 |
"Terry McRoberts of Goddard Space Flight Center, NASA\n", |
| 168 |
"",""}; |
| 169 |
char **u; |
| 170 |
|
| 171 |
/* print out explanation */ |
| 172 |
for(u= usage; **u || **(u+1); ++u) rdcputs(*u,stdout); |
| 173 |
#ifdef vms |
| 174 |
exit(1); |
| 175 |
#else |
| 176 |
exit(0); |
| 177 |
#endif |
| 178 |
} |
| 179 |
|
| 180 |
Sinit(); /* initialize sockets */ |
| 181 |
|
| 182 |
/* open a socket of the appropriate type */ |
| 183 |
skt= Sopenv(argv[1],argv[2],"SKTDBG"); |
| 184 |
if(!skt) { |
| 185 |
if(!strcmp(argv[2],"s")) { |
| 186 |
error(XTDIO_WARNING,"unable to Sopen(%s,%s), removing <%s>\n", argv[1],argv[2],argv[1]); |
| 187 |
Srmsrvr(argv[1]); |
| 188 |
skt= Sopenv(argv[1],argv[2],"SKTDBG"); |
| 189 |
if(!skt) error(XTDIO_ERROR,"2nd try: unable to Sopen(%s,%s)\n",argv[1],argv[2]); |
| 190 |
else error(XTDIO_NOTE,"second try to Sopen(%s,%s) succeeded\n",argv[1],argv[2]); |
| 191 |
} |
| 192 |
else error(XTDIO_ERROR,"unable to Sopen(%s,%s)\n",argv[1],argv[2]); |
| 193 |
} |
| 194 |
|
| 195 |
/* print out menu */ |
| 196 |
fputc('\n',stdout); |
| 197 |
Tmenu(""); |
| 198 |
|
| 199 |
/* command loop */ |
| 200 |
while(1) { |
| 201 |
|
| 202 |
/* get command from user */ |
| 203 |
switch(pflag) { |
| 204 |
|
| 205 |
case PLAINPROMPT: |
| 206 |
printf("%sEnter: %s",CYAN,GREEN); |
| 207 |
break; |
| 208 |
|
| 209 |
case BYTESPROMPT: |
| 210 |
printf("%s(%s%4d%s bytes) Enter: %s", |
| 211 |
CYAN,GREEN,Stest(skt), |
| 212 |
CYAN,GREEN); |
| 213 |
break; |
| 214 |
|
| 215 |
#ifdef ansi_escapes |
| 216 |
case DYNAMICPROMPT: |
| 217 |
/* set up Smaskwait */ |
| 218 |
Smaskset((Socket *) NULL); |
| 219 |
fd= fileno(stdin); |
| 220 |
Smaskfdset(fd); |
| 221 |
fdmask= Smaskget(); |
| 222 |
Smaskset(skt); |
| 223 |
sktmask= Smaskget(); |
| 224 |
printf("%s(%s%4d%s bytes) Enter: %s", |
| 225 |
CYAN,GREEN,Stest(skt), |
| 226 |
CYAN,GREEN); |
| 227 |
fflush(stdout); |
| 228 |
|
| 229 |
while(1) { |
| 230 |
/* block until skt or keyboard sends something */ |
| 231 |
Smaskwait(); |
| 232 |
Smaskuse(fdmask); |
| 233 |
if(Smasktest()) break; |
| 234 |
Smaskuse(sktmask); |
| 235 |
|
| 236 |
/* update screen */ |
| 237 |
newcnt= Stest(skt); |
| 238 |
if(newcnt == oldcnt) continue; |
| 239 |
oldcnt= newcnt; |
| 240 |
printf("\033[20D%s(%s%4d%s bytes) Enter: %s", |
| 241 |
CYAN,GREEN,newcnt, |
| 242 |
CYAN,GREEN); |
| 243 |
fflush(stdout); |
| 244 |
sleep(1); |
| 245 |
} |
| 246 |
break; |
| 247 |
#endif /* ansi_escapes */ |
| 248 |
} |
| 249 |
|
| 250 |
/* get prompt from user */ |
| 251 |
fgets(buf,BUFSIZE,stdin); |
| 252 |
srmtrblk(buf); |
| 253 |
b= stpblk(buf); |
| 254 |
if(!*b) continue; |
| 255 |
|
| 256 |
/* get command from b */ |
| 257 |
sscanf(buf,"%s",cmdbuf); |
| 258 |
b= stpnxt(buf,"%s%b"); /* point past command string */ |
| 259 |
|
| 260 |
/* handle option */ |
| 261 |
if(*b == '-') { |
| 262 |
switch(b[1]) { |
| 263 |
|
| 264 |
case 'p': /* prompt mode control */ |
| 265 |
#ifdef ansi_escapes |
| 266 |
if(++pflag > DYNAMICPROMPT) pflag= PLAINPROMPT; |
| 267 |
#else |
| 268 |
if(++pflag > BYTESPROMPT) pflag= PLAINPROMPT; |
| 269 |
#endif |
| 270 |
break; |
| 271 |
|
| 272 |
default: |
| 273 |
error(XTDIO_WARNING,"I don't know the <%s%s%s> command\n", |
| 274 |
MAGENTA,cmdbuf,GREEN); |
| 275 |
break; |
| 276 |
} |
| 277 |
continue; |
| 278 |
} |
| 279 |
|
| 280 |
/* identify command */ |
| 281 |
for(icmd= 0; icmd < nmenu; ++icmd) |
| 282 |
if(!strcmp(menu[icmd].cmd,cmdbuf)) break; |
| 283 |
if(icmd >= nmenu) { |
| 284 |
error(XTDIO_WARNING,"I don't know the <%s%s%s> command\n", |
| 285 |
MAGENTA,cmdbuf,GREEN); |
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
/* pass control to command */ |
| 290 |
(*menu[icmd].test)(b); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* ------------------------------------------------------------------------- */ |
| 295 |
|
| 296 |
/* Taccept: this function allows a server to accept a connection */ |
| 297 |
#ifdef __PROTOTYPE__ |
| 298 |
void Taccept(char *buf) |
| 299 |
#else /* __PROTOTYPE__ */ |
| 300 |
void Taccept(buf) |
| 301 |
char *buf; |
| 302 |
#endif /* __PROTOTYPE__ */ |
| 303 |
{ |
| 304 |
|
| 305 |
/* If server is null, then the skt *is* a server. Copy it over. |
| 306 |
* Otherwise, if there's a skt, close it before accepting |
| 307 |
*/ |
| 308 |
if(!server) { |
| 309 |
if(skt->type == PM_SERVER) { |
| 310 |
server= skt; |
| 311 |
skt = NULL; |
| 312 |
} |
| 313 |
else { |
| 314 |
error(XTDIO_WARNING,"cannot Saccept with a skt<%s>\n",Sprtskt(skt)); |
| 315 |
return; |
| 316 |
} |
| 317 |
} |
| 318 |
else if(skt) { |
| 319 |
Sclose(skt); |
| 320 |
skt= NULL; |
| 321 |
} |
| 322 |
|
| 323 |
/* only wait up to TIMEOUT seconds */ |
| 324 |
if(Stimeoutwait(server,TIMEOUT,0L) <= 0) { |
| 325 |
error(XTDIO_WARNING,"accept timed out (waited %ld seconds)\n",TIMEOUT); |
| 326 |
return; |
| 327 |
} |
| 328 |
skt = Saccept(server); |
| 329 |
|
| 330 |
printf(" %sSaccept%s => <%s%s%s>\n", |
| 331 |
YELLOW,GREEN,MAGENTA,skt? Sprtskt(skt) : "null",GREEN); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
/* ------------------------------------------------------------------------- */ |
| 336 |
|
| 337 |
/* Tclose: this function closes the socket and, |
| 338 |
* for non-accept sockets, exits the program |
| 339 |
*/ |
| 340 |
#ifdef __PROTOTYPE__ |
| 341 |
void Tclose(char *buf) |
| 342 |
#else /* __PROTOTYPE__ */ |
| 343 |
void Tclose(buf) |
| 344 |
char *buf; |
| 345 |
#endif /* __PROTOTYPE__ */ |
| 346 |
{ |
| 347 |
|
| 348 |
switch(skt->type) { |
| 349 |
|
| 350 |
case PM_ACCEPT: |
| 351 |
Sclose(skt); |
| 352 |
skt = server; |
| 353 |
server= NULL; |
| 354 |
printf(" %sSclose%s: closed accept Socket\n",YELLOW,GREEN); |
| 355 |
printf(" server socket still available\n"); |
| 356 |
break; |
| 357 |
|
| 358 |
case PM_CLIENT: |
| 359 |
Sclose(skt); |
| 360 |
printf(" %sSclose%s: closed client Socket\n",YELLOW,GREEN); |
| 361 |
#ifdef vms |
| 362 |
exit(1); |
| 363 |
#else |
| 364 |
exit(0); |
| 365 |
#endif |
| 366 |
|
| 367 |
case PM_SERVER: |
| 368 |
Sclose(skt); |
| 369 |
skt= server= NULL; |
| 370 |
printf(" %sSclose%s: closed server Socket\n",YELLOW,GREEN); |
| 371 |
#ifdef vms |
| 372 |
exit(1); |
| 373 |
#else |
| 374 |
exit(0); |
| 375 |
#endif |
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
/* ------------------------------------------------------------------------- */ |
| 381 |
|
| 382 |
/* Tmenu: this function print out the commands */ |
| 383 |
#ifdef __PROTOTYPE__ |
| 384 |
void Tmenu(char *buf) |
| 385 |
#else /* __PROTOTYPE__ */ |
| 386 |
void Tmenu(buf) |
| 387 |
char *buf; |
| 388 |
#endif /* __PROTOTYPE__ */ |
| 389 |
{ |
| 390 |
int i; |
| 391 |
|
| 392 |
|
| 393 |
printf("%sSocket Test Commands%s\n",UWHITE,GREEN); |
| 394 |
for(i= 0; i < nmenu; ++i) printf(" %s\n",menu[i].cmd); |
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
/* ------------------------------------------------------------------------- */ |
| 399 |
|
| 400 |
|
| 401 |
/* ------------------------------------------------------------------------- */ |
| 402 |
|
| 403 |
/* Tpeek: this function does a Speek */ |
| 404 |
#ifdef __PROTOTYPE__ |
| 405 |
void Tpeek(char *buf) |
| 406 |
#else /* __PROTOTYPE__ */ |
| 407 |
void Tpeek(buf) |
| 408 |
char *buf; |
| 409 |
#endif /* __PROTOTYPE__ */ |
| 410 |
{ |
| 411 |
static char locbuf[BIGBUFSIZE]; |
| 412 |
int buflen; |
| 413 |
|
| 414 |
|
| 415 |
buflen= Speek(skt,locbuf,BIGBUFSIZE); |
| 416 |
printf(" %sSpeek%s=> skt<%s> locbuf<%s> buflen=%d\n", |
| 417 |
YELLOW,GREEN,Sprtskt(skt),sprt(locbuf),buflen); |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
/* ------------------------------------------------------------------------- */ |
| 422 |
|
| 423 |
/* Ttest: this function does a STell */ |
| 424 |
#ifdef __PROTOTYPE__ |
| 425 |
void Ttest(char *buf) |
| 426 |
#else /* __PROTOTYPE__ */ |
| 427 |
void Ttest(buf) |
| 428 |
char *buf; |
| 429 |
#endif /* __PROTOTYPE__ */ |
| 430 |
{ |
| 431 |
int buflen; |
| 432 |
|
| 433 |
|
| 434 |
buflen= Stest(skt); |
| 435 |
buf[0]= '\0'; |
| 436 |
printf(" %sStest%s => skt<%s> %d\n",YELLOW,GREEN,Sprtskt(skt),buflen); |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
/* ------------------------------------------------------------------------- */ |
| 441 |
|
| 442 |
/* Tfwinit: sends a PM_FW to the PortMaster */ |
| 443 |
#ifdef __PROTOTYPE__ |
| 444 |
void Tfwinit(char *buf) |
| 445 |
#else /* __PROTOTYPE__ */ |
| 446 |
void Tfwinit(buf) |
| 447 |
char *buf; |
| 448 |
#endif /* __PROTOTYPE__ */ |
| 449 |
{ |
| 450 |
int trycnt; |
| 451 |
Socket *pmskt; |
| 452 |
SKTEVENT mesg; |
| 453 |
|
| 454 |
|
| 455 |
/* begin PortMaster firewall re-initialize */ |
| 456 |
pmskt= Sopen_clientport(buf,"PMClient",PORTMASTER); |
| 457 |
|
| 458 |
if(!pmskt) { |
| 459 |
freeSocket(pmskt); |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
/* send a PM_FWINIT */ |
| 464 |
for(trycnt= 0; trycnt < PM_MAXTRY; ++trycnt) { |
| 465 |
|
| 466 |
/* this is where the PM_FWINIT gets sent */ |
| 467 |
mesg= htons(PM_FWINIT); |
| 468 |
Swrite(pmskt,(char *) &mesg,sizeof(mesg)); |
| 469 |
|
| 470 |
/* wait for PortMaster to respond with PM_OK (I hope) */ |
| 471 |
if(Stimeoutwait(pmskt,20L,0L) < 0) { |
| 472 |
Sclose(pmskt); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
/* read PortMaster response */ |
| 477 |
Sread(pmskt,(char *) &mesg,sizeof(mesg)); |
| 478 |
mesg= ntohs(mesg); |
| 479 |
if(mesg != PM_RESEND) break; |
| 480 |
} |
| 481 |
if(mesg == PM_OK) printf("PortMaster firewall re-initialized\n"); |
| 482 |
else printf("PortMaster isn't handling firewalls\n"); |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
/* ------------------------------------------------------------------------- */ |
| 487 |
|
| 488 |
/* Tfput: puts the entire contents of a file, line by line, across a Socket */ |
| 489 |
#ifdef __PROTOTYPE__ |
| 490 |
void Tfput(char *buf) |
| 491 |
#else /* __PROTOTYPE__ */ |
| 492 |
void Tfput(buf) |
| 493 |
char *buf; |
| 494 |
#endif /* __PROTOTYPE__ */ |
| 495 |
{ |
| 496 |
char *b = NULL; |
| 497 |
char locbuf[BUFSIZE]; |
| 498 |
FILE *fp = NULL; |
| 499 |
|
| 500 |
|
| 501 |
srmtrblk(buf); |
| 502 |
b = stpblk(buf); |
| 503 |
if(*b && skt) { |
| 504 |
fp= fopen(b,"r"); |
| 505 |
if(!fp) { |
| 506 |
error(XTDIO_WARNING,"unable to open file<%s>\n",sprt(b)); |
| 507 |
return; |
| 508 |
} |
| 509 |
while(fgets(locbuf,BUFSIZE,fp)) { |
| 510 |
srmtrblk(locbuf); |
| 511 |
Sputs(locbuf,skt); |
| 512 |
printf(" %sSputs%s(locbuf<%s>,skt<%s>)\n", |
| 513 |
YELLOW, |
| 514 |
GREEN, |
| 515 |
sprt(locbuf), |
| 516 |
Sprtskt(skt)); |
| 517 |
} |
| 518 |
fclose(fp); |
| 519 |
} |
| 520 |
|
| 521 |
} |
| 522 |
|
| 523 |
/* --------------------------------------------------------------------- */ |
| 524 |
|
| 525 |
/* Tgets: this function does a Sread */ |
| 526 |
#ifdef __PROTOTYPE__ |
| 527 |
void Tgets(char *buf) |
| 528 |
#else /* __PROTOTYPE__ */ |
| 529 |
void Tgets(buf) |
| 530 |
char *buf; |
| 531 |
#endif /* __PROTOTYPE__ */ |
| 532 |
{ |
| 533 |
char locbuf[BUFSIZE]; |
| 534 |
|
| 535 |
|
| 536 |
/* only wait up to TIMEOUT seconds */ |
| 537 |
if(Stimeoutwait(skt,TIMEOUT,0L) <= 0) { |
| 538 |
error(XTDIO_WARNING,"get timed out (waited %ld seconds)\n",TIMEOUT); |
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
(void *) Sgets(locbuf,BUFSIZE,skt); |
| 543 |
|
| 544 |
printf(" %sSgets%s: skt<%s> locbuf<%s>\n", |
| 545 |
YELLOW,GREEN,Sprtskt(skt),sprt(locbuf)); |
| 546 |
|
| 547 |
} |
| 548 |
|
| 549 |
/* ------------------------------------------------------------------------- */ |
| 550 |
|
| 551 |
/* Tputs: this function does a Swrite |
| 552 |
* Supports \a - \z for control character insertion |
| 553 |
*/ |
| 554 |
#ifdef __PROTOTYPE__ |
| 555 |
void Tputs(char *buf) |
| 556 |
#else /* __PROTOTYPE__ */ |
| 557 |
void Tputs(buf) |
| 558 |
char *buf; |
| 559 |
#endif /* __PROTOTYPE__ */ |
| 560 |
{ |
| 561 |
char locbuf[BUFSIZE]; |
| 562 |
char *backslash; |
| 563 |
char *b; |
| 564 |
char *lb; |
| 565 |
|
| 566 |
|
| 567 |
backslash= strchr(buf,'\\'); |
| 568 |
if(backslash) { |
| 569 |
for(b= buf, lb= locbuf; *b; ++b) { |
| 570 |
*lb= *b; |
| 571 |
if(*b == '\\') { |
| 572 |
++b; |
| 573 |
if (*b == '\\') continue; |
| 574 |
else if('a' <= *b && *b <= 'z') *lb= (*b) - 'a' + 1; |
| 575 |
} |
| 576 |
} |
| 577 |
*lb= '\0'; |
| 578 |
Sputs(locbuf,skt); |
| 579 |
printf(" %sSputs%s(buf<%s>,skt<%s>)\n",YELLOW,GREEN,sprt(locbuf),Sprtskt(skt)); |
| 580 |
} |
| 581 |
else { |
| 582 |
Sputs(buf,skt); |
| 583 |
printf(" %sSputs%s(buf<%s>,skt<%s>)\n",YELLOW,GREEN,sprt(buf),Sprtskt(skt)); |
| 584 |
} |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
/* ------------------------------------------------------------------------- */ |
| 589 |
|
| 590 |
/* Tshut: this function shuts down the PortMaster */ |
| 591 |
#ifdef __PROTOTYPE__ |
| 592 |
void Tshut(char *buf) |
| 593 |
#else /* __PROTOTYPE__ */ |
| 594 |
void Tshut(buf) |
| 595 |
char *buf; |
| 596 |
#endif /* __PROTOTYPE__ */ |
| 597 |
{ |
| 598 |
int trycnt; |
| 599 |
Socket *pmskt; |
| 600 |
SKTEVENT mesg; |
| 601 |
|
| 602 |
|
| 603 |
/* shut down client/accept socket */ |
| 604 |
printf(" %sSclose%s(skt<%s>)",YELLOW,GREEN,Sprtskt(skt)); |
| 605 |
Sclose(skt); skt= NULL; |
| 606 |
|
| 607 |
/* if server, shut down server socket */ |
| 608 |
if(server) { |
| 609 |
printf(" %sSclose%s(server<%s>)",YELLOW,GREEN,Sprtskt(server)); |
| 610 |
Sclose(server); server= NULL; |
| 611 |
} |
| 612 |
|
| 613 |
/* begin PortMaster shutdown sequence */ |
| 614 |
pmskt= Sopen_clientport(buf,"PMClient",PORTMASTER); |
| 615 |
if(!pmskt) { |
| 616 |
freeSocket(pmskt); |
| 617 |
return; |
| 618 |
} |
| 619 |
|
| 620 |
/* send a QUIT */ |
| 621 |
for(trycnt= 0; trycnt < PM_MAXTRY; ++trycnt) { |
| 622 |
mesg= htons(PM_QUIT); |
| 623 |
Swrite(pmskt,(char *) &mesg,sizeof(mesg)); |
| 624 |
if(Stimeoutwait(pmskt,20L,0L) < 0) { |
| 625 |
Sclose(pmskt); |
| 626 |
return; |
| 627 |
} |
| 628 |
Sread(pmskt,(char *) &mesg,sizeof(mesg)); |
| 629 |
mesg= ntohs(mesg); |
| 630 |
if(mesg == PM_OK) break; |
| 631 |
} |
| 632 |
|
| 633 |
/* finish up termination sequence */ |
| 634 |
Sputs("PortMaster",pmskt); |
| 635 |
Sclose(pmskt); |
| 636 |
printf(" %sclosed down%s PortMaster\n",YELLOW,GREEN); |
| 637 |
|
| 638 |
/* ok, exit the program */ |
| 639 |
#ifdef vms |
| 640 |
exit(1); |
| 641 |
#else |
| 642 |
exit(0); |
| 643 |
#endif |
| 644 |
} |
| 645 |
|
| 646 |
/* ------------------------------------------------------------------------- */ |
| 647 |
|
| 648 |
/* Tquit: this function quits */ |
| 649 |
#ifdef __PROTOTYPE__ |
| 650 |
void Tquit(char *buf) |
| 651 |
#else /* __PROTOTYPE__ */ |
| 652 |
void Tquit(buf) |
| 653 |
char *buf; |
| 654 |
#endif /* __PROTOTYPE__ */ |
| 655 |
{ |
| 656 |
|
| 657 |
if(skt) { |
| 658 |
printf(" %sSclose%s(skt<%s>)\n",YELLOW,GREEN,Sprtskt(skt)); |
| 659 |
Sclose(skt); |
| 660 |
} |
| 661 |
if(server) { |
| 662 |
printf(" %sSclose%s(server<%s>)\n",YELLOW,GREEN,Sprtskt(server)); |
| 663 |
Sclose(server); |
| 664 |
} |
| 665 |
|
| 666 |
#ifdef vms |
| 667 |
exit(1); |
| 668 |
#else |
| 669 |
exit(0); |
| 670 |
#endif |
| 671 |
} |
| 672 |
|
| 673 |
/* ------------------------------------------------------------------------- */ |
| 674 |
|
| 675 |
/* Tread: this function does a Sread */ |
| 676 |
#ifdef __PROTOTYPE__ |
| 677 |
void Tread(char *buf) |
| 678 |
#else /* __PROTOTYPE__ */ |
| 679 |
void Tread(buf) |
| 680 |
char *buf; |
| 681 |
#endif /* __PROTOTYPE__ */ |
| 682 |
{ |
| 683 |
char locbuf[BUFSIZE]; |
| 684 |
int buflen; |
| 685 |
int ibuf; |
| 686 |
|
| 687 |
|
| 688 |
buflen= Sread(skt,locbuf,BUFSIZE); |
| 689 |
printf(" %sSread%s: skt<%s> locbuf<",YELLOW,GREEN,Sprtskt(skt)); |
| 690 |
for(ibuf=0; ibuf < buflen; ++ibuf) printf("%s ",cprt(locbuf[ibuf])); |
| 691 |
printf("> buflen=%d\n",buflen); |
| 692 |
|
| 693 |
} |
| 694 |
|
| 695 |
/* ------------------------------------------------------------------------- */ |
| 696 |
|
| 697 |
/* Twrite: this function does a Swrite */ |
| 698 |
#ifdef __PROTOTYPE__ |
| 699 |
void Twrite(char *buf) |
| 700 |
#else /* __PROTOTYPE__ */ |
| 701 |
void Twrite(buf) |
| 702 |
char *buf; |
| 703 |
#endif /* __PROTOTYPE__ */ |
| 704 |
{ |
| 705 |
int buflen; |
| 706 |
|
| 707 |
|
| 708 |
buflen= Swrite(skt,buf,strlen(buf)+1); |
| 709 |
printf(" %sSwrite%s: skt<%s> buf<%s> buflen=%d\n", |
| 710 |
YELLOW,GREEN,Sprtskt(skt),sprt(buf),buflen); |
| 711 |
|
| 712 |
} |
| 713 |
|
| 714 |
/* ------------------------------------------------------------------------- */ |
| 715 |
|
| 716 |
/* Twait: this function does a Swait */ |
| 717 |
#ifdef __PROTOTYPE__ |
| 718 |
void Twait(char *buf) |
| 719 |
#else /* __PROTOTYPE__ */ |
| 720 |
void Twait(buf) |
| 721 |
char *buf; |
| 722 |
#endif /* __PROTOTYPE__ */ |
| 723 |
{ |
| 724 |
int buflen; |
| 725 |
|
| 726 |
|
| 727 |
buflen= Swait(skt); |
| 728 |
printf(" %sSwait%s: skt<%s> buflen=%d\n",YELLOW,GREEN,Sprtskt(skt),buflen); |
| 729 |
|
| 730 |
} |
| 731 |
|
| 732 |
/* ------------------------------------------------------------------------- */ |
| 733 |
|
| 734 |
/* Tscanf: this function tests Sscanf with "%s %d %lf %s" */ |
| 735 |
#ifdef __PROTOTYPE__ |
| 736 |
void Tscanf(char *buf) |
| 737 |
#else /* __PROTOTYPE__ */ |
| 738 |
void Tscanf(buf) |
| 739 |
char *buf; |
| 740 |
#endif /* __PROTOTYPE__ */ |
| 741 |
{ |
| 742 |
char *b = NULL; |
| 743 |
char buf1[BUFSIZE]; |
| 744 |
int i; |
| 745 |
double d; |
| 746 |
|
| 747 |
|
| 748 |
for(b= strchr(buf,'%'); b; b= strchr(b+1,'%')) { |
| 749 |
|
| 750 |
/* only wait up to TIMEOUT seconds */ |
| 751 |
if(Stimeoutwait(skt,TIMEOUT,0L) <= 0) { |
| 752 |
error(XTDIO_WARNING,"scanf timed out (waited %ld seconds)\n",TIMEOUT); |
| 753 |
return; |
| 754 |
} |
| 755 |
|
| 756 |
/* skip over 'l' and 'h' modifier */ |
| 757 |
if (b[1] == 'l') ++b; |
| 758 |
else if(b[1] == 'h') ++b; |
| 759 |
|
| 760 |
/* process data on Socket */ |
| 761 |
switch(b[1]) { |
| 762 |
|
| 763 |
case 'c': |
| 764 |
Sscanf(skt,"%c",buf1); |
| 765 |
printf(" %sSscanf%s with %%c: rcvd<%s%c%s>\n", |
| 766 |
YELLOW,CYAN, |
| 767 |
GREEN,buf1[0],CYAN); |
| 768 |
break; |
| 769 |
|
| 770 |
case 'd': |
| 771 |
Sscanf(skt,"%d",&i); |
| 772 |
printf(" %sSscanf%s with %%d: rcvd<%s%d%s>\n", |
| 773 |
YELLOW,CYAN, |
| 774 |
GREEN,i,CYAN); |
| 775 |
break; |
| 776 |
|
| 777 |
case 'f': |
| 778 |
Sscanf(skt,"%lf",&d); |
| 779 |
printf(" %sSscanf%s with %%lf: rcvd<%s%f%s>\n", |
| 780 |
YELLOW,CYAN, |
| 781 |
GREEN,d,CYAN); |
| 782 |
break; |
| 783 |
|
| 784 |
case 's': |
| 785 |
Sscanf(skt,"%s",buf1); |
| 786 |
printf(" %sSscanf%s with %%s: rcvd<%s%s%s>\n", |
| 787 |
YELLOW,CYAN, |
| 788 |
GREEN,sprt(buf1),CYAN); |
| 789 |
break; |
| 790 |
|
| 791 |
default: |
| 792 |
error(XTDIO_WARNING,"sktdbg doesn't support format code <%s>\n",sprt(b)); |
| 793 |
break; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
} |
| 798 |
|
| 799 |
/* ------------------------------------------------------------------------- */ |
| 800 |
|
| 801 |
/* Tprintf: this function tests Sprintf with "%s %d %s" */ |
| 802 |
#ifdef __PROTOTYPE__ |
| 803 |
void Tprintf(char *buf) |
| 804 |
#else /* __PROTOTYPE__ */ |
| 805 |
void Tprintf(buf) |
| 806 |
char *buf; |
| 807 |
#endif /* __PROTOTYPE__ */ |
| 808 |
{ |
| 809 |
int i = 7; |
| 810 |
double d = 8.; |
| 811 |
|
| 812 |
|
| 813 |
Sprintf(skt,"%s %d %f itworked!",buf,i,d); |
| 814 |
printf(" %sSprintf%s: skt<%s> i=%d d=%f itworked!\n", |
| 815 |
YELLOW,GREEN,Sprtskt(skt),i,d); |
| 816 |
|
| 817 |
} |
| 818 |
|
| 819 |
/* ------------------------------------------------------------------------- */ |
| 820 |
|
| 821 |
/* Ttable: this function queries the PortMaster for a table, and then |
| 822 |
* prints it out |
| 823 |
*/ |
| 824 |
#ifdef __PROTOTYPE__ |
| 825 |
void Ttable(char *buf) |
| 826 |
#else /* __PROTOTYPE__ */ |
| 827 |
void Ttable(buf) |
| 828 |
char *buf; |
| 829 |
#endif /* __PROTOTYPE__ */ |
| 830 |
{ |
| 831 |
int trycnt; |
| 832 |
Socket *pmskt = NULL; |
| 833 |
SKTEVENT mesg; |
| 834 |
SKTEVENT cnt; |
| 835 |
|
| 836 |
|
| 837 |
pmskt= Sopen_clientport(buf,"PMClient",PORTMASTER); |
| 838 |
if(!pmskt) { |
| 839 |
freeSocket(pmskt); |
| 840 |
return; |
| 841 |
} |
| 842 |
|
| 843 |
/* send a PM_TABLE request */ |
| 844 |
for(trycnt= 0; trycnt < PM_MAXTRY; ++trycnt) { |
| 845 |
mesg= PM_TABLE; |
| 846 |
mesg= htons(mesg); |
| 847 |
Swrite(pmskt,(char *) &mesg,sizeof(mesg)); |
| 848 |
Sread(pmskt,(char *) &mesg,sizeof(mesg)); |
| 849 |
mesg= ntohs(mesg); |
| 850 |
if(mesg == PM_OK) break; |
| 851 |
} |
| 852 |
|
| 853 |
/* find out how many entries in table there are */ |
| 854 |
Sread(pmskt,(char *) &cnt,sizeof(cnt)); |
| 855 |
cnt= ntohs(cnt); |
| 856 |
|
| 857 |
/* begin to receive table */ |
| 858 |
for(; cnt > 0; --cnt) { |
| 859 |
Sgets(buf,BUFSIZE,pmskt); |
| 860 |
printf("%2d: %s\n",cnt,buf); |
| 861 |
} |
| 862 |
|
| 863 |
/* close socket to PortMaster */ |
| 864 |
Sclose(pmskt); |
| 865 |
|
| 866 |
} |
| 867 |
|
| 868 |
/* ------------------------------------------------------------------------- */ |
| 869 |
|
| 870 |
/* Trmsrvr: this function removes a server from the PortMaster's PortTable */ |
| 871 |
#ifdef __PROTOTYPE__ |
| 872 |
void Trmsrvr(char *buf) |
| 873 |
#else /* __PROTOTYPE__ */ |
| 874 |
void Trmsrvr(buf) |
| 875 |
char *buf; |
| 876 |
#endif /* __PROTOTYPE__ */ |
| 877 |
{ |
| 878 |
SKTEVENT mesg; |
| 879 |
|
| 880 |
|
| 881 |
mesg= Srmsrvr(buf); |
| 882 |
printf(" %sSrmsrvr%s: buf<%s> mesg=%s\n", |
| 883 |
YELLOW,GREEN,sprt(buf),(mesg == PM_OK)? "success" : "failure"); |
| 884 |
|
| 885 |
} |
| 886 |
|
| 887 |
/* ------------------------------------------------------------------------- */ |
| 888 |
|
| 889 |
/* Tisset: this function tests if Smaskisset works: |
| 890 |
* it does so by using Smaskwait() with zero timeout |
| 891 |
* and then invokves Smaskisset() to test the |
| 892 |
* Socket. This function is primarily intended to |
| 893 |
* debug & test the Smaskisset() function. |
| 894 |
*/ |
| 895 |
#ifdef __PROTOTYPE__ |
| 896 |
void Tisset(char *buf) |
| 897 |
#else |
| 898 |
void Tisset(buf) |
| 899 |
char *buf; |
| 900 |
#endif |
| 901 |
{ |
| 902 |
int ret; |
| 903 |
|
| 904 |
|
| 905 |
Smasktime(0L,1L); |
| 906 |
Smaskset(skt); |
| 907 |
Smaskwait(); |
| 908 |
ret= Smaskisset(skt); |
| 909 |
|
| 910 |
printf(" %ssocket %s%s%s read-ready\n", |
| 911 |
GREEN, |
| 912 |
YELLOW, |
| 913 |
ret? "is" : "is not", |
| 914 |
GREEN); |
| 915 |
|
| 916 |
} |
| 917 |
|
| 918 |
/* --------------------------------------------------------------------- */ |