diff --git a/Makefile b/Makefile index 2c81b800..80c23f63 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/debug.h b/debug.h index 8305c19a..ba91d744 100644 --- a/debug.h +++ b/debug.h @@ -3,6 +3,10 @@ * (c) 2010 Olaf Bergmann */ +#ifndef VERSION +# define VERSION "0.01" +#endif + #ifndef debug # ifndef NDEBUG # define debug(...) fprintf(stdout, __VA_ARGS__) diff --git a/main.c b/main.c index 1dbe492f..1a291eaa 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -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 \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);