home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3999 / net.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  7.8 KB  |  407 lines

  1. /*
  2.  * @(#)net.c    1.6 91/09/06    - communications system calls
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/ptrace.h>
  8. #include <sys/file.h>
  9. #include <sys/stat.h>
  10. #include <sys/time.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <netinet/in.h>
  14.  
  15. #include "defs.h"
  16.  
  17. static Xlat domains[] = {
  18.     PF_UNIX,    "PF_UNIX",
  19.     PF_INET,    "PF_INET",
  20.     PF_IMPLINK,    "PF_IMPLINK",
  21.     0,        NULL,
  22. };
  23. static Xlat socktypes[] = {
  24.     SOCK_STREAM,    "STREAM",
  25.     SOCK_DGRAM,    "DGRAM",
  26.     SOCK_RAW,    "RAW",
  27.     SOCK_SEQPACKET,    "SEQ_PKT",
  28.     SOCK_RDM,    "RDM",
  29.     0,        NULL,
  30. };
  31. static Xlat protocols[] = {
  32.     IPPROTO_IP,    "IPPROTO_IP",
  33.     IPPROTO_ICMP,    "IPPROTO_ICMP",
  34.     IPPROTO_IGMP,    "IPPROTO_IGMP",
  35.     IPPROTO_GGP,    "IPPROTO_GGP",
  36.     IPPROTO_TCP,    "IPPROTO_TCP",
  37.     IPPROTO_EGP,    "IPPROTO_EGP",
  38.     IPPROTO_PUP,    "IPPROTO_PUP",
  39.     IPPROTO_UDP,    "IPPROTO_UDP",
  40.     IPPROTO_IDP,    "IPPROTO_IDP",
  41.     IPPROTO_HELLO,    "IPPROTO_HELLO",
  42.     IPPROTO_ND,    "IPPROTO_ND",
  43.     IPPROTO_RAW,    "IPPROTO_RAW",
  44.     IPPROTO_MAX,    "IPPROTO_MAX",
  45.     0,        NULL,
  46. };
  47. static Xlat msg_flags[] = {
  48.     MSG_OOB,    "OOB",
  49.     MSG_DONTROUTE,    "DONTROUTE",
  50.     0,         NULL,
  51. };
  52.  
  53. static Xlat sockoptions[] = {
  54.     SO_DEBUG,    "DEBUG",
  55.     SO_ACCEPTCONN,    "ACCEPTCONN",
  56.     SO_REUSEADDR,    "REUSEADDR",
  57.     SO_KEEPALIVE,    "KEEPALIVE",
  58.     SO_DONTROUTE,    "DONTROUTE",
  59.     SO_BROADCAST,    "BROADCAST",
  60.     SO_USELOOPBACK,    "USELOOPBACK",
  61.     SO_LINGER,    "LINGER",
  62.     SO_OOBINLINE,    "OOBINLINE",
  63.     0,        NULL,
  64. };
  65.  
  66. void
  67. printsock(pid, addr)
  68. {
  69.     struct sockaddr sa;
  70.     struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
  71.     struct sockaddr_un sau;
  72.  
  73.     umove(pid, addr, sizeof sa, (char *)&sa);
  74.     switch (sa.sa_family) {
  75.     case AF_UNIX:
  76.         umove(pid, addr, sizeof sau, (char *)&sau);
  77.         fprintf(outf, "AF_UNIX(%s)", sau.sun_path);
  78.         break;
  79.     case AF_INET:
  80.         fprintf(outf, "AF_INET(%u, %s)",
  81.             ntohs(sin->sin_port), inet_ntoa(sin->sin_addr));
  82.         break;
  83.     default:
  84.         fprintf(outf, "PF %u addr %*s", sa.sa_family,
  85.                 sizeof sa.sa_data, sa.sa_data);
  86.         break;
  87.     }
  88. }
  89.  
  90. static void
  91. printmsghdr(pid, addr)
  92. {
  93.     struct msghdr msg;
  94.  
  95.     umove(pid, addr, sizeof msg, (char *)&msg);
  96.     fprintf(outf, "[name %s len %u iovec %#x iovlen %u acc %#x acclen %u]",
  97.         msg.msg_name, msg.msg_namelen,
  98.         msg.msg_iov, msg.msg_iovlen,
  99.         msg.msg_accrights, msg.msg_accrightslen);
  100. }
  101.  
  102. int
  103. sys_socket(tcp)
  104. struct tcb *tcp;
  105. {
  106.     char *pf = xlookup(domains, tcp->u_args[0]);
  107.     char *st = xlookup(socktypes, tcp->u_args[1]);
  108.  
  109.     if (entering(tcp)) {
  110.         fprintf(outf, "%s", pf==NULL?"PF_???":pf);
  111.         fprintf(outf, ", %s", st==NULL?"SOCK_???":st);
  112.         if (tcp->u_args[0] == PF_INET) {
  113.             char *pr = xlookup(protocols, tcp->u_args[2]);
  114.             fprintf(outf, ", %s", pr==NULL?"IPPROTO_???":pr);
  115.         } else {
  116.             fprintf(outf, ", %u", tcp->u_args[2]);
  117.         }
  118.     }
  119.     return 0;
  120. }
  121.  
  122. int
  123. sys_bind(tcp)
  124. struct tcb *tcp;
  125. {
  126.     if (entering(tcp)) {
  127.         fprintf(outf, "%u, ", tcp->u_args[0]);
  128.         printsock(tcp->pid, tcp->u_args[1]);
  129.         fprintf(outf, ", %u", tcp->u_args[2]);
  130.     }
  131.     return 0;
  132. }
  133.  
  134. int
  135. sys_connect(tcp)
  136. struct tcb *tcp;
  137. {
  138.     return sys_bind(tcp);
  139. }
  140.  
  141. int
  142. sys_listen(tcp)
  143. struct tcb *tcp;
  144. {
  145.     if (entering(tcp)) {
  146.         fprintf(outf, "%u, %u", tcp->u_args[0], tcp->u_args[1]);
  147.     }
  148.     return 0;
  149. }
  150.  
  151. int
  152. sys_accept(tcp)
  153. struct tcb *tcp;
  154. {
  155.     int len;
  156.  
  157.     if (entering(tcp)) {
  158.         fprintf(outf, "%u, ", tcp->u_args[0]);
  159.     } else {
  160.         umove(tcp->pid, tcp->u_args[2], sizeof len, (char *)&len);
  161.         if (syserror(tcp)) {
  162.             fprintf(outf, "%#x, %u", tcp->u_args[1], len);
  163.         } else {
  164.             printsock(tcp->pid, tcp->u_args[1]);
  165.             fprintf(outf, ", %u", len);
  166.         }
  167.     }
  168.     return 0;
  169. }
  170.  
  171. int
  172. sys_send(tcp)
  173. struct tcb *tcp;
  174. {
  175.     if (entering(tcp)) {
  176.         fprintf(outf, "%u, ", tcp->u_args[0]);
  177.         printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
  178.         /* flags */
  179.         fprintf(outf, ", ");
  180.         if (printflags(msg_flags, tcp->u_args[3]) == 0)
  181.             fprintf(outf, "0");
  182.     }
  183.     return 0;
  184. }
  185.  
  186. int
  187. sys_sendto(tcp)
  188. struct tcb *tcp;
  189. {
  190.     if (entering(tcp)) {
  191.         fprintf(outf, "%u, ", tcp->u_args[0]);
  192.         printstr(tcp->pid, tcp->u_args[1], tcp->u_args[2]);
  193.         /* flags */
  194.         fprintf(outf, ", ");
  195.         if (printflags(msg_flags, tcp->u_args[3]) == 0)
  196.             fprintf(outf, "0");
  197.  
  198.         /* to address */
  199.         fprintf(outf, ", ");
  200.         printsock(tcp->pid, tcp->u_args[4]);
  201.         /* to length */
  202.         fprintf(outf, ", %u", tcp->u_args[5]);
  203.     }
  204.     return 0;
  205. }
  206.  
  207. int
  208. sys_sendmsg(tcp)
  209. struct tcb *tcp;
  210. {
  211.     if (entering(tcp)) {
  212.         fprintf(outf, "%u, ", tcp->u_args[0]);
  213.         printmsghdr(tcp->pid, tcp->u_args[1]);
  214.         /* flags */
  215.         fprintf(outf, ", ");
  216.         if (printflags(msg_flags, tcp->u_args[2]) == 0)
  217.             fprintf(outf, "0");
  218.     }
  219.     return 0;
  220. }
  221.  
  222. int
  223. sys_recv(tcp)
  224. struct tcb *tcp;
  225. {
  226.     int fromlen;
  227.  
  228.     if (entering(tcp)) {
  229.         fprintf(outf, "%u, ", tcp->u_args[0]);
  230.     } else {
  231.         printstr(tcp->pid, tcp->u_args[1], tcp->u_rval);
  232.  
  233.         /* flags */
  234.         fprintf(outf, ", ");
  235.         if (printflags(msg_flags, tcp->u_args[3]) == 0)
  236.             fprintf(outf, "0");
  237.     }
  238.     return 0;
  239. }
  240.  
  241. int
  242. sys_recvfrom(tcp)
  243. struct tcb *tcp;
  244. {
  245.     int fromlen;
  246.  
  247.     if (entering(tcp)) {
  248.         fprintf(outf, "%u, ", tcp->u_args[0]);
  249.     } else {
  250.         printstr(tcp->pid, tcp->u_args[1], tcp->u_rval);
  251.  
  252.         /* flags */
  253.         fprintf(outf, ", ");
  254.         if (printflags(msg_flags, tcp->u_args[3]) == 0)
  255.             fprintf(outf, "0");
  256.  
  257.         /* from address */
  258.         fprintf(outf, ", ");
  259.         printsock(tcp->pid, tcp->u_args[4]);
  260.         /* from length */
  261.         umove(tcp->pid, tcp->u_args[5], sizeof fromlen, (char *)&fromlen);
  262.         fprintf(outf, ", %u", fromlen);
  263.     }
  264.     return 0;
  265. }
  266.  
  267. int
  268. sys_recvmsg(tcp)
  269. struct tcb *tcp;
  270. {
  271.     int fromlen;
  272.  
  273.     if (entering(tcp)) {
  274.         fprintf(outf, "%u, ", tcp->u_args[0]);
  275.     } else {
  276.         printmsghdr(tcp->pid, tcp->u_args[1]);
  277.         /* flags */
  278.         fprintf(outf, ", ");
  279.         if (printflags(msg_flags, tcp->u_args[2]) == 0)
  280.             fprintf(outf, "0");
  281.     }
  282.     return 0;
  283. }
  284.  
  285. int
  286. sys_shutdown(tcp)
  287. struct tcb *tcp;
  288. {
  289.     if (entering(tcp)) {
  290.         fprintf(outf, "%u, ", tcp->u_args[0]);
  291.         switch (tcp->u_args[1]) {
  292.         case 0:
  293.             fprintf(outf, "%s", "RECEIVE");
  294.             break;
  295.         case 1:
  296.             fprintf(outf, "%s", "SEND");
  297.             break;
  298.         case 2:
  299.             fprintf(outf, "%s", "SEND/RECEIVE");
  300.             break;
  301.         default:
  302.             fprintf(outf, "???(%u)", tcp->u_args[1]);
  303.             break;
  304.         }
  305.     }
  306.     return 0;
  307. }
  308.  
  309. int
  310. sys_getsockname(tcp)
  311. struct tcb *tcp;
  312. {
  313.     return sys_accept(tcp);
  314. }
  315.  
  316. int
  317. sys_getpeername(tcp)
  318. struct tcb *tcp;
  319. {
  320.     return sys_accept(tcp);
  321. }
  322.  
  323. int
  324. sys_pipe(tcp)
  325. struct tcb *tcp;
  326. {
  327.     if (exiting(tcp)) {
  328.         fprintf(outf, "[%u,%u]", tcp->u_rval, getrval2(tcp->pid));
  329.     }
  330.     return 0;
  331. }
  332.  
  333. int
  334. sys_socketpair(tcp)
  335. struct tcb *tcp;
  336. {
  337.     char *pf = xlookup(domains, tcp->u_args[0]);
  338.     char *st = xlookup(socktypes, tcp->u_args[1]);
  339.  
  340.     if (entering(tcp)) {
  341.         fprintf(outf, "%s", pf==NULL?"PF_???":pf);
  342.         fprintf(outf, ", %s", st==NULL?"SOCK_???":st);
  343.         if (tcp->u_args[0] == PF_INET) {
  344.             char *pr = xlookup(protocols, tcp->u_args[2]);
  345.             fprintf(outf, ", %s", pr==NULL?"IPPROTO_???":pr);
  346.         } else {
  347.             fprintf(outf, ", %u", tcp->u_args[2]);
  348.         }
  349.     } else {
  350.         fprintf(outf, ", [%u,%u]", tcp->u_rval, getrval2(tcp->pid));
  351.     }
  352.     return 0;
  353. }
  354.  
  355. int
  356. sys_getsockopt(tcp)
  357. struct tcb *tcp;
  358. {
  359.     int    optval;
  360.     char    *so;
  361.  
  362.     if (entering(tcp)) {
  363.         fprintf(outf, "%u, ", tcp->u_args[0]);
  364.         if (tcp->u_args[1] == SOL_SOCKET) {
  365.             so = xlookup(sockoptions, tcp->u_args[2]);
  366.             fprintf(outf, "SOL_SOCKET, %s, ", so?so:"SO_???");
  367.         } else {
  368.             /* XXX - should know socket family here */
  369.             char *pr = xlookup(protocols, tcp->u_args[1]);
  370.             fprintf(outf, ", %s", pr==NULL?"IPPROTO_???":pr);
  371.         }
  372.         fprintf(outf, "%u, ", tcp->u_args[2]);
  373.     } else {
  374.         umove(tcp->pid, tcp->u_args[3], sizeof optval, (char *)&optval);
  375.         fprintf(outf, "%u, ", optval);
  376.     }
  377.     if (entering(tcp)) {
  378.         fprintf(outf, "%u", tcp->u_args[4]);
  379.     }
  380.     return 0;
  381. }
  382.  
  383. int
  384. sys_setsockopt(tcp)
  385. struct tcb *tcp;
  386. {
  387.     int    optval;
  388.     char    *so;
  389.  
  390.     if (entering(tcp)) {
  391.         fprintf(outf, "%u, ", tcp->u_args[0]);
  392.         if (tcp->u_args[1] == SOL_SOCKET) {
  393.             so = xlookup(sockoptions, tcp->u_args[2]);
  394.             fprintf(outf, "SOL_SOCKET, %s, ", so?so:"SO_???");
  395.         } else {
  396.             /* XXX - should know socket family here */
  397.             char *pr = xlookup(protocols, tcp->u_args[1]);
  398.             fprintf(outf, ", %s", pr==NULL?"IPPROTO_???":pr);
  399.             fprintf(outf, "%u, ", tcp->u_args[2]);
  400.         }
  401.         umove(tcp->pid, tcp->u_args[3], sizeof optval, (char *)&optval);
  402.         fprintf(outf, "%u, ", optval);
  403.         fprintf(outf, "%u", tcp->u_args[4]);
  404.     }
  405.     return 0;
  406. }
  407.