home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part03 / c_nit.c next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  5.3 KB  |  252 lines

  1. /*
  2.     c_nit.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "c_proto.h"
  26.  
  27.  
  28. #ifdef CHANNEL_NIT
  29.  
  30. /* sun NIT (network interface tap) device for M0 */
  31.  
  32. #include <stdio.h>
  33. #include <malloc.h>
  34.  
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/time.h>
  38. #include <signal.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <errno.h>
  42.  
  43. #include <stropts.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/fcntlcom.h>
  46.  
  47. #include <net/if.h>
  48. #include <net/nit_if.h>
  49. #include <net/nit_pf.h>
  50. #include <net/packetfilt.h>
  51. #include <sys/uio.h>
  52.  
  53.  
  54. struct nit_data {
  55.     eindex chankey;
  56.     sint chan;
  57.     byte dest[6];
  58. };
  59.  
  60. #define MAXNITCHANNELS    2
  61. static int nit_fd[MAXNITCHANNELS];
  62. static ushort m0_eth_type;
  63. static byte eth_buf[1450];
  64.  
  65. eindex nit_addr;
  66. eindex nit_name;
  67.  
  68.  
  69. /* ------------------------------------------------------------------------ */
  70.  
  71.  
  72. char**
  73. get_eth_devices()
  74. {
  75. static char* names[] = {"le0", "le1", "le2", "ie0", "ie1", "ie2", 0};
  76.     return names;
  77. }
  78.  
  79.  
  80. int
  81. nit_init(char **devs, int proto)
  82. {
  83.     int i, j;
  84.     char **d;
  85.  
  86.     for (d = devs, i = 0; d && *d; d++, i++);
  87.     if (!i)
  88.         return -1;
  89.  
  90.     nit_addr = new_array(0, i);
  91.     nit_name = name_add("ether", 5, A_EXECUTABLE);
  92.     m0_eth_type = htons(proto);
  93.  
  94.     for (d = devs, i = 0, j = 0; d && *d && j < MAXNITCHANNELS; d++, i++) {
  95.         struct ifreq ifr;
  96.         struct packetfilt pf;
  97.         register u_short *fwp = pf.Pf_Filter;
  98.         int fd;
  99.         eindex e;
  100.         byteptr a;
  101.  
  102.         fd = open("/dev/nit", O_RDWR);
  103.         if (fd < 0)
  104.             break;
  105.  
  106.         if (ioctl(fd, I_SRDOPT, (char*)RMSGN) < 0 ||
  107.             ioctl(fd, I_PUSH, "pf") < 0)
  108.             continue;
  109.  
  110.         *fwp++ = ENF_PUSHWORD + 6;
  111.         *fwp++ = ENF_PUSHLIT;
  112.         *fwp++ = m0_eth_type;
  113.         *fwp++ = ENF_EQ;
  114.         pf.Pf_FilterLen = fwp - &pf.Pf_Filter[0];
  115.         if (ioctl(fd, NIOCSETF, (char*)&pf) < 0)
  116.             continue;
  117.  
  118.         strncpy(ifr.ifr_name, *d, sizeof(ifr.ifr_name));
  119.         ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  120.         if( ioctl(fd, NIOCBIND, (char*)&ifr) ||
  121.             ioctl(fd, NIOCSFLAGS, (char*)0) < 0 ||
  122.             ioctl(fd, I_FLUSH, (char*)FLUSHR) < 0 ||
  123.             ioctl(fd, SIOCGIFADDR, (char*)&ifr) < 0)
  124.             continue;
  125.  
  126.         nit_fd[j] = fd;
  127.         add_incoming(fd, nit_recv, nit_name, j);
  128.  
  129.         a = malloc(6);
  130.         memcpy(a, ifr.ifr_ifru.ifru_addr.sa_data, 6);
  131.         TRACE(3, printf("eth address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  132.                 a[0], a[1], a[2], a[3], a[4], a[5]))
  133.         e = str_import(0, a, 6, 6);
  134.         epattr(gaddr(e)) &= ~A_WRITE;
  135.  
  136.         array_put(0, nit_addr, j, e);
  137.         j++;
  138.     }
  139.  
  140.     if (!j) {
  141.         decref(0, nit_addr);
  142.         nit_addr = 0;
  143.         decref(0, nit_name);
  144.         nit_name = 0;
  145.         return -1;
  146.     }
  147.     eplen(gaddr(nit_addr)) = j;
  148.  
  149.     return 0;
  150. }
  151.  
  152.  
  153. void
  154. nit_recv(int fd, eindex *m, eindex *o)
  155. {
  156. #define ETHFRAMESIZE    1500
  157.     byteptr buf = malloc(ETHFRAMESIZE);
  158.     eindex s;
  159.     int len;
  160.  
  161.     len = read(fd, buf, ETHFRAMESIZE);
  162.     if( len < 0 ) {
  163.         *m = 0;
  164.         perror("nit_getmsg");
  165.         free(buf);
  166.         return;
  167.     }
  168.  
  169.     TRACE(3, printf("nit receive (%d bytes): ", len))
  170.     TRACE(3, printf("%02x %02x %02x %02x %02x %02x\n",
  171.             buf[14], buf[15], buf[16], buf[17], buf[18], buf[19]))
  172.  
  173.     s = str_import(0, buf, len, ETHFRAMESIZE);
  174.  
  175.     *o = make_sub(0, s, 6);
  176.     eplen(gaddr(*o)) = 6;
  177.     *m = make_sub(0, s, 14);
  178.     eplen(gaddr(*m)) = len - 14;
  179.  
  180.     decref(0, s);
  181. }
  182.  
  183.  
  184. int
  185. nit_send(sint chan, byteptr dest, byteptr packet, uint len)
  186. {
  187.     struct strbuf data, ctrl;
  188.     struct sockaddr sa;
  189.  
  190.     TRACE(3, printf("nit send (%d bytes, %02x:%02x:%02x:%02x:%02x:%02x)\n",
  191.         len, dest[0], dest[1], dest[2], dest[3], dest[4], dest[5]))
  192.  
  193.     sa.sa_family = AF_UNSPEC;
  194.  
  195.     memcpy(sa.sa_data, dest, 6);
  196.     sa.sa_data[12] = m0_eth_type>>8;
  197.     sa.sa_data[13] = m0_eth_type & 0xff;
  198.  
  199.     data.buf = (char *) packet;
  200.     data.len = len;
  201.     ctrl.buf = (char *) &sa;
  202.     ctrl.len = sizeof(sa);
  203.  
  204.     if( putmsg(nit_fd[chan], &ctrl, &data, 0) < 0 ) {
  205.         perror("nit_putmsg");
  206.         return -1;
  207.     }
  208.     return len;
  209. }
  210.  
  211.  
  212. static void
  213. nit_submit(mproc p, void *data, eindex m)
  214. {
  215.     struct nit_data *n = (struct nit_data*) data;
  216.     eptr mp = eaddr(p, m);
  217.     uint len = eplen(mp);
  218.  
  219.     len = len > sizeof(eth_buf) ? sizeof(eth_buf) : len;
  220.     if (epattr(mp) & (A_SUB | A_FRAG)) {
  221.         str_export(p, eth_buf, m, 0, len);
  222.         nit_send(n->chan, n->dest, eth_buf, len);
  223.     } else
  224.         nit_send(n->chan, n->dest, mp->V.str.s, len);
  225.  
  226.     dict_undef(0, channeldict, n->chankey);
  227.     free(n);
  228.     return;
  229. }
  230.  
  231.  
  232. eindex
  233. add_nit_channel(sint chan_no, byteptr dest)
  234. {
  235.     eindex key;
  236.     byte keybits[8];
  237.     struct nit_data *n;
  238.  
  239.     random64(keybits);
  240.     key = key_add(keybits);
  241.  
  242.     n = (struct nit_data*) malloc(sizeof(struct nit_data));
  243.     n->chankey = key;
  244.     n->chan = chan_no;
  245.     memcpy(n->dest, dest, 6);
  246.     new_channel(key, n, nit_submit);
  247.  
  248.     return key;
  249. }
  250.  
  251. #endif
  252.