int upnp_redirect_internal(unsigned short eport, const char * iaddr, unsigned short iport, int proto, const char * desc) { /*syslog(LOG_INFO, "redirecting port %hu to %s:%hu protocol %s for: %s", eport, iaddr, iport, protocol, desc); */ if(add_redirect_rule2(ext_if_name, eport, iaddr, iport, proto, desc) < 0) { return -1; } #ifdef ENABLE_LEASEFILE lease_file_add( eport, iaddr, iport, proto, desc); #endif /* syslog(LOG_INFO, "creating pass rule to %s:%hu protocol %s for: %s", iaddr, iport, protocol, desc);*/ if(add_filter_rule2(ext_if_name, iaddr, eport, iport, proto, desc) < 0) { /* clean up the redirect rule */ #if !defined(__linux__) delete_redirect_rule(ext_if_name, eport, proto); #endif return -1; } #ifdef ENABLE_EVENTS upnp_event_var_change_notify(EWanIPC); #endif return 0; }
/* update the port mapping decription and timestamp */ int update_portmapping_desc_timestamp(const char * ifname, unsigned short eport, int proto, const char * desc, unsigned int timestamp) { unsigned short iport; in_addr_t iaddr; char iaddr_str[16]; char rhost[32]; int r; r = priv_delete_redirect_rule_check_desc(ifname, eport, proto, &iport, &iaddr, rhost, sizeof(rhost), 1, desc); if(r < 0) return -1; if(r == 1) { /* only change timestamp */ remove_timestamp_entry(eport, proto); add_timestamp_entry(eport, proto, timestamp); return 0; } if (priv_delete_filter_rule(ifname, iport, proto, iaddr) < 0) return -1; inet_ntop(AF_INET, &iaddr, iaddr_str, sizeof(iaddr_str)); if(add_redirect_rule2(ifname, rhost, eport, iaddr_str, iport, proto, desc, timestamp) < 0) return -1; if(add_filter_rule2(ifname, rhost, iaddr_str, eport, iport, proto, desc) < 0) return -1; return 0; }
/* update the port mapping internal port, decription and timestamp */ int update_portmapping(const char * ifname, unsigned short eport, int proto, unsigned short iport, const char * desc, unsigned int timestamp) { unsigned short old_iport; in_addr_t iaddr; char iaddr_str[16]; char rhost[32]; if(priv_delete_redirect_rule(ifname, eport, proto, &old_iport, &iaddr, rhost, sizeof(rhost)) < 0) return -1; if (priv_delete_filter_rule(ifname, old_iport, proto, iaddr) < 0) return -1; inet_ntop(AF_INET, &iaddr, iaddr_str, sizeof(iaddr_str)); if(add_redirect_rule2(ifname, rhost, eport, iaddr_str, iport, proto, desc, timestamp) < 0) return -1; if(add_filter_rule2(ifname, rhost, iaddr_str, eport, iport, proto, desc) < 0) return -1; return 0; }
int main(int argc, char * * argv) { openlog("testipfwrdrd", LOG_CONS | LOG_PERROR, LOG_USER); init_redirect(); delete_redirect_rule("lo", 2222, IPPROTO_TCP); add_redirect_rule2("lo", 2222, "10.1.1.16", 4444, IPPROTO_TCP, "miniupnpd"); get_redirect_rule("lo", 2222, IPPROTO_TCP, NULL, 0, NULL, NULL, 0, NULL, NULL); shutdown_redirect(); return 0; }
int main(int arc, char * * argv) { char buf[32]; char desc[64]; /*char rhost[32];*/ unsigned short iport; unsigned int timestamp; u_int64_t packets = 0; u_int64_t bytes = 0; openlog("testobsdrdr", LOG_PERROR, LOG_USER); if(init_redirect() < 0) { fprintf(stderr, "init_redirect() failed\n"); return 1; } //add_redirect_rule("ep0", 12123, "192.168.1.23", 1234); //add_redirect_rule2("ep0", 12155, "192.168.1.155", 1255, IPPROTO_TCP); add_redirect_rule2("ep0", "8.8.8.8", 12123, "192.168.1.125", 1234, IPPROTO_UDP, "test description", 0); //add_redirect_rule2("em0", 12123, "127.1.2.3", 1234, // IPPROTO_TCP, "test description tcp"); list_rules(); list_eports_tcp(); if(get_redirect_rule("xl1", 4662, IPPROTO_TCP, buf, sizeof(buf), &iport, desc, sizeof(desc), ×tamp, &packets, &bytes) < 0) printf("get_redirect_rule() failed\n"); else { printf("\n%s:%d '%s' packets=%llu bytes=%llu\n", buf, (int)iport, desc, packets, bytes); } #if 0 if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP) < 0) printf("delete_redirect_rule() failed\n"); else printf("delete_redirect_rule() succeded\n"); if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP) < 0) printf("delete_redirect_rule() failed\n"); else printf("delete_redirect_rule() succeded\n"); #endif //test_index(); //clear_redirect_rules(); //list_rules(); return 0; }
int upnp_redirect_internal(const char * rhost, unsigned short eport, const char * iaddr, unsigned short iport, int proto, const char * desc, unsigned int timestamp, int enabled) { #ifdef MBB_FEATURE_PCP int wan_status; #endif NP_UPNP_INFO("enter upnp_redirect_internal, enabled arg is %d\n", enabled); int r = add_redirect_rule2(rhost, eport, iaddr, iport, proto, desc, enabled); NP_UPNP_DEBUG("add_redirect_rule2(...) returns %d\n", r); if(r < 0) { NP_UPNP_ERROR("add_redirect_rule2 returns value < 0\n"); return -1; } #ifdef MBB_FEATURE_PCP wan_status = MgntGetWanState(); if (WAN_STATUS_CONNECTED == wan_status) { add_pcp_rule(eport, proto, 0, enabled, iaddr, iport); } #endif #ifdef ENABLE_LEASEFILE lease_file_add( eport, iaddr, iport, proto, desc, timestamp); #endif //if(add_filter_rule2(ext_if_name, /*rhost, */iaddr, eport, iport, proto, desc) < 0) { /* clean up the redirect rule */ #if !defined(__linux__) //delete_redirect_rule(ext_if_name, eport, proto); #endif //return -1; //} if(timestamp > 0) { if(!nextruletoclean_timestamp || (timestamp < nextruletoclean_timestamp)) nextruletoclean_timestamp = timestamp; } #ifdef ENABLE_EVENTS /* the number of port mappings changed, we must * inform the subscribers */ upnp_event_var_change_notify(EWanIPC); #endif return 0; }
int upnp_redirect_internal(const char * rhost, unsigned short eport, const char * iaddr, unsigned short iport, int proto, const char * desc, unsigned int timestamp) { /*syslog(LOG_INFO, "redirecting port %hu to %s:%hu protocol %s for: %s", eport, iaddr, iport, protocol, desc); */ if(add_redirect_rule2(ext_if_name, rhost, eport, iaddr, iport, proto, desc, timestamp) < 0) { return -1; } #ifdef ENABLE_LEASEFILE lease_file_add( eport, iaddr, iport, proto, desc, timestamp); #endif /* syslog(LOG_INFO, "creating pass rule to %s:%hu protocol %s for: %s", iaddr, iport, protocol, desc);*/ if(add_filter_rule2(ext_if_name, rhost, iaddr, eport, iport, proto, desc) < 0) { /* clean up the redirect rule */ #if !defined(__linux__) delete_redirect_rule(ext_if_name, eport, proto); #endif return -1; } if(timestamp > 0) { if(!nextruletoclean_timestamp || (timestamp < nextruletoclean_timestamp)) nextruletoclean_timestamp = timestamp; } #ifdef ENABLE_EVENTS /* the number of port mappings changed, we must * inform the subscribers */ upnp_event_var_change_notify(EWanIPC); #endif return 0; }
/* upnp_redirect() * calls OS/fw dependant implementation of the redirection. * protocol should be the string "TCP" or "UDP" * returns: 0 on success * -1 failed to redirect * -2 already redirected * -3 permission check failed */ int upnp_redirect(unsigned short eport, const char * iaddr, unsigned short iport, const char * protocol, const char * desc) { int proto, r; char iaddr_old[32]; unsigned short iport_old; struct in_addr address; proto = proto_atoi(protocol); if(inet_aton(iaddr, &address) < 0) { syslog(LOG_ERR, "inet_aton(%s) : %m", iaddr); return -1; } if(!check_upnp_rule_against_permissions(upnppermlist, num_upnpperm, eport, address, iport)) { syslog(LOG_INFO, "redirection permission check failed for " "%hu->%s:%hu %s", eport, iaddr, iport, protocol); return -3; } r = get_redirect_rule(ext_if_name, eport, proto, iaddr_old, sizeof(iaddr_old), &iport_old, 0, 0, 0, 0); if(r == 0) { /* if existing redirect rule matches redirect request return success * xbox 360 does not keep track of the port it redirects and will * redirect another port when receiving ConflictInMappingEntry */ if(strcmp(iaddr,iaddr_old)==0 && iport==iport_old) { syslog(LOG_INFO, "ignoring redirect request as it matches existing redirect"); } else { syslog(LOG_INFO, "port %hu protocol %s already redirected to %s:%hu", eport, protocol, iaddr_old, iport_old); return -2; } } else { syslog(LOG_INFO, "redirecting port %hu to %s:%hu protocol %s for: %s", eport, iaddr, iport, protocol, desc); return upnp_redirect_internal(eport, iaddr, iport, proto, desc); #if 0 if(add_redirect_rule2(ext_if_name, eport, iaddr, iport, proto, desc) < 0) { return -1; } syslog(LOG_INFO, "creating pass rule to %s:%hu protocol %s for: %s", iaddr, iport, protocol, desc); if(add_filter_rule2(ext_if_name, iaddr, eport, iport, proto, desc) < 0) { /* clean up the redirect rule */ #if !defined(__linux__) delete_redirect_rule(ext_if_name, eport, proto); #endif return -1; } #endif } return 0; }