home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / SOCKUTIL.C < prev    next >
Text File  |  1994-10-09  |  4KB  |  146 lines

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