libnatpmpmodule.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* $Id: libnatpmpmodule.c,v 1.7 2012/03/05 19:38:37 nanard Exp $ */
  2. /* libnatpmp
  3. * http://miniupnp.free.fr/libnatpmp.html
  4. Copyright (c) 2007-2011, Thomas BERNARD
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <Python.h>
  28. #ifdef WIN32
  29. #include <winsock2.h>
  30. #else
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #endif
  34. #define STATICLIB
  35. #include "structmember.h"
  36. #include "natpmp.h"
  37. /* for compatibility with Python < 2.4 */
  38. #ifndef Py_RETURN_NONE
  39. #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  40. #endif
  41. #ifndef Py_RETURN_TRUE
  42. #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
  43. #endif
  44. #ifndef Py_RETURN_FALSE
  45. #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
  46. #endif
  47. typedef struct {
  48. PyObject_HEAD
  49. /* Type-specific fields go here. */
  50. unsigned int discoverdelay;
  51. natpmp_t natpmp;
  52. } NATPMPObject;
  53. static PyMemberDef NATPMP_members[] = {
  54. {"discoverdelay", T_UINT, offsetof(NATPMPObject, discoverdelay),
  55. 0/*READWRITE*/, "value in ms used to wait for NATPMP responses"
  56. },
  57. {NULL}
  58. };
  59. static PyObject *
  60. NATPMPObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  61. {
  62. NATPMPObject *self;
  63. self = (NATPMPObject *)type->tp_alloc(type, 0);
  64. if (self) {
  65. initnatpmp(&self->natpmp, 0, 0);
  66. }
  67. return (PyObject *)self;
  68. }
  69. static void
  70. NATPMPObject_dealloc(NATPMPObject *self)
  71. {
  72. closenatpmp(&self->natpmp);
  73. self->ob_type->tp_free((PyObject*)self);
  74. }
  75. static PyObject *
  76. NATPMP_externalipaddress(NATPMPObject *self)
  77. {
  78. int r;
  79. struct timeval timeout;
  80. fd_set fds;
  81. natpmpresp_t response;
  82. r = sendpublicaddressrequest(&self->natpmp);
  83. if (r < 0) {
  84. #ifdef ENABLE_STRNATPMPERR
  85. PyErr_SetString(PyExc_Exception, strnatpmperr(r));
  86. #endif
  87. return NULL;
  88. }
  89. do {
  90. FD_ZERO(&fds);
  91. FD_SET(self->natpmp.s, &fds);
  92. getnatpmprequesttimeout(&self->natpmp, &timeout);
  93. select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  94. r = readnatpmpresponseorretry(&self->natpmp, &response);
  95. if (r < 0 && r != NATPMP_TRYAGAIN) {
  96. #ifdef ENABLE_STRNATPMPERR
  97. PyErr_SetString(PyExc_Exception, strnatpmperr(r));
  98. #endif
  99. return NULL;
  100. }
  101. } while (r == NATPMP_TRYAGAIN);
  102. return Py_BuildValue("s", inet_ntoa(response.pnu.publicaddress.addr));
  103. }
  104. static PyObject *
  105. NATPMP_domapping(natpmp_t *n, unsigned short eport, unsigned short iport,
  106. const char *protocol, unsigned int lifetime)
  107. {
  108. int proto;
  109. struct timeval timeout;
  110. fd_set fds;
  111. natpmpresp_t response;
  112. int r;
  113. if (!strncasecmp("tcp", protocol, 3)) {
  114. proto = NATPMP_PROTOCOL_TCP;
  115. } else if (!strncasecmp("udp", protocol, 3)) {
  116. proto = NATPMP_PROTOCOL_UDP;
  117. } else {
  118. PyErr_SetString(PyExc_Exception, "Unknown protocol");
  119. return NULL;
  120. }
  121. r = sendnewportmappingrequest(n, proto, iport, eport,
  122. lifetime);
  123. if (r < 0) {
  124. #ifdef ENABLE_STRNATPMPERR
  125. PyErr_SetString(PyExc_Exception, strnatpmperr(r));
  126. #endif
  127. return NULL;
  128. }
  129. do {
  130. FD_ZERO(&fds);
  131. FD_SET(n->s, &fds);
  132. getnatpmprequesttimeout(n, &timeout);
  133. select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  134. r = readnatpmpresponseorretry(n, &response);
  135. if (r < 0 && r != NATPMP_TRYAGAIN) {
  136. #ifdef ENABLE_STRNATPMPERR
  137. PyErr_SetString(PyExc_Exception, strnatpmperr(r));
  138. #endif
  139. return NULL;
  140. }
  141. } while (r == NATPMP_TRYAGAIN);
  142. return Py_BuildValue("H", response.pnu.newportmapping.mappedpublicport);
  143. }
  144. /* AddPortMapping(externalPort, protocol, internalPort, lifetime)
  145. * protocol is 'UDP' or 'TCP' */
  146. static PyObject *
  147. NATPMP_addportmapping(NATPMPObject *self, PyObject *args)
  148. {
  149. unsigned short eport;
  150. unsigned short iport;
  151. unsigned int lifetime;
  152. const char *protocol;
  153. if (!PyArg_ParseTuple(args, "HsHI", &eport, &protocol, &iport, &lifetime))
  154. return NULL;
  155. return NATPMP_domapping(&self->natpmp, eport, iport, protocol, lifetime);
  156. }
  157. /* DeletePortMapping(externalPort, protocol, internalPort)
  158. * protocol is 'UDP' or 'TCP' */
  159. static PyObject *
  160. NATPMP_deleteportmapping(NATPMPObject *self, PyObject *args)
  161. {
  162. unsigned short eport;
  163. unsigned short iport;
  164. const char *protocol;
  165. if (!PyArg_ParseTuple(args, "HsH", &eport, &protocol, &iport))
  166. return NULL;
  167. return NATPMP_domapping(&self->natpmp, eport, iport, protocol, 0);
  168. }
  169. /* natpmp.NATPMP object Method Table */
  170. static PyMethodDef NATPMP_methods[] = {
  171. {"externalipaddress", (PyCFunction)NATPMP_externalipaddress, METH_NOARGS,
  172. "return external IP address"
  173. },
  174. {"addportmapping", (PyCFunction)NATPMP_addportmapping, METH_VARARGS,
  175. "add a port mapping"
  176. },
  177. {"deleteportmapping", (PyCFunction)NATPMP_deleteportmapping, METH_VARARGS,
  178. "delete a port mapping"
  179. },
  180. {NULL} /* Sentinel */
  181. };
  182. static PyTypeObject NATPMPType = {
  183. PyObject_HEAD_INIT(NULL)
  184. 0, /*ob_size*/
  185. "libnatpmp.NATPMP", /*tp_name*/
  186. sizeof(NATPMPObject), /*tp_basicsize*/
  187. 0, /*tp_itemsize*/
  188. (destructor)NATPMPObject_dealloc, /*tp_dealloc*/
  189. 0, /*tp_print*/
  190. 0, /*tp_getattr*/
  191. 0, /*tp_setattr*/
  192. 0, /*tp_compare*/
  193. 0, /*tp_repr*/
  194. 0, /*tp_as_number*/
  195. 0, /*tp_as_sequence*/
  196. 0, /*tp_as_mapping*/
  197. 0, /*tp_hash */
  198. 0, /*tp_call*/
  199. 0, /*tp_str*/
  200. 0, /*tp_getattro*/
  201. 0, /*tp_setattro*/
  202. 0, /*tp_as_buffer*/
  203. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  204. "NATPMP objects", /* tp_doc */
  205. 0, /* tp_traverse */
  206. 0, /* tp_clear */
  207. 0, /* tp_richcompare */
  208. 0, /* tp_weaklistoffset */
  209. 0, /* tp_iter */
  210. 0, /* tp_iternext */
  211. NATPMP_methods, /* tp_methods */
  212. NATPMP_members, /* tp_members */
  213. 0, /* tp_getset */
  214. 0, /* tp_base */
  215. 0, /* tp_dict */
  216. 0, /* tp_descr_get */
  217. 0, /* tp_descr_set */
  218. 0, /* tp_dictoffset */
  219. 0, /* tp_init */
  220. 0, /* tp_alloc */
  221. NATPMPObject_new, /* tp_new */
  222. };
  223. /* module methods */
  224. static PyMethodDef libnatpmp_methods[] = {
  225. {NULL} /* Sentinel */
  226. };
  227. #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
  228. #define PyMODINIT_FUNC void
  229. #endif
  230. PyMODINIT_FUNC
  231. initlibnatpmp(void)
  232. {
  233. PyObject* m;
  234. if (PyType_Ready(&NATPMPType) < 0)
  235. return;
  236. m = Py_InitModule3("libnatpmp", libnatpmp_methods,
  237. "libnatpmp module.");
  238. Py_INCREF(&NATPMPType);
  239. PyModule_AddObject(m, "NATPMP", (PyObject *)&NATPMPType);
  240. }