client.c: added command line options for DTLS credentials

The new option -u allows setting the user name (aka PSK identity) and
-k is used to specify the corresponding secret (the PSK).
This commit is contained in:
Olaf Bergmann committed 2016-03-22 14:48:28 +01:00
1 parent 69f3d97dce
commit ec89b066b6
1 file changed
+62 -7
+62 -7
View File
@@ -27,6 +27,10 @@
#include "coap_dtls.h"
#include "coap_list.h"
#define MAX_USER 128 /* Maximum length of a user name (i.e., PSK
* identity) in bytes. */
#define MAX_KEY 64 /* Maximum length of a key (i.e., PSK) in bytes. */
void dtls_set_log_level(int);
int flags = 0;
@@ -542,7 +546,8 @@ usage( const char *program, const char *version) {
"(c) 2010-2015 Olaf Bergmann <bergmann@tzi.org>\n\n"
"usage: %s [-A type...] [-t type] [-b [num,]size] [-B seconds] [-e text]\n"
"\t\t[-m method] [-N] [-o file] [-P addr[:port]] [-p port]\n"
"\t\t[-s duration] [-O num,text] [-T string] [-v num] [-a addr] URI\n\n"
"\t\t[-s duration] [-O num,text] [-T string] [-v num] [-a addr] \n\n"
"\t\t[-u user] [-k key] URI\n\n"
"\tURI can be an absolute or relative coap URI,\n"
"\t-a addr\tthe local interface address to use\n"
"\t-A type...\taccepted media types as comma-separated list of\n"
@@ -557,11 +562,15 @@ usage( const char *program, const char *version) {
"\t-e text\t\tinclude text as payload (use percent-encoding for\n"
"\t\t\tnon-ASCII characters)\n"
"\t-f file\t\tfile to send with PUT/POST (use '-' for STDIN)\n"
"\t-k key\t\tPre-shared key for the specified user. This argument\n"
"\t \t\trequires DTLS with PSK to be available.\n"
"\t-m method\trequest method (get|put|post|delete), default is 'get'\n"
"\t-N\t\tsend NON-confirmable message\n"
"\t-o file\t\toutput received data to this file (use '-' for STDOUT)\n"
"\t-p port\t\tlisten on specified port\n"
"\t-s duration\tsubscribe for given duration [s]\n"
"\t-u user\t\tuser identity for pre-shared key mode. This argument\n"
"\t \t\trequires DTLS with PSK to be available.\n"
"\t-v num\t\tverbosity level (default: 3)\n"
"\t-O num,text\tadd option num with contents text to request\n"
"\t-P addr[:port]\tuse proxy (automatically adds Proxy-Uri option to\n"
@@ -987,6 +996,26 @@ cmdline_method(char *arg) {
return i; /* note that we do not prevent illegal methods */
}
static ssize_t
cmdline_read_user(char *arg, unsigned char *buf, size_t maxlen) {
size_t len = strnlen(arg, maxlen);
if (len) {
memcpy(buf, arg, len);
return len;
}
return -1;
}
static ssize_t
cmdline_read_key(char *arg, unsigned char *buf, size_t maxlen) {
size_t len = strnlen(arg, maxlen);
if (len) {
memcpy(buf, arg, len);
return len;
}
return -1;
}
static coap_context_t *
get_context(const char *node, const char *port, int secure) {
coap_context_t *ctx = NULL;
@@ -1049,7 +1078,7 @@ main(int argc, char **argv) {
coap_address_t dst;
static char addr[INET6_ADDRSTRLEN];
void *addrptr = NULL;
int result;
int result = -1;
coap_pdu_t *pdu;
static str server;
unsigned short port = COAP_DEFAULT_PORT;
@@ -1058,8 +1087,10 @@ main(int argc, char **argv) {
int opt, res;
coap_log_t log_level = LOG_WARNING;
coap_tid_t tid = COAP_INVALID_TID;
unsigned char user[MAX_USER], key[MAX_KEY];
ssize_t user_length = 0, key_length = 0;
while ((opt = getopt(argc, argv, "Na:b:e:f:g:m:p:s:t:o:v:A:B:O:P:T:")) != -1) {
while ((opt = getopt(argc, argv, "Na:b:e:f:g:k:m:p:s:t:o:v:A:B:O:P:T:u:")) != -1) {
switch (opt) {
case 'a' :
strncpy(node_str, optarg, NI_MAXHOST-1);
@@ -1079,6 +1110,9 @@ main(int argc, char **argv) {
if (!cmdline_input_from_file(optarg,&payload))
payload.length = 0;
break;
case 'k' :
key_length = cmdline_read_key(optarg, key, MAX_KEY);
break;
case 'p' :
strncpy(port_str, optarg, NI_MAXSERV-1);
port_str[NI_MAXSERV - 1] = '\0';
@@ -1122,6 +1156,9 @@ main(int argc, char **argv) {
case 'T' :
cmdline_token(optarg);
break;
case 'u' :
user_length = cmdline_read_user(optarg, user, MAX_USER);
break;
case 'v' :
log_level = strtol(optarg, NULL, 10);
break;
@@ -1187,7 +1224,21 @@ main(int argc, char **argv) {
if (!ctx) {
coap_log(LOG_EMERG, "cannot create context\n");
return -1;
goto finish;
}
if ((user_length < 0) || (key_length < 0)) {
coap_log(LOG_CRIT, "Invalid user name or key specified\n");
goto finish;
}
if (user_length > 0) {
coap_keystore_item_t *psk;
psk = coap_keystore_new_psk(NULL, 0, user, (size_t)user_length,
key, (size_t)key_length, 0);
if (!psk || !coap_keystore_store_item(ctx->keystore, psk, NULL)) {
coap_log(LOG_WARNING, "cannot store key\n");
}
}
coap_register_option(ctx, COAP_OPTION_BLOCK2);
@@ -1211,8 +1262,9 @@ main(int argc, char **argv) {
if (flags & FLAGS_BLOCK)
set_blocksize();
if (! (pdu = coap_new_request(ctx, method, &optlist, payload.s, payload.length)))
return -1;
if (! (pdu = coap_new_request(ctx, method, &optlist, payload.s, payload.length))) {
goto finish;
}
#ifndef NDEBUG
if (LOG_DEBUG <= coap_get_log_level()) {
@@ -1260,10 +1312,13 @@ main(int argc, char **argv) {
}
}
result = 0;
finish:
close_output();
coap_delete_list(optlist);
coap_free_context( ctx );
return 0;
return result;
}