187866Ssheldonh/*
287866Ssheldonh * Copyright (c) 2000, Boris Popov
387866Ssheldonh * All rights reserved.
487866Ssheldonh *
587866Ssheldonh * Redistribution and use in source and binary forms, with or without
687866Ssheldonh * modification, are permitted provided that the following conditions
787866Ssheldonh * are met:
887866Ssheldonh * 1. Redistributions of source code must retain the above copyright
987866Ssheldonh *    notice, this list of conditions and the following disclaimer.
1087866Ssheldonh * 2. Redistributions in binary form must reproduce the above copyright
1187866Ssheldonh *    notice, this list of conditions and the following disclaimer in the
1287866Ssheldonh *    documentation and/or other materials provided with the distribution.
1387866Ssheldonh * 3. All advertising materials mentioning features or use of this software
1487866Ssheldonh *    must display the following acknowledgement:
1587866Ssheldonh *    This product includes software developed by Boris Popov.
1687866Ssheldonh * 4. Neither the name of the author nor the names of any co-contributors
1787866Ssheldonh *    may be used to endorse or promote products derived from this software
1887866Ssheldonh *    without specific prior written permission.
1987866Ssheldonh *
2087866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2187866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2287866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2387866Ssheldonh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2487866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2587866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2687866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2787866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2887866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2987866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3087866Ssheldonh * SUCH DAMAGE.
3187866Ssheldonh *
3287866Ssheldonh * $Id: lookup.c,v 1.3 2000/10/15 14:26:49 bp Exp $
3387866Ssheldonh */
3487866Ssheldonh#include <sys/param.h>
3587866Ssheldonh#include <sys/errno.h>
3687866Ssheldonh#include <sys/stat.h>
3787866Ssheldonh#include <sys/socket.h>
3887866Ssheldonh#include <err.h>
3987866Ssheldonh#include <stdio.h>
4087866Ssheldonh#include <unistd.h>
4187866Ssheldonh#include <strings.h>
4287866Ssheldonh#include <stdlib.h>
4387866Ssheldonh#include <sysexits.h>
4487866Ssheldonh
4587866Ssheldonh#include <netinet/in.h>
4687866Ssheldonh#include <arpa/inet.h>
4787866Ssheldonh
4887866Ssheldonh#include <cflib.h>
4987866Ssheldonh
5087866Ssheldonh#include <netsmb/netbios.h>
5187866Ssheldonh#include <netsmb/smb_lib.h>
5287866Ssheldonh#include <netsmb/nb_lib.h>
5387866Ssheldonh#include <netsmb/smb_conn.h>
5487866Ssheldonh
5587866Ssheldonh#include "common.h"
5687866Ssheldonh
5787866Ssheldonh
5887866Ssheldonhint
5987866Ssheldonhcmd_lookup(int argc, char *argv[])
6087866Ssheldonh{
6187866Ssheldonh	struct nb_ctx *ctx;
6287866Ssheldonh	struct sockaddr *sap;
6387866Ssheldonh	char *hostname;
6487866Ssheldonh	int error, opt;
6587866Ssheldonh
6687866Ssheldonh	if (argc < 2)
6787866Ssheldonh		lookup_usage();
6887866Ssheldonh	error = nb_ctx_create(&ctx);
6987866Ssheldonh	if (error) {
7087866Ssheldonh		smb_error("unable to create nbcontext", error);
7187866Ssheldonh		exit(1);
7287866Ssheldonh	}
7387866Ssheldonh	if (smb_open_rcfile() == 0) {
7487866Ssheldonh		if (nb_ctx_readrcsection(smb_rc, ctx, "default", 0) != 0)
7587866Ssheldonh			exit(1);
7687866Ssheldonh		rc_close(smb_rc);
7787866Ssheldonh	}
7887866Ssheldonh	while ((opt = getopt(argc, argv, "w:")) != EOF) {
7987866Ssheldonh		switch(opt){
8087866Ssheldonh		    case 'w':
8187866Ssheldonh			nb_ctx_setns(ctx, optarg);
8287866Ssheldonh			break;
8387866Ssheldonh		    default:
8487866Ssheldonh			lookup_usage();
8587866Ssheldonh			/*NOTREACHED*/
8687866Ssheldonh		}
8787866Ssheldonh	}
8887866Ssheldonh	if (optind >= argc)
8987866Ssheldonh		lookup_usage();
9087866Ssheldonh	if (nb_ctx_resolve(ctx) != 0)
9187866Ssheldonh		exit(1);
9287866Ssheldonh	hostname = argv[argc - 1];
9387866Ssheldonh/*	printf("Looking for %s...\n", hostname);*/
9487866Ssheldonh	error = nbns_resolvename(hostname, ctx, &sap);
9587866Ssheldonh	if (error) {
9687866Ssheldonh		smb_error("unable to resolve %s", error, hostname);
9787866Ssheldonh		exit(1);
9887866Ssheldonh	}
9987866Ssheldonh	printf("Got response from %s\n", inet_ntoa(ctx->nb_lastns.sin_addr));
10087866Ssheldonh	printf("IP address of %s: %s\n", hostname, inet_ntoa(((struct sockaddr_in*)sap)->sin_addr));
10187866Ssheldonh	return 0;
10287866Ssheldonh}
10387866Ssheldonh
10487866Ssheldonh
10587866Ssheldonhvoid
10687866Ssheldonhlookup_usage(void)
10787866Ssheldonh{
10887866Ssheldonh	printf("usage: smbutil lookup [-w host] name\n");
10987866Ssheldonh	exit(1);
11087866Ssheldonh}
111