home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / ixnet / ixnet_open.c < prev    next >
C/C++ Source or Header  |  1996-12-20  |  5KB  |  157 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 = getuser(me); /* 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.         /* We could check for the existance of the AMITCP port here
  78.            to make sure we're using the AmiTCP bsdsocket.library,
  79.            and not the bsdsocket.library emulation for AS225.
  80.            But in that case, the Miami package isn't recognized,
  81.            because they don't open an AMITCP port. Oh well... */
  82.         if ((p->u_TCPBase = OpenLibrary ("bsdsocket.library",3))) {
  83.         struct Process *thisproc = (struct Process *)me;
  84.         struct CommandLineInterface *cli = BTOCPTR(thisproc->pr_CLI);
  85.         char *progname = ((!thisproc->pr_CLI) ? me->tc_Node.ln_Name : BTOCPTR(cli->cli_CommandName));
  86.         struct TagItem list[] = {
  87.             /* { SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(int))), (ULONG)&u.u_errno }, */
  88.             /* { SBTM_SETVAL(SBTC_HERRNOLONGPTR), (ULONG)&h_errno }, */
  89.             { SBTM_SETVAL(SBTC_LOGTAGPTR), (ULONG)progname },
  90.             { SBTM_SETVAL(SBTC_SIGIOMASK), NULL },
  91.             { SBTM_SETVAL(SBTC_SIGURGMASK), NULL },
  92.             { SBTM_SETVAL(SBTC_BREAKMASK), SIGBREAKF_CTRL_C },
  93.             { TAG_END }
  94.         };
  95.  
  96.         p->u_sigurg     = AllocSignal (-1);
  97.         p->u_sigio     = AllocSignal (-1);
  98.  
  99.         list[1].ti_Data = (1L << p->u_sigio);
  100.         list[2].ti_Data = (1L << p->u_sigurg);
  101.  
  102.         /* I will assume this always is successful */
  103.         TCP_SocketBaseTagList(list);
  104.  
  105.         /* only use usergroup stuff when AmiTCP is started only */
  106.         /* I call OpenLibrary() with the full path since
  107.          * usergroup.library might open yet - some people bypass the
  108.          * "login" command which loads usergroup.library
  109.          */
  110.         p->u_UserGroupBase = OpenLibrary("AmiTCP:libs/usergroup.library",1);
  111.  
  112.         if (p->u_UserGroupBase) {
  113.             struct TagItem ug_list[] = {
  114.             { UGT_INTRMASK, SIGBREAKB_CTRL_C } ,
  115.             { UGT_ERRNOPTR(sizeof(int)), (ULONG)u.u_errno },
  116.             { TAG_END }
  117.             };
  118.             ug_SetupContextTagList(progname, ug_list);
  119.             p->u_networkprotocol = IX_NETWORK_AMITCP;
  120.             break;
  121.         }
  122.         FreeSignal(p->u_sigurg);
  123.         FreeSignal(p->u_sigio);
  124.         CloseLibrary(p->u_TCPBase);
  125.         }
  126.         /* don't fall through if not auto-detect */
  127.         if (network_type != IX_NETWORK_AUTO)
  128.         break;
  129.  
  130.     /* falls through if something failed above */
  131.     case IX_NETWORK_AS225:
  132.         p->u_SockBase = OpenLibrary("socket.library",3);
  133.  
  134.         if (p->u_SockBase) {
  135.         p->u_sigurg     = AllocSignal (-1);
  136.         p->u_sigio     = AllocSignal (-1);
  137.         p->u_networkprotocol = IX_NETWORK_AS225;
  138.          }
  139.         break;
  140.     }
  141.  
  142.     if (p->u_networkprotocol == IX_NETWORK_NONE) {
  143.         extern struct ixnet_base *ixnetbase;
  144.         FreeMem(ix_u->u_ixnet,sizeof(struct ixnet));
  145.         /* decrement the open count since no network libraries installed */
  146.         /* so we keep the correct open count */
  147.         ixnetbase->ixnet_lib.lib_OpenCnt--;
  148.         return 0;
  149.     }
  150.     return ixbase;
  151.     }
  152.     ixnetbase->ixnet_lib.lib_OpenCnt--;
  153.     return 0;
  154. }
  155.  
  156.  
  157.