summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorcelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-04-04 14:24:07 +0000
committercelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-04-04 14:24:07 +0000
commit177e5a3af1b7b421c027dfc06abc6e6b8ca10e28 (patch)
tree98e32720a188a73e195e237f9f70c11f3086077f /src/common
parent2b9d6d3e1fe89fe25fd79f57d283ee7aa4994a80 (diff)
downloadhercules-177e5a3af1b7b421c027dfc06abc6e6b8ca10e28.tar.gz
hercules-177e5a3af1b7b421c027dfc06abc6e6b8ca10e28.tar.bz2
hercules-177e5a3af1b7b421c027dfc06abc6e6b8ca10e28.tar.xz
hercules-177e5a3af1b7b421c027dfc06abc6e6b8ca10e28.zip
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/branches/stable@1397 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common')
-rw-r--r--src/common/socket.c79
1 files changed, 61 insertions, 18 deletions
diff --git a/src/common/socket.c b/src/common/socket.c
index 5dd2e38f9..7178c95e2 100644
--- a/src/common/socket.c
+++ b/src/common/socket.c
@@ -38,26 +38,45 @@ typedef int socklen_t;
#include "memwatch.h"
#endif
+fd_set readfds;
+int fd_max;
+time_t tick_;
+time_t stall_time_ = 60;
+int ip_rules = 1;
+
+#define UPNP
#ifdef UPNP
#if defined(CYGWIN)
+
#include <dlfcn.h>
+ void *upnp_dll;
+ int (*upnp_init)();
+ int (*upnp_final)();
+ int (*firewall_addport)(char *desc, int port);
+ int (*upnp_addport)(char *desc, char *ip, int port);
+ #define DLL_OPEN(x) dlopen(x,RTLD_NOW)
+ #define DLL_SYM(x,y,z) x=(void *)dlsym(y,z)
+ #define DLL_CLOSE(x) dlclose(x)
+
#elif _WIN32
+
// windows.h already included
+ HINSTANCE upnp_dll;
+ int (WINAPI* upnp_init)();
+ int (WINAPI* upnp_final)();
+ int (WINAPI* firewall_addport)(char *desc, int port);
+ int (WINAPI* upnp_addport)(char *desc, char *ip, int port);
+ #define DLL_OPEN(x) LoadLibrary(x)
+ #define DLL_SYM(x,y,z) (FARPROC)x=GetProcAddress(y,z)
+ #define DLL_CLOSE(x) FreeLibrary(x)
+
#else
#error This doesnt work with non-Windows yet
#endif
- void *upnp_dll;
- void (*upnp_init)();
- void (*upnp_final)();
+ extern char server_type[24];
#endif
-fd_set readfds;
-int fd_max;
-time_t tick_;
-time_t stall_time_ = 60;
-int ip_rules = 1;
-
int rfifo_size = 65536;
int wfifo_size = 65536;
@@ -318,6 +337,23 @@ int make_listen_bind(long ip,int port)
server_address.sin_addr.s_addr = ip;
server_address.sin_port = htons((unsigned short)port);
+#ifdef UPNP
+ if (upnp_dll) {
+ int localaddr = ntohl(addr_[0]);
+ unsigned char *natip = (unsigned char *)&localaddr;
+ char buf[16];
+ sprintf(buf, "%d.%d.%d.%d", natip[0], natip[1], natip[2], natip[3]);
+ //printf("natip=%d.%d.%d.%d\n", natip[0], natip[1], natip[2], natip[3]);
+ if (firewall_addport(server_type, port))
+ printf ("Firewall port %d successfully opened\n", port);
+ if (natip[0] == 192 && natip[1] == 168) {
+ if (upnp_addport(server_type, natip, port))
+ printf ("Upnp mappings successfull\n");
+ else printf ("Upnp mapping failed\n");
+ }
+ }
+#endif
+
result = bind(fd, (struct sockaddr*)&server_address, sizeof(server_address));
if( result == -1 ) {
perror("bind");
@@ -938,19 +974,26 @@ int Net_Init(void)
// not implemented yet ^^;
void do_init_upnp(void)
{
- upnp_dll = dlopen("upnp.dll", RTLD_NOW);
+ upnp_dll = DLL_OPEN ("upnp.dll");
if (!upnp_dll) {
printf ("Cannot open upnp.dll: %s\n", dlerror());
return;
}
- upnp_init = (void *)dlsym(upnp_dll, "do_init");
- upnp_final = (void *)dlsym(upnp_dll, "do_final");
- if (!upnp_init || !upnp_final) {
+ DLL_SYM (upnp_init, upnp_dll, "do_init");
+ DLL_SYM (upnp_final, upnp_dll, "do_final");
+ DLL_SYM (firewall_addport, upnp_dll, "Firewall_AddPort");
+ DLL_SYM (upnp_addport, upnp_dll, "UPNP_AddPort");
+ if (!upnp_init || !upnp_final || !firewall_addport || !upnp_addport) {
printf ("Cannot load symbol: %s\n", dlerror());
- dlclose (upnp_dll);
+ DLL_CLOSE (upnp_dll);
+ upnp_dll = NULL;
return;
}
- upnp_init();
+ if (upnp_init() == 0) {
+ printf ("Error initialising upnp.dll, unloading...\n");
+ DLL_CLOSE (upnp_dll);
+ upnp_dll = NULL;
+ }
return;
}
#endif
@@ -983,10 +1026,10 @@ void do_final_socket(void)
aFree(session[0]);
#ifdef UPNP
- if (upnp_final)
+ if (upnp_dll) {
upnp_final();
- if (upnp_dll)
- dlclose(upnp_dll);
+ DLL_CLOSE(upnp_dll);
+ }
#endif
}