1/*
2 * $Id: nbplkup.c,v 1.9 2009-10-29 11:35:57 didg Exp $
3 *
4 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appears in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the name of The University
12 * of Michigan not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. This software is supplied as is without expressed or
15 * implied warranties of any kind.
16 *
17 *	Research Systems Unix Group
18 *	The University of Michigan
19 *	c/o Mike Clark
20 *	535 W. William Street
21 *	Ann Arbor, Michigan
22 *	+1-313-763-0525
23 *	netatalk@itd.umich.edu
24 */
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif /* HAVE_CONFIG_H */
29
30#include <sys/types.h>
31#include <netatalk/endian.h>
32#include <netatalk/at.h>
33#include <atalk/nbp.h>
34#include <atalk/util.h>
35#include <string.h>
36#include <stdio.h>
37#ifdef HAVE_STDLIB_H
38#include <stdlib.h>
39#endif
40
41
42#include <atalk/unicode.h>
43
44static char *Obj = "=";
45static char *Type = "=";
46static char *Zone = "*";
47
48static void Usage(char *av0)
49{
50    char	*p;
51
52    if (( p = strrchr( av0, '/' )) == NULL ) {
53	p = av0;
54    } else {
55	p++;
56    }
57
58    printf( "Usage:\t%s [ -A address ] [ -r responses] [-m Mac charset] [ obj:type@zone ]\n", p );
59    exit( 1 );
60}
61
62int main(int ac, char **av)
63{
64    struct nbpnve	*nn;
65    char		*name;
66    int			i, c, nresp = 1000;
67    struct at_addr      addr;
68    char		*obj = NULL;
69    size_t		obj_len;
70    charset_t		chMac = CH_MAC;
71    char *		convname;
72
73    extern char		*optarg;
74    extern int		optind;
75
76    memset(&addr, 0, sizeof(addr));
77    while (( c = getopt( ac, av, "r:A:m:" )) != EOF ) {
78	switch ( c ) {
79	case 'A':
80  	    if (!atalk_aton(optarg, &addr)) {
81	        fprintf(stderr, "Bad address.\n");
82		exit(1);
83	    }
84	    break;
85	case 'r' :
86	    nresp = atoi( optarg );
87	    break;
88        case 'm':
89            if ((charset_t)-1 == (chMac = add_charset(optarg)) ) {
90	        fprintf(stderr, "Invalid Mac charset.\n");
91		exit(1);
92	    }
93            break;
94
95	default :
96	    Usage( av[ 0 ] );
97	    exit( 1 );
98	}
99    }
100
101    if (( nn = (struct nbpnve *)malloc( nresp * sizeof( struct nbpnve )))
102	    == NULL ) {
103	perror( "malloc" );
104	exit( 1 );
105    }
106
107    if ( ac - optind > 1 ) {
108	Usage( av[ 0 ] );
109	exit( 1 );
110    }
111
112    /*
113     * Get default values from the environment. We need to copy out
114     * the results, here, since nbp_name returns it's parameters
115     * in static space, and we'll clobber them when we call it again
116     * later.
117     */
118    if (( name = getenv( "NBPLKUP" )) != NULL ) {
119	if ( nbp_name( name, &Obj, &Type, &Zone )) {
120	    fprintf( stderr,
121		    "Environment variable syntax error: NBPLKUP = %s\n",
122		    name );
123	    exit( 1 );
124	}
125
126	if (( name = (char *)malloc( strlen( Obj ) + 1 )) == NULL ) {
127	    perror( "malloc" );
128	    exit( 1 );
129	}
130	strcpy( name, Obj );
131	Obj = name;
132
133	if (( name = (char *)malloc( strlen( Type ) + 1 )) == NULL ) {
134	    perror( "malloc" );
135	    exit( 1 );
136	}
137	strcpy( name, Type );
138	Type = name;
139
140	if (( name = (char *)malloc( strlen( Zone ) + 1 )) == NULL ) {
141	    perror( "malloc" );
142	    exit( 1 );
143	}
144	strcpy( name, Zone );
145	Zone = name;
146
147    }
148
149    if ( ac - optind == 1 ) {
150	if ((size_t)(-1) == convert_string_allocate( CH_UNIX, chMac,
151                           av[ optind ], -1, &convname))
152            convname = av[ optind ];
153
154	if ( nbp_name( convname, &Obj, &Type, &Zone )) {
155	    Usage( av[ 0 ] );
156	    exit( 1 );
157	}
158    }
159
160    if (( c = nbp_lookup( Obj, Type, Zone, nn, nresp, &addr)) < 0 ) {
161	perror( "nbp_lookup" );
162	exit( -1 );
163    }
164    for ( i = 0; i < c; i++ ) {
165
166	if ((size_t)(-1) == (obj_len = convert_string_allocate( chMac,
167                       CH_UNIX, nn[ i ].nn_obj, nn[ i ].nn_objlen, &obj)) ) {
168            obj_len = nn[ i ].nn_objlen;
169            if (( obj = strdup(nn[ i ].nn_obj)) == NULL ) {
170	        perror( "strdup" );
171	        exit( 1 );
172	    }
173        }
174
175	printf( "%31.*s:%-34.*s %u.%u:%u\n",
176		(int)obj_len, obj,
177		nn[ i ].nn_typelen, nn[ i ].nn_type,
178		ntohs( nn[ i ].nn_sat.sat_addr.s_net ),
179		nn[ i ].nn_sat.sat_addr.s_node,
180		nn[ i ].nn_sat.sat_port );
181
182	free(obj);
183    }
184
185    free(nn);
186    return 0;
187}
188