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

  1. /*
  2.  *  This file is part of ixemul.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.  
  25. /*
  26.  * Copyright (c) 1988 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms, with or without
  30.  * modification, are permitted provided that the following conditions
  31.  * are met:
  32.  * 1. Redistributions of source code must retain the above copyright
  33.  *    notice, this list of conditions and the following disclaimer.
  34.  * 2. Redistributions in binary form must reproduce the above copyright
  35.  *    notice, this list of conditions and the following disclaimer in the
  36.  *    documentation and/or other materials provided with the distribution.
  37.  * 3. All advertising materials mentioning features or use of this software
  38.  *    must display the following acknowledgement:
  39.  *    This product includes software developed by the University of
  40.  *    California, Berkeley and its contributors.
  41.  * 4. Neither the name of the University nor the names of its contributors
  42.  *    may be used to endorse or promote products derived from this software
  43.  *    without specific prior written permission.
  44.  *
  45.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  46.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  49.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55.  * SUCH DAMAGE.
  56.  */
  57.  
  58. /* interface with ixnet.library - just call the library (if its open) */
  59. /* sometimes do some local things if ixnet.library isn't open */
  60. #define _KERNEL
  61. #include "ixemul.h"
  62. #include <netdb.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <netinet/in.h>
  66. #include <arpa/inet.h>
  67.  
  68. struct hostent *
  69. gethostbyname(const char *name)
  70. {
  71.   if (u.u_ixnetbase)
  72.     return (struct hostent *)netcall(NET_gethostbyname, name);
  73.   return NULL;
  74. }
  75.  
  76. struct hostent *
  77. gethostbyaddr(const char *addr, int len, int type)
  78. {
  79.   if (u.u_ixnetbase)
  80.     return (struct hostent *)netcall(NET_gethostbyaddr, addr, len, type);
  81.   return NULL;
  82. }
  83.  
  84.  
  85. void
  86. sethostent(int stayopen)
  87. {
  88.   if (u.u_ixnetbase)
  89.     netcall(NET_sethostent, stayopen);
  90. }
  91.  
  92. void
  93. endhostent(void)
  94. {
  95.   if (u.u_ixnetbase)
  96.     netcall(NET_endhostent);
  97. }
  98.  
  99. struct netent *
  100. getnetbyname(const char *name)
  101. {
  102.   if (u.u_ixnetbase)
  103.     return (struct netent *)netcall(NET_getnetbyname, name);
  104.   return NULL;
  105. }
  106.  
  107. struct netent *
  108. getnetbyaddr(long net, int type)
  109. {
  110.   if (u.u_ixnetbase)
  111.     return (struct netent *)netcall(NET_getnetbyaddr, net, type);
  112.   return NULL;
  113. }
  114.  
  115. void
  116. setnetent(int f)
  117. {
  118.   if (u.u_ixnetbase)
  119.     netcall(NET_setnetent, f);
  120. }
  121.  
  122.  
  123. void
  124. endnetent(void)
  125. {
  126.   if (u.u_ixnetbase)
  127.     netcall(NET_endnetent);
  128. }
  129.  
  130. struct netent *
  131. getnetent(void)
  132. {
  133.   if (u.u_ixnetbase)
  134.     return (struct netent *)netcall(NET_getnetent);
  135.   return NULL;
  136. }
  137.  
  138. #define MAXALIASES 35
  139. #include <stdio.h>
  140. #include <netdb.h>
  141.  
  142. void
  143. setservent(int f)
  144. {
  145.   if (u.u_ixnetbase) {
  146.     netcall(NET_setservent, f);
  147.   }
  148.   else {
  149.     if (u.u_serv_fp == NULL) {
  150.       u.u_serv_fp = fopen(_PATH_SERVICES, "r" );
  151.       if (!u.u_serv_fp)
  152.         return;
  153.     }
  154.     else
  155.       rewind(u.u_serv_fp);
  156.       u.u_serv_stayopen |= f;
  157.   }
  158. }
  159.  
  160.  
  161. void
  162. endservent(void)
  163. {
  164.   if (u.u_ixnetbase) {
  165.     netcall(NET_endservent);
  166.   }
  167.   else {
  168.     if (u.u_serv_fp) {
  169.       fclose(u.u_serv_fp);
  170.       u.u_serv_fp = NULL;
  171.     }
  172.     u.u_serv_stayopen = 0;
  173.   }
  174. }
  175.  
  176.  
  177. struct servent *
  178. getservent(void)
  179. {
  180.   if (u.u_ixnetbase) {
  181.     return (struct servent *)netcall(NET_getservent);
  182.   }
  183.   else {
  184.     char *p;
  185.     register char *cp, **q;
  186.  
  187.     if (u.u_serv_line == NULL)
  188.       u.u_serv_line = malloc(BUFSIZ + 1);
  189.     if (u.u_serv_aliases == NULL)
  190.       u.u_serv_aliases = malloc(MAXALIASES * sizeof(char *));
  191.     if (u.u_serv_line == NULL || u.u_serv_aliases == NULL)
  192.       {
  193.         errno = ENOMEM;
  194.         return NULL;
  195.       }
  196.     if (u.u_serv_fp == NULL && (u.u_serv_fp = fopen(_PATH_SERVICES, "r" )) == NULL) {
  197.       return NULL;
  198.     }
  199. again1:
  200.     if ((p = fgets(u.u_serv_line, BUFSIZ, u.u_serv_fp)) == NULL)
  201.       return NULL;
  202.     if (*p == '#')
  203.       goto again1;
  204.  
  205.     cp = strpbrk(p, "#\n");
  206.     if (cp == NULL)
  207.       goto again1;
  208.  
  209.     *cp = '\0';
  210.     u.u_serv.s_name = p;
  211.     p = strpbrk(p, " \t");
  212.     if (p == NULL)
  213.       goto again1;
  214.  
  215.     *p++ = '\0';
  216.     while (*p == ' ' || *p == '\t')
  217.       p++;
  218.  
  219.     cp = strpbrk(p, ",/");
  220.     if (cp == NULL)
  221.       goto again1;
  222.  
  223.     *cp++ = '\0';
  224.     u.u_serv.s_port = htons((u_short)atoi(p));
  225.     u.u_serv.s_proto = cp;
  226.     q = u.u_serv.s_aliases = u.u_serv_aliases;
  227.     cp = strpbrk(cp, " \t");
  228.     if (cp != NULL)
  229.       *cp++ = '\0';
  230.  
  231.     while (cp && *cp) {
  232.       if (*cp == ' ' || *cp == '\t') {
  233.         cp++;
  234.         continue;
  235.       }
  236.       if (q < &u.u_serv_aliases[MAXALIASES - 1])
  237.         *q++ = cp;
  238.       cp = strpbrk(cp, " \t");
  239.       if (cp != NULL)
  240.         *cp++ = '\0';
  241.     }
  242.     *q = NULL;
  243.     return (&u.u_serv);
  244.   }
  245. }
  246.  
  247. struct servent *
  248. getservbyname(const char *name, const char *proto)
  249. {
  250.   if (u.u_ixnetbase) {
  251.     return (struct servent *)netcall(NET_getservbyname, name, proto);
  252.   }
  253.   else {
  254.     register struct servent *p;
  255.     register char **cp;
  256.  
  257.     setservent(u.u_serv_stayopen);
  258.     while ((p = getservent())) {
  259.       if (strcmp(name, p->s_name) == 0)
  260.         goto gotservname;
  261.  
  262.       for (cp = p->s_aliases; *cp; cp++)
  263.         if (strcmp(name, *cp) == 0)
  264.           goto gotservname;
  265.  
  266.         continue;
  267. gotservname:
  268.       if (proto == 0 || strcmp(p->s_proto, proto) == 0)
  269.         break;
  270.     }
  271.     if (!u.u_serv_stayopen)
  272.     endservent();
  273.  
  274.     return (p);
  275.   }
  276. }
  277.  
  278. struct servent *
  279. getservbyport(int port, const char * proto)
  280. {
  281.     if (u.u_ixnetbase) {
  282.     return (struct servent *)netcall(NET_getservbyport, port,proto);
  283.     }
  284.     else {
  285.     register struct servent *p;
  286.  
  287.     setservent(u.u_serv_stayopen);
  288.     while ((p = getservent())) {
  289.         if (p->s_port != port)
  290.         continue;
  291.  
  292.         if (proto == 0 || strcmp(p->s_proto, proto) == 0)
  293.         break;
  294.     }
  295.     if (!u.u_serv_stayopen)
  296.         endservent();
  297.  
  298.     return (p);
  299.     }
  300. }
  301.  
  302. long gethostid(void)
  303. {
  304.   if (u.u_ixnetbase)
  305.     return netcall(NET_gethostid);
  306.   return 0xaabbccdd;
  307. }
  308.  
  309. void
  310. herror(const char *s)
  311. {
  312.   if (u.u_ixnetbase)
  313.     netcall(NET_herror, s);
  314. }
  315.  
  316. struct protoent *
  317. getprotobynumber(int proto)
  318. {
  319.     if (u.u_ixnetbase) {
  320.     return (struct protoent *)netcall(NET_getprotobynumber, proto);
  321.     }
  322.     else {
  323.     register struct protoent *p;
  324.  
  325.     setprotoent(u.u_proto_stayopen);
  326.     while ((p = getprotoent()))
  327.         if (p->p_proto == proto)
  328.         break;
  329.  
  330.     if (!u.u_proto_stayopen)
  331.         endprotoent();
  332.     return (p);
  333.     }
  334. }
  335.  
  336. struct protoent *
  337. getprotobyname(const char *name)
  338. {
  339.     if (u.u_ixnetbase) {
  340.     return (struct protoent *)netcall(NET_getprotobyname, name);
  341.     }
  342.     else {
  343.     register struct protoent *p;
  344.     register char **cp;
  345.  
  346.     setprotoent(u.u_proto_stayopen);
  347.     while ((p = getprotoent())) {
  348.         if (strcmp(p->p_name, name) == 0)
  349.         break;
  350.         for (cp = p->p_aliases; *cp != 0; cp++)
  351.         if (strcmp(*cp, name) == 0)
  352.             goto found;
  353.     }
  354. found:
  355.     if (!u.u_proto_stayopen)
  356.         endprotoent();
  357.     return (p);
  358.     }
  359. }
  360.  
  361. void
  362. setprotoent(int f)
  363. {
  364.     if (u.u_ixnetbase) {
  365.     netcall(NET_setprotoent, f);
  366.     }
  367.     else {
  368.     if (u.u_proto_fp == NULL)
  369.         u.u_proto_fp = fopen(_PATH_PROTOCOLS, "r" );
  370.     else
  371.         rewind(u.u_proto_fp);
  372.     u.u_proto_stayopen |= f;
  373.     }
  374. }
  375.  
  376. void
  377. endprotoent(void)
  378. {
  379.     if (u.u_ixnetbase) {
  380.     netcall(NET_endprotoent);
  381.     }
  382.     else {
  383.     if (u.u_proto_fp) {
  384.         fclose(u.u_proto_fp);
  385.         u.u_proto_fp = NULL;
  386.     }
  387.     u.u_proto_stayopen = 0;
  388.     }
  389. }
  390.  
  391. struct protoent *
  392. getprotoent(void)
  393. {
  394.     if (u.u_ixnetbase) {
  395.     return (struct protoent *)netcall(NET_getprotoent);
  396.     }
  397.     else {
  398.     char *p;
  399.     register char *cp, **q;
  400.  
  401.         if (u.u_proto_line == NULL)
  402.           u.u_proto_line = malloc(BUFSIZ + 1);
  403.         if (u.u_proto_aliases == NULL)
  404.           u.u_proto_aliases = malloc(MAXALIASES * sizeof(char *));
  405.         if (u.u_proto_line == NULL || u.u_proto_aliases == NULL)
  406.           {
  407.             errno = ENOMEM;
  408.             return NULL;
  409.           }
  410.     if (u.u_proto_fp == NULL && (u.u_proto_fp = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
  411.         return (NULL);
  412.     }
  413. again3:
  414.  
  415.     if ((p = fgets(u.u_proto_line, BUFSIZ, u.u_proto_fp)) == NULL)
  416.         return (NULL);
  417.  
  418.     if (*p == '#')
  419.         goto again3;
  420.  
  421.     cp = strpbrk(p, "#\n");
  422.     if (cp == NULL)
  423.         goto again3;
  424.  
  425.     *cp = '\0';
  426.     u.u_proto.p_name = p;
  427.     cp = strpbrk(p, " \t");
  428.     if (cp == NULL)
  429.         goto again3;
  430.  
  431.     *cp++ = '\0';
  432.     while (*cp == ' ' || *cp == '\t')
  433.         cp++;
  434.  
  435.     p = strpbrk(cp, " \t");
  436.     if (p != NULL)
  437.         *p++ = '\0';
  438.  
  439.     u.u_proto.p_proto = atoi(cp);
  440.     q = u.u_proto.p_aliases = u.u_proto_aliases;
  441.     if (p != NULL) {
  442.         cp = p;
  443.         while (cp && *cp) {
  444.         if (*cp == ' ' || *cp == '\t') {
  445.             cp++;
  446.             continue;
  447.         }
  448.  
  449.         if (q < &u.u_proto_aliases[MAXALIASES - 1])
  450.             *q++ = cp;
  451.  
  452.         cp = strpbrk(cp, " \t");
  453.         if (cp != NULL)
  454.             *cp++ = '\0';
  455.         }
  456.     }
  457.     *q = NULL;
  458.     return (&u.u_proto);
  459.     }
  460. }
  461.  
  462. /* not sure if I want to put the full version here or not */
  463. /* see ixnet/getpass.c for the full version */
  464. char *
  465. getpass(const char *prompt)
  466. {
  467.   if (u.u_ixnetbase)
  468.     return (char *)netcall(NET_getpass, prompt);
  469.   return NULL;
  470. }
  471.  
  472. int
  473. _getlogin (char *s, int size)
  474. {
  475.   char *cp = getenv ("USER");
  476.  
  477.   if (! cp)
  478.     cp = getenv ("LOGNAME");
  479.   if (! cp)
  480.     cp = u.u_logname;
  481.  
  482.   strncpy (s, cp, size);
  483.   return 0;
  484. }
  485.  
  486. char *
  487. getlogin(void) 
  488. {
  489.   if (u.u_ixnetbase)
  490.     return (char *)netcall(NET_getlogin);
  491.   if (u.u_logname_valid == 0) {
  492.     if (_getlogin(u.u_logname_buf, MAXLOGNAME) < 0)
  493.       return ((char *)NULL);
  494.  
  495.     u.u_logname_valid = 1;
  496.   }
  497.   return (*u.u_logname_buf ? u.u_logname_buf : (char *)NULL);
  498. }
  499.  
  500.  
  501. int
  502. setlogin (const char *s)
  503. {
  504.   if (u.u_ixnetbase)
  505.     return netcall(NET_setlogin, s);
  506.  
  507.   strncpy (u.u_logname, s, MAXLOGNAME);
  508.   u.u_logname_valid = 0;
  509.   return 0;
  510. }
  511.  
  512. int
  513. getdomainname (char *name, int namelen)
  514. {
  515.   if (u.u_ixnetbase)
  516.     return netcall(NET_getdomainname, name, namelen);
  517.  
  518.   name[0] = 0;  /* anyone have a better suggestion? */
  519.   return 0;
  520. }
  521.