diff --git a/configure.in b/configure.in index fd5e335b..1e92a3a3 100644 --- a/configure.in +++ b/configure.in @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. AC_PREREQ([2.65]) -AC_INIT([libcoap], [0.08]) +AC_INIT([libcoap], [0.1.9]) AC_CONFIG_SRCDIR([coap.h]) # Checks for programs. diff --git a/examples/client.c b/examples/client.c index 2dadab01..713b1038 100644 --- a/examples/client.c +++ b/examples/client.c @@ -133,7 +133,7 @@ send_request( coap_context_t *ctx, coap_pdu_t *pdu, const char *server, unsign error = getaddrinfo(server, "", &hints, &res); if (error != 0) { - perror("getaddrinfo"); + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error)); exit(1); } @@ -337,7 +337,8 @@ join( coap_context_t *ctx, char *group_name ){ result = getaddrinfo("::", NULL, &hints, &reslocal); if ( result < 0 ) { - perror("join: cannot resolve link-local interface"); + fprintf(stderr, "join: cannot resolve link-local interface: %s\n", + gai_strerror(result)); goto finish; } @@ -358,7 +359,8 @@ join( coap_context_t *ctx, char *group_name ){ result = getaddrinfo(group_name, NULL, &hints, &resmulti); if ( result < 0 ) { - perror("join: cannot resolve multicast address"); + fprintf(stderr, "join: cannot resolve multicast address: %s\n", + gai_strerror(result)); goto finish; } @@ -593,6 +595,40 @@ cmdline_method(char *arg) { return i; /* note that we do not prevent illegal methods */ } +coap_context_t * +get_context(const char *node, const char *port) { + coap_context_t *ctx = NULL; + int s; + struct addrinfo hints; + struct addrinfo *result, *rp; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; + + s = getaddrinfo(node, port, &hints, &result); + if ( s != 0 ) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return NULL; + } + + /* iterate through results until success */ + for (rp = result; rp != NULL; rp = rp->ai_next) { + ctx = coap_new_context(rp->ai_addr, rp->ai_addrlen); + if (ctx) { + /* TODO: output address:port for successful binding */ + goto finish; + } + } + + fprintf(stderr, "no context available for interface '%s'\n", node); + + finish: + freeaddrinfo(result); + return ctx; +} + int main(int argc, char **argv) { coap_context_t *ctx; @@ -604,7 +640,8 @@ main(int argc, char **argv) { coap_pdu_t *pdu; static unsigned char *p; static str server; - unsigned short localport = COAP_DEFAULT_PORT, port = COAP_DEFAULT_PORT; + unsigned short port = COAP_DEFAULT_PORT; + char port_str[NI_MAXSERV] = "0"; int opt; char *group = NULL; @@ -620,7 +657,8 @@ main(int argc, char **argv) { group = optarg; break; case 'p' : - localport = atoi(optarg); + strncpy(port_str, optarg, NI_MAXSERV-1); + port_str[NI_MAXSERV - 1] = '\0'; break; case 'm' : method = cmdline_method(optarg); @@ -646,7 +684,7 @@ main(int argc, char **argv) { } } - ctx = coap_new_context( localport ); + ctx = get_context("::", port_str); if ( !ctx ) return -1; diff --git a/examples/server.c b/examples/server.c index be962d4c..3f13c41f 100644 --- a/examples/server.c +++ b/examples/server.c @@ -650,7 +650,8 @@ usage( const char *program, const char *version) { fprintf( stderr, "%s v%s -- a small CoAP implementation\n" "(c) 2010 Olaf Bergmann \n\n" - "usage: %s [-g group] [-p port]\n\n" + "usage: %s [-A address] [-g group] [-p port]\n\n" + "\t-A address\tinterface address to bind to\n" "\t-g group\tjoin the given multicast group\n" "\t-p port\t\tlisten on specified port\n", program, version, program ); @@ -1138,6 +1139,40 @@ init_resources(coap_context_t *ctx) { coap_add_resource(ctx, r); } +coap_context_t * +get_context(const char *node, const char *port) { + coap_context_t *ctx = NULL; + int s; + struct addrinfo hints; + struct addrinfo *result, *rp; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; + + s = getaddrinfo(node, port, &hints, &result); + if ( s != 0 ) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return NULL; + } + + /* iterate through results until success */ + for (rp = result; rp != NULL; rp = rp->ai_next) { + ctx = coap_new_context(rp->ai_addr, rp->ai_addrlen); + if (ctx) { + /* TODO: output address:port for successful binding */ + goto finish; + } + } + + fprintf(stderr, "no context available for interface '%s'\n", node); + + finish: + freeaddrinfo(result); + return ctx; +} + int main(int argc, char **argv) { coap_context_t *ctx; @@ -1146,17 +1181,23 @@ main(int argc, char **argv) { int result; time_t now; coap_queue_t *nextpdu; - unsigned short port = COAP_DEFAULT_PORT; + char addr_str[NI_MAXHOST] = "::"; + char port_str[NI_MAXSERV] = "61616"; int opt; char *group = NULL; - while ((opt = getopt(argc, argv, "g:p:")) != -1) { + while ((opt = getopt(argc, argv, "A:g:p:")) != -1) { switch (opt) { + case 'A' : + strncpy(addr_str, optarg, NI_MAXHOST-1); + addr_str[NI_MAXHOST - 1] = '\0'; + break; case 'g' : group = optarg; break; case 'p' : - port = atoi(optarg); + strncpy(port_str, optarg, NI_MAXSERV-1); + port_str[NI_MAXSERV - 1] = '\0'; break; default: usage( argv[0], PACKAGE_VERSION ); @@ -1164,7 +1205,7 @@ main(int argc, char **argv) { } } - ctx = coap_new_context( port ); + ctx = get_context(addr_str, port_str); if ( !ctx ) return -1; diff --git a/examples/tiny.c b/examples/tiny.c index d4be9b07..d8968b68 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -73,6 +73,40 @@ usage( const char *program ) { program, program ); } +coap_context_t * +get_context(const char *node, const char *port) { + coap_context_t *ctx = NULL; + int s; + struct addrinfo hints; + struct addrinfo *result, *rp; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; + + s = getaddrinfo(node, port, &hints, &result); + if ( s != 0 ) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return NULL; + } + + /* iterate through results until success */ + for (rp = result; rp != NULL; rp = rp->ai_next) { + ctx = coap_new_context(rp->ai_addr, rp->ai_addrlen); + if (ctx) { + /* TODO: output address:port for successful binding */ + goto finish; + } + } + + fprintf(stderr, "no context available for interface '%s'\n", node); + + finish: + freeaddrinfo(result); + return ctx; +} + int main(int argc, char **argv) { coap_context_t *ctx; @@ -86,9 +120,10 @@ main(int argc, char **argv) { exit( 1 ); } - ctx = coap_new_context(0); + ctx = get_context("::", NULL); if ( !ctx ) return -1; + id = rand() & INT_MAX; memset(&dst, 0, sizeof(struct sockaddr_in6 )); diff --git a/net.c b/net.c index c443f2f3..7132acf8 100644 --- a/net.c +++ b/net.c @@ -220,11 +220,15 @@ coap_pop_next( coap_context_t *context ) { } coap_context_t * -coap_new_context(in_port_t port) { +coap_new_context(const struct sockaddr *listen_addr, size_t addr_size) { coap_context_t *c = coap_malloc( sizeof( coap_context_t ) ); - struct sockaddr_in6 addr; time_t now; - int reuse = 1, need_port = 1; + int reuse = 1; + + if (!listen_addr) { + fprintf(stderr, "no listen address specified\n"); + return NULL; + } srand( getpid() ^ time(&now) ); @@ -235,7 +239,7 @@ coap_new_context(in_port_t port) { memset(c, 0, sizeof( coap_context_t ) ); - c->sockfd = socket(AF_INET6, SOCK_DGRAM, 0); + c->sockfd = socket(listen_addr->sa_family, SOCK_DGRAM, 0); if ( c->sockfd < 0 ) { perror("coap_new_context: socket"); goto onerror; @@ -244,25 +248,9 @@ coap_new_context(in_port_t port) { if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) perror("setsockopt SO_REUSEADDR"); - if ( port == 0 ) { - port = COAP_DEFAULT_PORT; - need_port = 0; - } - - memset(&addr, 0, sizeof addr ); - addr.sin6_family = AF_INET6; - addr.sin6_port = htons( port ); - memcpy( &addr.sin6_addr, &in6addr_any, sizeof addr.sin6_addr ); - - if ( bind (c->sockfd, (struct sockaddr *)&addr, sizeof addr) < 0 ) { - if (need_port) { - perror("coap_new_context: bind"); - goto onerror; - } - - do { - addr.sin6_port = htons( ++port ); - } while (bind (c->sockfd, (struct sockaddr *)&addr, sizeof addr) < 0); + if ( bind (c->sockfd, listen_addr, addr_size) < 0 ) { + perror("coap_new_context: bind"); + goto onerror; } return c; diff --git a/net.h b/net.h index bd03fb33..a7e9ce23 100644 --- a/net.h +++ b/net.h @@ -82,9 +82,8 @@ coap_queue_t *coap_peek_next( coap_context_t *context ); /* Returns the next pdu to send and removes it from the sendqeue. */ coap_queue_t *coap_pop_next( coap_context_t *context ); -/* Creates a new coap_context_t object that will hold the CoAP stack status. If port is - * set to zero, the next free port will be used as server port, starting with COAP_DEFAULT_PORT. */ -coap_context_t *coap_new_context(in_port_t port); +/* Creates a new coap_context_t object that will hold the CoAP stack status. */ +coap_context_t *coap_new_context(const struct sockaddr *listen_addr, size_t addr_size); /* CoAP stack context must be released with coap_free_context() */ void coap_free_context( coap_context_t *context );