CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2012
    Posts
    1

    UDP packet sent out the wrong interface

    I wrote code to proxy UDP packets from a routable subnet to a nonroutable subnet.* Everything works great in the debugger.* I see my packet copied to the nonroutable subnet with a new source IP address.* But when I run the executable outside the debugger it sends the packet out the wrong interface.* It sends it back out the routable subnet.*

    This is a sequence of when it works correctly (in debugger).
    1. Packet from x.x.x.147 to x.x.x.146 (NIC 1)
    2. Copied and sent** 192.168.0.10* to* 192.168.0.20 (NIC 2)



    This is a sequence of when it does not work correctly (outside of debugger)

    1. Packet from x.x.x.147 to x.x.x.146 (NIC 1)
    2. Copied and sent** x.x.x.146* to* 192.168.0.20* (NIC 1)



    My code does not change.* Why does it behave different when I'm not running in the debugger?

    Code:
    // udpproxy.cpp : main project file.
    
    #include "stdafx.h"
    
    #pragma comment(lib,"ws2_32.lib")
    #include<Winsock2.h>
    #include<stdio.h>
    #include<stdlib.h>
    #define BUFFER_LENGTH 1500
    #define MAX_PORT_COUNT 20
    #define DONT_FRAGMENT false
    
    using namespace System;
    using namespace System::Net;
    using namespace System::Net::Sockets;
    
    int main(int argc, char *argv[]) {
    	WSADATA wsaData;
    	Socket ^sSocket;
    	int iLen;
    	int iSend;
    	int iRecv;
    	int bytesSent = 0;
    	int srvPort = 0;
    	int dstPort = 0;
    	char recv_buf[BUFFER_LENGTH];
    	array<UdpClient^>^ destination = gcnew array<UdpClient^>(MAX_PORT_COUNT);
    	int iAddressCount = 0;
    	char *address;
    	char *srcAddressChar;
    	int i = 0;
    
    	struct sockaddr_in ser,cli;
    
    	if (argc < 4) {                  // Test for correct number of parameters
    		printf("Usage: UDPProxy <Server Port> <Dest Port> <Dest IP Address>[,<IP Address>,<IP Address>,...] ");
    		return -5;
    	}
    	
    	if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    	{
    		printf("Failed to load Winsock.\n");
    		return -1;
    	}
    
    	srvPort = atoi(argv[1]);
    	printf("--------------------------\n");
    	printf("Listening to port %i\n", srvPort);
    
    	dstPort = atoi(argv[2]);
    	printf("Forwarding to port %i:\n", dstPort);
    	
    	printf("Targets:\n");
    	address = strtok(argv[3], ",");
    	while(address != 0)
    	{
    		System::String ^dstAddress = gcnew System::String(address);
    		destination[iAddressCount] = gcnew UdpClient(dstAddress, dstPort);
    		printf("     %s\n", dstAddress);
    		iAddressCount++;
    		address = strtok(0, ",");
    	} 
        printf("--------------------------\n");
    
    	UdpClient^ client = gcnew UdpClient();
    	client->DontFragment = DONT_FRAGMENT;
    	
        Socket^ socket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp);
        IPEndPoint^ ipep = gcnew IPEndPoint(IPAddress::Any, srvPort);
    
        socket->Bind(ipep);
    
        while(true) 
        {
            array<unsigned char>^ message = gcnew array<unsigned char>(BUFFER_LENGTH);
            EndPoint^ Remote = (EndPoint^) gcnew IPEndPoint(IPAddress::Any, 0);
    
            int recv = socket->ReceiveFrom(message, Remote);
    		
    		//printf(".");
    
    		for(int i=0; i < iAddressCount; ++i)
    		{
    			bytesSent = destination[i]->Send(message, recv);
    			if(bytesSent == recv)
    			{
    				printf(".");
    			}
    		}
        }
    
    	WSACleanup();
    
    	return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: UDP packet sent out the wrong interface

    Your code has nothing to do with this Visual C++ Forum. Nor is it a valid native C++.
    If it is something like managed C++ code then please, ask your question in the corresponding Managed C++/CLI forum.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: UDP packet sent out the wrong interface

    Why does it behave different when I'm not running in the debugger?
    Usually the most probable reason is: your code has some error.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured