home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / ixnet / ixnet_open.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  154 lines

  1. /*
  2.  *  This file is part of ixnet.library for the Amiga.
  3.  *  Copyright (C) 1996 Jeff Shepherd
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  $Id:$
  20.  *
  21.  *  $Log:$
  22.  */
  23.  
  24. #define _KERNEL
  25. #include "ixnet.h"
  26. #include "kprintf.h"
  27.  
  28. #include <amitcp/socketbasetags.h>
  29. #include <amitcp/usergroup.h>
  30. #include <exec/memory.h>
  31. #include <string.h>
  32. #include "ixprotos.h"
  33.  
  34. extern struct ixemul_base *ixemulbase;
  35.  
  36. struct ixnet_base *
  37. ixnet_open (struct ixnet_base *ixbase)
  38. {
  39.     struct ixnet *p;
  40.     struct Task *me;
  41.     struct user *ix_u;
  42.     struct ix_settings *settings; /* don't call this *ix there is a macro called ix!!! */
  43.     int network_type;
  44.  
  45.     me = SysBase->ThisTask;
  46.     ix_u = (struct user *)me->tc_TrapData; /* already initialized by ixemul.library */
  47.  
  48.     /* need this here instead of ixnet_init.c */
  49.     if (!ixemulbase)
  50.     ixemulbase = ix_u->u_ixbase;
  51.  
  52.     if (ixnetbase->ixnet_lib.lib_Version != ixemulbase->ix_lib.lib_Version ||
  53.         ixnetbase->ixnet_lib.lib_Revision != ixemulbase->ix_lib.lib_Revision)
  54.       {
  55.         ix_panic(
  56. "ixnet.library has version %ld.%ld while ixemul.library has version %ld.%ld.
  57. Both libraries should have the same version, therefore ixnet.library
  58. won't be used.", ixnetbase->ixnet_lib.lib_Version, ixnetbase->ixnet_lib.lib_Revision,
  59.                  ixemulbase->ix_lib.lib_Version, ixemulbase->ix_lib.lib_Revision);
  60.         ixnetbase->ixnet_lib.lib_OpenCnt--;
  61.     settings = ix_get_settings();
  62.     settings->network_type = IX_NETWORK_NONE;
  63.     ix_set_settings(settings);
  64.         return 0;
  65.       }
  66.  
  67.     p = ix_u->u_ixnet = (struct ixnet *)AllocMem(sizeof(struct ixnet),MEMF_PUBLIC|MEMF_CLEAR);
  68.  
  69.     settings = ix_get_settings();
  70.     network_type = settings->network_type;
  71.  
  72.     if (p) {
  73.       p->u_networkprotocol = IX_NETWORK_NONE;
  74.       switch (network_type) {
  75.     case IX_NETWORK_AUTO:
  76.     case IX_NETWORK_AMITCP:
  77.         p->u_TCPBase = OpenLibrary ("bsdsocket.library",3);
  78.  
  79.         if (p->u_TCPBase) {
  80.         struct Process *thisproc = (struct Process *)me;
  81.         struct CommandLineInterface *cli = BTOCPTR(thisproc->pr_CLI);
  82.         char *progname = ((!thisproc->pr_CLI) ? me->tc_Node.ln_Name : BTOCPTR(cli->cli_CommandName));
  83.         struct TagItem list[] = {
  84.             /* { SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(int))), (ULONG)&u.u_errno }, */
  85.             /* { SBTM_SETVAL(SBTC_HERRNOLONGPTR), (ULONG)&h_errno }, */
  86.             { SBTM_SETVAL(SBTC_LOGTAGPTR), (ULONG)progname },
  87.             { SBTM_SETVAL(SBTC_SIGIOMASK), NULL },
  88.             { SBTM_SETVAL(SBTC_SIGURGMASK), NULL },
  89.             { SBTM_SETVAL(SBTC_BREAKMASK), SIGBREAKF_CTRL_C },
  90.             { TAG_END }
  91.         };
  92.  
  93.         p->u_sigurg     = AllocSignal (-1);
  94.         p->u_sigio     = AllocSignal (-1);
  95.  
  96.         list[1].ti_Data = (1L << p->u_sigio);
  97.         list[2].ti_Data = (1L << p->u_sigurg);
  98.  
  99.         /* I will assume this always is successful */
  100.         TCP_SocketBaseTagList(list);
  101.  
  102.         /* only use usergroup stuff when AmiTCP is started only */
  103.         /* I call OpenLibrary() with the full path since
  104.          * usergroup.library might open yet - some people bypass the
  105.          * "login" command which loads usergroup.library
  106.          */
  107.         p->u_UserGroupBase = OpenLibrary("AmiTCP:libs/usergroup.library",1);
  108.  
  109.         if (p->u_UserGroupBase) {
  110.             struct TagItem ug_list[] = {
  111.             { UGT_INTRMASK, SIGBREAKB_CTRL_C } ,
  112.             { UGT_ERRNOPTR(sizeof(int)), (ULONG)u.u_errno },
  113.             { TAG_END }
  114.             };
  115.             ug_SetupContextTagList(progname, ug_list);
  116.             p->u_networkprotocol = IX_NETWORK_AMITCP;
  117.             break;
  118.         }
  119.         FreeSignal(p->u_sigurg);
  120.         FreeSignal(p->u_sigio);
  121.         CloseLibrary(p->u_TCPBase);
  122.         }
  123.         /* don't fall through if not auto-detect */
  124.         if (network_type != IX_NETWORK_AUTO)
  125.         break;
  126.  
  127.     /* falls through if something failed above */
  128.     case IX_NETWORK_AS225:
  129.         p->u_SockBase = OpenLibrary("socket.library",3);
  130.  
  131.         if (p->u_SockBase) {
  132.         p->u_sigurg     = AllocSignal (-1);
  133.         p->u_sigio     = AllocSignal (-1);
  134.         p->u_networkprotocol = IX_NETWORK_AS225;
  135.          }
  136.         break;
  137.     }
  138.  
  139.     if (p->u_networkprotocol == IX_NETWORK_NONE) {
  140.         extern struct ixnet_base *ixnetbase;
  141.         FreeMem(ix_u->u_ixnet,sizeof(struct ixnet));
  142.         /* decrement the open count since no network libraries installed */
  143.         /* so we keep the correct open count */
  144.         ixnetbase->ixnet_lib.lib_OpenCnt--;
  145.         return 0;
  146.     }
  147.     return ixbase;
  148.     }
  149.     ixnetbase->ixnet_lib.lib_OpenCnt--;
  150.     return 0;
  151. }
  152.  
  153.  
  154.