aboutsummaryrefslogtreecommitdiffstats
path: root/metis/ccnx/forwarder/metis/config/metis_CommandLineInterface.c
blob: 10857a29377d96425757458aaced9faf8283a9a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * NB: binds to all interfaces on the listen port, which might be a security issue.
 *
 * The CLI runs as an event managed listener.  The Api here creates, starts, stops, and destroys it.
 *
 * The CLI is a user interface to the programmatic interface in <code>metis_Configuration.h</code>
 *
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#include <LongBow/runtime.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_ArrayList.h>

#include "metis_CommandLineInterface.h"

struct metis_command_line_interface {
    MetisForwarder *metis;
    PARCEventSocket *listener;
    PARCArrayList *openSessions;

    uint16_t port;
};

typedef struct metis_cli_session {
    MetisCommandLineInterface *parentCli;
    MetisSocketType clientSocket;
    struct sockaddr *clientAddress;
    int clientAddressLength;
    PARCEventQueue *streamBuffer;
    bool doingTheRightThing;
} _MetisCommandLineInterface_Session;

struct metis_cli_command;
typedef struct metis_cli_command _MetisCommandLineInterface_Command;

struct metis_cli_command {
    char *text;
    char *helpDescription;
    void (*func)(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);
};

static void _metisCommandLineInterface_ListenerCallback(MetisSocketType client_socket,
                                                        struct sockaddr *client_addr, int socklen, void *user_data);

static _MetisCommandLineInterface_Session *metisCliSession_Create(MetisCommandLineInterface *cli, MetisSocketType client_socket, struct sockaddr *client_addr, int socklen);
static void _metisCliSession_Destory(_MetisCommandLineInterface_Session **cliSessionPtr);
static void _metisCliSession_ReadCallback(PARCEventQueue *event, PARCEventType type, void *cliSessionVoid);
static void _metisCliSession_EventCallback(PARCEventQueue *event, PARCEventQueueEventType what, void *cliSessionVoid);
static bool _metisCliSession_ProcessCommand(_MetisCommandLineInterface_Session *session, char *cmdline);
static void _metisCliSession_DisplayMotd(_MetisCommandLineInterface_Session *session);
static void _metisCliSession_DisplayPrompt(_MetisCommandLineInterface_Session *session);

// used by PARCArrayList
static void
_session_VoidDestroyer(void **cliSessionVoidPtr)
{
    _MetisCommandLineInterface_Session **cliSessionPtr = (_MetisCommandLineInterface_Session **) cliSessionVoidPtr;
    (*cliSessionPtr)->doingTheRightThing = true;
    _metisCliSession_Destory(cliSessionPtr);
}

// ====================================================================================

static void _cmd_Help(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);
static void _cmd_Show(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);
static void _cmd_Exit(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);
static void _cmd_Tunnel(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);
static void _cmd_Version(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params);

/**
 * @typedef _cliCommands
 * The commands, their short help, and their function pointer
 * @constant <#name#> <#description#>
 *  List must be terminated with a NULL entry
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static _MetisCommandLineInterface_Command _cliCommands[] = {
    { "exit",   "Ends the session",       _cmd_Exit    },
    { "help",   "Displays the help menu", _cmd_Help    },
    { "show",   "Displays state",         _cmd_Show    },
    { "tunnel", "manage tunnels",         _cmd_Tunnel  },
    { "ver",    "Forwarder version",      _cmd_Version },
    { NULL,     NULL,                     NULL         }
};

// ====================================================================================

MetisCommandLineInterface *
metisCommandLineInterface_Create(MetisForwarder *metis, uint16_t port)
{
    MetisCommandLineInterface *cli = parcMemory_AllocateAndClear(sizeof(MetisCommandLineInterface));
    assertNotNull(cli, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisCommandLineInterface));
    cli->port = port;
    cli->listener = NULL;
    cli->metis = metis;
    cli->openSessions = parcArrayList_Create(_session_VoidDestroyer);

    return cli;
}

void
metisCommandLineInterface_Start(MetisCommandLineInterface *cli)
{
    // listen address
    struct sockaddr_in6 addr6;
    memset(&addr6, 0, sizeof(addr6));
    addr6.sin6_family = PF_INET6;
    addr6.sin6_port = htons(cli->port);

    MetisDispatcher *dispatcher = metisForwarder_GetDispatcher(cli->metis);
    PARCEventSocket *listener = metisDispatcher_CreateListener(dispatcher, _metisCommandLineInterface_ListenerCallback, cli, -1, (struct sockaddr *) &addr6, sizeof(addr6));
    assertNotNull(listener, "Got null listener");

    cli->listener = listener;
}

void
metisCommandLineInterface_Destroy(MetisCommandLineInterface **cliPtr)
{
    assertNotNull(cliPtr, "Parameter must be non-null double pointer");
    assertNotNull(*cliPtr, "Parameter must dereference to non-null pointer");

    MetisCommandLineInterface *cli = *cliPtr;

    parcArrayList_Destroy(&cli->openSessions);

    if (cli->listener) {
        MetisDispatcher *dispatcher = metisForwarder_GetDispatcher(cli->metis);
        metisDispatcher_DestroyListener(dispatcher, &(cli->listener));
    }

    parcMemory_Deallocate((void **) &cli);
    *cliPtr = NULL;
}

/**
 * Creates a client-specific session
 *
 *   <#Discussion#>
 *
 * @param <#param1#>
 * @return <#return#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static _MetisCommandLineInterface_Session *
metisCliSession_Create(MetisCommandLineInterface *cli, MetisSocketType clientSocket, struct sockaddr *clientAddress, int clientAddressLength)
{
    _MetisCommandLineInterface_Session *session = parcMemory_AllocateAndClear(sizeof(_MetisCommandLineInterface_Session));
    assertNotNull(session, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(_MetisCommandLineInterface_Session));
    session->parentCli = cli;
    session->clientAddress = parcMemory_Allocate(clientAddressLength);
    assertNotNull(session->clientAddress, "parcMemory_Allocate(%d) returned NULL", clientAddressLength);
    session->clientAddressLength = clientAddressLength;
    session->clientSocket = clientSocket;

    memcpy(session->clientAddress, clientAddress, clientAddressLength);

    MetisDispatcher *dispatcher = metisForwarder_GetDispatcher(cli->metis);
    PARCEventScheduler *eventBase = metisDispatcher_GetEventScheduler(dispatcher);
    session->streamBuffer = parcEventQueue_Create(eventBase, clientSocket, PARCEventQueueOption_CloseOnFree | PARCEventQueueOption_DeferCallbacks);

    parcEventQueue_SetCallbacks(session->streamBuffer, _metisCliSession_ReadCallback, NULL, _metisCliSession_EventCallback, session);
    parcEventQueue_Enable(session->streamBuffer, PARCEventType_Read);

    return session;
}

/**
 * SHOULD ONLY BE CALLED FROM ARRAYLIST
 *
 *   Do not call this on your own!!  It should only be called when an
 *   item is removed from the cli->openSessions array list.
 *
 *   Will close the tcp session and free memory.
 *
 * @param <#param1#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
_metisCliSession_Destory(_MetisCommandLineInterface_Session **cliSessionPtr)
{
    assertNotNull(cliSessionPtr, "Parameter must be non-null double pointer");
    assertNotNull(*cliSessionPtr, "Parameter must dereference to non-null pointer");
    _MetisCommandLineInterface_Session *session = *cliSessionPtr;

    assertTrue(session->doingTheRightThing, "Ha! caught you!  You called Destroy outside the PARCArrayList");

    parcEventQueue_Destroy(&(session->streamBuffer));
    parcMemory_Deallocate((void **) &(session->clientAddress));
    parcMemory_Deallocate((void **) &session);
    *cliSessionPtr = NULL;
}

/**
 * Called on a new connection to the server socket
 *
 *   Will allocate a new _MetisCommandLineInterface_Session and put it in the
 *   server's PARCArrayList
 *
 * @param <#param1#>
 * @return <#return#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
_metisCommandLineInterface_ListenerCallback(MetisSocketType client_socket,
                                            struct sockaddr *client_addr, int socklen, void *user_data)
{
    MetisCommandLineInterface *cli = (MetisCommandLineInterface *) user_data;
    _MetisCommandLineInterface_Session *session = metisCliSession_Create(cli, client_socket, client_addr, socklen);
    parcArrayList_Add(cli->openSessions, session);

    _metisCliSession_DisplayMotd(session);
    _metisCliSession_DisplayPrompt(session);
}

static void
_metisCliSession_ReadCallback(PARCEventQueue *event, PARCEventType type, void *cliSessionVoid)
{
    assertTrue(type == PARCEventType_Read, "Illegal type: expected read event, got %d\n", type);
    _MetisCommandLineInterface_Session *session = (_MetisCommandLineInterface_Session *) cliSessionVoid;
    PARCEventBuffer *input = parcEventBuffer_GetQueueBufferInput(event);

    while (parcEventBuffer_GetLength(input) > 0) {
        size_t readLength = 0;
        char *cmdline = parcEventBuffer_ReadLine(input, &readLength);
        if (cmdline == NULL) {
            // we have run out of input, we're done
            parcEventBuffer_Destroy(&input);
            return;
        }

        // we have a whole command line
        bool success = _metisCliSession_ProcessCommand(session, cmdline);
        parcEventBuffer_FreeLine(input, &cmdline);

        if (!success) {
            // the session is dead
            parcEventBuffer_Destroy(&input);
            return;
        }

        _metisCliSession_DisplayPrompt(session);
    }
    parcEventBuffer_Destroy(&input);
}

static void
_metisCommandLineInterface_RemoveSession(MetisCommandLineInterface *cli, _MetisCommandLineInterface_Session *session)
{
    size_t length = parcArrayList_Size(cli->openSessions);
    for (size_t i = 0; i < length; i++) {
        _MetisCommandLineInterface_Session *x = parcArrayList_Get(cli->openSessions, i);
        if (x == session) {
            // removing from list will call the session destroyer
            parcArrayList_RemoveAndDestroyAtIndex(cli->openSessions, i);
            return;
        }
    }
    assertTrue(0, "Illegal state: did not find a session in openSessions %p", (void *) session);
}

static void
_metisCliSession_EventCallback(PARCEventQueue *event, PARCEventQueueEventType what, void *cliSessionVoid)
{
    _MetisCommandLineInterface_Session *session = (_MetisCommandLineInterface_Session *) cliSessionVoid;
    if (what & PARCEventQueueEventType_Error) {
        MetisCommandLineInterface *cli = session->parentCli;
        _metisCommandLineInterface_RemoveSession(cli, session);
    }
}

static void
_metisCliSession_DisplayMotd(_MetisCommandLineInterface_Session *session)
{
    parcEventQueue_Printf(session->streamBuffer, "Metis Forwarder CLI\n");
    parcEventQueue_Printf(session->streamBuffer, "Copyright (c) 2017 Cisco and/or its affiliates.\n\n");

    parcEventQueue_Flush(session->streamBuffer, PARCEventType_Write);
}

static void
_metisCliSession_DisplayPrompt(_MetisCommandLineInterface_Session *session)
{
    parcEventQueue_Printf(session->streamBuffer, "metis> ");
    parcEventQueue_Flush(session->streamBuffer, PARCEventType_Write);
}

/**
 * Process commands until there's not a whole line (upto CRLF)
 *
 *   <#Discussion#>
 *
 * @param <#param1#>
 * @return false if the session died, true if its still going
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static bool
_metisCliSession_ProcessCommand(_MetisCommandLineInterface_Session *session, char *cmdline)
{
    // parse out the first word.  The NULL goes in after a space or tab.
    // "cmd" will be the string prior to the NULL, and "cmdline" will be what's after the NULL.
    char *cmd = strsep(&cmdline, " \t");

    // there's a secret command for unit testing
    if (strcasecmp(cmd, "~~") == 0) {
        parcEventQueue_Printf(session->streamBuffer, "success: %s\n", cmdline);
        return true;
    }

    for (int i = 0; _cliCommands[i].text != NULL; i++) {
        if (strcasecmp(_cliCommands[i].text, cmd) == 0) {
            _cliCommands[i].func(session, &_cliCommands[i], cmdline);
            if (_cliCommands[i].func == _cmd_Exit) {
                return false;
            }
            return true;
        }
    }

    // could not find command, print the help menu
    parcEventQueue_Printf(session->streamBuffer, "Unrecognized command: %s, displaying help menu\n", cmdline);
    _cmd_Help(session, NULL, NULL);
    return true;
}

static void
_cmd_Help(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    for (int i = 0; _cliCommands[i].text != NULL; i++) {
        parcEventQueue_Printf(session->streamBuffer, "%-8s %s\n", _cliCommands[i].text, _cliCommands[i].helpDescription);
    }
}

static void
_cmd_Show(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    parcEventQueue_Printf(session->streamBuffer, "not implemented\n");
}

static void
_cmd_Tunnel(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    parcEventQueue_Printf(session->streamBuffer, "not implemented\n");
}

static void
_cmd_Exit(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    parcEventQueue_Printf(session->streamBuffer, "Exiting session, goodby\n\n");
    parcEventQueue_Flush(session->streamBuffer, PARCEventType_Write);
    _metisCommandLineInterface_RemoveSession(session->parentCli, session);
}

static void
_cmd_Version(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    PARCJSON *versionJson = metisConfiguration_GetVersion(metisForwarder_GetConfiguration(session->parentCli->metis));
    char *str = parcJSON_ToString(versionJson);
    parcEventQueue_Printf(session->streamBuffer, "%s", str);
    parcMemory_Deallocate((void **) &str);
    parcJSON_Release(&versionJson);
}