Example #1
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),
	                     &timestamp,
	                     &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;
}
Example #2
0
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;
}
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;
}
Example #4
0
/* called from natpmp.c too */
int
_upnp_delete_redir(unsigned short eport, int proto)
{
	int r;
#if defined(__linux__)
	r = delete_redirect_and_filter_rules(eport, proto);
#else
	r = delete_redirect_rule(ext_if_name, eport, proto);
	delete_filter_rule(ext_if_name, eport, proto);
#endif
#ifdef ENABLE_LEASEFILE
	lease_file_remove( eport, proto);
#endif

#ifdef ENABLE_EVENTS
	upnp_event_var_change_notify(EWanIPC);
#endif
	return r;
}
/* called from natpmp.c too */
int
_upnp_delete_redir(unsigned short eport, int proto)
{
    int r;
#ifdef MBB_FEATURE_PCP
    int wan_status;
    PARA_TO_PCP_PROC_ST pcp_para;
#endif

#ifdef MBB_FEATURE_PCP
    wan_status = MgntGetWanState();
    if (WAN_STATUS_CONNECTED == wan_status)
    {
        memset(&pcp_para, 0, sizeof(pcp_para));
        //eport是pcp的iport
        fill_pcp_process_para(&pcp_para, 0, NULL, eport, proto, 0, 0, NULL);
        send_msg_to_pcp_client(ATP_MSG_PCP_DELETE_PORTMAP, &pcp_para);
    }

#endif

#if defined(__linux__)
    NP_UPNP_INFO("enter _upnp_delete_redir()\n");
    NP_UPNP_DEBUG("defined(__linux__), call delete_redirect_and_filter_rules()\n");
    r = delete_redirect_and_filter_rules(eport, proto);
#else
    r = delete_redirect_rule(ext_if_name, eport, proto);
    delete_filter_rule(ext_if_name, eport, proto);
#endif
#ifdef ENABLE_LEASEFILE
    lease_file_remove( eport, proto);
#endif

#ifdef ENABLE_EVENTS
    upnp_event_var_change_notify(EWanIPC);
#endif
    return r;
}
Example #6
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;
}
Example #7
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;
}