-
August 26th, 2012, 07:30 AM
#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).
- Packet from x.x.x.147 to x.x.x.146 (NIC 1)
- 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)
- Packet from x.x.x.147 to x.x.x.146 (NIC 1)
- 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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|