Skip to content

Commit

Permalink
Add visual studio 2015 solution and project files for building libcoa…
Browse files Browse the repository at this point in the history
…p on windows.

Merge changes from PR obgm#47 related to windows platform support.
Add full windows platform support including correct error checking and working regression tests and examples.
Remove need to build applications using libcoap with -DWITH_POSIX and to include coap_config.h. This applies to the examples as well.

# Conflicts:
#	examples/client.c
#	examples/coap-rd.c
#	examples/coap-server.c
#	include/coap/address.h
#	include/coap/coap_io.h
#	include/coap/net.h
#	include/coap/prng.h
#	src/address.c
#	src/block.c
#	src/net.c
#	src/pdu.c
#	src/platform/posix/coap_io.c
#	src/resource.c
  • Loading branch information
jcmichelou committed May 25, 2017
1 parent 2586304 commit 9b1d796
Show file tree
Hide file tree
Showing 50 changed files with 1,982 additions and 207 deletions.
111 changes: 111 additions & 0 deletions coap_config.h.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef _COAP_CONFIG_H_
#define _COAP_CONFIG_H_

#if defined(_WIN32)

/* Define to 1 if you have <ws2tcpip.h> header file. */
#define HAVE_WS2TCPIP_H 1

/* Define to 1 if you have <winsock2.h> header file. */
#define HAVE_WINSOCK2_H 1

/* Define to 1 if you have the <assert.h> header file. */
#define HAVE_ASSERT_H 1

/* Define to 1 if you have the `getaddrinfo' function. */
#define HAVE_GETADDRINFO 1

/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1

/* Define to 1 if you have the <limits.h> header file. */
#define HAVE_LIMITS_H 1

/* Define to 1 if you have the `malloc' function. */
#define HAVE_MALLOC 1

/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1

/* Define to 1 if you have the `memset' function. */
#define HAVE_MEMSET 1

/* Define to 1 if you have the `select' function. */
#define HAVE_SELECT 1

/* Define to 1 if you have the `socket' function. */
#define HAVE_SOCKET 1

/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1

/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1

/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1

/* Define to 1 if you have the `strnlen' function. */
#define HAVE_STRNLEN 1

/* Define to 1 if you have the `strrchr' function. */
#define HAVE_STRRCHR 1

/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1

/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1

/* Define to 1 if you have the <time.h> header file. */
#define HAVE_TIME_H 1

#if defined(_MSC_VER) && (_MSC_VER < 1900) && !defined(snprintf)
#define snprintf _snprintf
#endif

/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "[email protected]"

/* Define to the full name of this package. */
#define PACKAGE_NAME "libcoap"

/* Define to the full name and version of this package. */
#define PACKAGE_STRING "libcoap 4.1.2"

/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "libcoap"

/* Define to the home page for this package. */
#define PACKAGE_URL "https://libcoap.net/"

/* Define to the version of this package. */
#define PACKAGE_VERSION "4.1.2"

/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
significant byte first (like Motorola and SPARC, unlike Intel). */
#if defined AC_APPLE_UNIVERSAL_BUILD
# if defined __BIG_ENDIAN__
# define WORDS_BIGENDIAN 1
# endif
#else
# ifndef WORDS_BIGENDIAN
/* # undef WORDS_BIGENDIAN */
# endif
#endif

/* Define this to 1 for ancillary data on MacOS */
/* #undef __APPLE_USE_RFC_3542 */

/* Define to `unsigned int' if <sys/types.h> does not define. */
/* #undef size_t */

/* Define to `int' if <sys/types.h> does not define. */
/* #undef ssize_t */

#endif

#endif /* _COAP_CONFIG_H_ */
21 changes: 14 additions & 7 deletions examples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
* use.
*/

#include "coap_config.h"

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#include "getopt.c"
#else
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

#include "coap.h"
#include "coap_dtls.h"
Expand Down Expand Up @@ -67,7 +70,9 @@ unsigned int obs_seconds = 30; /* default observe time */
coap_tick_t obs_wait = 0; /* timeout for current subscription */
int observe = 0; /* set to 1 if resource is being observed */

#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif

#ifdef __GNUC__
#define UNUSED_PARAM __attribute__ ((unused))
Expand Down Expand Up @@ -149,7 +154,7 @@ coap_new_request(coap_context_t *ctx,
pdu->hdr->id = coap_new_message_id(ctx);
pdu->hdr->code = m;

pdu->hdr->token_length = the_token.length;
pdu->hdr->token_length = (uint16_t)the_token.length;
if ( !coap_add_token(pdu, the_token.length, the_token.s)) {
debug("cannot add token to request\n");
}
Expand Down Expand Up @@ -1161,11 +1166,12 @@ main(int argc, char **argv) {
log_level = strtol(optarg, NULL, 10);
break;
default:
usage( argv[0], PACKAGE_VERSION );
usage( argv[0], LIBCOAP_PACKAGE_VERSION );
exit( 1 );
}
}

coap_startup();
coap_dtls_set_log_level(log_level);
coap_set_log_level(log_level);

Expand All @@ -1176,7 +1182,7 @@ main(int argc, char **argv) {
exit(1);
}
} else {
usage( argv[0], PACKAGE_VERSION );
usage( argv[0], LIBCOAP_PACKAGE_VERSION );
exit( 1 );
}

Expand Down Expand Up @@ -1322,6 +1328,7 @@ main(int argc, char **argv) {

coap_delete_list(optlist);
coap_free_context( ctx );
coap_cleanup();

return result;
}
38 changes: 27 additions & 11 deletions examples/coap-rd.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#include "getopt.c"
#else
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#endif

#include "coap_config.h"
#include "utlist.h"
#include "resource.h"
#include "coap.h"
Expand Down Expand Up @@ -613,8 +619,8 @@ join(coap_context_t *ctx, char *group_name) {
hints.ai_socktype = SOCK_DGRAM;

result = getaddrinfo("::", NULL, &hints, &reslocal);
if ( result < 0 ) {
perror("join: cannot resolve link-local interface");
if ( result != 0 ) {
fprintf( stderr, "join: cannot resolve link-local interface: %s\n", gai_strerror( result ) );
goto finish;
}

Expand All @@ -634,8 +640,8 @@ join(coap_context_t *ctx, char *group_name) {
/* resolve the multicast group address */
result = getaddrinfo(group_name, NULL, &hints, &resmulti);

if ( result < 0 ) {
perror("join: cannot resolve multicast address");
if ( result != 0 ) {
fprintf( stderr, "join: cannot resolve multicast address: %s\n", gai_strerror( result ) );
goto finish;
}

Expand All @@ -651,8 +657,15 @@ join(coap_context_t *ctx, char *group_name) {
result = setsockopt(ctx->endpoint->handle.fd,
IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char *)&mreq, sizeof(mreq) );
if ( result < 0 )
perror("join: setsockopt");
if ( result < 0 ) {
#ifdef _WIN32
char *szErrorMsg = NULL;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)WSAGetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&szErrorMsg, 0, NULL );
fprintf( stderr, "join: setsockopt: %s\n", szErrorMsg );
LocalFree( szErrorMsg );
#else
perror("join: setsockopt");
#endif
} else {
result = -1;
}
Expand Down Expand Up @@ -690,11 +703,12 @@ main(int argc, char **argv) {
log_level = strtol(optarg, NULL, 10);
break;
default:
usage( argv[0], PACKAGE_VERSION );
usage( argv[0], LIBCOAP_PACKAGE_VERSION );
exit( 1 );
}
}

coap_startup();
coap_set_log_level(log_level);

ctx = get_context(addr_str, port_str);
Expand All @@ -710,5 +724,7 @@ main(int argc, char **argv) {

coap_free_context(ctx);

coap_cleanup();

return 0;
}
36 changes: 25 additions & 11 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#include "getopt.c"
#else
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#endif

#include "coap_config.h"
#include "resource.h"
#include "coap.h"
#include "coap_dtls.h"
Expand Down Expand Up @@ -437,7 +441,7 @@ join(coap_context_t *ctx, char *group_name){
hints.ai_socktype = SOCK_DGRAM;

result = getaddrinfo("::", NULL, &hints, &reslocal);
if (result < 0) {
if (result != 0) {
fprintf(stderr, "join: cannot resolve link-local interface: %s\n",
gai_strerror(result));
goto finish;
Expand All @@ -459,7 +463,7 @@ join(coap_context_t *ctx, char *group_name){
/* resolve the multicast group address */
result = getaddrinfo(group_name, NULL, &hints, &resmulti);

if (result < 0) {
if (result != 0) {
fprintf(stderr, "join: cannot resolve multicast address: %s\n",
gai_strerror(result));
goto finish;
Expand All @@ -474,10 +478,18 @@ join(coap_context_t *ctx, char *group_name){
}

if (ctx->endpoint) {
result = setsockopt(ctx->endpoint->handle.fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
result = setsockopt(ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char *)&mreq, sizeof(mreq));
if (result < 0)
if (result < 0) {
#ifdef _WIN32
char *szErrorMsg = NULL;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)WSAGetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&szErrorMsg, 0, NULL );
fprintf( stderr, "join: setsockopt: %s\n", szErrorMsg );
LocalFree( szErrorMsg );
#else
perror("join: setsockopt");
#endif
}
} else {
result = -1;
}
Expand Down Expand Up @@ -519,11 +531,12 @@ main(int argc, char **argv) {
log_level = strtol(optarg, NULL, 10);
break;
default:
usage( argv[0], PACKAGE_VERSION );
usage( argv[0], LIBCOAP_PACKAGE_VERSION );
exit( 1 );
}
}

coap_startup();
coap_set_log_level(log_level);

ctx = get_context(addr_str, port_str);
Expand Down Expand Up @@ -565,6 +578,7 @@ main(int argc, char **argv) {
}

coap_free_context(ctx);
coap_cleanup();

return 0;
}
1 change: 1 addition & 0 deletions examples/coap_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdio.h>
#include <string.h>

#include "libcoap.h"
#include "debug.h"
#include "mem.h"
#include "coap_list.h"
Expand Down
Loading

0 comments on commit 9b1d796

Please sign in to comment.