Advertisement
Guest User

Untitled

a guest
Mar 19th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 8.26 KB | None | 0 0
  1. diff --git a/meson.build b/meson.build
  2. index 8b0bd404..5233671e 100644
  3. --- a/meson.build
  4. +++ b/meson.build
  5. @@ -18,6 +18,8 @@ libgmodule_dep = dependency('gmodule-2.0')
  6.  
  7.  libcanberra_dep = dependency('libcanberra', version: '>= 0.22',
  8.                               required: get_option('libcanberra'))
  9. +miniupnpc_dep = dependency('miniupnpc', version: '>= 2.0.0',
  10. +                             required: get_option('miniupnpc'))
  11.  dbus_glib_dep = dependency('dbus-glib-1', required: get_option('dbus'))
  12.  
  13.  global_deps = []
  14. @@ -183,6 +185,7 @@ if meson.version().version_compare('>= 0.53.0')
  15.      'Plugin Support': get_option('plugin'),
  16.      'DBus Support': dbus_glib_dep.found(),
  17.      'libcanberra': libcanberra_dep.found(),
  18. +    'miniupnpc': miniupnpc_dep.found(),
  19.    }, section: 'Features')
  20.  
  21.    summary({
  22. diff --git a/meson_options.txt b/meson_options.txt
  23. index c2ca54a2..9527f3b1 100644
  24. --- a/meson_options.txt
  25. +++ b/meson_options.txt
  26. @@ -22,6 +22,9 @@ option('dbus', type: 'feature', value: 'auto',
  27.  option('libcanberra', type: 'feature', value: 'auto',
  28.    description: 'Support for sound alerts, Unix only'
  29.  )
  30. +option('miniupnpc', type: 'feature', value: 'auto',
  31. +  description: 'Support for DCC Universal Plug & Play, Unix only'
  32. +)
  33.  
  34.  # Install options
  35.  option('dbus-service-use-appid', type: 'boolean', value: false,
  36. diff --git a/src/common/dcc.c b/src/common/dcc.c
  37. index df129e68..5e1091c1 100644
  38. --- a/src/common/dcc.c
  39. +++ b/src/common/dcc.c
  40. @@ -57,6 +57,7 @@
  41.  #include "text.h"
  42.  #include "url.h"
  43.  #include "hexchatc.h"
  44. +#include "upnp.h"
  45.  
  46.  /* Setting _FILE_OFFSET_BITS to 64 doesn't change lseek to use off64_t on Windows, so override lseek to the version that does */
  47.  #if defined(WIN32) && (!defined(__MINGW32__) && !defined(__MINGW64__))
  48. @@ -371,6 +372,11 @@ dcc_connect_sok (struct DCC *dcc)
  49.  static void
  50.  dcc_close (struct DCC *dcc, enum dcc_state dccstat, int destroy)
  51.  {
  52. +   if (dcc->port > 0)  // && dcc->pasvid ??
  53. +   {
  54. +       upnp_rem_redir (dcc->port);
  55. +   }
  56. +
  57.     if (dcc->wiotag)
  58.     {
  59.         fe_input_remove (dcc->wiotag);
  60. @@ -1660,6 +1666,8 @@ dcc_listen_init (struct DCC *dcc, session *sess)
  61.  
  62.     SAddr.sin_family = AF_INET;
  63.  
  64. +   /*printf("prefs.local_ip=%08x\n", prefs.local_ip);*/
  65. +   /*printf("SAddr.sin_addr.s_addr=%s => ip interne\n", inet_ntoa(SAddr.sin_addr));*/
  66.     /*if local_ip is specified use that*/
  67.     if (prefs.local_ip != 0xffffffff)
  68.     {
  69. @@ -1706,6 +1714,7 @@ dcc_listen_init (struct DCC *dcc, session *sess)
  70.     getsockname (dcc->sok, (struct sockaddr *) &SAddr, &len);
  71.  
  72.     dcc->port = ntohs (SAddr.sin_port);
  73. +   /*printf("dcc port = %d\n", dcc->port);*/
  74.  
  75.     /*if we have a dcc_ip, we use that, so the remote client can connect*/
  76.     /*else we try to take an address from hex_dcc_ip*/
  77. @@ -1718,12 +1727,17 @@ dcc_listen_init (struct DCC *dcc, session *sess)
  78.  
  79.     dcc->addr = ntohl (dcc->addr);
  80.  
  81. +   /*printf("dcc_get_my_address=%s = ext ip\n", net_ip(dcc->addr));*/
  82. +
  83.     set_nonblocking (dcc->sok);
  84.     listen (dcc->sok, 1);
  85.     set_blocking (dcc->sok);
  86.  
  87.     dcc->iotag = fe_input_add (dcc->sok, FIA_READ|FIA_EX, dcc_accept, dcc);
  88.  
  89. +   /* redirect port dcc->port => dcc->addr:dcc->port */
  90. +   upnp_add_redir (inet_ntoa(SAddr.sin_addr), dcc->port);
  91. +
  92.     return TRUE;
  93.  }
  94.  
  95. diff --git a/src/common/hexchat.c b/src/common/hexchat.c
  96. index 3ba7ed6d..e5b7a694 100644
  97. --- a/src/common/hexchat.c
  98. +++ b/src/common/hexchat.c
  99. @@ -52,6 +52,7 @@
  100.  #include "text.h"
  101.  #include "url.h"
  102.  #include "hexchatc.h"
  103. +#include "upnp.h"
  104.  
  105.  #if ! GLIB_CHECK_VERSION (2, 36, 0)
  106.  #include <glib-object.h>           /* for g_type_init() */
  107. @@ -844,6 +845,7 @@ xchat_init (void)
  108.     sound_load ();
  109.     notify_load ();
  110.     ignore_load ();
  111. +   init_upnp ();
  112.  
  113.     g_snprintf (buf, sizeof (buf),
  114.         "NAME %s~%s~\n"             "CMD query %%s\n\n"\
  115. diff --git a/src/common/meson.build b/src/common/meson.build
  116. index 84e2fca3..23285fa9 100644
  117. --- a/src/common/meson.build
  118. +++ b/src/common/meson.build
  119. @@ -21,7 +21,8 @@ common_sources = [
  120.    'tree.c',
  121.    'url.c',
  122.    'userlist.c',
  123. -  'util.c'
  124. +  'util.c',
  125. +  'upnp.c'
  126.  ]
  127.  
  128.  common_sysinfo_deps = []
  129. @@ -77,6 +78,11 @@ if libssl_dep.found()
  130.    common_deps += libssl_dep
  131.  endif
  132.  
  133. +if miniupnpc_dep.found()
  134. +  common_sources += 'upnp.c'
  135. +  common_deps += miniupnpc_dep
  136. +endif
  137. +
  138.  if dbus_glib_dep.found()
  139.    subdir('dbus')
  140.    common_deps += hexchat_dbus_dep
  141. diff --git a/src/common/upnp.c b/src/common/upnp.c
  142. new file mode 100644
  143. index 00000000..4d9ef357
  144. --- /dev/null
  145. +++ b/src/common/upnp.c
  146. @@ -0,0 +1,141 @@
  147. +/* upnp.c */
  148. +
  149. +/*
  150. +Adapted from XChat patches here: http://miniupnp.free.fr/ (c) Thomas Bernard
  151. +Changes to work in Hexchat (c) somercet at gmail, released under GPL 2.0
  152. +*/
  153. +
  154. +#include <stdio.h>
  155. +#include <string.h>
  156. +
  157. +#include "hexchat.h"
  158. +#include "network.h"
  159. +#include "upnp.h"
  160. +
  161. +#include <miniupnpc/miniwget.h>
  162. +#include <miniupnpc/miniupnpc.h>
  163. +#include <miniupnpc/upnpcommands.h>
  164. +
  165. +static struct UPNPUrls urls;
  166. +static struct IGDdatas data;
  167. +
  168. +void init_upnp (void)
  169. +{
  170. +   struct UPNPDev * devlist;
  171. +   struct UPNPDev * dev;
  172. +   char * descXML;
  173. +   int descXMLsize = 0;
  174. +   unsigned char Ttl = 2;
  175. +   int upnperror = 0, statusCode = 0;
  176. +   printf("TB : init_upnp()\n");
  177. +   memset(&urls, 0, sizeof(struct UPNPUrls));
  178. +   memset(&data, 0, sizeof(struct IGDdatas));
  179. +   devlist = upnpDiscover(
  180. +       2000,
  181. +       NULL, /*multicast interface*/
  182. +       NULL, /*minissdpd socket path*/
  183. +       0, /*sameport*/
  184. +       0, /*ipv6*/
  185. +       Ttl, /* "miniupnpc.h: should default to 2 */
  186. +       &upnperror);
  187. +
  188. +/*
  189. +upnpDiscover(  int delay,
  190. +       const char * multicastif,
  191. +       const char * minissdpdsock,
  192. +       int localport,
  193. +       int ipv6,
  194. +       unsigned char ttl,
  195. +       int * error);
  196. +*/
  197. +
  198. +
  199. +   if (devlist)
  200. +   {
  201. +       dev = devlist;
  202. +       while (dev)
  203. +       {
  204. +           if (strstr (dev->st, "InternetGatewayDevice"))
  205. +               break;
  206. +           dev = dev->pNext;
  207. +       }
  208. +       if (!dev)
  209. +           dev = devlist; /* defaulting to first device */
  210. +
  211. +       printf("UPnP device :\n"
  212. +              " desc: %s\n st: %s\n",
  213. +              dev->descURL, dev->st);
  214. +
  215. +       descXML = miniwget(dev->descURL, &descXMLsize, 1, &statusCode);
  216. +
  217. +       /* miniupnpc-2.2.3/src/miniwget.c
  218. +       scope is only for IPv6, say connecting from inside a link-local
  219. +       network.
  220. +
  221. +       IPv6 Services for UPnP Residential Networks draft-bnss-v6ops-upnp-01.txt
  222. +
  223. +       status_code is "HTTP status code"
  224. +       void *
  225. +       miniwget(
  226. +           const char * url,
  227. +           int * size,
  228. +           unsigned int scope_id,
  229. +           int * status_code)
  230. +       */
  231. +
  232. +       if (descXML)
  233. +       {
  234. +           parserootdesc (descXML, descXMLsize, &data);
  235. +           free (descXML); descXML = 0;
  236. +           GetUPNPUrls (&urls, &data, dev->descURL, 1);
  237. +       /* miniupnpc-2.2.3/src/miniupnpc.c build_absolute_url() calls
  238. +       char *if_indextoname(unsigned int ifindex, char *ifname);
  239. +       another IPv6 LL only thing
  240. +
  241. +       MINIUPNP_LIBSPEC void
  242. +       GetUPNPUrls(
  243. +           struct UPNPUrls * urls,
  244. +           struct IGDdatas * data,
  245. +           const char * descURL,
  246. +           unsigned int scope_id)
  247. +       */
  248. +       }
  249. +       freeUPNPDevlist(devlist);
  250. +   }
  251. +   else
  252. +   {
  253. +       /* error ! */
  254. +   }
  255. +}
  256. +
  257. +void upnp_add_redir (const char * addr, int port)
  258. +{
  259. +   char port_str[16];
  260. +   int r;
  261. +   printf("TB : upnp_add_redir (%s, %d)\n", addr, port);
  262. +   if(urls.controlURL[0] == '\0')
  263. +   {
  264. +       printf("TB : the init was not done !\n");
  265. +       return;
  266. +   }
  267. +   sprintf(port_str, "%d", port);
  268. +   r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
  269. +                           port_str, port_str, addr, "xchat", "TCP", NULL, NULL);
  270. +   if(r==0)
  271. +       printf("AddPortMapping(%s, %s, %s) failed\n", port_str, port_str, addr);
  272. +}
  273. +
  274. +void upnp_rem_redir (int port)
  275. +{
  276. +   char port_str[16];
  277. +   //int t; // unused
  278. +   printf("TB : upnp_rem_redir (%d)\n", port);
  279. +   if(urls.controlURL[0] == '\0')
  280. +   {
  281. +       printf("TB : the init was not done !\n");
  282. +       return;
  283. +   }
  284. +   sprintf(port_str, "%d", port);
  285. +   UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port_str, "TCP", NULL);
  286. +}
  287. +
  288. diff --git a/src/common/upnp.h b/src/common/upnp.h
  289. new file mode 100644
  290. index 00000000..adb84989
  291. --- /dev/null
  292. +++ b/src/common/upnp.h
  293. @@ -0,0 +1,16 @@
  294. +/* upnp.h */
  295. +
  296. +/*
  297. +Adapted from XChat patches here: http://miniupnp.free.fr/ (c) Thomas Bernard
  298. +Changes to work in Hexchat (c) somercet at gmail, released under GPL 2.0
  299. +*/
  300. +
  301. +#ifndef XCHAT_UPNP_H
  302. +#define XCHAT_UPNP_H
  303. +
  304. +void init_upnp (void);
  305. +void upnp_add_redir (const char * addr, int port);
  306. +void upnp_rem_redir (int port);
  307. +
  308. +#endif
  309. +
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement