Core part of code is as below.
Actually, the code is copied from https://github.com/ECToo/world-opponent-network/blob/fbb35876ae26006606d07b6297d557bd53234066/%20world-opponent-network/TitanApi/Samples/ServerTest%20-%20Copy/Sniffer/main.cpp
The code can capture UDP both incoming/outgoing packets and TCP outgoing packets, but cannot capture any TCP incoming packets on Windows10.
however, it works quite well on Windows XP.
sniffer = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if (sniffer == INVALID_SOCKET) {
printf("Failed to create raw socket.\n");
return 1;
}
memset(&dest, 0, sizeof(dest));
memcpy(&dest.sin_addr.s_addr, local->h_addr_list[in], sizeof(dest.sin_addr.s_addr));
dest.sin_family = AF_INET;
dest.sin_port = 0;
printf("\nBinding socket to local system and port 0 ...");
if (bind(sniffer, (struct sockaddr *)&dest, sizeof(dest)) == SOCKET_ERROR) {
printf("bind(%s) failed.\n", inet_ntoa(addr));
return 1;
}
printf("Binding successful");
//Enable this socket with the power to sniff : SIO_RCVALL is the key Receive ALL ;)
j = 1;
printf("\nSetting socket to sniff...");
if (WSAIoctl(sniffer, SIO_RCVALL, &j, sizeof(j), 0, 0, (LPDWORD)&in, 0, 0) == SOCKET_ERROR) {
printf("WSAIoctl() failed.\n");
perror("Error:");
return 1;
}
printf("Socket set.");
//Begin
printf("\nStarted Sniffing\n");
printf("Packet Capture Statistics...\n");
StartSniffing(sniffer); //Happy Sniffing
//End
closesocket(sniffer);
WSACleanup();
Then, I tried https://www.netresec.com/?page=RawCap which is written in C#, same result as C++ code.
At last, I tried raw socket in python, it works quite well on Win10.
import socket
from struct import *
host = '10.0.0.18'
# create a raw socket and bind it to the public interface
socket_protocol = socket.IPPROTO_IP
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 0))
# we want the IP headers included in the capture
#sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
packet = sniffer.recvfrom(65565)
#packet string from tuple
packet = packet[0]
#take first 20 characters for the ip header
ip_header = packet[0:20]
#now unpack them :)
iph = unpack('!BBHHHBBH4s4s' , ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
total_len = iph[2]
ttl = iph[5]
protocol = iph[6]
s_addr = socket.inet_ntoa(iph[8]);
d_addr = socket.inet_ntoa(iph[9]);
if protocol == 6:
print 'Length : ' + str(total_len) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)
Quite strange, is there any mistake I made for the C++ code?
Related
I want to create same message and send it with C# as I do it with C++ where all works. Note that I have C# client where I have troubles, C++ client where all works fine and C++ server that should read messages from both C# and C++ clients.
Here is how I send the message from C++:
void ConnectAuthserverCommand::SendLogin(tcp::socket &s, const flatbuffers::FlatBufferBuilder &builder) const {
ClientOpcode opc = CLIENT_LOGIN_REQUEST;
flatbuffers::FlatBufferBuilder builder2;
auto email = builder2.CreateString("test#abv.bg");
auto password = builder2.CreateString("test");
auto loginRequest = Vibranium::CreateLoginRequest(builder2, email, password);
builder2.FinishSizePrefixed(loginRequest);
size_t size2 = builder2.GetSize();
uint8_t *buf2 = builder2.GetBufferPointer();
uint8_t *actualBuffer2 = new uint8_t[size2 + 2];
actualBuffer2[1] = (opc >> 8);
actualBuffer2[0] = (opc&0xFF);
memcpy(actualBuffer2 + 2, buf2, size2);
boost::asio::write(s, boost::asio::buffer(actualBuffer2,size));
}
ClientOpcode is as follows:
enum ClientOpcode : uint16_t{
CLIENT_AUTH_CONNECTION = 0x001,
CLIENT_LOGIN_REQUEST = 0x002,
CLIENT_NUM_MSG_TYPES = 0x003,
};
What I do is the following: I get a ClientOpcode which I want to put infront of FlatBuffers message. So I create an array of uint8_t which I extend with exactly 2 bytes(Because the size of uint16_t is 2 bytes.) Than on the server I read the first 2 bytes in order to get the header and here is how I do that:
void Vibranium::Client::read_header() {
auto self(shared_from_this());
_packet.header_buffer.resize(_packet.header_size);
boost::asio::async_read(socket,
boost::asio::buffer(_packet.header_buffer.data(), _packet.header_size),
[this, self](boost::system::error_code ec,std::size_t bytes_transferred)
{
if ((boost::asio::error::eof == ec) || (boost::asio::error::connection_reset == ec))
{
Disconnect();
}
else
{
assert(_packet.header_buffer.size() >= sizeof(_packet.headerCode));
std::memcpy(&_packet.headerCode, &_packet.header_buffer[0], sizeof (_packet.headerCode));
if(_packet.headerCode)
read_size();
else
Logger::Log("UNKNOWN HEADER CODE", Logger::FatalError);
}
});
}
So far so good, however I am not able to send correctly formatted same message from the C# client. Note that I send exactly same data, take a look:
Client authClient = GameObject.Find("Client").GetComponent<AuthClient>().client; // This is how I get Client class instance.
ClientOpcode clientOpcode = ClientOpcode.CLIENT_LOGIN_REQUEST;
var builder = new FlatBuffers.FlatBufferBuilder(1);
var email = builder.CreateString("test#abv.bg");
var password = builder.CreateString("test");
var loginRequest = LoginRequest.CreateLoginRequest(builder, email, password);
builder.FinishSizePrefixed(loginRequest.Value);
authClient.Send(builder, clientOpcode);
And here is how I actually prepend the header and send the data in C#:
public static Byte[] PrependClientOpcode(FlatBufferBuilder byteBuffer, ClientOpcode code)
{
var originalArray = byteBuffer.SizedByteArray();
byte[] buffer = new byte[originalArray.Length + 2];
buffer[1] = (byte)((ushort)code / 0x0100);
buffer[0] = (byte)code;
Array.Copy(originalArray, 0, buffer, 2, originalArray.Length);
return buffer;
}
public void Send(FlatBufferBuilder builder, ClientOpcode opcode)
{
byte[] buffer = builder.SizedByteArray();
var bufferToSend = PrependClientOpcode(builder, opcode);
if (bufferToSend.Length > MaxMessageSize)
{
Logger.LogError("Client.Send: message too big: " + bufferToSend.Length + ". Limit: " + MaxMessageSize);
return;
}
if (Connected)
{
// respect max message size to avoid allocation attacks.
if (bufferToSend.Length <= MaxMessageSize)
{
// add to send queue and return immediately.
// calling Send here would be blocking (sometimes for long times
// if other side lags or wire was disconnected)
sendQueue.Enqueue(bufferToSend);
sendPending.Set(); // interrupt SendThread WaitOne()
}
}
else
{
Logger.LogWarning("Client.Send: not connected!");
}
}
ClientOpcode enum on C# is as follows:
public enum ClientOpcode : ushort
{
CLIENT_AUTH_CONNECTION = 0x001,
CLIENT_LOGIN_REQUEST = 0x002,
CLIENT_NUM_MSG_TYPES = 0x003,
}
I think I can use ushort as a replacement of uint16_t in C#. That is why ClientOpcode is ushort.
When I send the message I get error on the client saying UNKNOWN HEADER CODE. If you take a look at the C++ server code to read the header you'll see that this message is displayed when the server is unable to read the header code. So somehow I am unable to place the ClientOpcode header correctly infront of the TCP message send from the C# client.
In order to find out what are the differences I installed WireShark on the host to track both messages. Here are they:
This one is from the correctly working C++ client:
And this one is the dump of the C# client:
As you can see on the second image of the TCP dump the Length of is bigger. C++ message is with length of 58 where C# message's length is 62. Why?
The C++ client is sending data:
0200340000000c00000008000c00040008000800000014000000040000000400000074657374000000000b00000074657374406162762e626700
When the C# client is sending:
0000003a0200340000000c00000008000c00040008000800000014000000040000000400000074657374000000000b00000074657374406162762e626700
The C# client is adding to it's message in front 0000003a. If I remove that messages should be the same and all will work.
Why is my C# client adding those extra data in front and how can I fix it?
I'm working with a WiFi module written in C that connects to a TCP server written in C++/WinAPI. At the moment, I'm trying to write up some documentation on the protocol being used, as none currently exists. In order to make this a bit easier, I've written a simple application in C#/WinForms in order to more easily send and read received packets.
When working with the existing server, the module connects and then sends a packet with a serial number (i.e. #12345) as a byte array. A green LED also begins blinking when connected to the socket.
When using my new testing server, the server detects that the client is connected (socket.Connected == true), then StreamReader.ReadLine() begins receiving endless nulls. The LED on the client also does not blink green, so it looks as though the client is not connected to the socket.
I don't have a ton of experience with TCP/IP, can anyone see any flaws with the route I'm taking or think of any possible reason why the client end isn't detecting the connection/ sending it's serial number, while the server seems to be working just fine? Thanks in advance for any input.
EDIT:
Here is some of the state machine the module uses to connect to the server..
switch(TransmitState)
{
case CHECK_POLLTIME:
if(PollCounter >= Device.MinutesPerPoll)
{
PollCounter = 0;
TransmitState++; // initiate server communications
}
break;
case LOAD_SOCKET:
if(AppConfig.IsLinked != LINKED) break;
// Connect a socket to the remote TCP server
if(!TCPIsConnected(socket))
socket= TCPOpen(server_ip, server_port);
TransmitState++;
Timer = TickGet();
break;
case POLL_SERVER:
// Wait for the remote server to accept our connection request
if(!TCPIsConnected(socket))
{
// Time out if too much time is spent in this state
if(TickGet()-Timer > 5*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TransmitState = SM_DISCONNECT;
}
break;
}
Timer = TickGet();
if(Bootloader)
break;
// Make certain the socket can be written to
if(U2Tx.count != 0) break;
// Place the application protocol data into the transmit buffer.
//packet needs to include UART commands to get through
TCPBuffer[0] = 0x1B; //ESC
TCPBuffer[1] = 'Z';
TCPBuffer[2] = AppConfig.ConnectionID;
TCPBuffer[3] = '0';
TCPBuffer[4] = '0';
TCPBuffer[5] = '0';
TCPBuffer[6] = '6';
TCPBuffer[7] = '#'; // represents serial number
TCPBuffer[8] = Device.Serial[0];
TCPBuffer[9] = Device.Serial[1];
TCPBuffer[10] = Device.Serial[2];
TCPBuffer[11] = Device.Serial[3];
TCPBuffer[12] = Device.Serial[4];
WF_TxBlock(TCPBuffer, 13);
TransmitState++;
break;
case PROCESS_RESPONSE:
...
I am trying to send a word over to an Arduino running as a server, from a WPF C# application. Every now and again the complete work is not sent.
C# Code
public void send(String message)
{
TcpClient tcpclnt = new TcpClient();
ConState.Content = "Connecting.....";
try
{
tcpclnt.Connect("192.168.0.177", 23);
ConState.Content = "Connected";
String str = message;
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
tcpclnt.Close();
}
catch (Exception)
{
ConState.Content = "Not Connected";
return;
}
}
How it is sent to the method:
String mes = "back;";
send(mes);
Arduino code:
if (client.available() > 0) {
// Read the bytes incoming from the client:
char thisChar = client.read();
if (thisChar == ';')
{
//Add a space
Serial.println("");
}
else {
//Print because it's not a space
Serial.write(thisChar);
}
}
The Arduino is using the chat server example. I am sending "back;" and "forward;" across. The results on the serial monitor:
back
forwaback
forward
back
forwaforwar
The problem seems to be with this code:
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
...
}
What it does is:
Check if we have received data from the client
Read a single byte from the client buffer
Exit, and go on to do other things
As the OP pointed out, this comes direct from Arduino chat server example. In that example, this working correctly in loop() depends on the alreadyConnected flag being set right after a new connection is made: if it isn't, then the buffer is flushed before any data is read. That's one possible landmine.
Nonetheless, there is no reason to change the if block to be a while loop in the OP's case so, in other words instead of
if (client.available() > 0) {
have
while (client.available() > 0) {
The only reason to have an if statement there is to make sure that you frequently do other processing in loop() if you have clients that send a lot of data: If the reading of client data is done from inside a while this loop will not exit until the there is no more data from the client. Since this doesn't seem to be an issue in the asked-about case, the if to while change makes sense.
We have web application , it works as Socket Listner. I wanted to check before the AcceptSocket , whether there is any Client Socket available to connect to this listner. I want to display a message if there is no Client Socket to connect and cancel send/receive of data.
I am using TCPListner using ASP.net C# for the web application. The Client socket is windows VB6 application using Winsock control.
Socketing programme, ASP.Net C#, VS2008
Thanks you for the reply ,
see below my sample code for communicate with server
clsCommunication.cs, this class file imports into .aspx page. I tried using Console.WriteLine(), but cann't shown this message.
I wanted to display alert box , so that user understands there is no connection OR display on status bar or highlight a frame/box etc.
public void Communicationcation()
{
byte[] bytes = new byte[1024];
string strSiteID = "SiteID";
socket.Start();
if (!socket.Pending())
{
Console.WriteLine("Sorry, no connection requests have arrived");
}
else
{
client = socket.AcceptSocket();
if (client.Connected == true)
{
decimal dSiteID = decimal.Parse(GetSiteSetting(ref strSiteID).ToString());
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
WriteLog(string.Format("Connected with {0} at port {1}", clientep.Address, clientep.Port));
string strWelcome = STARTMESSAGE + "WHOAMI" + " " + dSiteID + " " + dSiteID + FINISHMESSAGE;
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(strWelcome);
int i = client.Send(data, data.Length, SocketFlags.None);
WriteLog(String.Format("Message sent {0} bytes!", i));
//Get reply from the Connected cleint.
i = client.Receive(bytes, bytes.Length, SocketFlags.None);
if (i != -1)
{
WriteLog(string.Format(System.Text.Encoding.UTF8.GetString(bytes)));
}
}
}
}
You can use TcpListener's Pending method, see http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.pending.aspx
I read the tutorials and so, but I am not getting it. It does let you send packets, but how can you tell Winpcap where to send those packets? Is there any header I should put on the packets so it will know to which ip/port's to forward it? I mean. Let's imagine I want to send some data to my MSN, as if I had wrote something to someone on my list. I can use the sendpacket(), but it will only take the packet/byte array as argument, not specifing to which app/ip/port so send it.
Thanks
You don't tell Winpcap where to send packets. You tell it to put a packet on the wire. The network switch will send the packet to the right destination. The TCP stack on the receiving end will send the packet to the right application/service. Obviously this means the routing information has to be in the packet itself.
To take your example, you'd need to put the IP address and TCP port of the appropriate MSN server in the packet. If you don't, your networking hardware will discard or misroute that packet.
This is how i sent an ARP request over the wire.
1. Define structures for the protocols i.e if you want to send ARP packets you will need a structure that will contain the data link layer(Ethernet header) and network layer (ARP Header). Correspondingly if you want to send a tcp packet over IP you will need a data structure for the ethernet header, ip header and tcp header.
once you defined the structures, initialize an instance of the structure with values you want i.e. if you want the packet to go to all machines in the network set the destination mac value of the Ethernet header to ff:ff:ff:ff:ff:ff if you want to send the packet to machine X with(IP address 192.168.0.88) then set the destination address in the ip layer to that value.
Once done you will need to declare a char* array and copy all the structures to the char* array to create a byte sequence and send it over the wire.
//Just to show you what i mean by defining structures doesn't //relate to the rest of the code snippet
typedef struct IP_header
{
u_char VersionNInternetHeaderLength; // Version (4 bits) + Internet header length (4 bits)
/*u_char version:4;
u_char HeaderLength:4;*/
u_char Type; // Type of service
u_short TotalLength; // Total length
u_short Identification; // Identification
u_char rsv : 1;
u_char df : 1;
u_char mf : 1;
u_char FragmentOffset1 : 5;
u_char FragmentOffset2;
//u_short Flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char TimeToLive; // Time to live
u_char Protocol; // Next level Protocol of the encapsulated payload
u_short Checksum; // Header checksum
IP_address SourceAddress; // Source address
IP_address DestinationAddress; // Destination address
u_int OptionNPadding; // Option + Padding
IP_header()
{
mf = 0;
rsv = 0;
df = 0;
FragmentOffset1 = 0;
FragmentOffset2 = 0;
TimeToLive = 128;
TotalLength = sizeof(IP_header);
Identification = 0xABCD;
Checksum = 0xABCD;
OptionNPadding = 0;
}
}IP_header;
Ethernet_header EthernetHeader;// = (Ethernet_header*)malloc(sizeof(Ethernet_header));
ARP_header ARPHeader ;//= (ARP_header*)malloc(sizeof(ARP_header));
ARPHeader.HardwareType = htons(1);
ARPHeader.ProtocolType = htons(0x800);
ARPHeader.OPCODE = htons(1);
ARPHeader.HeaderLength = 6;
ARPHeader.ProtocolLength = 4;
ARPHeader.SenderMAC = MY_FAKE_MAC;
ARPHeader.SenderIP = MY_IP;
ARPHeader.TargetMAC = MAC_address();
ARPHeader.TargetIP = Whose;
EthernetHeader.DestinationMAC = BROADCASTMAC;
EthernetHeader.SourceMAC = MY_FAKE_MAC;
EthernetHeader.EtherType = htons(0x806);
u_char* packet = (u_char*)malloc(sizeof(EthernetHeader) + sizeof(ARPHeader));
memcpy(packet, &EthernetHeader, sizeof(EthernetHeader));
memcpy(packet + sizeof(EthernetHeader), &ARPHeader, sizeof(ARPHeader));
SendPacket(packet);