diff --git a/examples/python/example.py b/examples/python/example.py index ccecf3d..58c3b97 100644 --- a/examples/python/example.py +++ b/examples/python/example.py @@ -117,18 +117,14 @@ def main(): conn, addr = serv.accept() print("Accepted connection from: ", addr) while True: - print("recv:") data = conn.recv(4096) if data: - print("data = ", data) - # print(type(b'what')) - # exit(0) + print("recv: ", data) if not data: break - print("send:") - # bytes(data, 'ascii') + b'\x00' - n_bytes = conn.send(data) # echo back to the server - print("sent " + str(n_bytes) + " byte(s)") + print("send: ", data) + sent_bytes = conn.send(data) # echo back to the server + print("sent: " + str(sent_bytes) + " byte(s)") conn.close() print("client disconnected") except Exception as ex: @@ -144,11 +140,12 @@ def main(): try: print("connecting...") client.connect((remote_ip, remote_port)) - print("send:") data = "Hello, world!" - client.send(data) + print("send: ", data) + sent_bytes = client.send(data) + print("sent: " + str(sent_bytes) + " byte(s)") data = client.recv(1024) - print("Received", repr(data)) + print("recv: ", repr(data)) except Exception as ex: print(ex) print("errno=", libzt.errno()) diff --git a/include/ZeroTierSockets.h b/include/ZeroTierSockets.h index c4e770d..68247e2 100644 --- a/include/ZeroTierSockets.h +++ b/include/ZeroTierSockets.h @@ -870,7 +870,7 @@ int zts_py_connect(int fd, int family, int type, PyObject *addro); PyObject * zts_py_accept(int fd); int zts_py_listen(int fd, int backlog); PyObject * zts_py_recv(int fd, int len, int flags); -int zts_py_send(int fd, PyObject *buf, int len, int flags); +int zts_py_send(int fd, PyObject *buf, int flags); int zts_py_close(int fd); int zts_py_setblocking(int fd, int flag); int zts_py_getblocking(int fd); diff --git a/pkg/pypi/LICENSE b/pkg/pypi/LICENSE deleted file mode 100644 index 9784015..0000000 --- a/pkg/pypi/LICENSE +++ /dev/null @@ -1,149 +0,0 @@ ------------------------------------------------------------------------------ - -Business Source License 1.1 - -License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. -"Business Source License" is a trademark of MariaDB Corporation Ab. - ------------------------------------------------------------------------------ - -Parameters - -Licensor: ZeroTier, Inc. -Licensed Work: ZeroTier Network Virtualization Engine 1.4.4 - The Licensed Work is (c)2019 ZeroTier, Inc. -Additional Use Grant: You may make use of the Licensed Work, provided you - do not use it in any of the following ways: - - * Sell hosted ZeroTier services as a "SaaS" Product - - (1) Operate or sell access to ZeroTier root servers, - network controllers, or authorization key or certificate - generation components of the Licensed Work as a - for-profit service, regardless of whether the use of - these components is sold alone or is bundled with other - services. Note that this does not apply to the use of - ZeroTier behind the scenes to operate a service not - related to ZeroTier network administration. - - * Create Non-Open-Source Commercial Derviative Works - - (2) Link or directly include the Licensed Work in a - commercial or for-profit application or other product - not distributed under an Open Source Initiative (OSI) - compliant license. See: https://opensource.org/licenses - - (3) Remove the name, logo, copyright, or other branding - material from the Licensed Work to create a "rebranded" - or "white labeled" version to distribute as part of - any commercial or for-profit product or service. - - * Certain Government Uses - - (4) Use or deploy the Licensed Work in a government - setting in support of any active government function - or operation with the exception of the following: - physical or mental health care, family and social - services, social welfare, senior care, child care, and - the care of persons with disabilities. - -Change Date: 2023-01-01 - -Change License: Apache License version 2.0 as published by the Apache - Software Foundation - https://www.apache.org/licenses/ - -Alternative Licensing - -If you would like to use the Licensed Work in any way that conflicts with -the stipulations of the Additional Use Grant, contact ZeroTier, Inc. to -obtain an alternative commercial license. - -Visit us on the web at: https://www.zerotier.com/ - -Notice - -The Business Source License (this document, or the "License") is not an Open -Source license. However, the Licensed Work will eventually be made available -under an Open Source License, as stated in this License. - -For more information on the use of the Business Source License for ZeroTier -products, please visit our pricing page which contains license details and -and license FAQ: https://zerotier.com/pricing - -For more information on the use of the Business Source License generally, -please visit the Adopting and Developing Business Source License FAQ at -https://mariadb.com/bsl-faq-adopting. - ------------------------------------------------------------------------------ - -Business Source License 1.1 - -Terms - -The Licensor hereby grants you the right to copy, modify, create derivative -works, redistribute, and make non-production use of the Licensed Work. The -Licensor may make an Additional Use Grant, above, permitting limited -production use. - -Effective on the Change Date, or the fourth anniversary of the first publicly -available distribution of a specific version of the Licensed Work under this -License, whichever comes first, the Licensor hereby grants you rights under -the terms of the Change License, and the rights granted in the paragraph -above terminate. - -If your use of the Licensed Work does not comply with the requirements -currently in effect as described in this License, you must purchase a -commercial license from the Licensor, its affiliated entities, or authorized -resellers, or you must refrain from using the Licensed Work. - -All copies of the original and modified Licensed Work, and derivative works -of the Licensed Work, are subject to this License. This License applies -separately for each version of the Licensed Work and the Change Date may vary -for each version of the Licensed Work released by Licensor. - -You must conspicuously display this License on each original or modified copy -of the Licensed Work. If you receive the Licensed Work in original or -modified form from a third party, the terms and conditions set forth in this -License apply to your use of that work. - -Any use of the Licensed Work in violation of this License will automatically -terminate your rights under this License for the current and all other -versions of the Licensed Work. - -This License does not grant you any right in any trademark or logo of -Licensor or its affiliates (provided that you may use a trademark or logo of -Licensor as expressly required by this License). - -TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -TITLE. - ------------------------------------------------------------------------------ - -MariaDB hereby grants you permission to use this License’s text to license -your works, and to refer to it using the trademark "Business Source License", -as long as you comply with the Covenants of Licensor below. - -Covenants of Licensor - -In consideration of the right to use this License’s text and the "Business -Source License" name and trademark, Licensor covenants to MariaDB, and to all -other recipients of the licensed work to be provided by Licensor: - -1. To specify as the Change License the GPL Version 2.0 or any later version, - or a license that is compatible with GPL Version 2.0 or a later version, - where "compatible" means that software provided under the Change License can - be included in a program with software provided under GPL Version 2.0 or a - later version. Licensor may specify additional Change Licenses without - limitation. - -2. To either: (a) specify an additional grant of rights to use that does not - impose any additional restriction on the right granted in this License, as - the Additional Use Grant; or (b) insert the text "None". - -3. To specify a Change Date. - -4. Not to modify this License in any other way. \ No newline at end of file diff --git a/pkg/pypi/build.sh b/pkg/pypi/build.sh index cdf5fb4..c223d9f 100755 --- a/pkg/pypi/build.sh +++ b/pkg/pypi/build.sh @@ -1,7 +1,6 @@ #!/bin/bash -PYBIN=python3.9 -#PYBIN=/opt/python/cp39-cp39/bin/python3 +PYBIN=python3 # Build the extension module ext() @@ -11,8 +10,6 @@ ext() # Copy language bindings into module directory cp -f native/src/bindings/python/*.py libzt/ cp -f native/LICENSE.txt LICENSE - #mkdir -p build/temp.macosx-11-x86_64-3.9 - #mkdir -p build/temp.linux-x86_64-3.8 # Build C libraries (and then) C++ extension $PYBIN setup.py build_clib --verbose build_ext -i --verbose } @@ -36,26 +33,4 @@ clean() rm -rf LICENSE } -manylinux() -{ - CONTAINER="quay.io/pypa/manylinux_2_24_x86_64" - docker pull ${CONTAINER} - docker run --rm -it --entrypoint bash -v $(pwd)/../../:/media/libzt ${CONTAINER} -} - -cycle() -{ - #clean - #swig -c++ -python -o ../../src/bindings/python/zt_wrap.cpp -I../../include ../../src/bindings/python/zt.i - #wheel - #pip3 uninstall -y libzt - #pip3 install dist/libzt-1.3.3-cp39-cp39-macosx_11_0_x86_64.whl - -} - -update-version() -{ - echo "__version__ = \"$(git describe)\"" > libzt/version.py -} - "$@" diff --git a/src/Controls.cpp b/src/Controls.cpp index d2f64ab..082fdcb 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -43,6 +43,10 @@ using namespace ZeroTier; WSADATA wsaData; #endif +#ifdef ZTS_ENABLE_PYTHON + #define ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS 1 +#endif + namespace ZeroTier { extern NodeService *service; diff --git a/src/Signals.hpp b/src/Signals.hpp index e1151ce..1d360d5 100644 --- a/src/Signals.hpp +++ b/src/Signals.hpp @@ -20,10 +20,14 @@ #ifndef SIGNALS_HPP #define SIGNALS_HPP +#ifdef ZTS_ENABLE_PYTHON + #define ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS 1 +#endif + #ifdef ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS /** - * + * */ void _signal_handler(int signal); @@ -34,4 +38,4 @@ void _install_signal_handlers(); #endif // ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS -#endif // _H \ No newline at end of file +#endif // _H diff --git a/src/bindings/python/LICENSE b/src/bindings/python/LICENSE new file mode 100644 index 0000000..bace1da --- /dev/null +++ b/src/bindings/python/LICENSE @@ -0,0 +1,287 @@ +NOTICE: + +Portions of the libzt Python wrapper were directly inspired by the cpython +socketmodule.c. For this reason the following license text is included. The +following license text applies only to the C++ wrapper code that interfaces +with the Python API and the Python code that attempts to replicate the +functionality of the Python low-level socket API: + +A. HISTORY OF THE SOFTWARE +========================== + +Python was created in the early 1990s by Guido van Rossum at Stichting +Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands +as a successor of a language called ABC. Guido remains Python's +principal author, although it includes many contributions from others. + +In 1995, Guido continued his work on Python at the Corporation for +National Research Initiatives (CNRI, see http://www.cnri.reston.va.us) +in Reston, Virginia where he released several versions of the +software. + +In May 2000, Guido and the Python core development team moved to +BeOpen.com to form the BeOpen PythonLabs team. In October of the same +year, the PythonLabs team moved to Digital Creations, which became +Zope Corporation. In 2001, the Python Software Foundation (PSF, see +https://www.python.org/psf/) was formed, a non-profit organization +created specifically to own Python-related Intellectual Property. +Zope Corporation was a sponsoring member of the PSF. + +All Python releases are Open Source (see http://www.opensource.org for +the Open Source Definition). Historically, most, but not all, Python +releases have also been GPL-compatible; the table below summarizes +the various releases. + + Release Derived Year Owner GPL- + from compatible? (1) + + 0.9.0 thru 1.2 1991-1995 CWI yes + 1.3 thru 1.5.2 1.2 1995-1999 CNRI yes + 1.6 1.5.2 2000 CNRI no + 2.0 1.6 2000 BeOpen.com no + 1.6.1 1.6 2001 CNRI yes (2) + 2.1 2.0+1.6.1 2001 PSF no + 2.0.1 2.0+1.6.1 2001 PSF yes + 2.1.1 2.1+2.0.1 2001 PSF yes + 2.1.2 2.1.1 2002 PSF yes + 2.1.3 2.1.2 2002 PSF yes + 2.2 and above 2.1.1 2001-now PSF yes + +Footnotes: + +(1) GPL-compatible doesn't mean that we're distributing Python under + the GPL. All Python licenses, unlike the GPL, let you distribute + a modified version without making your changes open source. The + GPL-compatible licenses make it possible to combine Python with + other software that is released under the GPL; the others don't. + +(2) According to Richard Stallman, 1.6.1 is not GPL-compatible, + because its license has a choice of law clause. According to + CNRI, however, Stallman's lawyer has told CNRI's lawyer that 1.6.1 + is "not incompatible" with the GPL. + +Thanks to the many outside volunteers who have worked under Guido's +direction to make these releases possible. + + +B. TERMS AND CONDITIONS FOR ACCESSING OR OTHERWISE USING PYTHON +=============================================================== + +Python software and documentation are licensed under the +Python Software Foundation License Version 2. + +Starting with Python 3.8.6, examples, recipes, and other code in +the documentation are dual licensed under the PSF License Version 2 +and the Zero-Clause BSD license. + +Some software incorporated into Python is under different licenses. +The licenses are listed with code falling under that license. + + +PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 +-------------------------------------------- + +1. This LICENSE AGREEMENT is between the Python Software Foundation +("PSF"), and the Individual or Organization ("Licensee") accessing and +otherwise using this software ("Python") in source or binary form and +its associated documentation. + +2. Subject to the terms and conditions of this License Agreement, PSF hereby +grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, +analyze, test, perform and/or display publicly, prepare derivative works, +distribute, and otherwise use Python alone or in any derivative version, +provided, however, that PSF's License Agreement and PSF's notice of copyright, +i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, +2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Python Software Foundation; +All Rights Reserved" are retained in Python alone or in any derivative version +prepared by Licensee. + +3. In the event Licensee prepares a derivative work that is based on +or incorporates Python or any part thereof, and wants to make +the derivative work available to others as provided herein, then +Licensee hereby agrees to include in any such work a brief summary of +the changes made to Python. + +4. PSF is making Python available to Licensee on an "AS IS" +basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND +DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT +INFRINGE ANY THIRD PARTY RIGHTS. + +5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON +FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS +A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, +OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +6. This License Agreement will automatically terminate upon a material +breach of its terms and conditions. + +7. Nothing in this License Agreement shall be deemed to create any +relationship of agency, partnership, or joint venture between PSF and +Licensee. This License Agreement does not grant permission to use PSF +trademarks or trade name in a trademark sense to endorse or promote +products or services of Licensee, or any third party. + +8. By copying, installing or otherwise using Python, Licensee +agrees to be bound by the terms and conditions of this License +Agreement. + + +BEOPEN.COM LICENSE AGREEMENT FOR PYTHON 2.0 +------------------------------------------- + +BEOPEN PYTHON OPEN SOURCE LICENSE AGREEMENT VERSION 1 + +1. This LICENSE AGREEMENT is between BeOpen.com ("BeOpen"), having an +office at 160 Saratoga Avenue, Santa Clara, CA 95051, and the +Individual or Organization ("Licensee") accessing and otherwise using +this software in source or binary form and its associated +documentation ("the Software"). + +2. Subject to the terms and conditions of this BeOpen Python License +Agreement, BeOpen hereby grants Licensee a non-exclusive, +royalty-free, world-wide license to reproduce, analyze, test, perform +and/or display publicly, prepare derivative works, distribute, and +otherwise use the Software alone or in any derivative version, +provided, however, that the BeOpen Python License is retained in the +Software, alone or in any derivative version prepared by Licensee. + +3. BeOpen is making the Software available to Licensee on an "AS IS" +basis. BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND +DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT +INFRINGE ANY THIRD PARTY RIGHTS. + +4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE +SOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS +AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY +DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +5. This License Agreement will automatically terminate upon a material +breach of its terms and conditions. + +6. This License Agreement shall be governed by and interpreted in all +respects by the law of the State of California, excluding conflict of +law provisions. Nothing in this License Agreement shall be deemed to +create any relationship of agency, partnership, or joint venture +between BeOpen and Licensee. This License Agreement does not grant +permission to use BeOpen trademarks or trade names in a trademark +sense to endorse or promote products or services of Licensee, or any +third party. As an exception, the "BeOpen Python" logos available at +http://www.pythonlabs.com/logos.html may be used according to the +permissions granted on that web page. + +7. By copying, installing or otherwise using the software, Licensee +agrees to be bound by the terms and conditions of this License +Agreement. + + +CNRI LICENSE AGREEMENT FOR PYTHON 1.6.1 +--------------------------------------- + +1. This LICENSE AGREEMENT is between the Corporation for National +Research Initiatives, having an office at 1895 Preston White Drive, +Reston, VA 20191 ("CNRI"), and the Individual or Organization +("Licensee") accessing and otherwise using Python 1.6.1 software in +source or binary form and its associated documentation. + +2. Subject to the terms and conditions of this License Agreement, CNRI +hereby grants Licensee a nonexclusive, royalty-free, world-wide +license to reproduce, analyze, test, perform and/or display publicly, +prepare derivative works, distribute, and otherwise use Python 1.6.1 +alone or in any derivative version, provided, however, that CNRI's +License Agreement and CNRI's notice of copyright, i.e., "Copyright (c) +1995-2001 Corporation for National Research Initiatives; All Rights +Reserved" are retained in Python 1.6.1 alone or in any derivative +version prepared by Licensee. Alternately, in lieu of CNRI's License +Agreement, Licensee may substitute the following text (omitting the +quotes): "Python 1.6.1 is made available subject to the terms and +conditions in CNRI's License Agreement. This Agreement together with +Python 1.6.1 may be located on the Internet using the following +unique, persistent identifier (known as a handle): 1895.22/1013. This +Agreement may also be obtained from a proxy server on the Internet +using the following URL: http://hdl.handle.net/1895.22/1013". + +3. In the event Licensee prepares a derivative work that is based on +or incorporates Python 1.6.1 or any part thereof, and wants to make +the derivative work available to others as provided herein, then +Licensee hereby agrees to include in any such work a brief summary of +the changes made to Python 1.6.1. + +4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS" +basis. CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND +DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 1.6.1 WILL NOT +INFRINGE ANY THIRD PARTY RIGHTS. + +5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON +1.6.1 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS +A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, +OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +6. This License Agreement will automatically terminate upon a material +breach of its terms and conditions. + +7. This License Agreement shall be governed by the federal +intellectual property law of the United States, including without +limitation the federal copyright law, and, to the extent such +U.S. federal law does not apply, by the law of the Commonwealth of +Virginia, excluding Virginia's conflict of law provisions. +Notwithstanding the foregoing, with regard to derivative works based +on Python 1.6.1 that incorporate non-separable material that was +previously distributed under the GNU General Public License (GPL), the +law of the Commonwealth of Virginia shall govern this License +Agreement only as to issues arising under or with respect to +Paragraphs 4, 5, and 7 of this License Agreement. Nothing in this +License Agreement shall be deemed to create any relationship of +agency, partnership, or joint venture between CNRI and Licensee. This +License Agreement does not grant permission to use CNRI trademarks or +trade name in a trademark sense to endorse or promote products or +services of Licensee, or any third party. + +8. By clicking on the "ACCEPT" button where indicated, or by copying, +installing or otherwise using Python 1.6.1, Licensee agrees to be +bound by the terms and conditions of this License Agreement. + + ACCEPT + + +CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2 +-------------------------------------------------- + +Copyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, +The Netherlands. All rights reserved. + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Stichting Mathematisch +Centrum or CWI not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior +permission. + +STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO +THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE +FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +ZERO-CLAUSE BSD LICENSE FOR CODE IN THE PYTHON DOCUMENTATION +---------------------------------------------------------------------- + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/src/bindings/python/PythonSockets.cpp b/src/bindings/python/PythonSockets.cpp index 423c255..febc050 100644 --- a/src/bindings/python/PythonSockets.cpp +++ b/src/bindings/python/PythonSockets.cpp @@ -17,6 +17,8 @@ * ZeroTier Socket API (Python) */ +#include + #include "lwip/sockets.h" #include "lwip/inet.h" @@ -139,8 +141,9 @@ PyObject * zts_py_recv(int fd, int len, int flags) int bytes_read; buf = PyBytes_FromStringAndSize((char *) 0, len); - if (buf == NULL) + if (buf == NULL) { return NULL; + } bytes_read = zts_recv(fd, PyBytes_AS_STRING(buf), len, flags); t = PyTuple_New(2); @@ -163,16 +166,43 @@ PyObject * zts_py_recv(int fd, int len, int flags) return t; } -int zts_py_send(int fd, PyObject *buf, int len, int flags) +int zts_py_send(int fd, PyObject *buf, int flags) { - int err = ZTS_ERR_OK; - PyObject *encodedStr = PyUnicode_AsEncodedString(buf, "UTF-8", "strict"); - if (encodedStr) { - char *bytes = PyBytes_AsString(encodedStr); - err = zts_send(fd, bytes, len, flags); - Py_DECREF(encodedStr); + int bytes_sent = ZTS_ERR_OK; + char *bytes = NULL; + PyObject *encodedStr = NULL; + + // Check for various encodings, or lack thereof + + if (PyByteArray_Check(buf)) { + bytes = PyByteArray_AsString(buf); } - return err; + + if (PyUnicode_Check(buf)) { + encodedStr = PyUnicode_AsEncodedString(buf, "UTF-8", "strict"); + if (!encodedStr) { + return ZTS_ERR_ARG; + } + bytes = PyBytes_AsString(encodedStr); + } + + if (!bytes) { + // No encoding detected + bytes = PyBytes_AsString(buf); + } + + // If we still don't have a valid pointer to a C-string, fail + if (!bytes) { + bytes_sent = ZTS_ERR_ARG; + } + else { + bytes_sent = zts_send(fd, bytes, strlen(bytes), flags); + } + + if (encodedStr) { + Py_DECREF(encodedStr); + } + return bytes_sent; } int zts_py_close(int fd) diff --git a/src/bindings/python/libzt.py b/src/bindings/python/libzt.py index e372973..bfc77de 100644 --- a/src/bindings/python/libzt.py +++ b/src/bindings/python/libzt.py @@ -484,8 +484,8 @@ def zts_py_listen(fd, backlog): def zts_py_recv(fd, len, flags): return _libzt.zts_py_recv(fd, len, flags) -def zts_py_send(fd, buf, len, flags): - return _libzt.zts_py_send(fd, buf, len, flags) +def zts_py_send(fd, buf, flags): + return _libzt.zts_py_send(fd, buf, flags) def zts_py_close(fd): return _libzt.zts_py_close(fd) diff --git a/src/bindings/python/sockets.py b/src/bindings/python/sockets.py index e8d3dc3..547f84e 100644 --- a/src/bindings/python/sockets.py +++ b/src/bindings/python/sockets.py @@ -351,7 +351,7 @@ class socket: def send(self, data, flags=0): """Write data to the socket""" - err = libzt.zts_py_send(self._fd, data, len(data), flags) + err = libzt.zts_py_send(self._fd, data, flags) if err < 0: handle_error(err) return err