home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / SOCKUTIL.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  3KB  |  157 lines

  1. /* Low level socket routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "socket.h"
  8. #include "usock.h"
  9. #include "lapb.h"
  10. #include "tcp.h"
  11. #include "nr4.h"
  12. #include "config.h"
  13.  
  14. /* Convert a socket (address + port) to an ascii string of the form
  15.  * aaa.aaa.aaa.aaa:ppppp
  16.  */
  17. char *
  18. psocket(p)
  19. void *p;    /* Pointer to structure to decode */
  20. {
  21. #if    defined(AX25) || defined(NETROM)
  22.     char tmp[11];
  23. #endif
  24.     static char buf[30];
  25.     union sp sp;
  26.     struct socket socket;
  27.  
  28.     sp.p = p;
  29.     switch(sp.sa->sa_family){
  30.     case AF_LOCAL:
  31.         buf[0] = '\0';
  32.         break;
  33.     case AF_INET:
  34.         socket.address = sp.in->sin_addr.s_addr;
  35.         socket.port = sp.in->sin_port;
  36.         strcpy(buf,pinet(&socket));
  37.         break;
  38. #ifdef    AX25
  39.     case AF_AX25:
  40.         pax25(tmp,sp.ax->ax25_addr);
  41.         if(strlen(sp.ax->iface) != 0)
  42.             sprintf(buf,"%s on %s",tmp,sp.ax->iface);
  43.         else
  44.             strcpy(buf,tmp);
  45.         break;
  46. #endif
  47. #ifdef    NETROM
  48.     case AF_NETROM:
  49.         pax25(tmp,sp.nr->nr_addr.user);
  50.         sprintf(buf,"%s @ ",tmp);
  51.         pax25(tmp,sp.nr->nr_addr.node);
  52.         strcat(buf,tmp);
  53.         break;
  54. #endif
  55.     }
  56.     return buf;
  57. }
  58.  
  59. /* Return ASCII string giving reason for connection closing */
  60. char *
  61. sockerr(s)
  62. int s;    /* Socket index */
  63. {
  64.     register struct usock *up;
  65.  
  66.     if((up = itop(s)) == NULLUSOCK){
  67.         errno = EBADF;
  68.         return Badsocket;
  69.     }
  70.     switch(up->type){
  71. #ifdef    AX25
  72.     case TYPE_AX25I:
  73.         if(up->cb.ax25 != NULLAX25)
  74.             return NULLCHAR;    /* nothing wrong */
  75.         return Axreasons[up->errcodes[0]];
  76. #endif
  77. #ifdef    NETROM
  78.     case TYPE_NETROML4:
  79.         if(up->cb.nr4 != NULLNR4CB)
  80.             return NULLCHAR;    /* nothing wrong */
  81.         return Nr4reasons[up->errcodes[0]];
  82. #endif
  83.     case TYPE_TCP:
  84.         if(up->cb.tcb != NULLTCB)
  85.             return NULLCHAR;    /* nothing wrong */        
  86.         return Tcpreasons[up->errcodes[0]];
  87.     default:
  88.         errno = EOPNOTSUPP;    /* not yet, anyway */
  89.         return NULLCHAR;
  90.     }
  91. }
  92. /* Get state of protocol. Valid only for connection-oriented sockets. */
  93. char *
  94. sockstate(s)
  95. int s;        /* Socket index */
  96. {
  97.     register struct usock *up;
  98.  
  99.     if((up = itop(s)) == NULLUSOCK){
  100.         errno = EBADF;
  101.         return NULLCHAR;
  102.     }
  103.     if(up->cb.p == NULLCHAR){
  104.         errno = ENOTCONN;
  105.         return NULLCHAR;
  106.     }
  107.     switch(up->type){
  108.     case TYPE_TCP:
  109.         return Tcpstates[up->cb.tcb->state];
  110. #ifdef    AX25
  111.     case TYPE_AX25I:
  112.         return Ax25states[up->cb.ax25->state];
  113. #endif
  114. #ifdef    NETROM
  115.     case TYPE_NETROML4:
  116.         return Nr4states[up->cb.nr4->state];
  117. #endif
  118.     default:
  119.         /* Datagram sockets don't have state */
  120.         errno = EOPNOTSUPP;
  121.         return NULLCHAR;
  122.     }
  123.     /*NOTREACHED*/
  124. }
  125. /* Convert a socket index to an internal user socket structure pointer */
  126. struct usock *
  127. itop(s)
  128. register int s;    /* Socket index */
  129. {
  130.     register struct usock *up;
  131.  
  132.     s -= SOCKBASE;
  133.     if(s < 0 || s >= Nusock)
  134.         return NULLUSOCK;
  135.  
  136.     up = &Usock[s];
  137.  
  138.     if(up->type == NOTUSED)
  139.         return NULLUSOCK;
  140.     return up;
  141. }
  142.  
  143. void
  144. st_garbage(red)
  145. int red;
  146. {
  147.     int i;
  148.     struct usock *up;
  149.  
  150.     for(i=SOCKBASE;i<SOCKBASE+Nusock;i++){
  151.         up = &Usock[i];
  152.         if(up->type == TYPE_LOCAL_STREAM)
  153.             mbuf_crunch(&up->cb.local->q);
  154.     }
  155. }
  156.  
  157.