home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / SUN / PPP / SUNOS_OL / DDP_PPP.TAR / magic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-03  |  1.6 KB  |  66 lines

  1. /*
  2.  * magic.c - PPP Magic Number routines.
  3.  *
  4.  * Copyright (c) 1989 Carnegie Mellon University.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by Carnegie Mellon University.  The name of the
  13.  * University may not be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23.  
  24. #include "magic.h"
  25.  
  26.  
  27. static u_long next;        /* Next value to return */
  28.  
  29. long gethostid();
  30. long random();
  31.  
  32.  
  33. /*
  34.  * magic_init - Initialize the magic number generator.
  35.  *
  36.  * Computes first magic number and seed for random number generator.
  37.  * Attempts to compute a random number seed which will not repeat.
  38.  * The current method uses the current hostid and current time.
  39.  */
  40. void magic_init()
  41. {
  42.     struct timeval tv;
  43.  
  44.     next = (u_long) gethostid();
  45.     if (gettimeofday(&tv, NULL)) {
  46.     perror("gettimeofday");
  47.     exit(1);
  48.     }
  49.     next ^= (u_long) tv.tv_sec ^ (u_long) tv.tv_usec;
  50.  
  51.     srandom((int) next);
  52. }
  53.  
  54.  
  55. /*
  56.  * magic - Returns the next magic number.
  57.  */
  58. u_long magic()
  59. {
  60.     u_long m;
  61.  
  62.     m = next;
  63.     next = (u_long) random();
  64.     return (m);
  65. }
  66.