1/*
2 * $Id: nbprgstr.c,v 1.9 2009-10-29 11:35:58 didg Exp $
3 *
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved.  See COPYRIGHT.
6 */
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif /* HAVE_CONFIG_H */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/types.h>
16#include <sys/param.h>
17
18#include <netatalk/endian.h>
19#include <netatalk/at.h>
20#include <atalk/netddp.h>
21#include <atalk/nbp.h>
22#include <atalk/util.h>
23#include <atalk/unicode.h>
24
25static void Usage(char *av0)
26{
27    char	*p;
28
29    if (( p = strrchr( av0, '/' )) == NULL ) {
30	p = av0;
31    } else {
32	p++;
33    }
34
35    fprintf( stderr, "Usage: %s [ -A address ] [-m Mac charset] [ -p port] obj:type@zone\n", p );
36    exit( 1 );
37}
38
39int main(int ac, char **av)
40{
41    struct sockaddr_at	addr;
42    struct at_addr      ataddr;
43    char		*Obj = NULL, *Type = NULL, *Zone = NULL;
44    char		*convname = NULL;
45    int			s, c, port = 0;
46    charset_t		chMac = CH_MAC;
47
48    extern char		*optarg;
49    extern int		optind;
50
51    memset(&ataddr, 0, sizeof(ataddr));
52    while (( c = getopt( ac, av, "p:A:m:" )) != EOF ) {
53	switch ( c ) {
54	case 'A':
55	    if (!atalk_aton(optarg, &ataddr)) {
56	        fprintf(stderr, "Bad address.\n");
57		exit(1);
58	    }
59	    break;
60
61        case 'm':
62            if ((charset_t)-1 == (chMac = add_charset(optarg)) ) {
63                fprintf(stderr, "Invalid Mac charset.\n");
64                exit(1);
65            }
66            break;
67
68	case 'p' :
69	    port = atoi( optarg );
70	    break;
71
72	default :
73	    Usage( av[ 0 ] );
74	}
75    }
76
77    if ( ac - optind != 1 ) {
78	Usage( av[ 0 ] );
79    }
80
81    /* Convert the name */
82    if ((size_t)(-1) == convert_string_allocate(CH_UNIX, chMac,
83                        av[optind], -1, &convname))
84        convname = av[optind];
85
86    /*
87     * Get the name. If Type or Obj aren't specified, error.
88     */
89    if ( nbp_name( convname, &Obj, &Type, &Zone ) || !Obj || !Type ) {
90	Usage( av[ 0 ] );
91    }
92
93    memset(&addr, 0, sizeof(addr));
94    memcpy(&addr.sat_addr, &ataddr, sizeof(addr.sat_addr));
95    if ((s = netddp_open(&addr, NULL)) < 0)
96	return( -1 );
97
98    if ( port ) {
99	addr.sat_port = port;
100    }
101
102    if ( nbp_rgstr( &addr, Obj, Type, Zone ) < 0 ) {
103	perror( "nbp_rgstr" );
104	fprintf( stderr, "Can't register %s:%s@%s\n", Obj, Type,
105		Zone ? Zone : "*" );
106	exit( 1 );
107    }
108    netddp_close(s);
109
110    return 0;
111}
112