Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.56 KB)

1
/* 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
**
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
**        Copyright (c) 2015-2019, Regents of the University of California.
14
**        All rights reserved.
15
**
16
**        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
**
22
**        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
**
27
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
28
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29
**        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
**        ----- END LICENSE BLOCK -----
34
*/
35

    
36
#include <gdp/gdp.h>
37
#include "../gdp/gdp_hongd.h"
38

    
39
#include <ep/ep_app.h>
40
#include <ep/ep_dbg.h>
41
#include <ep/ep_hexdump.h>
42

    
43
#include <ctype.h>
44
#include <getopt.h>
45
#include <string.h>
46
#include <sysexits.h>
47

    
48
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
EP_STAT
60
parse_hex(const char *s, gdp_name_t gdpiname)
61
{
62
        int i;
63

    
64
        if (strlen(s) != 64)
65
                return EP_STAT_ERROR;
66

    
67
        for (i = 0; i < 32; i++)
68
        {
69
                if (!isxdigit(s[0]) || !isxdigit(s[1]))
70
                        return EP_STAT_ERROR;
71
                gdpiname[i] = (Xlations[s[0] - '0']) << 4 | (Xlations[s[1] - '0']);
72
                s += 2;
73
        }
74

    
75
        return GDP_STAT_OK_NAME_HEX;
76
}
77

    
78
void
79
usage(void)
80
{
81
        fprintf(stderr,
82
                        "Usage: %s [-b] [-D dbgspec] [-f] [-h] [-o] gdp_name\n"
83
                        "    -b  print printable base64 name\n"
84
                        "    -D  set debugging flags\n"
85
                        "    -f  show file name root\n"
86
                        "    -h  print hexadecimal name\n"
87
                        "    -o  show old-style (sha256) name\n",
88
                        ep_app_getprogname());
89
        exit(EX_USAGE);
90
}
91

    
92
int
93
main(int argc, char **argv)
94
{
95
        int opt;
96
        EP_STAT estat;
97
        bool show_usage = false;
98
        gdp_name_t gdpiname;
99
        gdp_pname_t gdppname;
100
        gdp_pname_t *gdp_psha;
101
        gdp_pname_t gdp_psha_buf;
102
        bool show_b64 = false;
103
        bool show_hex = false;
104
        bool show_file_name = false;
105
        bool show_old_form = false;
106

    
107
        while ((opt = getopt(argc, argv, "bD:fho")) > 0)
108
        {
109
                switch (opt)
110
                {
111
                        case 'b':
112
                                show_b64 = true;
113
                                break;
114

    
115
                        case 'D':
116
                                ep_dbg_set(optarg);
117
                                break;
118

    
119
                        case 'f':
120
                                show_file_name = true;
121
                                break;
122

    
123
                        case 'h':
124
                                show_hex = true;
125
                                break;
126

    
127
                        case 'o':
128
                                show_old_form = true;
129
                                break;
130

    
131
                        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
        // 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
        // internal interfaces.
145
        // DON'T TRY THIS AT HOME, KIDS!!!
146
        gdp_init_phase_0(NULL, 0);
147
        _gdp_name_init(NULL);
148

    
149
        // Do conversion of external name (in many possible forms) to the
150
        // internal 256-bit binary form of the GDPname.
151
        const char *xname = argv[0];
152
        estat = parse_hex(argv[0], gdpiname);
153
        if (!EP_STAT_ISOK(estat))
154
        {
155
                estat = gdp_name_parse(argv[0], gdpiname, &xname);
156
        }
157
        if (EP_STAT_ISFAIL(estat))
158
        {
159
                ep_app_message(estat, "Cannot parse name \"%s\"", argv[0]);
160
                exit(EX_USAGE);
161
        }
162
        if (xname == NULL)
163
                xname = argv[0];
164
        gdp_printable_name(gdpiname, gdppname);
165
        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
        // 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
        if (show_b64)
193
        {
194
                fprintf(stdout, "%s\n", gdppname);
195
        }
196
        else if (show_hex)
197
        {
198
                unsigned int i;
199
                for (i = 0; i < sizeof gdpiname; i++)
200
                        fprintf(stdout, "%02x", gdpiname[i]);
201
                fprintf(stdout, "\n");
202
        }
203
        else if (show_file_name)
204
        {
205
                fprintf(stdout, "_%02x/%s\n", gdpiname[0], gdppname);
206
        }
207
        else if (show_old_form)
208
        {
209
                fprintf(stdout, "%s\n", *gdp_psha);
210
        }
211
        else
212
        {
213
                char ebuf[64];
214
                char *method = "HONGD";
215
                if (!EP_STAT_ISOK(estat))
216
                        method = "SHA256";
217
                fprintf(stdout,
218
                                "fqhn:      %s\n"
219
                                "method:    %s (%s)\n"
220
                                "printable: %s\n"
221
                                "old form:  %s\n"
222
                                "hex:       ",
223
                                xname,
224
                                method, ep_stat_tostr_terse(estat, ebuf, sizeof ebuf),
225
                                gdppname, *gdp_psha);
226
                ep_hexdump(gdpiname, sizeof gdpiname, stdout, EP_HEXDUMP_TERSE, 0);
227
                if (hbuf[0] != '\0')
228
                        fprintf(stdout,
229
                                "hongd:     %s\n",
230
                                hbuf);
231
        }
232
        exit(EX_OK);
233
}