home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / netuse / part03 < prev    next >
Encoding:
Text File  |  1993-01-09  |  49.9 KB  |  1,595 lines

  1. Newsgroups: comp.sources.misc
  2. From: amber@engin.umich.edu (Lee Liming)
  3. Subject: v34i101:  netuse - A Network Host Usage Monitoring System, Part03/06
  4. Message-ID: <1993Jan11.023724.25434@sparky.imd.sterling.com>
  5. X-Md4-Signature: ca2824f51af585f7ecb6a82e93f674aa
  6. Date: Mon, 11 Jan 1993 02:37:24 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: amber@engin.umich.edu (Lee Liming)
  10. Posting-number: Volume 34, Issue 101
  11. Archive-name: netuse/part03
  12. Environment: UNIX, MS-DOS, OS/2, INET, MSC
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then feed it
  16. # into a shell via "sh file" or similar.  To overwrite existing files,
  17. # type "sh file -c".
  18. # Contents:  netuse/clients/hostinfo.c netuse/clients/libresp3.os2
  19. #   netuse/daemons/getmodel.c netuse/daemons/wakeup.c
  20. #   netuse/lib/getlist.c netuse/lib/network.c
  21. # Wrapped by kent@sparky on Sun Jan 10 20:28:35 1993
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 3 (of 6)."'
  25. if test -f 'netuse/clients/hostinfo.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'netuse/clients/hostinfo.c'\"
  27. else
  28.   echo shar: Extracting \"'netuse/clients/hostinfo.c'\" \(4694 characters\)
  29.   sed "s/^X//" >'netuse/clients/hostinfo.c' <<'END_OF_FILE'
  30. X/******************************************************************************
  31. X HOSTINFO.C - Netuse Host Use Monitor Host List Display Utility
  32. X
  33. X This utility contacts the NETUSE server and obtains a listing of machines
  34. X which meet certain criteria.  These criteria currently include only the type
  35. X of machine and the status (up/down).  The list is sorted by relative use
  36. X (least-used first).  Only 10 list entries are normally given, but the -all
  37. X option allows the entire list to be received and displayed.
  38. X
  39. X Lee Liming and Michael Neil, The Computer Aided Engineering Network
  40. X The University of Michigan
  41. X
  42. X Copyright (C) 1990, 1991, 1992 by the Regents of the University of Michigan.
  43. X
  44. X User agrees to reproduce said copyright notice on all copies of the software
  45. X made by the recipient.  
  46. X
  47. X All Rights Reserved. Permission is hereby granted for the recipient to make
  48. X copies and use this software for its own internal purposes only. Recipient of
  49. X this software may re-distribute this software outside of their own
  50. X institution. Permission to market this software commercially, to include this
  51. X product as part of a commercial product, or to make a derivative work for
  52. X commercial purposes, is explicitly prohibited.  All other uses are also
  53. X prohibited unless authorized in writing by the Regents of the University of
  54. X Michigan.
  55. X
  56. X This software is offered without warranty. The Regents of the University of
  57. X Michigan disclaim all warranties, express or implied, including but not
  58. X limited to the implied warranties of merchantability and fitness for any
  59. X particular purpose. In no event shall the Regents of the University of
  60. X Michigan be liable for loss or damage of any kind, including but not limited
  61. X to incidental, indirect, consequential, or special damages. 
  62. X******************************************************************************/
  63. X
  64. X#include <stdio.h>
  65. X#include <netuse.h>
  66. X
  67. X
  68. X#ifdef __STDC__
  69. Xmain(int argc,char *argv[])
  70. X#else
  71. Xmain(argc,argv)
  72. Xint argc;
  73. Xchar *argv[];
  74. X#endif
  75. X{
  76. X     u_char machtype,modeltype,mode;
  77. X     char buffer[512];
  78. X     int i;
  79. X
  80. X     machtype=0; modeltype=0;
  81. X     mode=0;
  82. X     for (i=1; i<argc; i++) {
  83. X          strcpy(buffer,argv[i]);
  84. X          stoupper(buffer);
  85. X          if (!strcmp(buffer,"-SUN")) {
  86. X               machtype=MACH_SUN;
  87. X               continue;
  88. X          }
  89. X          if (!strcmp(buffer,"-IBM")) {
  90. X               machtype=MACH_IBM_RS6000;
  91. X               continue;
  92. X          }
  93. X          if (!strcmp(buffer,"-DEC")) {
  94. X               machtype=MACH_DEC;
  95. X               continue;
  96. X          }
  97. X          if (!strcmp(buffer,"-APOLLO")) {
  98. X               machtype=MACH_APOLLO;
  99. X               continue;
  100. X          }
  101. X          if (!strcmp(buffer,"-HP")) {
  102. X               machtype=MACH_HP;
  103. X               continue;
  104. X          }
  105. X          if (!strcmp(buffer,"-ANY")) {
  106. X               machtype=0;
  107. X               continue;
  108. X          }
  109. X          if (!strcmp(buffer,"-MODEL")) {
  110. X               ++i;
  111. X               if (!(modeltype=netStrToModel(argv[i]))) {
  112. X                    printf("Unknown model specifier (%s,%s).\n",argv[i-1],argv[i]);
  113. X                    --i;
  114. X               }
  115. X               continue;
  116. X          }
  117. X          if (!strcmp(buffer,"-ALL")) {
  118. X               mode=(mode & ~MODE_MASK) | MODE_ALL;
  119. X               continue;
  120. X          }
  121. X          if (!strcmp(buffer,"-DOWN")) {
  122. X               mode=(mode & ~MODE_MASK) | MODE_DOWN;
  123. X               continue;
  124. X          }
  125. X          if (!strcmp(buffer,"-MODELS")) {
  126. X               mode|=MODE_MODELS;
  127. X               continue;
  128. X          }
  129. X          if (!strcmp(buffer,"-NET")) {
  130. X               mode|=MODE_NET;
  131. X               continue;
  132. X          }
  133. X          if (!strcmp(buffer,"-HELP")) {
  134. X               printf("\nUsage: %s [options]\n\n",argv[0]);
  135. X               printf("Options: -sun             Only Sun SparcStations\n");
  136. X               printf("         -dec             Only DECstations\n");
  137. X               printf("         -ibm             Only IBM RS/6000s\n");
  138. X               printf("         -apollo          Only Apollos\n");
  139. X               printf("         -hp              Only HP9000s\n");
  140. X               printf("         -model <model>   Only machines of model type <model>\n");
  141. X               printf("         -down            Only down nodes\n");
  142. X               printf("         -any             Any type of machine\n");
  143. X               printf("         -all             Unlimited list length\n");
  144. X               printf("         -models          Show models instead of last report time\n");
  145. X               printf("         -net             Network server mode\n\n");
  146. X               exit(0);
  147. X          }
  148. X     }
  149. X     netSetup();
  150. X     netuseGetList(machtype,modeltype,mode);
  151. X     netShutdown();
  152. X}
  153. END_OF_FILE
  154.   if test 4694 -ne `wc -c <'netuse/clients/hostinfo.c'`; then
  155.     echo shar: \"'netuse/clients/hostinfo.c'\" unpacked with wrong size!
  156.   fi
  157.   # end of 'netuse/clients/hostinfo.c'
  158. fi
  159. if test -f 'netuse/clients/libresp3.os2' -a "${1}" != "-c" ; then 
  160.   echo shar: Will not clobber existing file \"'netuse/clients/libresp3.os2'\"
  161. else
  162.   echo shar: Extracting \"'netuse/clients/libresp3.os2'\" \(100 characters\)
  163.   sed "s/^X//" >'netuse/clients/libresp3.os2' <<'END_OF_FILE'
  164. X/st:6000 /NOE /packcode /f /co netusead
  165. X
  166. Xnetusead
  167. X..\lib\netuse.lib c:\xln\toolkit\lib\slibsock.lib
  168. END_OF_FILE
  169.   if test 100 -ne `wc -c <'netuse/clients/libresp3.os2'`; then
  170.     echo shar: \"'netuse/clients/libresp3.os2'\" unpacked with wrong size!
  171.   fi
  172.   # end of 'netuse/clients/libresp3.os2'
  173. fi
  174. if test -f 'netuse/daemons/getmodel.c' -a "${1}" != "-c" ; then 
  175.   echo shar: Will not clobber existing file \"'netuse/daemons/getmodel.c'\"
  176. else
  177.   echo shar: Extracting \"'netuse/daemons/getmodel.c'\" \(10274 characters\)
  178.   sed "s/^X//" >'netuse/daemons/getmodel.c' <<'END_OF_FILE'
  179. X/***************************************************************************
  180. X GETMODEL.C - Routines to determine model types
  181. X Written by Lee Liming, 6/6/92
  182. X NOTE: The code in this module is based on many heuristics.  It is not 
  183. X       guaranteed to work forever, and will probably break in the future 
  184. X       when new OS releases or hardware types are introduced.
  185. X
  186. X Copyright (C) 1992 by the Regents of the University of Michigan.
  187. X
  188. X User agrees to reproduce said copyright notice on all copies of the software
  189. X made by the recipient.  
  190. X
  191. X All Rights Reserved. Permission is hereby granted for the recipient to make
  192. X copies and use this software for its own internal purposes only. Recipient of
  193. X this software may re-distribute this software outside of their own
  194. X institution. Permission to market this software commercially, to include this
  195. X product as part of a commercial product, or to make a derivative work for
  196. X commercial purposes, is explicitly prohibited.  All other uses are also
  197. X prohibited unless authorized in writing by the Regents of the University of
  198. X Michigan.
  199. X
  200. X This software is offered without warranty. The Regents of the University of
  201. X Michigan disclaim all warranties, express or implied, including but not
  202. X limited to the implied warranties of merchantability and fitness for any
  203. X particular purpose. In no event shall the Regents of the University of
  204. X Michigan be liable for loss or damage of any kind, including but not limited
  205. X to incidental, indirect, consequential, or special damages. 
  206. X******************************************************************************/
  207. X
  208. X#include "../lib/netuse.h"
  209. X
  210. X
  211. X/* #define TESTING */
  212. X
  213. X
  214. X#ifdef sun
  215. X
  216. X/***************************************************************************
  217. X The following code is derived from the table on page CPU-13 of the Sun
  218. X Field Engineer Handbook, Technical Volume 1.  At present, it only gives
  219. X five possible return types: 0 (unknown), MODEL_SUN3 (unspecified Sun 3),
  220. X MODEL_SUN4 (unspecified Sun 4), MODEL_SUN4_65 (SPARCstation 1+), and 
  221. X MODEL_SUN4_50 (SPARCstation IPX).
  222. X***************************************************************************/
  223. X
  224. X#include <stdio.h>
  225. X#include <sys/types.h>
  226. X
  227. X
  228. X#ifdef __STDC__
  229. Xu_char modelDetect(void)
  230. X#else
  231. Xu_char modelDetect()
  232. X#endif
  233. X{
  234. X     unsigned int hostid;
  235. X
  236. X     hostid=gethostid();
  237. X     if (hostid<0x02000000) return(0);
  238. X     if (hostid<0x21000000) return(MODEL_SUN3);
  239. X     if (hostid<0x31000000) return(MODEL_SUN4);
  240. X     if (hostid<0x41000000) return(0);
  241. X     if (hostid<0x51000000) return(MODEL_SUN3);
  242. X     if (hostid<0x53000000) return(MODEL_SUN4);
  243. X     if (hostid<0x54000000) return(MODEL_SUN4_65);
  244. X     if (hostid<0x57000000) return(MODEL_SUN4);
  245. X     if (hostid<0x58000000) return(MODEL_SUN4_50);
  246. X     if (hostid<0x72000000) return(MODEL_SUN4);
  247. X     return(0);
  248. X}
  249. X
  250. X
  251. X#endif
  252. X#ifdef _AIX
  253. X
  254. X/***************************************************************************
  255. X The following code uses the uname -m command and an unpublished table of
  256. X values to obtain model information for IBM RS/6000 workstations.  This
  257. X may prove somewhat less reliable than the Sun method...
  258. X***************************************************************************/
  259. X
  260. X#include <stdio.h>
  261. X
  262. X
  263. X#define UNAME  "/bin/uname -m"
  264. X
  265. X
  266. X#ifdef __STDC__
  267. Xu_char modelDetect(void)
  268. X#else
  269. Xu_char modelDetect()
  270. X#endif
  271. X{
  272. X     char uname[128];
  273. X     FILE *inf;
  274. X     int modelnum;
  275. X
  276. X     if ((inf=popen(UNAME,"r"))==NULL) {
  277. X#ifdef TESTING
  278. X          fprintf(stderr,"Unable to execute popen() command.\n");
  279. X#endif
  280. X          return(0);
  281. X     }
  282. X     if (fgets(uname,sizeof(uname),inf)==NULL) {
  283. X#ifdef TESTING
  284. X          fprintf(stderr,"Unable to read uname command's output.\n");
  285. X#endif
  286. X          return(0);
  287. X     }
  288. X     else if (uname[strlen(uname)-1]=='\n') uname[strlen(uname)-1]='\0';
  289. X     pclose(inf);
  290. X     if (strlen(uname)<10) {
  291. X#ifdef TESTING
  292. X          fprintf(stderr,"Unable to get model id from uname command's output.\n");
  293. X#endif
  294. X          return(0);
  295. X     }
  296. X     modelnum=(uname[8]-'0')*0x10+(uname[9]-'0');
  297. X#ifdef TESTING
  298. X     printf("Model number value is %02x.\n",modelnum);
  299. X#endif
  300. X     switch (modelnum) {
  301. X          case 0x10: return(MODEL_RS530);  /* Could also be 730... */
  302. X          case 0x11:
  303. X          case 0x14: return(MODEL_RS540);
  304. X          case 0x18: return(MODEL_RS530H);
  305. X          case 0x1C: return(MODEL_RS550);
  306. X          case 0x20: return(MODEL_RS930);
  307. X          case 0x2E: return(MODEL_RS950);  /* Could also be 950E... */
  308. X          case 0x30: return(MODEL_RS520);
  309. X          case 0x31: return(MODEL_RS320);
  310. X          case 0x35: return(MODEL_RS320H);
  311. X          case 0x37: return(MODEL_RS340);
  312. X          case 0x38: return(MODEL_RS350);
  313. X          case 0x41: return(MODEL_RS220);
  314. X          case 0x5C: return(MODEL_RS560);
  315. X     }
  316. X     return(0);
  317. X}
  318. X
  319. X
  320. X#endif
  321. X#if (defined(apollo) || defined(hpux))
  322. X
  323. X/***************************************************************************
  324. X The following code uses the uname -m command to obtain model information
  325. X for HP/Apollo workstations.
  326. X***************************************************************************/
  327. X
  328. X#include <stdio.h>
  329. X
  330. X
  331. X#ifdef apollo
  332. X#define UNAME  "/usr/local/bin/uname -m"
  333. X#else
  334. X#define UNAME  "/bin/uname -m"
  335. X#endif
  336. X
  337. X
  338. X#ifdef __STDC__
  339. Xu_char modelDetect(void)
  340. X#else
  341. Xu_char modelDetect()
  342. X#endif
  343. X{
  344. X     char uname[128];
  345. X     FILE *inf;
  346. X
  347. X     if ((inf=popen(UNAME,"r"))==NULL) {
  348. X#ifdef TESTING
  349. X          fprintf(stderr,"Unable to execute popen() command.\n");
  350. X#endif
  351. X          return(0);
  352. X     }
  353. X     if (fgets(uname,sizeof(uname),inf)==NULL) {
  354. X#ifdef TESTING
  355. X          fprintf(stderr,"Unable to read uname command's output.\n");
  356. X#endif
  357. X          return(0);
  358. X     }
  359. X     else if (uname[strlen(uname)-1]=='\n') uname[strlen(uname)-1]='\0';
  360. X     pclose(inf);
  361. X     if (!strcmp(uname,"DN2500") || !strcmp(uname,"DSP2500"))
  362. X          return(MODEL_AP2500);
  363. X     if (!strcmp(uname,"DN3000") || !strcmp(uname,"DSP3000"))
  364. X          return(MODEL_AP3000);
  365. X     if (!strcmp(uname,"DN3500") || !strcmp(uname,"DSP3500"))
  366. X          return(MODEL_AP3500);
  367. X     if (!strcmp(uname,"DN4000") || !strcmp(uname,"DSP4000"))
  368. X          return(MODEL_AP4000);
  369. X     if (!strcmp(uname,"DN4500") || !strcmp(uname,"DSP4500"))
  370. X          return(MODEL_AP4500);
  371. X     if (!strcmp(uname,"DN5500") || !strcmp(uname,"DSP5500"))
  372. X          return(MODEL_AP5500);
  373. X     if (!strcmp(uname,"DN10010") || !strcmp(uname,"DSP10010"))
  374. X          return(MODEL_AP10010);
  375. X     if (!strcmp(uname,"DN10020") || !strcmp(uname,"DSP10020"))
  376. X          return(MODEL_AP10020);
  377. X     if (!strcmp(uname,"425T") || !strcmp(uname,"425t")) 
  378. X          return(MODEL_HP425T);
  379. X     if (!strcmp(uname,"425E") || !strcmp(uname,"425e")) 
  380. X          return(MODEL_HP425E);
  381. X     if (!strcmp(uname,"9000/705")) return(MODEL_HP9000705);
  382. X     if (!strcmp(uname,"9000/710")) return(MODEL_HP9000710);
  383. X     if (!strcmp(uname,"9000/720")) return(MODEL_HP9000720);
  384. X     if (!strcmp(uname,"9000/730")) return(MODEL_HP9000730);
  385. X     if (!strcmp(uname,"9000/750")) return(MODEL_HP9000750);
  386. X     return(0);
  387. X}
  388. X
  389. X
  390. X#endif
  391. X#ifdef mips
  392. X
  393. X/***************************************************************************
  394. X The following code uses the DEC uerf command to determine the model type
  395. X of a DECstations based on the processor type.  This is one of our sicker
  396. X heuristics, and will probably fail more often...
  397. X***************************************************************************/
  398. X
  399. X#include <stdio.h>
  400. X#include "../lib/parser.h"
  401. X
  402. X#define UERF  "/usr/etc/uerf -r 300 -R -H %s"
  403. X
  404. X
  405. Xtypedef struct _procmapping {
  406. X             char   procname[16];
  407. X             char   modelname[16];
  408. X             u_char model;
  409. X        } PROCMAPPING;
  410. X
  411. X
  412. XPROCMAPPING pmap[]={{"KN01",      "",   MODEL_DS3100},
  413. X                    {"KN02-BA",   "120",MODEL_DS5000120},
  414. X                    {"KN02-BA",   "133",MODEL_DS5000133},
  415. X                    {"KN02/R3000","",   MODEL_DS5000200},
  416. X                    {"?",         "",   0}};
  417. X
  418. X
  419. X#ifdef __STDC__
  420. Xu_char modelDetect(void)
  421. X#else
  422. Xu_char modelDetect()
  423. X#endif
  424. X{
  425. X     char processor[128],myhostname[128],uerfcommand[128],temp[128],temp2[128];
  426. X     FILE *inf;
  427. X     int found=0;
  428. X     PROCMAPPING *pmptr;
  429. X
  430. X#ifdef TESTING
  431. X     fprintf(stderr,"Entering modelDetect().\n");
  432. X#endif
  433. X     gethostname(myhostname,sizeof(myhostname));
  434. X     myhostname[sizeof(myhostname)-1]='\0';
  435. X     myhostname[12]='\0';
  436. X#ifdef TESTING
  437. X     fprintf(stderr,"myhostname is set.\n");
  438. X#endif
  439. X     sprintf(uerfcommand,UERF,myhostname);
  440. X#ifdef TESTING
  441. X     fprintf(stderr,"uerfcommand=%s\n",uerfcommand);
  442. X#endif
  443. X     if ((inf=popen(uerfcommand,"r"))==NULL) {
  444. X#ifdef TESTING
  445. X          fprintf(stderr,"Unable to execute popen() command.\n");
  446. X#endif
  447. X          return(0);
  448. X     }
  449. X     while ((found!=2) && (fgets(processor,sizeof(processor),inf)!=NULL)) {
  450. X          if (processor[strlen(processor)-1]=='\n') 
  451. X               processor[strlen(processor)-1]='\0';
  452. X#ifdef TESTING
  453. X          printf("\"%s\"\n",processor);
  454. X#endif
  455. X          if ((found==1) && !strcmp(textparam(processor,0,temp2),"DECstation")) {
  456. X               if (strlen(textparam(processor,3,temp2))) {
  457. X                    for (pmptr=pmap; pmptr->model; pmptr++)
  458. X                         if (!strcmp(pmptr->procname,temp) &&
  459. X                             !strcmp(pmptr->modelname,temp2)) break;
  460. X                    found=2;
  461. X               }
  462. X               continue;
  463. X          }
  464. X          if (!found && !strcmp(textparam(processor,0,temp),"PROCESSOR")) {
  465. X               if (strlen(textparam(processor,2,temp))) {
  466. X                    for (pmptr=pmap; 
  467. X                         pmptr->model && strcmp(pmptr->procname,temp);
  468. X                         pmptr++)
  469. X                         ;
  470. X                    if (strlen(pmptr->modelname)) found=1;
  471. X                    else found=2;
  472. X               }
  473. X               continue;
  474. X          }
  475. X     }
  476. X     if (found!=2) {
  477. X#ifdef TESTING
  478. X          fprintf(stderr,"Unable to get processor type from uerf's output.\n");
  479. X#endif
  480. X          return(0);
  481. X     }
  482. X     pclose(inf);
  483. X     return(pmptr->model);
  484. X}
  485. X
  486. X
  487. X#endif
  488. X#if !(defined(sun) || defined(apollo) || defined(mips) || defined(hpux) || defined(_AIX))
  489. X
  490. X#ifdef __STDC__
  491. Xu_char modelDetect(void)
  492. X#else
  493. Xu_char modelDetect()
  494. X#endif
  495. X{
  496. X     return(0);
  497. X}
  498. X
  499. X#endif
  500. X
  501. X
  502. X#ifdef TESTING
  503. X
  504. Xmain(argc,argv)
  505. Xint argc;
  506. Xchar *argv[];
  507. X{
  508. X     printf("modelDetect() returns %d.\n",modelDetect());
  509. X}
  510. X
  511. X#endif
  512. END_OF_FILE
  513.   if test 10274 -ne `wc -c <'netuse/daemons/getmodel.c'`; then
  514.     echo shar: \"'netuse/daemons/getmodel.c'\" unpacked with wrong size!
  515.   fi
  516.   # end of 'netuse/daemons/getmodel.c'
  517. fi
  518. if test -f 'netuse/daemons/wakeup.c' -a "${1}" != "-c" ; then 
  519.   echo shar: Will not clobber existing file \"'netuse/daemons/wakeup.c'\"
  520. else
  521.   echo shar: Extracting \"'netuse/daemons/wakeup.c'\" \(9866 characters\)
  522.   sed "s/^X//" >'netuse/daemons/wakeup.c' <<'END_OF_FILE'
  523. X/******************************************************************************
  524. X WAKEUP.C - Wake up a host's netused by sending a datagram to the netused port
  525. X
  526. X Lee Liming and Michael Neil, The Computer Aided Engineering Network
  527. X The University of Michigan
  528. X
  529. X Copyright (C) 1990, 1991, 1992 by the Regents of the University of Michigan.
  530. X
  531. X User agrees to reproduce said copyright notice on all copies of the software
  532. X made by the recipient.  
  533. X
  534. X All Rights Reserved. Permission is hereby granted for the recipient to make
  535. X copies and use this software for its own internal purposes only. Recipient of
  536. X this software may re-distribute this software outside of their own
  537. X institution. Permission to market this software commercially, to include this
  538. X product as part of a commercial product, or to make a derivative work for
  539. X commercial purposes, is explicitly prohibited.  All other uses are also
  540. X prohibited unless authorized in writing by the Regents of the University of
  541. X Michigan.
  542. X
  543. X This software is offered without warranty. The Regents of the University of
  544. X Michigan disclaim all warranties, express or implied, including but not
  545. X limited to the implied warranties of merchantability and fitness for any
  546. X particular purpose. In no event shall the Regents of the University of
  547. X Michigan be liable for loss or damage of any kind, including but not limited
  548. X to incidental, indirect, consequential, or special damages. 
  549. X******************************************************************************/
  550. X
  551. X#include <stdio.h>
  552. X#include <sys/types.h>
  553. X#include <sys/time.h>
  554. X#include <sys/socket.h>
  555. X#include <netinet/in.h>
  556. X#include <netdb.h>
  557. X#include <signal.h>
  558. X#include <setjmp.h>
  559. X#include <pwd.h>
  560. X#include <errno.h>
  561. X#if (defined(_AIX) || defined(apollo))
  562. X#include <sys/ioctl.h>
  563. X#else
  564. X#include <sys/termios.h>
  565. X#endif
  566. X
  567. X#include "../lib/protocol.h"
  568. X#include "../lib/netuse.h"
  569. X#include "../lib/config.h"
  570. X#include "../lib/variable.h"
  571. X
  572. X
  573. Xextern long lTime;
  574. Xextern VARTABLE variables;
  575. X
  576. Xint sock;
  577. Xstruct sockaddr_in myname;
  578. Xjmp_buf jumpTimeout;
  579. Xlong lastCheck=0,lastSave=0,netuseStartTime;
  580. X
  581. X
  582. X#ifdef __STDC__
  583. Xint packetSend(USEREC *msg,struct sockaddr_in *dest)
  584. X#else
  585. Xint packetSend(msg,dest)
  586. XUSEREC *msg;
  587. Xstruct sockaddr_in *dest;
  588. X#endif
  589. X{
  590. X     u_char chk;
  591. X     int rv,count,i;
  592. X     PACKET out;
  593. X
  594. X     msg->load1=htons(msg->load1);
  595. X     msg->load2=htons(msg->load2);
  596. X     msg->load3=htons(msg->load3);
  597. X     msg->users=htons(msg->users);
  598. X     msg->console=htons(msg->console);
  599. X     msg->tmp=htonl(msg->tmp);
  600. X     msg->uid=htons(msg->uid);
  601. X     bcopy(msg,out,sizeof(PACKET));
  602. X     for (chk=0,i=1; i<PACKET_SIZE; i++)
  603. X          chk=(chk+out[i]) % 0xFF;
  604. X     out[0]=chk;
  605. X#ifdef DEBUG
  606. X     printf("Server sending:\n");
  607. X     printf("Opcode=%02x Retcode=%02x Ack=%02x Load1=%04x Load2=%04x Load3=%04x\n",
  608. X            msg->opcode,msg->retcode,msg->ack,ntohs(msg->load1),
  609. X            ntohs(msg->load2),ntohs(msg->load3));
  610. X     printf("Users=%04x Console=%04x Tmp=%08x Mach=%2x Model=%02x Chksum=%02x\n",
  611. X            ntohs(msg->users),ntohs(msg->console),ntohl(msg->tmp),
  612. X            msg->machine,msg->model,out[0]);
  613. X     printf("Data:");
  614. X     for (i=0; i<PACKET_SIZE; i++) {
  615. X          if (i && !(i % 16)) printf("\n     ");
  616. X          printf(" %02x",out[i]);
  617. X     }
  618. X     printf("\n");
  619. X#endif
  620. X     count=0;
  621. X     do {
  622. X          rv=sendto(sock,out,PACKET_SIZE,0,dest,sizeof(struct sockaddr_in));
  623. X          if (rv==(-1)) {
  624. X               perror("sendto");
  625. X               if (++count>SEND_RETRIES) return(RV_nBADSEND);
  626. X          }
  627. X     } while (rv!=PACKET_SIZE);
  628. X     return(RV_nOK);
  629. X}
  630. X
  631. X
  632. X#ifdef __STDC__
  633. Xint AlarmHandler(int sig)
  634. X#else
  635. Xint AlarmHandler(sig)
  636. Xint sig;
  637. X#endif
  638. X{
  639. X     longjmp(jumpTimeout,1);
  640. X     return(0);
  641. X}
  642. X
  643. X
  644. X#ifdef __STDC__
  645. Xint packetReceiveT(USEREC *msg,struct sockaddr_in *src)
  646. X#else
  647. Xint packetReceiveT(msg,src)
  648. XUSEREC *msg;
  649. Xstruct sockaddr_in *src;
  650. X#endif
  651. X{
  652. X     u_char chk;
  653. X     int i;
  654. X     PACKET in;
  655. X
  656. X     do {
  657. X          signal(SIGALRM,AlarmHandler);
  658. X          if (setjmp(jumpTimeout)!=0) return(RV_nTIMEOUT);
  659. X          alarm(TIMEOUT);
  660. X          i=sizeof(struct sockaddr_in);
  661. X          if (recvfrom(sock,in,PACKET_SIZE,0,src,&i)==(-1)) {
  662. X               perror("recvfrom");
  663. X               return(RV_nBADRECV);
  664. X          }
  665. X          alarm(0);
  666. X          signal(SIGALRM,SIG_DFL);
  667. X          bcopy(in,msg,sizeof(PACKET));
  668. X#ifdef DEBUG
  669. X          printf("Server received:\n");
  670. X          printf("Opcode=%02x Retcode=%02x Ack=%02x Load1=%04x Load2=%04x Load3=%04x\n",
  671. X                 msg->opcode,msg->retcode,msg->ack,ntohs(msg->load1),
  672. X                 ntohs(msg->load2),ntohs(msg->load3));
  673. X          printf("Users=%04x Console=%04x Tmp=%08x Mach=%02x Model=%02x Chksum=%02x\n",
  674. X                 ntohs(msg->users),ntohs(msg->console),ntohl(msg->tmp),
  675. X                 msg->machine,msg->model,in[0]);
  676. X          printf("Data:");
  677. X          for (i=0; i<PACKET_SIZE; i++) {
  678. X               if (i && !(i % 16)) printf("\n     ");
  679. X               printf(" %02x",in[i]);
  680. X          }
  681. X          printf("\n");
  682. X#endif
  683. X          for (chk=0,i=1; i<PACKET_SIZE; i++)
  684. X               chk=(chk+in[i]) % 0xFF;
  685. X     } while (chk!=msg->chksum);
  686. X     msg->load1=ntohs(msg->load1);
  687. X     msg->load2=ntohs(msg->load2);
  688. X     msg->load3=ntohs(msg->load3);
  689. X     msg->users=ntohs(msg->users);
  690. X     msg->console=ntohs(msg->console);
  691. X     msg->tmp=ntohl(msg->tmp);
  692. X     msg->uid=ntohs(msg->uid);
  693. X     return(RV_nOK);
  694. X}
  695. X
  696. X
  697. X#ifdef __STDC__
  698. Xint packetReceive(USEREC *msg,struct sockaddr_in *src)
  699. X#else
  700. Xint packetReceive(msg,src)
  701. XUSEREC *msg;
  702. Xstruct sockaddr_in *src;
  703. X#endif
  704. X{
  705. X     u_char chk;
  706. X     int i;
  707. X     PACKET in;
  708. X
  709. X     do {
  710. X          i=sizeof(struct sockaddr_in);
  711. X          if (recvfrom(sock,in,PACKET_SIZE,0,src,&i)==(-1)) {
  712. X               perror("recvfrom");
  713. X               return(RV_nBADRECV);
  714. X          }
  715. X          bcopy(in,msg,sizeof(PACKET));
  716. X#ifdef DEBUG
  717. X          printf("Server received:\n");
  718. X          printf("Opcode=%02x Retcode=%02x Ack=%02x Load1=%04x Load2=%04x Load3=%04x\n",
  719. X                 msg->opcode,msg->retcode,msg->ack,ntohs(msg->load1),
  720. X                 ntohs(msg->load2),ntohs(msg->load3));
  721. X          printf("Users=%04x Console=%04x Tmp=%08x Mach=%02x Model=%02x Chksum=%02x\n",
  722. X                 ntohs(msg->users),ntohs(msg->console),ntohl(msg->tmp),
  723. X                 msg->machine,msg->model,in[0]);
  724. X          printf("PACKET_SIZE=%d\n",PACKET_SIZE);
  725. X          printf("Data:");
  726. X          for (i=0; i<PACKET_SIZE; i++) {
  727. X               if (i && !(i % 16)) printf("\n     ");
  728. X               printf(" %02x",in[i]);
  729. X          }
  730. X          printf("\n");
  731. X#endif
  732. X          for (chk=0,i=1; i<PACKET_SIZE; i++)
  733. X               chk=(chk+in[i]) % 0xFF;
  734. X#ifdef DEBUG
  735. X          if (chk!=msg->chksum)
  736. X               printf("Bad checksum! %2x should be %2x\n",msg->chksum,chk);
  737. X#endif
  738. X     } while (chk!=msg->chksum);
  739. X     msg->load1=ntohs(msg->load1);
  740. X     msg->load2=ntohs(msg->load2);
  741. X     msg->load3=ntohs(msg->load3);
  742. X     msg->users=ntohs(msg->users);
  743. X     msg->console=ntohs(msg->console);
  744. X     msg->tmp=ntohl(msg->tmp);
  745. X     msg->uid=ntohs(msg->uid);
  746. X     return(RV_nOK);
  747. X}
  748. X
  749. X
  750. X#ifdef __STDC__
  751. Xvoid netSetup(void)
  752. X#else
  753. Xvoid netSetup()
  754. X#endif
  755. X{
  756. X     struct servent *servptr;
  757. X     int portno;
  758. X
  759. X     sock=socket(AF_INET,SOCK_DGRAM,0);
  760. X     if (sock<0) {
  761. X          perror("Acquiring a socket");
  762. X          exit(1);
  763. X     }
  764. X     myname.sin_family=AF_INET;
  765. X     myname.sin_addr.s_addr=INADDR_ANY;
  766. X     myname.sin_port=0;
  767. X     if (bind(sock,&myname,sizeof(myname))) {
  768. X          perror("Binding socket");
  769. X          exit(1);
  770. X     }
  771. X}
  772. X
  773. X
  774. X#ifdef __STDC__
  775. Xvoid netShutdown(void)
  776. X#else
  777. Xvoid netShutdown()
  778. X#endif
  779. X{
  780. X     close(sock);
  781. X}
  782. X
  783. X
  784. X/*****************************************************************************
  785. X void logit(msg)
  786. X
  787. X This function logs a message in the (previously opened) logfile.  Note that
  788. X fflush() is used to ensure that the message gets logged to disk.
  789. X*****************************************************************************/
  790. X
  791. X#ifdef __STDC__
  792. Xvoid logit(char *msg)
  793. X#else
  794. Xvoid logit(msg)
  795. Xchar *msg;
  796. X#endif
  797. X{
  798. X     char atime[64];
  799. X     struct timeval tv;
  800. X     struct timezone tz;
  801. X     int retries;
  802. X     FILE *logf;
  803. X
  804. X     gettimeofday(&tv,&tz);
  805. X     strcpy(atime,asctime(localtime(&(tv.tv_sec))));
  806. X     atime[strlen(atime)-1]='\0';
  807. X     for (retries=0; (retries<5) && ((logf=fopen(NETUSELOG,"a"))==NULL); ++retries)
  808. X          sleep(5);
  809. X     if (retries==5) {
  810. X          fprintf(stderr,"Unable to open log file %s for update.\n",NETUSELOG);
  811. X          return;
  812. X     }
  813. X     fprintf(logf,"%s -- %s\n",atime,msg);
  814. X     fclose(logf);
  815. X     chmod(NETUSELOG,0644);
  816. X}
  817. X
  818. X
  819. X#ifdef __STDC__
  820. Xvoid hostWakeup(char *host)
  821. X#else
  822. Xvoid hostWakeup(host)
  823. Xchar *host;
  824. X#endif
  825. X{
  826. X     struct servent *servptr;
  827. X     struct sockaddr_in dest;
  828. X     u_long ip;
  829. X     char buf[128];
  830. X
  831. X     servptr=getservbyname(NETUSED_SERVICE,NULL);
  832. X     if (servptr==NULL) dest.sin_port=NETUSED_PORT;
  833. X     else dest.sin_port=servptr->s_port;
  834. X     dest.sin_family=AF_INET;
  835. X     if (!(ip=netGetAddress(host))) {
  836. X          fprintf(stderr,"Unable to get IP address for %s.\n",host);
  837. X          return;
  838. X     }
  839. X     dest.sin_addr.s_addr=ip;
  840. X     printf("Yo!  (Waking up %s)...\n",host);
  841. X     if (sendto(sock,"Wakeup!",9,0,&dest,sizeof(dest))==(-1)) {
  842. X          sprintf(buf,"Error %d from sendto() in WakeTheDead().\n",errno);
  843. X          logit(buf);
  844. X     }
  845. X     close(socket);
  846. X}
  847. X
  848. X
  849. X#ifdef __STDC__
  850. Xmain(int argc,char *argv[])
  851. X#else
  852. Xmain(argc,argv)
  853. Xint argc;
  854. Xchar *argv[];
  855. X#endif
  856. X{
  857. X     int i;
  858. X     char buffer[1024];
  859. X
  860. X     netSetup();
  861. X     for (i=1; i<argc; i++) {
  862. X          strcpy(buffer,argv[i]);
  863. X          stoupper(buffer);
  864. X          if (buffer[0]=='-') {
  865. X               if (!strcmp(buffer,"-HELP")) {
  866. X                    printf("\nUsage: %s [options] host1 ...\n\n",argv[0]);
  867. X                    printf("Options: -help            Display usage info\n\n");
  868. X               }
  869. X          }
  870. X          else hostWakeup(buffer);
  871. X     }
  872. X     netShutdown();
  873. X}
  874. END_OF_FILE
  875.   if test 9866 -ne `wc -c <'netuse/daemons/wakeup.c'`; then
  876.     echo shar: \"'netuse/daemons/wakeup.c'\" unpacked with wrong size!
  877.   fi
  878.   # end of 'netuse/daemons/wakeup.c'
  879. fi
  880. if test -f 'netuse/lib/getlist.c' -a "${1}" != "-c" ; then 
  881.   echo shar: Will not clobber existing file \"'netuse/lib/getlist.c'\"
  882. else
  883.   echo shar: Extracting \"'netuse/lib/getlist.c'\" \(10276 characters\)
  884.   sed "s/^X//" >'netuse/lib/getlist.c' <<'END_OF_FILE'
  885. X/******************************************************************************
  886. X GETLIST.C - NETUSE Client List-receiving Routines
  887. X
  888. X The functions defined in this module are used to receive host lists from the
  889. X NETUSE server.
  890. X
  891. X Lee Liming and Michael Neil, The Computer Aided Engineering Network
  892. X The University of Michigan
  893. X
  894. X Copyright (C) 1990, 1991, 1992 by the Regents of the University of Michigan.
  895. X
  896. X User agrees to reproduce said copyright notice on all copies of the software
  897. X made by the recipient.  
  898. X
  899. X All Rights Reserved. Permission is hereby granted for the recipient to make
  900. X copies and use this software for its own internal purposes only. Recipient of
  901. X this software may re-distribute this software outside of their own
  902. X institution. Permission to market this software commercially, to include this
  903. X product as part of a commercial product, or to make a derivative work for
  904. X commercial purposes, is explicitly prohibited.  All other uses are also
  905. X prohibited unless authorized in writing by the Regents of the University of
  906. X Michigan.
  907. X
  908. X This software is offered without warranty. The Regents of the University of
  909. X Michigan disclaim all warranties, express or implied, including but not
  910. X limited to the implied warranties of merchantability and fitness for any
  911. X particular purpose. In no event shall the Regents of the University of
  912. X Michigan be liable for loss or damage of any kind, including but not limited
  913. X to incidental, indirect, consequential, or special damages. 
  914. X******************************************************************************/
  915. X
  916. X#include <stdio.h>
  917. X#include <sys/types.h>
  918. X#ifdef IBMTCPIP
  919. X#include <types.h>
  920. X#endif
  921. X#ifndef MSDOS
  922. X#include <sys/time.h>
  923. X#endif
  924. X#include <sys/socket.h>
  925. X#include <netinet/in.h>
  926. X#include <netdb.h>
  927. X#include "network.h"
  928. X#include "protocol.h"
  929. X#include "netuse.h"
  930. X
  931. X
  932. Xextern long netuseTimeout;                                 /* From network.c */
  933. X
  934. Xchar buf1[255];
  935. Xint buf1bytes=0,bufptr=0;
  936. X
  937. X
  938. X/******************************************************************************
  939. X This function queries the NETUSE server for a host list of a type specified
  940. X by the machine (vendor type) and mode (listing mode) parameters.  The list is
  941. X printed directly on stdout (Ok, ok...  So write you're own!).
  942. X
  943. X The function uses a UDP packet for the initial request, and includes the port
  944. X number of a TCP socket which it wants to receive the list at.  The server
  945. X will send the list in text format to this TCP socket.  If the server never
  946. X responds, this function will wait until the accept() fails before returning
  947. X (i.e., there's no timeout on this one).
  948. X
  949. X Valid modes and machine and model types are given in netuse.h.
  950. X******************************************************************************/
  951. X
  952. X#ifdef __STDC__
  953. Xint netuseGetList(u_char machine,u_char model,int mode)
  954. X#else
  955. Xint netuseGetList(machine,model,mode)
  956. Xu_char machine,model;
  957. Xint mode;
  958. X#endif
  959. X{
  960. X     USEREC msg;
  961. X     int tcpsock,readsock,i,rv;
  962. X     struct sockaddr_in newaddr;
  963. X     FILE *inf;
  964. X     char buf[132],*ch,eol[3];
  965. X#ifdef IBMTCPIP
  966. X     int fdset[2];
  967. X     long tv;
  968. X#else
  969. X     struct fd_set fdset;
  970. X     struct timeval tv;
  971. X#endif
  972. X
  973. X     tcpsock=socket(AF_INET,SOCK_STREAM,0);
  974. X     if (tcpsock<0) {
  975. X          perror("socket");
  976. X          netShutdown();
  977. X          return(1);
  978. X     }
  979. X     newaddr.sin_family=AF_INET;
  980. X     newaddr.sin_addr.s_addr=INADDR_ANY;
  981. X     newaddr.sin_port=0;
  982. X     if (bind(tcpsock,(struct sockaddr *)&newaddr,sizeof(newaddr))) {
  983. X          perror("bind");
  984. X          return(1);
  985. X     }
  986. X     rv=listen(tcpsock,2);
  987. X#ifdef DEBUG
  988. X     if (rv) printf("listen() returned %d in netuseGetList().\n",rv);
  989. X#endif
  990. X     if (mode & MODE_MODELS) {
  991. X          switch (mode & MODE_MASK) {
  992. X               case MODE_DISP:
  993. X                    msg.opcode=OP_DISPMLIST;
  994. X                    break;
  995. X               case MODE_ALL:
  996. X                    msg.opcode=OP_GETMLIST;
  997. X                    break;
  998. X               case MODE_DOWN:
  999. X                    msg.opcode=OP_GETMDOWN;
  1000. X                    break;
  1001. X          }
  1002. X     }
  1003. X     else {
  1004. X          switch (mode & MODE_MASK) {
  1005. X               case MODE_DISP:
  1006. X                    msg.opcode=OP_DISPLIST;
  1007. X                    break;
  1008. X               case MODE_ALL:
  1009. X                    msg.opcode=OP_GETLIST;
  1010. X                    break;
  1011. X               case MODE_DOWN:
  1012. X                    msg.opcode=OP_GETDOWN;
  1013. X                    break;
  1014. X          }
  1015. X     }
  1016. X     msg.machine=machine;
  1017. X     msg.model=model;
  1018. X     i=sizeof(newaddr);
  1019. X     if (rv=getsockname(tcpsock,(struct sockaddr *)&newaddr,&i)) {
  1020. X#ifdef DEBUG
  1021. X          printf("getsockname() returned %d in netuseGetList().\n",rv);
  1022. X#endif
  1023. X          return(-1);
  1024. X     }
  1025. X#ifdef DEBUG
  1026. X     printf("Using port number %04x in netuseGetList().\n",newaddr.sin_port);
  1027. X#endif
  1028. X     msg.portno=ntohs(newaddr.sin_port);
  1029. X#ifdef DEBUG
  1030. X     printf("Sending port number %04x in netuseGetList().\n",msg.portno);
  1031. X#endif
  1032. X#ifdef MSDOS
  1033. X     msg.uid=0;
  1034. X#else
  1035. X     msg.uid=getuid();
  1036. X#endif
  1037. X     if (rv=packetSend(&msg)) {
  1038. X#ifdef DEBUG
  1039. X          printf("packetSend() returned %d in netuseGetList().\n",rv);
  1040. X#endif
  1041. X     }
  1042. X#ifdef IBMTCPIP
  1043. X     fdset[0]=tcpsock;
  1044. X     tv=netuseTimeout*(REQ_RETRIES+1)*1000;
  1045. X#ifdef DEBUG
  1046. X     printf("About to call IBM's lobotomized select() in netuseGetList().\n");
  1047. X     fflush(stdout);
  1048. X#endif
  1049. X     rv=select(fdset,1,0,0,tv);
  1050. X#else /* ! IBMTCPIP */
  1051. X     FD_ZERO(&fdset);
  1052. X     FD_SET(tcpsock,&fdset);
  1053. X     tv.tv_sec=netuseTimeout*(REQ_RETRIES+1);
  1054. X     tv.tv_usec=0;
  1055. X#ifdef DEBUG
  1056. X     printf("About to call select() in netuseGetList().\n");
  1057. X     fflush(stdout);
  1058. X#endif
  1059. X     rv=select(FD_SETSIZE,&fdset,NULL,NULL,&tv);
  1060. X#endif /* !IBMTCPIP */
  1061. X#ifdef DEBUG
  1062. X     printf("Back from select() in netuseGetList().\n");
  1063. X     fflush(stdout);
  1064. X#endif
  1065. X     if (rv<0) {
  1066. X          fprintf(stderr,"netuseGetList(): Error %d from select().\n",rv);
  1067. X#if (defined(MSDOS) || defined(OS2))
  1068. X          soclose(tcpsock);
  1069. X#else
  1070. X          close(tcpsock);
  1071. X#endif
  1072. X          return(RV_nBADRECV);
  1073. X     }
  1074. X#ifdef IBMTCPIP
  1075. X     if (fdset[0]!=(-1)) {
  1076. X#else
  1077. X     if (FD_ISSET(tcpsock,&fdset)) {
  1078. X#endif
  1079. X#ifdef DEBUG
  1080. X          printf("About to call accept() in netuseGetList()...\n");
  1081. X          fflush(stdout);
  1082. X#endif
  1083. X          if ((readsock=accept(tcpsock,(struct sockaddr *)&newaddr,&i))<0) {
  1084. X               perror("accept");
  1085. X#if (defined(MSDOS) || defined(IBMTCPIP))
  1086. X               soclose(tcpsock);
  1087. X#else
  1088. X               close(tcpsock);
  1089. X#endif
  1090. X               return(1);
  1091. X          }
  1092. X#ifdef DEBUG
  1093. X          printf("Successful accept() in netuseGetList().\n");
  1094. X          fflush(stdout);
  1095. X#endif
  1096. X#if (defined(MSDOS) || defined(IBMTCPIP))
  1097. X          soclose(tcpsock);
  1098. X#else
  1099. X          close(tcpsock);
  1100. X#endif
  1101. X     }
  1102. X     else {
  1103. X#if (defined(MSDOS) || defined(IBMTCPIP))
  1104. X          soclose(tcpsock);
  1105. X#else
  1106. X          close(tcpsock);
  1107. X#endif
  1108. X#ifdef DEBUG
  1109. X          printf("select() failed to get a connection in netuseGetList().\n");
  1110. X          fflush(stdout);
  1111. X#endif
  1112. X          return(RV_nTIMEOUT);
  1113. X     }
  1114. X     if (mode & MODE_NET) strcpy(eol,"\r\n");
  1115. X     else strcpy(eol,"\n");
  1116. X     printf("%s",eol);
  1117. X     while (!netuseTCPReceiveMessage(readsock,buf)) {
  1118. X          for (ch=buf; *ch!='\0'; ch++)
  1119. X               if ((*ch=='\n') || (*ch=='\r')) strcpy(ch,ch+1);
  1120. X          printf("%s%s",buf,eol);
  1121. X     }
  1122. X#if (defined(MSDOS) || defined(IBMTCPIP))
  1123. X     soclose(readsock);
  1124. X#else
  1125. X     close(readsock);
  1126. X#endif
  1127. X     return(0);
  1128. X}
  1129. X
  1130. X
  1131. X#ifdef __STDC__
  1132. Xint netuseTCPReceiveMessage(int sock,char *msg)
  1133. X#else
  1134. Xint netuseTCPReceiveMessage(sock,msg)
  1135. Xint sock;
  1136. Xchar *msg;
  1137. X#endif
  1138. X{
  1139. X     int bytesRead,done,rv;
  1140. X     char *bp;
  1141. X#ifdef IBMTCPIP
  1142. X     int fdset[2];
  1143. X     long tv;
  1144. X#else
  1145. X     struct fd_set fdset;
  1146. X     struct timeval tv;
  1147. X#endif
  1148. X
  1149. X#ifdef DEBUG
  1150. X     printf("\nInside netuseTCPReceiveMessage().\n");
  1151. X#endif
  1152. X     done=0; bp=msg;
  1153. X     do {
  1154. X          for ( ; !done && (bufptr<buf1bytes); bufptr++) {
  1155. X               if ((*(bp++)=buf1[bufptr])=='\n') {
  1156. X                    *bp='\0';
  1157. X                    done=1;
  1158. X               }
  1159. X          }
  1160. X          if (!done) {
  1161. X#ifdef IBMTCPIP
  1162. X               fdset[0]=sock;
  1163. X               tv=netuseTimeout*(REQ_RETRIES+1)*1000;
  1164. X#ifdef DEBUG
  1165. X               printf("About to call IBM's lobotomized select() in netuseTCPReceiveMessage().\n");
  1166. X               fflush(stdout);
  1167. X#endif
  1168. X               rv=select(fdset,1,0,0,tv);
  1169. X#else /* ! IBMTCPIP */
  1170. X               FD_ZERO(&fdset);
  1171. X               FD_SET(sock,&fdset);
  1172. X               tv.tv_sec=netuseTimeout*(REQ_RETRIES+1);
  1173. X               tv.tv_usec=0;
  1174. X#ifdef DEBUG
  1175. X               printf("About to call select() in netuseTCPReceiveMessage().\n");
  1176. X               fflush(stdout);
  1177. X#endif
  1178. X               rv=select(FD_SETSIZE,&fdset,NULL,NULL,&tv);
  1179. X#endif /* ! IBMTCPIP */
  1180. X#ifdef DEBUG
  1181. X               printf("select() in netuseTCPReceiveMessage() returns %d.\n",rv);
  1182. X               fflush(stdout);
  1183. X#endif
  1184. X               if (rv==(-1)) {
  1185. X                    fprintf(stderr,"select: Error in netuseTCPReceiveMessage().\n",rv);
  1186. X                    return(RV_nBADRECV);
  1187. X               }
  1188. X#ifdef IBMTCPIP
  1189. X               if (fdset[0]!=(-1)) {
  1190. X#else
  1191. X               if (FD_ISSET(sock,&fdset)) {
  1192. X#endif
  1193. X#ifdef DEBUG
  1194. X                    printf("Calling soread() in netuseTCPReceiveMessage()...\n",rv);
  1195. X#endif
  1196. X#ifdef NOVELL
  1197. X                    if ((buf1bytes=soread(sock,buf1,sizeof(buf1)))<=0) {
  1198. X#else
  1199. X#ifdef IBMTCPIP
  1200. X                    if ((buf1bytes=recv(sock,buf1,sizeof(buf1),0))<=0) {                    
  1201. X#else
  1202. X                    if ((buf1bytes=read(sock,buf1,sizeof(buf1)))<=0) {
  1203. X#endif
  1204. X#endif
  1205. X#ifdef DEBUG
  1206. X                         printf("Connection to NETUSE server was dropped by server.\n");
  1207. X#endif
  1208. X                         return(-2);
  1209. X                    }
  1210. X                    else bufptr=0;
  1211. X#ifdef DEBUG
  1212. X                    printf("soread() success in netuseTCPReceiveMessage()...\n",rv);
  1213. X#endif
  1214. X               }
  1215. X               else {
  1216. X#ifdef DEBUG
  1217. X                    printf("select() timed out in netuseTCPReceiveMessage().\n",rv);
  1218. X#endif
  1219. X                    return(RV_nTIMEOUT);
  1220. X               }
  1221. X          }
  1222. X     } while (!done);
  1223. X     if (strlen(msg) && (msg[strlen(msg)-1]=='\n'))
  1224. X          msg[strlen(msg)-1]='\0';
  1225. X     if (strlen(msg) && (msg[strlen(msg)-1]=='\r'))
  1226. X          msg[strlen(msg)-1]='\0';
  1227. X#ifdef DEBUG
  1228. X     printf("RECV: \"%s\"\n",msg);
  1229. X#endif
  1230. X     return(0);
  1231. X}
  1232. X
  1233. X
  1234. END_OF_FILE
  1235.   if test 10276 -ne `wc -c <'netuse/lib/getlist.c'`; then
  1236.     echo shar: \"'netuse/lib/getlist.c'\" unpacked with wrong size!
  1237.   fi
  1238.   # end of 'netuse/lib/getlist.c'
  1239. fi
  1240. if test -f 'netuse/lib/network.c' -a "${1}" != "-c" ; then 
  1241.   echo shar: Will not clobber existing file \"'netuse/lib/network.c'\"
  1242. else
  1243.   echo shar: Extracting \"'netuse/lib/network.c'\" \(10106 characters\)
  1244.   sed "s/^X//" >'netuse/lib/network.c' <<'END_OF_FILE'
  1245. X/******************************************************************************
  1246. X NETWORK.C - NETUSE Client-side Network Interface
  1247. X
  1248. X This file contains the basic network routines used by the NETUSE clients for
  1249. X sending and receiving data from the server.
  1250. X
  1251. X Lee Liming and Michael Neil, The Computer Aided Engineering Network
  1252. X The University of Michigan
  1253. X
  1254. X Copyright (C) 1990, 1991, 1992 by the Regents of the University of Michigan.
  1255. X
  1256. X User agrees to reproduce said copyright notice on all copies of the software
  1257. X made by the recipient.  
  1258. X
  1259. X All Rights Reserved. Permission is hereby granted for the recipient to make
  1260. X copies and use this software for its own internal purposes only. Recipient of
  1261. X this software may re-distribute this software outside of their own
  1262. X institution. Permission to market this software commercially, to include this
  1263. X product as part of a commercial product, or to make a derivative work for
  1264. X commercial purposes, is explicitly prohibited.  All other uses are also
  1265. X prohibited unless authorized in writing by the Regents of the University of
  1266. X Michigan.
  1267. X
  1268. X This software is offered without warranty. The Regents of the University of
  1269. X Michigan disclaim all warranties, express or implied, including but not
  1270. X limited to the implied warranties of merchantability and fitness for any
  1271. X particular purpose. In no event shall the Regents of the University of
  1272. X Michigan be liable for loss or damage of any kind, including but not limited
  1273. X to incidental, indirect, consequential, or special damages. 
  1274. X******************************************************************************/
  1275. X
  1276. X#include <stdio.h>
  1277. X#include <sys/types.h>
  1278. X#ifndef MSDOS
  1279. X#include <sys/time.h>
  1280. X#endif
  1281. X#ifdef IBMTCPIP
  1282. X#include <types.h>
  1283. X#endif
  1284. X#include <sys/socket.h>
  1285. X#include <netinet/in.h>
  1286. X#include <netdb.h>
  1287. X#include <signal.h>
  1288. X#include <setjmp.h>
  1289. X#include "protocol.h"
  1290. X
  1291. X
  1292. X/* #define DEBUG */
  1293. X
  1294. X
  1295. Xint sock;                               /* Socket used for UDP send/receives */
  1296. Xstruct sockaddr_in servername,clientname;           /* Server & client names */
  1297. Xjmp_buf jumpTimeout;                              /* Timeout handler address */
  1298. Xlong netuseTimeout=TIMEOUT;                        /* Override timeout value */
  1299. X
  1300. X
  1301. X/******************************************************************************
  1302. X int packetSend(USEREC *msg)
  1303. X
  1304. X This function sends a NETUSE packet to the server.  netSetup() must have been
  1305. X previously called.  Transmit failures are returned (interpreted) to the
  1306. X caller.  All numeric fields in the packet are converted to network order
  1307. X before sending.
  1308. X******************************************************************************/
  1309. X
  1310. X#ifdef __STDC__
  1311. Xint packetSend(USEREC *msg)
  1312. X#else
  1313. Xint packetSend(msg)
  1314. XUSEREC *msg;
  1315. X#endif
  1316. X{
  1317. X     u_char chk;
  1318. X     int rv,count,i;
  1319. X     PACKET out;
  1320. X
  1321. X     msg->load1=htons(msg->load1);
  1322. X     msg->load2=htons(msg->load2);
  1323. X     msg->load3=htons(msg->load3);
  1324. X     msg->users=htons(msg->users);
  1325. X     msg->console=htons(msg->console);
  1326. X     msg->tmp=htonl(msg->tmp);
  1327. X     msg->uid=htons(msg->uid);
  1328. X     bcopy((char *)msg,(char *)out,sizeof(PACKET));
  1329. X     for (chk=0,i=1; i<PACKET_SIZE; i++)
  1330. X          chk=(chk+out[i]) % 0xFF;
  1331. X     out[0]=chk;
  1332. X#ifdef DEBUG
  1333. X     printf("NETUSE Client sending:\n");
  1334. X     printf("Opcode=%02x Retcode=%02x Ack=%02x Load1=%04x Load2=%04x Load3=%04x Users=%04x Console=%04x /tmp=%08x Mach=%02x Model=%02x Chksum=%02x\n",
  1335. X            msg->opcode,msg->retcode,msg->ack,ntohs(msg->load1),
  1336. X            ntohs(msg->load2),ntohs(msg->load3),ntohs(msg->users),
  1337. X            ntohs(msg->console),ntohl(msg->tmp),msg->machine,msg->model,out[0]);
  1338. X     printf("Data:");
  1339. X     for (i=0; i<PACKET_SIZE; i++) {
  1340. X          if (i && !(i % 16)) printf("\n     ");
  1341. X          printf(" %02x",out[i]);
  1342. X     }
  1343. X     printf("\n");
  1344. X#endif
  1345. X     count=0;
  1346. X     do {
  1347. X          rv=sendto(sock,out,PACKET_SIZE,0,
  1348. X                    &servername,sizeof(struct sockaddr));
  1349. X          if (rv==(-1)) {
  1350. X               perror("sendto");
  1351. X               if (++count>SEND_RETRIES) return(RV_nBADSEND);
  1352. X          }
  1353. X     } while (rv!=PACKET_SIZE);
  1354. X     return(RV_nOK);
  1355. X}
  1356. X
  1357. X
  1358. X/******************************************************************************
  1359. X int packetReceive(USEREC *msg)
  1360. X
  1361. X This function receives a packet from the NETUSE server.  If no packet arrives
  1362. X within one timeout period, RV_nTIMEOUT is returned.  Packets with bad
  1363. X checksum fields are ignored.  Otherwise, either a network error is returned,
  1364. X or the msg buffer is filled with the received packet's data (all numeric
  1365. X fields are converted to host format).
  1366. X******************************************************************************/
  1367. X
  1368. X#ifdef __STDC__
  1369. Xint packetReceive(USEREC *msg)
  1370. X#else
  1371. Xint packetReceive(msg)
  1372. XUSEREC *msg;
  1373. X#endif
  1374. X{
  1375. X     u_char chk;
  1376. X     int i,rv;
  1377. X     PACKET in;
  1378. X#ifndef IBMTCPIP
  1379. X     struct fd_set fdset;
  1380. X     struct timeval tv;
  1381. X#else
  1382. X     int fdset[2];
  1383. X     long tv;
  1384. X#endif
  1385. X
  1386. X     do {
  1387. X#ifdef IBMTCPIP
  1388. X          fdset[0]=sock;
  1389. X          tv=netuseTimeout*1000;
  1390. X#ifdef DEBUG
  1391. X          printf("About to call IBM's lobotomized select() in packetReceive().\n");
  1392. X          fflush(stdout);
  1393. X#endif
  1394. X          rv=select(fdset,1,0,0,tv);
  1395. X#else /* !IBMTCPIP */
  1396. X          FD_ZERO(&fdset);
  1397. X          FD_SET(sock,&fdset);
  1398. X          tv.tv_sec=netuseTimeout;
  1399. X          tv.tv_usec=0;
  1400. X#ifdef DEBUG
  1401. X          printf("About to call select() in packetReceive().\n");
  1402. X          fflush(stdout);
  1403. X#endif
  1404. X          rv=select(FD_SETSIZE,&fdset,NULL,NULL,&tv);
  1405. X#endif /* !IBMTCPIP */
  1406. X#ifdef DEBUG
  1407. X          printf("Back from select() in packetReceive().\n");
  1408. X          fflush(stdout);
  1409. X#endif
  1410. X          if (rv==(-1)) {
  1411. X               fprintf(stderr,"packetReceive(): Error %d from select().\n",rv);
  1412. X               return(RV_nBADRECV);
  1413. X          }
  1414. X#ifdef IBMTCPIP
  1415. X               if (fdset[0]!=(-1)) {
  1416. X#else
  1417. X               if (FD_ISSET(sock,&fdset)) {
  1418. X#endif
  1419. X               i=sizeof(struct sockaddr);
  1420. X               if (recvfrom(sock,in,PACKET_SIZE,0,
  1421. X                            &servername,&i)==(-1)) {
  1422. X                    perror("recvfrom");
  1423. X                    return(RV_nBADRECV);
  1424. X               }
  1425. X               bcopy((char *)in,(char *)msg,sizeof(PACKET));
  1426. X          }
  1427. X          else return(RV_nTIMEOUT);
  1428. X#ifdef DEBUG
  1429. X          printf("NETUSE Client received:\n");
  1430. X          printf("Opcode=%02x Retcode=%02x Ack=%02x Load1=%04x Load2=%04x Load3=%04x Users=%04x Console=%04x /tmp=%08x Mach=%02x Model=%02x Chksum=%02x\n",
  1431. X                 msg->opcode,msg->retcode,msg->ack,ntohs(msg->load1),
  1432. X                 ntohs(msg->load2),ntohs(msg->load3),ntohs(msg->users),
  1433. X                 ntohs(msg->console),ntohl(msg->tmp),msg->machine,msg->model,in[0]);
  1434. X          printf("Data:");
  1435. X          for (i=0; i<PACKET_SIZE; i++) {
  1436. X               if (i && !(i % 16)) printf("\n     ");
  1437. X               printf(" %02x",in[i]);
  1438. X          }
  1439. X          printf("\n");
  1440. X#endif
  1441. X          for (chk=0,i=1; i<PACKET_SIZE; i++)
  1442. X               chk=(chk+in[i]) % 0xFF;
  1443. X#ifdef DEBUG
  1444. X          if (chk!=in[0]) printf("Bad checksum!!!\n");
  1445. X#endif
  1446. X     } while (chk!=in[0]);
  1447. X     msg->load1=ntohs(msg->load1);
  1448. X     msg->load2=ntohs(msg->load2);
  1449. X     msg->load3=ntohs(msg->load3);
  1450. X     msg->users=ntohs(msg->users);
  1451. X     msg->console=ntohs(msg->console);
  1452. X     msg->tmp=ntohl(msg->tmp);
  1453. X     msg->uid=ntohs(msg->uid);
  1454. X     return(RV_nOK);
  1455. X}
  1456. X
  1457. X
  1458. X#ifdef __STDC__
  1459. Xchar *textIPAddress(u_long inaddr,char *buf)
  1460. X#else
  1461. Xchar *textIPAddress(inaddr,buf)
  1462. Xu_long inaddr;
  1463. Xchar *buf;
  1464. X#endif
  1465. X{
  1466. X     struct hostent *hp;
  1467. X     u_char ipbytes[4];
  1468. X     char *cp;
  1469. X
  1470. X#if (defined(MSDOS) && defined(NOVELL))
  1471. X     if ((cp=raddr(inaddr))==(char *)(-1)) {
  1472. X          bcopy((char *)&inaddr,ipbytes,sizeof(ipbytes));
  1473. X          sprintf(buf,"%d.%d.%d.%d",ipbytes[0],ipbytes[1],ipbytes[2],ipbytes[3]);
  1474. X     }
  1475. X     else strcpy(buf,cp);
  1476. X#else
  1477. X     hp=gethostbyaddr(&inaddr,sizeof(u_long),AF_INET);
  1478. X     if (hp==NULL) {
  1479. X          bcopy((char *)&inaddr,ipbytes,sizeof(ipbytes));
  1480. X          sprintf(buf,"%d.%d.%d.%d",ipbytes[0],ipbytes[1],ipbytes[2],ipbytes[3])
  1481. X;
  1482. X     }
  1483. X     else strcpy(buf,hp->h_name);
  1484. X#endif
  1485. X     return(buf);
  1486. X}
  1487. X
  1488. X
  1489. X/******************************************************************************
  1490. X void netSetup(void)
  1491. X
  1492. X Set up the client side of a NETUSE session.  Get a socket, fill in the
  1493. X server's adddress, bind to a local port, etc.
  1494. X******************************************************************************/
  1495. X
  1496. X#ifdef __STDC__
  1497. Xvoid netSetup(void)
  1498. X#else
  1499. Xvoid netSetup()
  1500. X#endif
  1501. X{
  1502. X     struct hostent *hp;
  1503. X     char buf[132],*cp,**cpp;
  1504. X     long iaddr;
  1505. X
  1506. X#ifdef DEBUG
  1507. X     printf("Getting a socket...\n");
  1508. X#endif
  1509. X     sock=socket(AF_INET,SOCK_DGRAM,0);
  1510. X     if (sock<0) {
  1511. X          perror("Acquiring a socket");
  1512. X          exit(1);
  1513. X     }
  1514. X     clientname.sin_family=AF_INET;
  1515. X     clientname.sin_addr.s_addr=INADDR_ANY;
  1516. X     clientname.sin_port=0;
  1517. X#ifdef DEBUG
  1518. X     printf("Binding socket...\n");
  1519. X#endif
  1520. X     if (bind(sock,&clientname,sizeof(clientname))) {
  1521. X          perror("Binding socket");
  1522. X          exit(1);
  1523. X     }
  1524. X#ifdef DEBUG
  1525. X     printf("Resolving server IP address...\n");
  1526. X#endif
  1527. X#if (defined(MSDOS) && defined(NOVELL))
  1528. X     cp=NETUSE_SERVER;
  1529. X     cpp=(&cp);
  1530. X     if ((iaddr=rhost(cpp))==(-1)) {
  1531. X          perror("Getting server address");
  1532. X          exit(1);
  1533. X     }
  1534. X     else bcopy((char *)&iaddr,(char *)&servername.sin_addr,sizeof(iaddr));
  1535. X#else
  1536. X     hp=gethostbyname(NETUSE_SERVER);
  1537. X     if (hp==NULL) {
  1538. X          perror("Getting server address");
  1539. X          exit(1);
  1540. X     }
  1541. X     bcopy((char *)hp->h_addr,(char *)&servername.sin_addr,hp->h_length);
  1542. X#endif
  1543. X#ifdef DEBUG
  1544. X     printf("Server address = %s\n",
  1545. X            textIPAddress(servername.sin_addr.s_addr,buf));
  1546. X#endif
  1547. X     servername.sin_family=AF_INET;
  1548. X     servername.sin_port=htons(NETUSE_PORT);
  1549. X}
  1550. X
  1551. X
  1552. X/******************************************************************************
  1553. X void netShutdown(void)
  1554. X
  1555. X Shut down the client side of the NETUSE session.  Close the local socket.
  1556. X******************************************************************************/
  1557. X
  1558. X#ifdef __STDC__
  1559. Xvoid netShutdown(void)
  1560. X#else
  1561. Xvoid netShutdown()
  1562. X#endif
  1563. X{
  1564. X#if (defined(NOVELL) || defined(IBMTCPIP))
  1565. X     soclose(sock);
  1566. X#else
  1567. X     close(sock);
  1568. X#endif
  1569. X}
  1570. END_OF_FILE
  1571.   if test 10106 -ne `wc -c <'netuse/lib/network.c'`; then
  1572.     echo shar: \"'netuse/lib/network.c'\" unpacked with wrong size!
  1573.   fi
  1574.   # end of 'netuse/lib/network.c'
  1575. fi
  1576. echo shar: End of archive 3 \(of 6\).
  1577. cp /dev/null ark3isdone
  1578. MISSING=""
  1579. for I in 1 2 3 4 5 6 ; do
  1580.     if test ! -f ark${I}isdone ; then
  1581.     MISSING="${MISSING} ${I}"
  1582.     fi
  1583. done
  1584. if test "${MISSING}" = "" ; then
  1585.     echo You have unpacked all 6 archives.
  1586.     rm -f ark[1-9]isdone
  1587. else
  1588.     echo You still must unpack the following archives:
  1589.     echo "        " ${MISSING}
  1590. fi
  1591. exit 0
  1592. exit 0 # Just in case...
  1593.