home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part04 / o_chan.c < prev   
Encoding:
C/C++ Source or Header  |  1994-06-06  |  5.4 KB  |  219 lines

  1. /*
  2.     o_chan.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 <stdlib.h>
  26.  
  27. #include "c_proto.h"
  28. #include "l_proto.h"
  29. #include "o_proto.h"
  30.  
  31.  
  32. #ifdef CHANNEL_CONSOLE
  33. static retcode
  34. o_console()
  35. {
  36.     eindex key;
  37.  
  38.     if (current->osp < 3)
  39.         return ERR_STACK_UNDERFLOW;
  40.  
  41.     decref(current, current->os[current->osp-1]);
  42.     decref(current, current->os[current->osp-2]);
  43.     decref(current, current->os[current->osp-3]);
  44.  
  45.     key = add_console_channel();
  46.     return_ok_result(3, key);
  47. }
  48. #endif
  49.  
  50.  
  51. #ifdef CHANNEL_NIT
  52. static retcode
  53. o_nit()
  54. {
  55.     eindex key;
  56.     byte desthost[6];
  57.  
  58.     load_3_args(addr, srcchan, chk, ap, sp, cp);
  59.  
  60.     if (eptype(ap) != T_STRING || eplen(ap) != 6 || eptype(sp) != T_INT)
  61.         return ERR_TYPE_CHECK;
  62.     if (!(epattr(ap) & A_READ))
  63.         return ERR_ACCESS_CHECK;
  64.  
  65.     str_export(current, desthost, addr, 0, 6);
  66.  
  67.     if (sp->V.i < 0 || sp->V.i > eplen(gaddr(nit_addr)))
  68.         return ERR_RANGE_CHECK;
  69.  
  70.     key = add_nit_channel(sp->V.i, desthost);
  71.  
  72.     decrefp(current,addr,ap);
  73.     decrefp(current,srcchan,sp);
  74.     decrefp(current,chk,cp);
  75.  
  76.     return_ok_result(3, key);
  77. }
  78. #endif
  79.  
  80.  
  81. #ifdef CHANNEL_UDP
  82. static retcode
  83. o_udp()
  84. {
  85.     eindex desthost, port, key;
  86.  
  87.     load_3_args(addr, srcchan, chk, ap, sp, cp);
  88.  
  89.     if (eptype(ap) != T_ARRAY || eplen(ap) != 2 || eptype(sp) != T_INT)
  90.         return ERR_TYPE_CHECK;
  91.     if (!(epattr(ap) & A_READ))
  92.         return ERR_ACCESS_CHECK;
  93.  
  94.     desthost = array_get(current, addr, 0);
  95.     port = array_get(current, addr, 1);
  96.     if (etype(current,desthost) != T_INT || etype(current,port) != T_INT)
  97.         return ERR_TYPE_CHECK;
  98.  
  99.     if (sp->V.i < 0 || sp->V.i > eplen(gaddr(udp_addr)))
  100.         return ERR_RANGE_CHECK;
  101.  
  102.     key = add_udp_channel(    sp->V.i,
  103.                 eaddr(current, desthost)->V.i,
  104.                 eaddr(current, port)->V.i);
  105.  
  106.     decrefp(current,addr,ap);
  107.     decrefp(current,srcchan,sp);
  108.     decrefp(current,chk,cp);
  109.  
  110.     return_ok_result(3, key);
  111. }
  112. #endif
  113.  
  114.  
  115. enum {no_check, car_check};
  116. enum {asc_target, mzr_target};
  117.  
  118. struct ch_s {
  119.     eindex *name;
  120.     int type, check, target;
  121.     uint mtu;
  122.     eindex* addr_array;
  123.     retcode (*create_key)();
  124.     byteptr descr;
  125. };
  126.  
  127. static struct ch_s ch[] = {
  128. #ifdef CHANNEL_CONSOLE
  129.     {&console_name, 1, no_check, asc_target, 0, &console_addr, o_console,
  130.                 (byteptr)"M0 console (stdout)"},
  131. #endif
  132. #ifdef CHANNEL_NIT
  133.     {&nit_name, 1, no_check, mzr_target, 1400, &nit_addr, o_nit,
  134.                 (byteptr)"intel/dec/xerox ethernet"},
  135. #endif
  136. #ifdef CHANNEL_UDP
  137.     {&udp_name, 1, no_check, mzr_target, 7500, &udp_addr, o_udp,
  138.                 (byteptr)"DoD user datagram protocol over IP"},
  139. #endif
  140. };
  141.  
  142. retcode
  143. channel_defs()
  144. {
  145.     eindex n_cha, n_adr;
  146.     eindex n_aut, n_des, n_key, n_mtu, n_tar, n_typ;
  147.     eindex check[2], target[2];
  148.     eindex cdict, adict;
  149.     int i, nofch = sizeof(ch)/sizeof(struct ch_s);
  150.  
  151.     n_cha = name_add((byteptr)"_cha", 4, A_EXECUTABLE);
  152.     n_adr = name_add((byteptr)"_adr", 4, A_EXECUTABLE);
  153.  
  154.     n_aut = name_add((byteptr)"_aut", 4, A_EXECUTABLE);
  155.     n_des = name_add((byteptr)"_des", 4, A_EXECUTABLE);
  156.     n_key = name_add((byteptr)"_key", 4, A_EXECUTABLE);
  157.     n_mtu = name_add((byteptr)"_mtu", 4, A_EXECUTABLE);
  158.     n_tar = name_add((byteptr)"_tar", 4, A_EXECUTABLE);
  159.     n_typ = name_add((byteptr)"_typ", 4, A_EXECUTABLE);
  160.     check[0] = name_add((byteptr)"_nch", 4, 0);
  161.     check[1] = name_add((byteptr)"_car", 4, 0);
  162.     target[0] = name_add((byteptr)"_asc", 4, 0);
  163.     target[1] = name_add((byteptr)"_mzr", 4, 0);
  164.  
  165.     cdict = new_dict(0);
  166.     dict_def(0, systemdict, n_cha, cdict);
  167.     decref(0, n_cha); decref(0, cdict);
  168.     adict = new_dict(0);
  169.     dict_def(0, systemdict, n_adr, adict);
  170.     decref(0, n_adr); decref(0, adict);
  171.  
  172.     for (i = 0; i < nofch; i++) {
  173.         eindex e, val, d;
  174.  
  175.         if (!*(ch[i].name))
  176.             continue;
  177.  
  178.         val = *(ch[i].name);
  179.         if (ch[i].addr_array && *(ch[i].addr_array)) {
  180.             dict_def(0, adict, val, *(ch[i].addr_array));
  181.             decref(0, val);
  182.             decref(0, *(ch[i].addr_array));
  183.         }
  184.  
  185.         d = new_dict(0);
  186.         dict_def(0, cdict, val, d);
  187.         decref(0, val);
  188.         decref(0, d);
  189.  
  190. #define add_entry(k,v)    e = (v); dict_def(0,d,k,e);
  191.         add_entry(n_aut, check[ch[i].check]);
  192.         add_entry(n_tar, target[ch[i].target]);
  193.         val = new_element(0, T_INT);
  194.             gaddr(val)->V.i = ch[i].type;
  195.             add_entry(n_typ, val);
  196.             decref(0, val);
  197.         val = str_import(0, ch[i].descr, strlen((char*)(ch[i].descr)), 0);
  198.             add_entry(n_des, val);
  199.             decref(0, val);
  200.         val = new_element(0, T_PROC);
  201.             gaddr(val)->V.pro.fct = ch[i].create_key;
  202.             epattr(gaddr(val)) |= A_EXECUTABLE;
  203.             add_entry(n_key, val);
  204.             decref(0, val);
  205.         val = new_element(0, T_INT);
  206.             gaddr(val)->V.i = ch[i].mtu;
  207.             add_entry(n_mtu, val);
  208.             decref(0, val);
  209.     }
  210.  
  211.     decref(0, n_aut);    decref(0, n_des);
  212.     decref(0, n_key);    decref(0, n_mtu);
  213.     decref(0, n_tar);    decref(0, n_typ);
  214.     decref(0, check[0]);    decref(0, check[1]);
  215.     decref(0, target[0]);    decref(0, target[1]);
  216.  
  217.     return OK;
  218. }
  219.