home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3299 / getsocket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  4.3 KB  |  144 lines

  1. /* History:
  2. 5/3/91 DJB various modifications
  3. 5/1/91 DJB baseline public domain
  4. */
  5.  
  6. /*
  7.  
  8. int getsocket(f,sbuf) struct socket *f; struct socketbuf *sbuf; looks up
  9. socket f (in kernel memory) and places its vital statistics into sbuf.
  10. sbuf->socktype is the general socket type, such as SOCK_STREAM.
  11. sbuf->strsockt is the name of the socket type, or 0 if the type is
  12. unknown. sbuf->flag{accept,reuse,head} are the accept, reuse, and head
  13. flags on the socket respectively. sbuf->family is the socket's address
  14. family, such as AF_INET; sbuf->famname is the name of the family, or 0
  15. if the family is unknown. sbuf->famname may be truncated to 30
  16. characters.
  17.  
  18. sbuf->fsw is a family switch to control sbuf->fu: FSW_INET for AF_INET,
  19. FSW_UNIX for AF_UNIX, and FSW_UNK for other types.
  20.  
  21. If sbuf->fsw is FSW_INET: sbuf->fu.inet has several members. proto is
  22. the Internet protocol, such as IPPROTO_TCP. strpro is the name for
  23. proto, or 0 if it is unknown. r0, r1, r2, r3 are the bytes (from network
  24. down to host) of the IP address of the remote host; rp is the remote
  25. port, for protocols where that is meaningful. l0, l1, l2, l3 and lp form
  26. the local address.
  27.  
  28. If sbuf->fsw is FSW_UNIX: sbuf->fu.un has several members. node is the
  29. address of the filesystem node associated with the socket, if any. conn
  30. is the address of the connected node, if any. path is the bound pathname
  31. of the socket, if applicable; it may be either a character array or a
  32. pointer in future releases of this library. unpcb is the address of the
  33. socket's protocol control block.
  34.  
  35. getsocket() returns 0 upon success, -1 upon error.
  36.  
  37. */
  38.  
  39. #include <stdio.h>
  40. #include "structinpcb.h"
  41. #include "structmbuf.h"
  42. #include "structprotosw.h"
  43. #include "structsocket.h"
  44. #include "structunpcb.h"
  45. #include "structxnode.h"
  46. #include "printfamily.h"
  47. #include "printprotoinet.h"
  48. #include "printsocktype.h"
  49. #include "kmem.h"
  50. #include <sys/un.h>
  51. #include "getsocket.h"
  52. #include "confdomain.h"
  53.  
  54. getsocket(f,sbuf)
  55. struct socket *f;
  56. struct socketbuf *sbuf;
  57. {
  58.  struct socket s;
  59.  struct protosw psw;
  60. #ifdef DOMAIN
  61.  struct domain dom;
  62. #endif
  63.  int family;
  64.  
  65.  kmemcpy(&s,f,sizeof(s));
  66.  
  67.  /* XXX: state, follow head, error, pgrp */
  68.  kmemcpy(&psw,s.so_proto,sizeof(psw));
  69. #ifdef DOMAIN
  70.  kmemcpy(&dom,psw.pr_domain,sizeof(dom));
  71.  family = dom.dom_family;
  72. #else
  73.  family = psw.pr_family;
  74. #endif
  75.  
  76.  sbuf->socktype = s.so_type;
  77.  sbuf->strsockt = printsocktype(s.so_type);
  78.  sbuf->flagaccept = !!(s.so_options & SO_ACCEPTCONN);
  79.  sbuf->flagreuse = !!(s.so_options & SO_REUSEADDR);
  80.  sbuf->flaghead = !!(s.so_head);
  81.  /* N.B. psw.pr_type always equals s.so_type */
  82.  sbuf->family = family;
  83. #ifdef DOMAIN
  84.  if (dom.dom_name && (family >= 0) && (family < 50))
  85.   {
  86.    char domname[50][30]; /* XXX: so I'm scum. so sue me. */
  87.    kmemcpy(domname[family],dom.dom_name,sizeof(domname[family]));
  88.    domname[family][29] = 0;
  89.    sbuf->famname = domname[family];
  90.   }
  91.  else
  92. #endif
  93.  sbuf->famname = printfamily(family);
  94.  sbuf->fsw = FSW_UNK;
  95.  
  96.  if (family == AF_INET)
  97.   {
  98.    struct inpcb inp;
  99.    unsigned char *a;
  100.  
  101.    sbuf->fsw = FSW_INET;
  102.    sbuf->fu.inet.proto = psw.pr_protocol;
  103.    sbuf->fu.inet.strpro = printprotoinet(psw.pr_protocol);
  104.  
  105.    /*XXX: do port lookups? */
  106.    /*XXX: do host lookups? */
  107.    kmemcpy(&inp,s.so_pcb,sizeof(inp));
  108.    a = (unsigned char *) &inp.inp_faddr;
  109.    sbuf->fu.inet.r0 = a[0]; sbuf->fu.inet.r1 = a[1];
  110.    sbuf->fu.inet.r2 = a[2]; sbuf->fu.inet.r3 = a[3];
  111.    a = (unsigned char *) &inp.inp_fport;
  112.    sbuf->fu.inet.rp = a[0] * 256 + a[1];
  113.    a = (unsigned char *) &inp.inp_laddr;
  114.    sbuf->fu.inet.l0 = a[0]; sbuf->fu.inet.l1 = a[1];
  115.    sbuf->fu.inet.l2 = a[2]; sbuf->fu.inet.l3 = a[3];
  116.    a = (unsigned char *) &inp.inp_lport;
  117.    sbuf->fu.inet.lp = a[0] * 256 + a[1];
  118.   }
  119.  else if (family == AF_UNIX)
  120.   {
  121.    struct unpcb unp;
  122.  
  123.    sbuf->fsw = FSW_UNIX;
  124.    kmemcpy(&unp,s.so_pcb,sizeof(unp));
  125.    sbuf->fu.un.unpcb = (char *) s.so_pcb;
  126.    sbuf->fu.un.node = (char *) unp.unp_Xnode;
  127.    sbuf->fu.un.conn = (char *) unp.unp_conn;
  128.    sbuf->fu.un.path[0] = 0;
  129.    if (unp.unp_addr)
  130.     {
  131.      struct sockaddr_un *sa;
  132.      struct mbuf mb;
  133.      kmemcpy((char *) &mb,(char *) unp.unp_addr,sizeof(mb));
  134.      sa = (struct sockaddr_un *) (((char *) &mb) + mb.m_off);
  135.      /* XXX: check that length is less than 108 */
  136.      sprintf(sbuf->fu.un.path,
  137.        " %.*s",mb.m_len - sizeof(sa->sun_family),sa->sun_path); /*XXX*/
  138.     }
  139.   }
  140.  else
  141.    ; /* XXX: other s.so_pcb's? */
  142.  return 0;
  143. }
  144.