home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sources / misc / 4243 < prev    next >
Encoding:
Text File  |  1993-01-10  |  50.1 KB  |  1,601 lines

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