You are printing decimal codes for 127 0 0 1.
That normally isn't sent as a message! So either something is wrong with the sender, or those particular bytes are suppose to have the IP address within the message! For an ASCII type message it normally isn't sent!


I guess I didn't cull enough. It allows you to print formatted headers like printf!

void Mem( const void * const vp, uint nSize, const char *szText, ... )

Something like one of the following!
Mem( buffer, nPacketSize, "Packet Header #%d", 1 );
Mem( buffer, nPacketSize, "Packet Header" );

I think you'll need #include <stdarg.h> at the top of your Code file.

If you have compile issues just modifiy this code block or remove it!

#if 0
	if ( NULL != szText )	{		char	buff[ LOG_BUF_MAX ];		va_list	args; 		va_start( args, szText ); 		vsprintf(buff, szText, args);		va_end( args );		RawPrint(buff);					// Print a Banner		flg = false;	}	else
#endif

If still problems, I will repost a stripped version that will do the memory dump job.

Interesting. YOu have a fixed header,
contains

'S','A','M','P',127,0,0,1 ';', 5 Then a letter code

70 'p' contains four more bytes
69 'i'
63 'c'
72 'r'


So from that dump, they're some kind of valid message.
What's it suppose to be?

this means that your print function is totally wrong, if these are the messages being sent!

Remove the NULL check, only use the packet length!

Alright, when I use the packet length, it works.

I do not know what they are supposed to be, but I guess they are supposed to be there. All I want to do for now is let the client send it to my program, my program prints it and the program sends it to the server, and the other way around.

Question though, the program returns characters with an int value that's negative, is that correct?

Another thing, if I remove filter(buffer) by cout << buffer << endl; all it prints is 'S','A','M','P',127.

decimal value (0) is not a printable character! And negative characters are a problem.

The code has scrolled of the display but change

void printfunction( char * buffer, int size )
{
    unsigned char *p = (unsigned char *)buffer;
    unsigned char *e = p + size;

   if (size <= 0)
      return;

   while ( p < e )
   {
#if 0           // 7-bit ASCII only
      if ( ' ' <= *p && *p <= 127)
          cout << *p;
#else         // 8-bit ASCII
      cout << *p;
#endif
     p++;
   }
}

Alright, now we covered the receiving part.

But let's say I want to sent it back. How am I going to do this, because at the moment, I just sent 5 characters instead of the whole packet.

Do I have to "recreate" the buffer before sending?

You received a packet length, send the whole packet back! If their code is written properly they should be able to parse the bundled messages.

OR you know what the lead tag is, build a parser to find the start of the new message! And send back as individual messages.
I don't see a message length encoded within the message so you'll have to do it the hardway. Look for 'SAMP' header bytes.

You are acting as a proxy and relaying packets. Do you have a message specification that describes the format of the messages? You can teach your program that format so it is more aware of what it is relaying. A few years ago one of the Servers I built was the gateway. It was the connection point for clients around the world and the servers behind the firewall. It's job was to receive a message, examine the message's header and decide which server to route the message to! The same applied for responses from servers to redirect the message to the correct client. Thousands of clients connected through one or more gateways to several servers. Think MMOG.

Indeed, I am acting as a proxy.

It's about one client, sending messages to my program, and my program passing the messages trough to a server.

And the server sends messages back to me, which I send back to the client.

Just so I can read the packages.

The thing is, I still haven't been able to connect to the server this way, it should be possible by just reading the buffer and sending it to the client/server. It's done in Python by only 25 lines. However, I want to be able to do it in C++.

So in fact there is nothing like a message formatter required, right?

Oh, the server keeps sending 4,294,967,295 bytes, while the client sends 10 to 15. Is this right?

I doubt it.

There are different socket type protocols. Do you know what protocol the source and destination sockets are communicating with? Based upon the data you're seeing it doesn't look encrypted!

You're trying to intercept the message, by listening on any address.
Is your computer setup as a bridge? Two network cards? If not, how do you know the source isn't still communicating with the target!

Normally if you're trying to intercept, you aren't a node on a network, you put yourself in the data stream.

NODE --------------------HOST  --------------------------YOU
                      NODE--/       \-----NODE


NODE  --------- > YOU  < -------HOST

Which requires two separate sockets.
Otherwise you're just sniffing packets, not trying to relay them on the sly. Unless you have told the sender to send straight to you, and then you send them to where the sender originally wanted to go!


I'm not familiar with Python but what does its code look like!

4,294,967,295

0xffffffff (-1)

Check the error!

Sounds like you don't have your second connection between proxy and Host established!

let's see your code again!

int rval = send( hSock, (const char *)vp, nLen, 0 );
	if (SOCKET_ERROR == rval)
	{
		closesocket( hSock );
		hSock = INVALID_SOCKET;

		ELog( "Error: sendto() %s", ErrText( WSAGetLastError() ) );
		return false;
	}
//
//	Error String
//

char *WSock::ErrText( int err )
{
	static char buf[80];

	switch (err)
	{
		case WSAEINTR:				// 10004
			return "(10004) WSAEINTR - Interrupted function call.";

		case WSAEACCES:				// 10013
			return "(10013) WSAEACCES - Permission Denied.";

		case WSAEFAULT:				// 10014
			return "(10014) WSAEFAULT - Bad Address.";

		case WSAEINVAL:				// 10022
			return "(10022) WSAEINVAL - Invalid Argument.";

		case WSAEMFILE:				// 10024
			return "(10024) WSAEMFILE - Too many open files.";

		case WSAEWOULDBLOCK:		// 10035
			return "(10035) WSAEWOULDBLOCK - Resource temporarily unavailable.";

		case WSAEINPROGRESS:		// 10036
			return "(10036) WSAEINPROGRESS , Operation now in progress.";

		case WSAEALREADY:			// 10037
			return "(10037) WSAEALREADY - Operation already in progress.";

		case WSAENOTSOCK:			// 10038
			return "(10038) WSAENOTSOCK - Socket operation on nonsocket.";

		case WSAEDESTADDRREQ:		// 10039
			return "(10039) WSAEDESTADDRREQ - Destination address required.";

		case WSAEMSGSIZE:			// 10040
			return "(10040) WSAEMSGSIZE - Message too long.";

		case WSAEPROTOTYPE:			// 10041
			return "(10041) WSAEPROTOTYPE - Protocol wrong type for socket.";

		case WSAENOPROTOOPT:		// 10042
			return "(10042) WSAENOPROTOOPT - Bad protocol option.";

		case WSAEPROTONOSUPPORT:	// 10043
			return "(10043) WSAEPROTONOSUPPORT - Protocol not supported.";

		case WSAESOCKTNOSUPPORT:	// 10044
			return "(10044) WSAESOCKTNOSUPPORT - Socket type not supported.";

		case WSAEOPNOTSUPP:			// 10045
			return "(10045) WSAEOPNOTSUPP - Operation not supported.";

		case WSAEPFNOSUPPORT:		// 10046
			return "(10046) WSAEPFNOSUPPORT - Protobol family not supported.";

		case WSAEAFNOSUPPORT:		// 10047	
			return "(10047) WSAEAFNOSUPPORT - Address family not supported by protocol family.";

		case WSAEADDRINUSE:			// 10048
			return "(10048) WSAEADDRINUSE - Address already in use.";

		case WSAEADDRNOTAVAIL:		// 10049
			return "(10049) WSAEADDRNOTAVAIL - Cannot assign requested address.";

		case WSAENETDOWN:			// 10050
			return "(10050) WSAENETDOWN - Network is down.";

		case WSAENETUNREACH:		// 10051
			return "(10051) WSAENETUNREACH - Network is unreachable.";

		case WSAENETRESET:			// 10052
			return "(10052) WSAENETRESET - Network dropped connection on reset.";

		case WSAECONNABORTED:		// 10053
			return "(10053) WSAECONNABORTED - Software caused connection abort.";

		case WSAECONNRESET:			// 10054
			return "(10054) WSAECONNRESET - Connection reset by peer.";

		case WSAENOBUFS:			// 10055
			return "(10055) WSAENOBUFS - No buffer space available.";

		case WSAEISCONN:			// 10056
			return "(10056) WSAEIRCONN - Socket is already connected.";

		case WSAENOTCONN:			// 10057
			return "(10057) WSAENOTCONN - Socket is not connected.";

		case WSAESHUTDOWN:			// 10058
			return "(10058) WSAESHUTDOWN - Cannot Twx/Rcv after socket shutdown.";

		case WSAETIMEDOUT:			// 10060
			return "(10060) WSAETIMEDOUT - Connection timed out.";

		case WSAECONNREFUSED:		// 10061
			return "(10061) WSAECONNREFUSED - Connection refused.";

		case WSAEHOSTDOWN:			// 10064
			return "(10064) WSAEHOSTDOWN - Host is down.";

		case WSAEHOSTUNREACH:		// 10065
			return "(10065) WSAEHOSTUNREACH - No route to host.";

		case WSAEPROCLIM:			// 10067
			return "(10067) WSAEPROCLIM - Too many processes.";

		case WSASYSNOTREADY:		// 10091
			return "(10091) WSASYSNOTREADY - Network subsystem is unavailable.";

		case WSAVERNOTSUPPORTED:	// 10092
			return "(10092) WSAVERNOTSUPPORTED - Winsock.dll version out of range.";

		case WSANOTINITIALISED:		// 10093
			return "(10093) WSANOTINITIALISED - Successful WSARtartup not yet performed.";

		case WSAEDISCON:			// 10101
			return "(10101) WSAEDISCON - Graceful shutdown in progress.";

#ifdef WSATYPE_NOT_FOUND
		case WSATYPE_NOT_FOUND:		// 10109
			return "(10109) WSATYPE_NOT_FOUND - Class type not found.";
#endif

		case WSAHOST_NOT_FOUND:		// 11001
			return "(11001) WSAHOST_NOT_FOUND - Host not found.";

		case WSATRY_AGAIN:			// 11002
			return "(11002) WSATRY_AGAIN - Nonauthoritative host not found.";

		case WSANO_RECOVERY:		// 11003
			return "(11003) WSANO_RECOVERY - This is a nonrecoverable error.";

		case WSANO_DATA:			// 11004
			return "(11004) WSANO_DATA - Valid name, no data record of requested type.";

		default:
			sprintf( buf, "(%d)", err );
			return buf;
			break;
	}
}

I know I intercept all the messages, because I can set up in the config what the server IP is, which is 127.0.0.1:1339 in this case.

I also know you need two sockets, I got a program running two sockets, but for the previous problem I just made a simplified version of my program.

I am not either familiar with Python, my cousin made this one:

from socket import *;import thread,time,pickle;caddr=("127.0.0.1", 1337);saddr=("94.75.229.241", 7777);tijdbalk = [];infobalk = []

cl = socket(AF_INET,SOCK_DGRAM)
cl.bind(("",1337))

sv = socket(AF_INET,SOCK_DGRAM)
sv.connect(("94.75.229.241", 7777))

def a():
	global caddr,saddr,infobalk,tijdbalk
	while 1:
		try:
			(data, caddr) = cl.recvfrom(999999)
			tijdbalk.append(time.time())
			infobalk.append(data)
			sv.sendto(data, (saddr))
		except:
			None
		
def b():
	global caddr,saddr
	while 1:
		try:
			(data, saddr) = sv.recvfrom(999999)
			cl.sendto(data, (caddr))
		except:
			None
		
thread.start_new_thread(a,())
thread.start_new_thread(b,())

raw_input("Done?")
cl.close()
sv.close()
file = raw_input("Profile name: ")
data  = pickle.dumps([tijdbalk,infobalk])
c = open(file,"wb")
c.write(data)
c.close()

I am now trying to find out what the error message is, I will let you know in a few minutes.

I am sure it works trough UDP, because so does the Python script.

This is my real code:

#include <cstdio>
#include <winsock2.h>
#include <iostream>
#include <string>

using namespace std;

struct incSockets
{
	SOCKET* sv;
	SOCKET* cl;
	sockaddr_in* clSockAddr;
	sockaddr_in* svSockAddr;
};

void filter( char* packet, int nPacketLen )
{
	while (nPacketLen > 0)
	{
		cout << "(" << int(*packet) << ")";
		packet++;
		nPacketLen--;
	}
	cout << endl;
}

DWORD WINAPI svtocl(void* param)
{
	cout << "Arived in thread SVTOCL" << endl;
	struct incSockets* incs = static_cast<struct incSockets*>(param);
	
	SOCKET cl = *incs->cl;
	SOCKET sv = *incs->sv;
	sockaddr_in clSockAddr = *incs->clSockAddr;
	sockaddr_in svSockAddr = *incs->svSockAddr;

	char buffer[999999];
	size_t bytesReceived;
	int fromlen = 999999;
	int bytes_sent;

	while(true){
	
		bytesReceived = recvfrom(sv, buffer, 999999, 0, (struct sockaddr *)&svSockAddr, &fromlen);
		
		if(bytesReceived < 0) {
			cout << "svtocl error" << endl;
		}
		
		if(bytesReceived > 0)
		{
			cout << "Server: ";
			cout << "Bytes: " << bytesReceived << endl;
			Sleep(10000);
		}
		
		bytes_sent = sendto(cl, buffer, 999999, 0, (struct sockaddr*)&clSockAddr, sizeof(struct sockaddr_in));
		
	}			
	
}

DWORD WINAPI cltosv(void* param)
{
	cout << "Arived in thread CLTOSV" << endl;
	struct incSockets* incs = static_cast<struct incSockets*>(param);
	
	SOCKET cl = *incs->cl;
	SOCKET sv = *incs->sv;
	sockaddr_in clSockAddr = *incs->clSockAddr;
	sockaddr_in svSockAddr = *incs->svSockAddr;

	char buffer[999999];
	size_t bytesReceived;
	int fromlen = 999999;
	int bytes_sent;

	while(true){
	
		bytesReceived = recvfrom(cl, buffer, 999999, 0, (struct sockaddr *)&clSockAddr, &fromlen);
		
		if(bytesReceived < 0) {
			cout << "cltosv error" << endl;
		}
		
		if(bytesReceived > 0)
		{
			cout << "Client: ";
			cout << "Bytes: " << bytesReceived << endl;
			//filter(buffer, bytesReceived);
		}
		
		bytes_sent = sendto(sv, buffer, 999999, 0, (struct sockaddr*)&svSockAddr, sizeof(struct sockaddr_in));
	
		//Sleep(3000);
	}			
	
}

int main(int argc, char** argv) {

	struct incSockets incs;

	const int iReqWinsockVer = 2;   

	WSADATA wsaData;

	if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
	{
		if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
		{
			SOCKET cl = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
			SOCKET sv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
			
			if (cl==INVALID_SOCKET)
			{
				cout << "Client: 1" << endl;
			}
			
			if(sv == INVALID_SOCKET)
			{
				cout << "Server: 1" << endl;
			}
					
			sockaddr_in clSockAddr;
			
			memset(&clSockAddr, 0, sizeof(clSockAddr));
			
			clSockAddr.sin_family = AF_INET;
			clSockAddr.sin_port = htons(1339);
			clSockAddr.sin_addr.S_un.S_addr = INADDR_ANY;

			if (bind(cl, (sockaddr*)(&clSockAddr), sizeof(clSockAddr))!=0)
			{
				cout << "Client: 2" << endl;			
			}
									
			sockaddr_in svSockAddr;
			
			memset(&svSockAddr, 0, sizeof(svSockAddr));

			svSockAddr.sin_family = AF_INET;
			svSockAddr.sin_port = htons(7777);
			svSockAddr.sin_addr.S_un.S_addr = inet_addr("94.75.229.241");
			
			incs.clSockAddr = &clSockAddr;
			incs.svSockAddr = &svSockAddr;
			incs.cl = &cl;
			incs.sv = &sv;
			
			void* pIncs = &incs;
				
			DWORD threadId1;
			CreateThread(0, 0, cltosv, pIncs, 0, &threadId1);
			
			DWORD threadId2;
			CreateThread(0, 0, svtocl, pIncs, 0, &threadId2);

			while(true) {}
			
			closesocket(cl);
			closesocket(sv);
		}
		else
		{
			// Required version not available
		}

		// Cleanup winsock
		if (WSACleanup()!=0)
		{
			// cleanup failed
		}
	}
	else
	{
		//  startup failed
	}
    return 0;
}

Okay the Python code uses two threads. One deals with catching and relaying. The other the returned data.

The question now comes, which has been tickling at me, why are you trying to intercept client-host traffic?

(10022) WSAEINVAL - Invalid Argument.
Is the error I get.

Oh, why I do this.

Well, it is for Grand Theft Auto San Andreas Multi-Player. GTA SAMP.

If I know the packets I receive I can alter them. I am planning to make some kind of bot, but most of all, just because it's possible, and I thought this was a nice way to learn programming better.

You do not send (-) or 0 bytes! You need to only relay if bytes received is (> 0).

As to an error, you need to handle different errors appropriately.

good luck however if the server was written properly, the client is merely a playback. It can only request things of the server and its the servers job to validate a message, detect an invalid message, then log it and notify a service rep about a possible violation of rules!

commented: You've been very helpful. +1

The thing is, I guess when I fixed the (10022) WSAEINVAL - Invalid Argument we might've fixed it.

My program doesn't work yet, but my packet print problem has been solved. Thank you all very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.