Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / apps / gdp-name-xlate.c @ master

History | View | Annotate | Download (5.56 KB)

1 6d7ad27e Eric Allman
/* vim: set ai sw=4 sts=4 ts=4 : */
2
3
/*
4
**  GDP-NAME-XLATE --- show GDP name in various forms
5
**
6
**                Given an external name, shows the internal name in base64
7
**                and as a hex number.
8 055d3009 Eric Allman
**
9
**        ----- BEGIN LICENSE BLOCK -----
10
**        Applications for the Global Data Plane
11
**        From the Ubiquitous Swarm Lab, 490 Cory Hall, U.C. Berkeley.
12
**
13 c87dd166 Eric Allman
**        Copyright (c) 2015-2019, Regents of the University of California.
14 6bd5476b Eric Allman
**        All rights reserved.
15 055d3009 Eric Allman
**
16 6bd5476b Eric Allman
**        Permission is hereby granted, without written agreement and without
17
**        license or royalty fees, to use, copy, modify, and distribute this
18
**        software and its documentation for any purpose, provided that the above
19
**        copyright notice and the following two paragraphs appear in all copies
20
**        of this software.
21 055d3009 Eric Allman
**
22 6bd5476b Eric Allman
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
23
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
24
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
25
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 055d3009 Eric Allman
**
27 6bd5476b Eric Allman
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
28 055d3009 Eric Allman
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 6bd5476b Eric Allman
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
30
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
31
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
32
**        OR MODIFICATIONS.
33 055d3009 Eric Allman
**        ----- END LICENSE BLOCK -----
34 6d7ad27e Eric Allman
*/
35
36
#include <gdp/gdp.h>
37 0e2c49f6 Eric Allman
#include "../gdp/gdp_hongd.h"
38 6d7ad27e Eric Allman
39
#include <ep/ep_app.h>
40
#include <ep/ep_dbg.h>
41 dec37a7b Eric Allman
#include <ep/ep_hexdump.h>
42 6d7ad27e Eric Allman
43 11796a80 Eric Allman
#include <ctype.h>
44 6d7ad27e Eric Allman
#include <getopt.h>
45 11796a80 Eric Allman
#include <string.h>
46 6d7ad27e Eric Allman
#include <sysexits.h>
47
48 11796a80 Eric Allman
uint8_t        Xlations[] =
49
        {
50
                0,        1,        2,        3,        4,        5,        6,        7,
51
                8,        9,        0,        0,        0,        0,        0,        0,
52
                0,        10, 11, 12, 13, 14, 15, 0,
53
                0,        0,        0,        0,        0,        0,        0,        0,
54
                0,        0,        0,        0,        0,        0,        0,        0,
55
                0,        0,        0,        0,        0,        0,        0,        0,
56
                0,        10, 11, 12, 13, 14, 15, 0,
57
        };
58
59 b2d65f3d Eric Allman
EP_STAT
60 11796a80 Eric Allman
parse_hex(const char *s, gdp_name_t gdpiname)
61
{
62
        int i;
63
64
        if (strlen(s) != 64)
65 b2d65f3d Eric Allman
                return EP_STAT_ERROR;
66 11796a80 Eric Allman
67
        for (i = 0; i < 32; i++)
68
        {
69
                if (!isxdigit(s[0]) || !isxdigit(s[1]))
70 b2d65f3d Eric Allman
                        return EP_STAT_ERROR;
71
                gdpiname[i] = (Xlations[s[0] - '0']) << 4 | (Xlations[s[1] - '0']);
72 11796a80 Eric Allman
                s += 2;
73
        }
74
75 dec37a7b Eric Allman
        return GDP_STAT_OK_NAME_HEX;
76 11796a80 Eric Allman
}
77
78 6d7ad27e Eric Allman
void
79
usage(void)
80
{
81
        fprintf(stderr,
82 ac30c435 Eric Allman
                        "Usage: %s [-b] [-D dbgspec] [-f] [-h] [-o] gdp_name\n"
83 ac96b51b Eric Allman
                        "    -b  print printable base64 name\n"
84
                        "    -D  set debugging flags\n"
85 eb4547d2 Eric Allman
                        "    -f  show file name root\n"
86 ac30c435 Eric Allman
                        "    -h  print hexadecimal name\n"
87
                        "    -o  show old-style (sha256) name\n",
88 6d7ad27e Eric Allman
                        ep_app_getprogname());
89
        exit(EX_USAGE);
90
}
91
92
int
93
main(int argc, char **argv)
94
{
95
        int opt;
96 b2d65f3d Eric Allman
        EP_STAT estat;
97 6d7ad27e Eric Allman
        bool show_usage = false;
98
        gdp_name_t gdpiname;
99
        gdp_pname_t gdppname;
100 ac30c435 Eric Allman
        gdp_pname_t *gdp_psha;
101
        gdp_pname_t gdp_psha_buf;
102 ac96b51b Eric Allman
        bool show_b64 = false;
103
        bool show_hex = false;
104 eb4547d2 Eric Allman
        bool show_file_name = false;
105 ac30c435 Eric Allman
        bool show_old_form = false;
106 6d7ad27e Eric Allman
107 ac30c435 Eric Allman
        while ((opt = getopt(argc, argv, "bD:fho")) > 0)
108 6d7ad27e Eric Allman
        {
109
                switch (opt)
110
                {
111 ac96b51b Eric Allman
                        case 'b':
112
                                show_b64 = true;
113
                                break;
114
115 6d7ad27e Eric Allman
                        case 'D':
116
                                ep_dbg_set(optarg);
117
                                break;
118
119 eb4547d2 Eric Allman
                        case 'f':
120
                                show_file_name = true;
121
                                break;
122
123 ac96b51b Eric Allman
                        case 'h':
124
                                show_hex = true;
125
                                break;
126
127 ac30c435 Eric Allman
                        case 'o':
128
                                show_old_form = true;
129
                                break;
130
131 6d7ad27e Eric Allman
                        default:
132
                                show_usage = true;
133
                                break;
134
                }
135
        }
136
        argc -= optind;
137
        argv += optind;
138
139
        if (show_usage || argc != 1)
140
                usage();
141
142 59c6c7e0 Eric Allman
        // don't really need to initialize the GDP library for this, but
143
        // we do need the name lookup part of this --- cheat and use
144 b2d65f3d Eric Allman
        // internal interfaces.
145
        // DON'T TRY THIS AT HOME, KIDS!!!
146 866c2ca2 Eric Allman
        gdp_init_phase_0(NULL, 0);
147 0e2c49f6 Eric Allman
        _gdp_name_init(NULL);
148 b2d65f3d Eric Allman
149 ad583a18 Eric Allman
        // Do conversion of external name (in many possible forms) to the
150
        // internal 256-bit binary form of the GDPname.
151 c6ccd9ef Eric Allman
        const char *xname = argv[0];
152 b2d65f3d Eric Allman
        estat = parse_hex(argv[0], gdpiname);
153
        if (!EP_STAT_ISOK(estat))
154 dec37a7b Eric Allman
        {
155 b2d65f3d Eric Allman
                estat = gdp_name_parse(argv[0], gdpiname, &xname);
156 dec37a7b Eric Allman
        }
157 b2d65f3d Eric Allman
        if (EP_STAT_ISFAIL(estat))
158
        {
159
                ep_app_message(estat, "Cannot parse name \"%s\"", argv[0]);
160
                exit(EX_USAGE);
161
        }
162 ac30c435 Eric Allman
        if (xname == NULL)
163
                xname = argv[0];
164 6d7ad27e Eric Allman
        gdp_printable_name(gdpiname, gdppname);
165 ac30c435 Eric Allman
        if (EP_STAT_IS_SAME(estat, GDP_STAT_OK_NAME_PNAME))
166
        {
167
                gdp_psha = &gdppname;
168
        }
169
        else
170
        {
171
                gdp_name_t rawsha;
172
                ep_crypto_md_sha256(xname, strlen(xname), rawsha);
173
                gdp_printable_name(rawsha, gdp_psha_buf);
174
                gdp_psha = &gdp_psha_buf;
175
        }
176
177 ad583a18 Eric Allman
        // As a serious hack, see if you can look up the GDPname and
178
        // turn it back into an original, human-oriented name.  That's the
179
        // arbitrary string assigned by the creator of the object.
180
        // This violates layering in many ways.  If it's actually useful
181
        // for more than just developers, the guts should be moved into
182
        // the library.
183
184
        // look up in HONGD,
185
        char hbuf[200];
186
        extern EP_STAT _gdp_name_gethname(gdp_name_t, char[], size_t);
187
        estat = _gdp_name_gethname(gdpiname, hbuf, sizeof hbuf);
188
        EP_STAT_CHECK(estat, hbuf[0] = '\0');
189
190
        // Now we can print the internal names in various formats.
191
        // This is the easy part.
192 ac96b51b Eric Allman
        if (show_b64)
193
        {
194
                fprintf(stdout, "%s\n", gdppname);
195
        }
196
        else if (show_hex)
197
        {
198 b2d65f3d Eric Allman
                unsigned int i;
199 ac96b51b Eric Allman
                for (i = 0; i < sizeof gdpiname; i++)
200
                        fprintf(stdout, "%02x", gdpiname[i]);
201
                fprintf(stdout, "\n");
202
        }
203 eb4547d2 Eric Allman
        else if (show_file_name)
204
        {
205
                fprintf(stdout, "_%02x/%s\n", gdpiname[0], gdppname);
206
        }
207 ac30c435 Eric Allman
        else if (show_old_form)
208
        {
209
                fprintf(stdout, "%s\n", *gdp_psha);
210
        }
211 ac96b51b Eric Allman
        else
212
        {
213 dec37a7b Eric Allman
                char ebuf[64];
214 ad583a18 Eric Allman
                char *method = "HONGD";
215
                if (!EP_STAT_ISOK(estat))
216
                        method = "SHA256";
217 ac96b51b Eric Allman
                fprintf(stdout,
218 ad583a18 Eric Allman
                                "fqhn:      %s\n"
219
                                "method:    %s (%s)\n"
220 ac96b51b Eric Allman
                                "printable: %s\n"
221 ac30c435 Eric Allman
                                "old form:  %s\n"
222 ac96b51b Eric Allman
                                "hex:       ",
223 ad583a18 Eric Allman
                                xname,
224
                                method, ep_stat_tostr_terse(estat, ebuf, sizeof ebuf),
225
                                gdppname, *gdp_psha);
226 dec37a7b Eric Allman
                ep_hexdump(gdpiname, sizeof gdpiname, stdout, EP_HEXDUMP_TERSE, 0);
227 ad583a18 Eric Allman
                if (hbuf[0] != '\0')
228
                        fprintf(stdout,
229
                                "hongd:     %s\n",
230
                                hbuf);
231 ac96b51b Eric Allman
        }
232 6d7ad27e Eric Allman
        exit(EX_OK);
233
}