home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / TIMED.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  2KB  |  88 lines

  1. /* Internet Time server
  2.  * Copyright 1993 Mark Turner, G7LEU mt@kram.org
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "socket.h"
  9. #include "session.h"
  10. #include "proc.h"
  11.  
  12. static int Stime = -1;
  13.  
  14. static void timed __ARGS((int s,void *unused,void *p));
  15.  
  16. /* Start up time service */
  17. int
  18. timestart(argc,argv,p)
  19. int argc;
  20. char *argv[];
  21. void *p;
  22. {
  23.     struct sockaddr_in lsocket;
  24.     int s;
  25.  
  26.     if(Stime != -1){
  27.         return 0;
  28.     }
  29.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  30.     chname(Curproc,"Time listener");
  31.  
  32.     lsocket.sin_family = AF_INET;
  33.     lsocket.sin_addr.s_addr = INADDR_ANY;
  34.     if(argc < 2)
  35.         lsocket.sin_port = IPPORT_TIME;
  36.     else
  37.         lsocket.sin_port = atoi(argv[1]);
  38.  
  39.     Stime = socket(AF_INET,SOCK_STREAM,0);
  40.     bind(Stime,(char *)&lsocket,sizeof(lsocket));
  41.     listen(Stime,1);
  42.     for(;;){
  43.         if((s = accept(Stime,NULLCHAR,(int *)NULL)) == -1)
  44.             break;    /* Service is shutting down */
  45.  
  46.         /* Spawn a server */
  47.         newproc("timed",512,timed,s,NULL,NULL,0);
  48.     }
  49.     return 0;
  50. }
  51.  
  52. static void
  53. timed(s,unused,p)
  54. int s;
  55. void *unused;
  56. void *p;
  57. {
  58.     char t;
  59.     union
  60.         {
  61.         time_t time;
  62.         char bytes[sizeof (time_t)];
  63.         } result;
  64.     sockmode(s,SOCK_ASCII);
  65.     sockowner(s,Curproc);
  66.     result.time = time(NULL);
  67.     result.time += 2208988800L;
  68.     t = result.bytes[0];
  69.     result.bytes[0] = result.bytes[3];
  70.     result.bytes[3] = t;
  71.     t = result.bytes[1];
  72.     result.bytes[1] = result.bytes[2];
  73.     result.bytes[2] = t;
  74.     send(s,result.bytes,sizeof (time_t),0);
  75.     close_s(s);
  76. }
  77.  
  78. int
  79. time0(argc,argv,p)
  80. int argc;
  81. char *argv[];
  82. void *p;
  83. {
  84.     close_s(Stime);
  85.     Stime = -1;
  86.     return 0;
  87. }
  88.