nb_net.c revision 9c9af2590af49bb395bc8d2eace0f2d4ea16d165
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Copyright (c) 2000, Boris Popov
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Redistribution and use in source and binary forms, with or without
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * modification, are permitted provided that the following conditions
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * are met:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 1. Redistributions of source code must retain the above copyright
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * notice, this list of conditions and the following disclaimer.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 2. Redistributions in binary form must reproduce the above copyright
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * notice, this list of conditions and the following disclaimer in the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * documentation and/or other materials provided with the distribution.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 3. All advertising materials mentioning features or use of this software
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * must display the following acknowledgement:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * This product includes software developed by Boris Popov.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 4. Neither the name of the author nor the names of any co-contributors
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * may be used to endorse or promote products derived from this software
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * without specific prior written permission.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * SUCH DAMAGE.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * $Id: nb_net.c,v 1.8 2004/03/19 01:49:47 lindak Exp $
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/param.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/socket.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/ioctl.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/sockio.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <net/if.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <ctype.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netdb.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <errno.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <stdlib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <string.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <strings.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <stdio.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <unistd.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <err.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/netbios.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/smb_lib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/nb_lib.h>
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross#include "private.h"
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross/*
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross * General networking stuff, in spite of the names
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross * that imply they're specific to NetBIOS.
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlownb_getlocalname(char *name, size_t maxlen)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char buf[1024], *cp;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (gethostname(buf, sizeof (buf)) != 0)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (errno);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cp = strchr(buf, '.');
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (cp)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *cp = 0;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow strlcpy(name, buf, maxlen);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (0);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlownb_resolvehost_in(const char *name, struct sockaddr **dest)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow struct hostent *h;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow struct sockaddr_in *sinp;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow in_addr_t addr;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow struct in_addr in;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int len;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char **p;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow h = gethostbyname(name);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (!h) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#ifdef DEBUG
4bff34e37def8a90f9194d81bc345c52ba20086athurlow warnx("can't get server address `%s': ", name);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (ENETDOWN);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (h->h_addrtype != AF_INET) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#ifdef DEBUG
4bff34e37def8a90f9194d81bc345c52ba20086athurlow warnx("address for `%s' is not in the AF_INET family", name);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (EAFNOSUPPORT);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (h->h_length != 4) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#ifdef DEBUG
4bff34e37def8a90f9194d81bc345c52ba20086athurlow warnx("address for `%s' has invalid length", name);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (EAFNOSUPPORT);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow len = sizeof (struct sockaddr_in);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sinp = malloc(len);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (sinp == NULL)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (ENOMEM);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow bzero(sinp, len);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * There is no sin_len in sockaddr_in structure on Solaris.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * sinp->sin_len = len;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sinp->sin_family = h->h_addrtype;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow memcpy(&sinp->sin_addr.s_addr, *h->h_addr_list,\
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sizeof (sinp->sin_addr.s_addr));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sinp->sin_port = htons(SMB_TCP_PORT);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *dest = (struct sockaddr *)sinp;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (0);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}