diff --git a/Makefile b/Makefile index 559f599..c0a0a68 100644 --- a/Makefile +++ b/Makefile @@ -328,14 +328,12 @@ static_lib: picotcp picotcp_driver libzt_socket_layer utilities $(ZTO_OBJS) mv ext/picotcp/build/lib/*.o obj mv ext/picotcp/build/modules/*.o obj $(ARTOOL) $(ARFLAGS) -o $(STATIC_LIB) obj/*.o - @date +"Build script finished on %F %T" endif ifeq ($(STACK_LWIP),1) static_lib: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS) @mkdir -p $(BUILD) obj mv *.o obj $(ARTOOL) $(ARFLAGS) -o $(STATIC_LIB) obj/*.o - @date +"Build script finished on %F %T" endif # for layer-2 only (this will omit all userspace network stack code) ifeq ($(NO_STACK),1) @@ -343,7 +341,6 @@ static_lib: libzt_socket_layer utilities $(ZTO_OBJS) @mkdir -p $(BUILD) obj mv *.o obj $(ARTOOL) $(ARFLAGS) -o $(STATIC_LIB) obj/*.o - @date +"Build script finished on %F %T" endif ############################################################################## @@ -379,28 +376,24 @@ tests: selftest nativetest ztproxy ZT_UTILS:=zto/node/Utils.cpp -Izto/node +sample: + $(CXX) $(CXXFLAGS) -D__SELFTEST__ $(STACK_DRIVER_DEFS) $(LIBZT_DEFS) \ + $(SANFLAGS) $(LIBZT_INCLUDES) $(ZT_INCLUDES) $(ZT_UTILS) test/sample.cpp -o \ + $(BUILD)/sample -L$(BUILD) -lzt selftest: $(CXX) $(CXXFLAGS) -D__SELFTEST__ $(STACK_DRIVER_DEFS) $(LIBZT_DEFS) \ $(SANFLAGS) $(LIBZT_INCLUDES) $(ZT_INCLUDES) $(ZT_UTILS) test/selftest.cpp -o \ $(BUILD)/selftest -L$(BUILD) -lzt -lpthread - @./check.sh $(BUILD)/selftest - @date +"Build script finished on %F %T" nativetest: $(CXX) $(CXXFLAGS) -D__NATIVETEST__ $(STACK_DRIVER_DEFS) $(SANFLAGS) \ $(LIBZT_INCLUDES) $(ZT_INCLUDES) test/selftest.cpp -o $(BUILD)/nativetest - @./check.sh $(BUILD)/nativetest - @date +"Build script finished on %F %T" ztproxy: $(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) $(ZT_INCLUDES) \ examples/apps/ztproxy/ztproxy.cpp -o $(BUILD)/ztproxy $< -L$(BUILD) -lzt -lpthread $(WINDEFS) - @./check.sh $(BUILD)/ztproxy - @date +"Build script finished on %F %T" intercept: $(CXX) $(CXXFLAGS) $(SANFLAGS) $(STACK_DRIVER_DEFS) $(LIBZT_INCLUDES) \ $(ZT_INCLUDES) examples/intercept/intercept.cpp -D_GNU_SOURCE \ -shared -o $(BUILD)/intercept.so $< -ldl - @./check.sh $(BUILD)/intercept.so - @date +"Build script finished on %F %T" dlltest: $(CXX) $(CXXFLAGS) diff --git a/README.md b/README.md index 0b11c86..cfba6cd 100644 --- a/README.md +++ b/README.md @@ -14,46 +14,49 @@ Pre-Built Binaries Here: [zerotier.com/download.shtml](https://zerotier.com/down *** -### Example +### C++ Example ``` +#include +#include +#include + #include "libzt.h" -char *str = "welcome to the machine"; -char *nwid = "c7cd7c9e1b0f52a2"; // network to join -char *path = "zt1"; // path where this node's keys and configs will be stored -char *ip = "10.8.8.42"; // remote address -int port = 8080; // remote port +int main() +{ + char *str = "welcome to the machine"; + char *nwid = "c7cd7c981b0f52a2"; // network + char *path = "config_path"; // where this instance's keys and configs are stored + char *ip = "10.8.8.42"; // remote address + int port = 8080; // remote port -struct sockaddr_in addr; -addr.sin_family = AF_INET; -addr.sin_addr.s_addr = inet_addr(ip); -addr.sin_port = hton(port); + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(ip); + addr.sin_port = htons(port); -zts_startjoin(path, nwid); + zts_startjoin(path, nwid); -int fd, err = 0; -if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { - printf("error creating socket\n"); - return -1; + int fd, err = 0; + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { + printf("error creating socket\n"); + } + if ((err = zts_connect(fd, (const struct sockaddr *)&addr, sizeof(addr))) < 0) { + printf("error connecting to remote host\n"); + } + if ((err = zts_write(fd, str, strlen(str))) < 0) { + printf("error writing to socket\n"); + } + if ((err = zts_close(fd)) < 0) { + printf("error closing socket\n"); + } + zts_stop(); + return 0; } -if ((err = zts_connect(fd, (const struct sockaddr *)addr, sizeof(addr))) < 0) { - printf("error connecting to remote host\n"); - return -1; -} -if ((err = zts_write(fd, str, strlen(str))) < 0) { - printf("error writing to socket\n"); - return -1; -} -if ((err = zts_close(fd)) < 0) { - printf("error closing socket\n"); - return -1; -} - -zts_stop(); ``` -Bindings for various [languages](examples) +Other bindings for popular [languages](examples) For an example using only the [Virtual Layer 2](https://www.zerotier.com/manual.shtml#2_2?pk_campaign=github_libzt), see [test/layer2.cpp](test/layer2.cpp) diff --git a/test/sample.cpp b/test/sample.cpp new file mode 100644 index 0000000..5aebc34 --- /dev/null +++ b/test/sample.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +#include "libzt.h" + +int main() +{ + char *str = "welcome to the machine"; + char *nwid = "c7cd7c9e1b0f52a2"; // network + char *path = "config_path"; // where this instance's keys and configs are stored + char *ip = "10.8.8.42"; // remote address + int port = 8080; // remote port + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(ip); + addr.sin_port = htons(port); + + zts_startjoin(path, nwid); + + int fd, err = 0; + if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) { + printf("error creating socket\n"); + } + if ((err = zts_connect(fd, (const struct sockaddr *)&addr, sizeof(addr))) < 0) { + printf("error connecting to remote host\n"); + } + if ((err = zts_write(fd, str, strlen(str))) < 0) { + printf("error writing to socket\n"); + } + if ((err = zts_close(fd)) < 0) { + printf("error closing socket\n"); + } + zts_stop(); + return 0; +} \ No newline at end of file