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

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "ip.h"
  4. #include "usock.h"
  5. #include "socket.h"
  6.  
  7. char Inet_eol[] = "\r\n";
  8.  
  9. static void rip_recv __ARGS((struct raw_ip *rp));
  10. static void autobind __ARGS((struct usock *up));
  11.  
  12. int
  13. so_ip_sock(up,protocol)
  14. struct usock *up;
  15. int protocol;
  16. {
  17.     int s;
  18.  
  19.     s = up->index;
  20.     up->cb.rip = raw_ip(protocol,rip_recv);
  21.     up->cb.rip->user = s;
  22.     return 0;
  23. }
  24. int
  25. so_ip_conn(up)
  26. struct usock *up;
  27. {
  28.     if(up->name == NULLCHAR)
  29.         autobind(up);
  30.     return 0;
  31. }
  32. int
  33. so_ip_recv(up,bpp,from,fromlen)
  34. struct usock *up;
  35. struct mbuf **bpp;
  36. char *from;
  37. int *fromlen;
  38. {
  39.     struct raw_ip *rip;
  40.     struct sockaddr_in *remote;
  41.     struct ip ip;
  42.     int cnt;
  43.  
  44.     while((rip = up->cb.rip) != NULLRIP && rip->rcvq == NULLBUF){
  45.         if(up->noblock){
  46.             errno = EWOULDBLOCK;
  47.             return -1;
  48.         } else if((errno = pwait(up)) != 0){
  49.             return -1;
  50.         }
  51.     }
  52.     if(rip == NULLRIP){
  53.         /* Connection went away */
  54.         errno = ENOTCONN;
  55.         return -1;
  56.     }
  57.     *bpp = dequeue(&rip->rcvq);
  58.     ntohip(&ip,bpp);
  59.  
  60.     cnt = len_p(*bpp);
  61.     if(from != NULLCHAR && fromlen != (int *)NULL && *fromlen >= SOCKSIZE){
  62.         remote = (struct sockaddr_in *)from;
  63.         remote->sin_family = AF_INET;
  64.         remote->sin_addr.s_addr = ip.source;
  65.         remote->sin_port = 0;
  66.         *fromlen = SOCKSIZE;
  67.     }
  68.     return cnt;
  69. }
  70. int
  71. so_ip_send(up,bp,to)
  72. struct usock *up;
  73. struct mbuf *bp;
  74. char *to;
  75. {
  76.     struct sockaddr_in *local,*remote;
  77.  
  78.     if(up->name == NULLCHAR)
  79.         autobind(up);
  80.     local = (struct sockaddr_in *)up->name;
  81.     if(to != NULLCHAR){
  82.         remote = (struct sockaddr_in *)to;
  83.     } else if(up->peername != NULLCHAR) {
  84.         remote = (struct sockaddr_in *)up->peername;
  85.     } else {
  86.         free_p(bp);
  87.         errno = ENOTCONN;
  88.         return -1;
  89.     }    
  90.     ip_send(local->sin_addr.s_addr,remote->sin_addr.s_addr,
  91.         (char)up->cb.rip->protocol,0,0,bp,0,0,0);
  92.     return 0;
  93. }
  94. int
  95. so_ip_qlen(up,rtx)
  96. struct usock *up;
  97. int rtx;
  98. {
  99.     int len;
  100.  
  101.     switch(rtx){    
  102.     case 0:
  103.         len = len_q(up->cb.rip->rcvq);
  104.         break;
  105.     case 1:
  106.         len = 0;        
  107.         break;
  108.     }
  109.     return len;
  110. }
  111. int
  112. so_ip_close(up)
  113. struct usock *up;
  114. {
  115.     del_ip(up->cb.rip);
  116.     return 0;
  117. }
  118. int
  119. checkipaddr(name,namelen)
  120. char *name;
  121. int namelen;
  122. {
  123.     struct sockaddr_in *sock;
  124.  
  125.     sock = (struct sockaddr_in *)name;
  126.     if(sock->sin_family != AF_INET || namelen != sizeof(struct sockaddr_in))
  127.         return -1;
  128.     return 0;
  129. }
  130.  
  131. /* Raw IP receive upcall routine */
  132. static void
  133. rip_recv(rp)
  134. struct raw_ip *rp;
  135. {
  136.     psignal(itop(rp->user),1);
  137.     pwait(NULL);
  138. }
  139. /* Issue an automatic bind of a local address */
  140. static void
  141. autobind(up)
  142. struct usock *up;
  143. {
  144.     struct sockaddr_in local;
  145.     int s;
  146.  
  147.     s = up->index;
  148.     local.sin_family = AF_INET;
  149.     local.sin_addr.s_addr = INADDR_ANY;
  150.     local.sin_port = Lport++;
  151.     bind(s,(char *)&local,sizeof(struct sockaddr_in));
  152. }
  153. char *
  154. ippsocket(p)
  155. struct sockaddr *p;
  156. {
  157.     struct sockaddr_in *sp;
  158.     struct socket socket;
  159.     static char buf[30];
  160.  
  161.     sp = (struct sockaddr_in *)p;
  162.     socket.address = sp->sin_addr.s_addr;
  163.     socket.port = sp->sin_port;
  164.     strcpy(buf,pinet(&socket));
  165.  
  166.     return buf;
  167. }
  168.