home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / SOCKUTIL.C < prev    next >
C/C++ Source or Header  |  1992-05-30  |  2KB  |  97 lines

  1. /* Low level socket routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <errno.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "netuser.h"
  8. #include "socket.h"
  9. #include "usock.h"
  10.  
  11. /* Convert a socket (address + port) to an ascii string of the form
  12.  * aaa.aaa.aaa.aaa:ppppp
  13.  */
  14. char *
  15. psocket(p)
  16. void *p;
  17. {
  18.     struct sockaddr *sp;    /* Pointer to structure to decode */
  19.  
  20.     sp = (struct sockaddr *)p;
  21.     if(sp->sa_family < AF_INET || sp->sa_family >= NAF)
  22.         return NULLCHAR;
  23.  
  24.     return (*Psock[sp->sa_family])(sp);
  25. }
  26.  
  27. /* Return ASCII string giving reason for connection closing */
  28. char *
  29. sockerr(s)
  30. int s;    /* Socket index */
  31. {
  32.     register struct usock *up;
  33.     struct socklink *sp;
  34.  
  35.     if((up = itop(s)) == NULLUSOCK){
  36.         errno = EBADF;
  37.         return Badsocket;
  38.     }
  39.     sp = up->sp;
  40.     if(sp->error != NULL){
  41.         return sp->error[up->errcodes[0]];
  42.     } else {
  43.         errno = EOPNOTSUPP;    /* not yet, anyway */
  44.         return NULLCHAR;
  45.     }
  46. }
  47. /* Get state of protocol. Valid only for connection-oriented sockets. */
  48. char *
  49. sockstate(s)
  50. int s;        /* Socket index */
  51. {
  52.     register struct usock *up;
  53.     struct socklink *sp;
  54.  
  55.     if((up = itop(s)) == NULLUSOCK){
  56.         errno = EBADF;
  57.         return NULLCHAR;
  58.     }
  59.     if(up->cb.p == NULLCHAR){
  60.         errno = ENOTCONN;
  61.         return NULLCHAR;
  62.     }
  63.     sp = up->sp;    
  64.     if(sp->state != NULL)
  65.         return (*sp->state)(up);
  66.     
  67.     /* Datagram sockets don't have state */
  68.     errno = EOPNOTSUPP;
  69.     return NULLCHAR;
  70. }
  71. /* Convert a socket index to an internal user socket structure pointer */
  72. struct usock *
  73. itop(s)
  74. register int s;    /* Socket index */
  75. {
  76.     s -= Nfiles;
  77.     if(s < 0 || s >= Nsock)
  78.         return NULLUSOCK;
  79.  
  80.     return Usock[s];
  81. }
  82.  
  83. void
  84. st_garbage(red)
  85. int red;
  86. {
  87.     int i;
  88.     struct usock *up;
  89.  
  90.     for(i=Nfiles;i<Nfiles+Nsock;i++){
  91.         up = Usock[i];
  92.         if(up != NULLUSOCK && up->type == TYPE_LOCAL_STREAM)
  93.             mbuf_crunch(&up->cb.local->q);
  94.     }
  95. }
  96.  
  97.