pass URI for get request on command line

added usage info
This commit is contained in:
Olaf Bergmann committed 2010-07-05 01:37:58 +02:00
1 parent 05e46fd1bd
commit b464c9e47c
3 files changed
+84 -11

No files matched your search

+4 -3
View File
@@ -1,9 +1,10 @@
PROGRAM:=coap
VERSION:=0.01
SOURCES:= pdu.c net.c
OBJECTS:= $(patsubst %.c, %.o, $(SOURCES))
HEADERS:=coap.h debug.h pdu.h net.h
CFLAGS:=-g -Wall -std=c99 # -pedantic # FIXME: pdu.h contains GCC extensions
PROGRAM:=coap
VERSION:=0.01
DISTDIR=$(PROGRAM)-$(VERSION)
FILES:=main.c Makefile $(SOURCES) $(HEADERS)
LIB:=libcoap.a
@@ -24,7 +25,7 @@ $(LIB): $(OBJECTS)
# build main.o separately as c99 has no getaddrinfo, so CC will complain
main.o: main.c
$(CC) -g -Wall -c -o main.o main.c
$(CC) -DVERSION=\"$(VERSION)\" -g -Wall -c -o main.o main.c
clean:
@rm -f $(PROGRAM) main.o $(LIB) $(OBJECTS)
+4
View File
@@ -3,6 +3,10 @@
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
*/
#ifndef VERSION
# define VERSION "0.01"
#endif
#ifndef debug
# ifndef NDEBUG
# define debug(...) fprintf(stdout, __VA_ARGS__)
+76 -8
View File
@@ -7,6 +7,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -20,13 +21,13 @@ coap_pdu_t *
coap_new_get( const char *uri ) {
coap_pdu_t *pdu;
if ( !uri || ! ( pdu = coap_new_pdu() ) )
if ( ! ( pdu = coap_new_pdu() ) )
return NULL;
pdu->hdr->type = COAP_MESSAGE_CON;
pdu->hdr->code = COAP_REQUEST_GET;
if ( *uri ) {
if ( uri && *uri ) {
if ( *uri != '/' )
coap_add_option ( pdu, COAP_OPTION_URI_FULL, strlen( uri ), (unsigned char *)uri );
else {
@@ -40,7 +41,7 @@ coap_new_get( const char *uri ) {
}
void
send_request( coap_context_t *ctx, coap_pdu_t *pdu ) {
send_request( coap_context_t *ctx, coap_pdu_t *pdu, const char *server, unsigned short port ) {
struct addrinfo *res, *ainfo;
struct addrinfo hints;
int error;
@@ -50,8 +51,7 @@ send_request( coap_context_t *ctx, coap_pdu_t *pdu ) {
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET6;
/* error = getaddrinfo("ns.tzi.org", "", &hints, &res);*/
error = getaddrinfo("::1", "", &hints, &res);
error = getaddrinfo(server, "", &hints, &res);
if (error != 0) {
perror("getaddrinfo");
@@ -65,7 +65,7 @@ send_request( coap_context_t *ctx, coap_pdu_t *pdu ) {
memset(&dst, 0, sizeof dst );
dst.sin6_family = AF_INET6;
dst.sin6_port = htons( COAP_DEFAULT_PORT );
dst.sin6_port = htons( port );
memcpy( &dst.sin6_addr, &((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr, sizeof(dst.sin6_addr) );
coap_send_confirmed( ctx, &dst, pdu );
@@ -85,6 +85,65 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) {
#endif
}
void
split_uri( char *str, char **server, unsigned short *port, char **path ) {
char *p;
*port = COAP_DEFAULT_PORT;
if ( strncmp( str, "coap://", 7 ) == 0 ) {
*server = str + 7;
*path = *server;
/* note that we do not support URIs like coap://server?query or coap://server#fragment */
while ( **path && **path != '/' )
++*path;
if ( **path ) {
**path = '\0';
++*path;
}
/* split server address and port */
if ( **server == '[' ) { /* IPv6 address reference */
p = ++*server;
while ( *p && *p != ']' )
++p;
*p++ = '\0';
} else { /* IPv4 address or hostname */
p = *server;
while ( *p && *p != ':' )
++p;
}
if ( *p++ == ':' ) { /* handle port */
*port = 0;
while ( isdigit(*p) ) {
*port *= 10;
*port += *p - '0';
++p;
}
}
} else {
*path = str;
}
}
void
usage( const char *program, const char *version) {
const char *p;
p = strrchr( program, '/' );
if ( p )
program = ++p;
fprintf( stderr, "%s v%s -- a small CoAP implementation\n"
"(c) 2010 Olaf Bergmann <bergmann@tzi.org>\n\n"
"usage: %s URI\n"
"where URI can be an absolute or relative coap URI\n",
program, version, program );
}
int
main(int argc, char **argv) {
coap_context_t *ctx;
@@ -94,6 +153,8 @@ main(int argc, char **argv) {
time_t now;
coap_queue_t *nextpdu;
coap_pdu_t *pdu;
static char *server = NULL, *path = NULL;
unsigned short port = COAP_DEFAULT_PORT;
ctx = coap_new_context();
if ( !ctx )
@@ -101,10 +162,17 @@ main(int argc, char **argv) {
coap_register_message_handler( ctx, message_handler );
if (! (pdu = coap_new_get( COAP_DEFAULT_URI_WELLKNOWN ) ) )
if ( argc > 1 )
split_uri( argv[1], &server, &port, &path );
else {
usage( argv[0], VERSION );
exit( 1 );
}
if (! (pdu = coap_new_get( path ) ) )
return -1;
send_request( ctx, pdu );
send_request( ctx, pdu, server ? server : "::1", port );
while ( 1 ) {
FD_ZERO(&readfds);