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

  1. #include <errno.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "usock.h"
  6.  
  7. int
  8. so_los(up,protocol)
  9. struct usock *up;
  10. int protocol;
  11. {
  12.     up->cb.local = (struct loc *) callocw(1,sizeof(struct loc));
  13.     up->cb.local->peer = up;    /* connect to self */
  14.     up->type = TYPE_LOCAL_STREAM;
  15.     up->cb.local->hiwat = LOCSFLOW;
  16.     return 0;
  17. }
  18. int
  19. so_lod(up,protocol)
  20. struct usock *up;
  21. int protocol;
  22. {
  23.     up->cb.local = (struct loc *) callocw(1,sizeof(struct loc));
  24.     up->cb.local->peer = up;    /* connect to self */
  25.     up->type = TYPE_LOCAL_DGRAM;
  26.     up->cb.local->hiwat = LOCDFLOW;
  27.     return 0;
  28. }
  29. int
  30. so_lo_recv(up,bpp,from,fromlen)
  31. struct usock *up;
  32. struct mbuf **bpp;
  33. char *from;
  34. int *fromlen;
  35. {
  36.     int s;
  37.  
  38.     while(up->cb.local != NULLLOC && up->cb.local->q == NULLBUF
  39.       && up->cb.local->peer != NULLUSOCK){
  40.         if(up->noblock){
  41.             errno = EWOULDBLOCK;
  42.             return -1;
  43.         } else if((errno = pwait(up)) != 0){
  44.             return -1;
  45.         }
  46.     }
  47.     if(up->cb.local == NULLLOC){
  48.         /* Socket went away */
  49.         errno = EBADF;
  50.         return -1;
  51.     }
  52.     if(up->cb.local->q == NULLBUF &&
  53.        up->cb.local->peer == NULLUSOCK){
  54.         errno = ENOTCONN;
  55.         return -1;
  56.     }
  57.     /* For datagram sockets, this will return the
  58.      * first packet on the queue. For stream sockets,
  59.      * this will return everything.
  60.      */
  61.     *bpp = dequeue(&up->cb.local->q);
  62.     if(up->cb.local->q == NULLBUF && (up->cb.local->flags & LOC_SHUTDOWN)){
  63.         s = up->index;
  64.         close_s(s);
  65.     }
  66.     psignal(up,0);
  67.     return len_p(*bpp);
  68. }
  69. int
  70. so_los_send(up,bp,to)
  71. struct usock *up;
  72. struct mbuf *bp;
  73. char *to;
  74. {
  75.     if(up->cb.local->peer == NULLUSOCK){
  76.         free_p(bp);
  77.         errno = ENOTCONN;
  78.         return -1;
  79.     }
  80.     append(&up->cb.local->peer->cb.local->q,bp);
  81.     psignal(up->cb.local->peer,0);
  82.     /* If high water mark has been reached, block */
  83.     while(up->cb.local->peer != NULLUSOCK &&
  84.           len_p(up->cb.local->peer->cb.local->q) >=
  85.           up->cb.local->peer->cb.local->hiwat){
  86.         if(up->noblock){
  87.             errno = EWOULDBLOCK;
  88.             return -1;
  89.         } else if((errno = pwait(up->cb.local->peer)) != 0){
  90.             return -1;
  91.         }
  92.     }
  93.     if(up->cb.local->peer == NULLUSOCK){
  94.         errno = ENOTCONN;
  95.         return -1;
  96.     }
  97.     return 0;
  98. }
  99. int    
  100. so_lod_send(up,bp,to)
  101. struct usock *up;
  102. struct mbuf *bp;
  103. char *to;
  104. {
  105.     if(up->cb.local->peer == NULLUSOCK){
  106.         free_p(bp);
  107.         errno = ENOTCONN;
  108.         return -1;
  109.     }
  110.     enqueue(&up->cb.local->peer->cb.local->q,bp);
  111.     psignal(up->cb.local->peer,0);
  112.     /* If high water mark has been reached, block */
  113.     while(up->cb.local->peer != NULLUSOCK &&
  114.           len_q(up->cb.local->peer->cb.local->q) >=
  115.           up->cb.local->peer->cb.local->hiwat){
  116.         if(up->noblock){
  117.             errno = EWOULDBLOCK;
  118.             return -1;
  119.         } else if((errno = pwait(up->cb.local->peer)) != 0){
  120.             return -1;
  121.         }
  122.     }
  123.     if(up->cb.local->peer == NULLUSOCK){
  124.         errno = ENOTCONN;
  125.         return -1;
  126.     }
  127.     return 0;
  128. }
  129. int
  130. so_lod_qlen(up,rtx)
  131. struct usock *up;
  132. int rtx;
  133. {
  134.     int len;
  135.  
  136.     switch(rtx){
  137.     case 0:
  138.         len = len_q(up->cb.local->q);
  139.         break;
  140.     case 1:
  141.         if(up->cb.local->peer != NULLUSOCK)
  142.             len = len_q(up->cb.local->peer->cb.local->q);
  143.         break;
  144.     }
  145.     return len;
  146. }
  147. int
  148. so_los_qlen(up,rtx)
  149. struct usock *up;
  150. int rtx;
  151. {
  152.     int len;
  153.  
  154.     switch(rtx){
  155.     case 0:
  156.         len = len_p(up->cb.local->q);
  157.         break;
  158.     case 1:
  159.         if(up->cb.local->peer != NULLUSOCK)
  160.             len = len_p(up->cb.local->peer->cb.local->q);
  161.         break;
  162.     }
  163.     return len;
  164. }
  165. int
  166. so_loc_shut(up,how)
  167. struct usock *up;
  168. int how;
  169. {
  170.     int s;
  171.  
  172.     s = up->index;
  173.  
  174.     if(up->cb.local->q == NULLBUF)
  175.         close_s(s);
  176.     else
  177.         up->cb.local->flags = LOC_SHUTDOWN;
  178.     return 0;
  179. }
  180. int
  181. so_loc_close(up)
  182. struct usock *up;
  183. {
  184.     if(up->cb.local->peer != NULLUSOCK){
  185.         up->cb.local->peer->cb.local->peer = NULLUSOCK;
  186.         psignal(up->cb.local->peer,0);
  187.     }
  188.     free_q(&up->cb.local->q);
  189.     free(up->cb.local);
  190.     return 0;
  191. }
  192. char *
  193. lopsocket(p)
  194. struct sockaddr *p;
  195. {
  196.     return "";
  197. }
  198. so_loc_stat(up)
  199. struct usock *up;
  200. {
  201.     int s;
  202.  
  203.     s = up->index;
  204.  
  205.     printf("Inqlen: %d packets\n",socklen(s,0));
  206.     printf("Outqlen: %d packets\n",socklen(s,1));
  207.     return 0;
  208. }
  209.