Is there a module replace with winpcap and scapy in Windows? or binding network-interface directly like using SO_BINDTODEVICE in linux - sockets

I'm looking for new solution to play h.264 steaming video ,which is based on 1722 protocol and entered in Ethernet from other device, in Windows 7 or 10 by using socket similarly with the way from linux environment.
I can bind network-interface directly using option of SO_BINDTODEVICE in linux and if I use that, video streaming is so smooth in vlc player and vlc statistics show bitrate is over 20,000 kb/s
so I tried two manners in Windows like below:
using scapy module in python
sniffing all raw packet Ethernet
attach data on payload and send packet to vlc player
result is poor, because the bitrate in vlc statistics is almost under 1500kb/s
using winpcap lib in C(VS)
sniffing all raw packet through pcap_next_ex or pcap_loop
attach data on payload and send packet to vlc player
result is bad, the bitrate in vlc statistics is 3000kb/s
1.
global dgramSock
dgramSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def prn(pkt):
global dgramSock
...
#filter to pick specific packet
...
#attach data on payload and naming myPacket
...
send_len = dgramSock.sendto(myPacket, ('127.0.0.1', 44514))
sniff(prn = prn, filter='ether)
2.
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(44514);
...
while( (pcap_next_ex(_handle, &header, &pkt_data)) >=0){
...
#filter to pick specific packet
...
#attach data on payload and naming myPacket
...
sendto(sock, (char *)myPacket, myPacketSize, 0, (struct sockaddr *)&(addr), sizeof(addr));
}
I think that two manner what I tried looks not essential way.
In Windows, what is the best solution to send raw packet to other program fastly without loss?

I don't know exactly my solution is correct approach or not but I left my solution for one who seek some solution same problem like me
My wrong approach what I have been was using VLC in Windows.
I guess VLC listener buffer seem to small not enough to get all streaming data from socket, so streaming data looks corrupted or broken, even though little part of top in video played normal streaming.
So I tried new player, FFmpeg. FFmpeg provide ffplay.exe. It seems to decoder same with VLC.
I just send streaming data through UDP stack and valid port from Visual studio or Python. There is no issue using UDP socket, but you need to check listener buffer is enough to get all packets coming via network card.(like API: pcap_set_buffer_size)
If you can success to send your streaming data toward specific port what you want to send, next step is run ffplay with some command.
simple example command to run ffplay
ffplay.exe -codec:v h264 -i udp://127.0.0.1:44514 -framerate 30
Tip) For beginner in ffmpeg like me, you should keep order of command, ffplay and ffmpeg have strong rule of arguments, but simple rule is explained as input and output rule. You have to give input option first and output option come behind -i(input) option
There is many branch to using ffplay or to your environment so my explanation would not match correctly in many situations.
If I can answer your question, I will keep track on that.
Thank you

Related

How do I programm a PC as a PLC that has registers that are readable via Modbus using Beckhoff Twin CAT 3 TCP Modbus?

I would like to use Beckhoff Twin CAT 3 TCP Modbus module to make registers in a PC which is running as a PLC readable via Modbus.
I have downloaded the function Modbus TCP from the Backhoff website. I have followed the example in the Manual TF6250 TwinCAT 3 | Modbus TCP page 55. When I try to read the register at address 0x3000 with a modbus client I get an invalid address error.
The code looks as follows:
PROGRAM MAIN
VAR
ipAddr : STRING(15) := '';
nValue AT%MB0 : ST_EM_Ausgangsdaten_Float;
fbWriteRegs : FB_MBWriteRegs;
bWriteRegs : BOOL;
END_VAR
IF NOT bWriteRegs THEN
nValue.BlindleistungL1 := nValue.BlindleistungL1+1;
nValue.BlindleistungL2 := nValue.BlindleistungL2+1;
nValue.BlindleistungL3 := nValue.BlindleistungL3+1;
bWriteRegs :=TRUE;
fbWriteRegs.sIPAddr :=ipAddr;
fbWriteRegs.nQuantity := 1;
fbWriteRegs.nMBAddr := 16#3000;
fbWriteRegs.cbLength := SIZEOF(nValue);
fbWriteRegs.pSrcAddr := ADR(nValue);
fbWriteRegs.tTimeout := T#5S;
fbWriteRegs(bExecute:=TRUE);
ELSE
IF NOT fbWriteRegs.bBUSY THEN
bWriteRegs :=FALSE;
END_IF
fbWriteRegs(bExecute:=FALSE);
END_IF
Could someone point me to the direction of how to read a variable in a PLC via Modbus.
If I understand your question correctly you are wanting run a Modbus TCP Server (and from your comments it sounds like you have already got something running, but you might not understand exactly why).
I am sure that you know this, but Modbus TCP works by Clients issuing Modbus commands to Read/Write data to/from a Modbus Server and the server responds with the data (or success). The TF6250 communication module allows you to do this in a few ways.
The first issue you have is that the sample code on page 55 you implemented is for the "FB_MBWriteRegs" function. This is a function where your program is acting as a Modbus client (and not a server). It is trying to connect to a remote server and write data to the Modbus address on that server. The description in the manual probably isn't the best and I can see how it may be misleading.
In your case (as it is in the sample code) the STRING ipAddr is empty. I wouldn't be surprised if your fbWriteRegs is reporting an error. You could check this by inspecting the value of the fbWriteRegs.bError and fbWriteRegs.nErrId tags.
For this code to work you would need to connect to an existing Modbus TCP Server and populate the correct IP address.
Additionally, I don't know what data type "ST_EM_Ausgangsdaten_Float" is, but given that this function is for writing to output registers, I wouldn't be surprised if there were issues there as well.
In any case, this isn't what you are wanting to do. I think you will find that if you remove/delete this code and leave your variables mapped as globals it will still 'work'.
What you are probably interested in, is section 4.2 and 4.3.
TF6250 installs a Windows Application that acts as a Modbus TCP server. This server acts as a Modbus to ADS converter which maps values from Modus registers to PLC memory areas via ADS.
You can access the configuration of the Modbus TCP server and the mapping from the TwinCAT Modbus TCP Configuration Tool. For windows this is usually located in the "C:\TwinCAT3\Functions\TF6250-Modbus-TCP" directory. (For Twicat/BSD it is a different procedure all together).
The config app looks like this;
If you click "Get Configuration" - wait a while until it loads, and then "Export Configuration" you can save the mapping/config in a XML file.
The Default mapping is shown on page 19 in section 4.3, which is how I suspect yours is currently working.
If you want to map directly to memory areas rather than via global you will need to know your IndexGroup and Index Offset available here and here. Note: I understand mapping this way improves performance for larger amounts of data but I haven't tested it.
You can manipulate the XML file for the mapping you require. However if you are able to choose whatever memory area you like, I would leave the default configuration for what you want to do and delete the rest of the config, then I would map my data to the appropriate TwinCAT memory area, but that is entirely up to you.
After you have modified your XML file, you can use the Config tool to "Import Configuration" select your modified XML file, and then "Set Configuration" to update the mapping.
You should then be able to use a Modbus Client to connect to your modbus server and know EXACTLY what data is being written to what Modbus address and thus memory area (%M, %Q, %I etc...)
Good luck!

How do I decode a websocket packet?

I'm using Wireshark packet analyzer & when I filter for all "Websocket" packets I see what I am sending /receiving to the host. When I check individual packets mine always show as [MASKED], but you can 'Umask Payload' which shows the data in clear text that looks like this:
<IC sid="52ccc752-6080-4668-8f55-662020d83979" msqid="120l93l9l114l30l104"/>
However, if I 'Follow TCP stream & look at that same packet, the data shows up as encoded in some way like this:
....K#....../...y#..|...}...f...s...~...}...{G..r...kN.."G..z...r...'...'...z...d.
The problem is all Websocket packets I receive from the host come as encoded, it is NOT SSL & I can't figure out how to decode them, I have no idea what they are even encoded as (but yet my browser can decode it).
I assume that whatever method they are coming back to me as encoded data is the same method that my data is encoded when I use 'Follow TCP stream'.
Can someone please help me figure out how to decode the data the host is sending me? See host data below
~.^jVpZc9y4Ef4ryFQ5+yJpeB+JJJdmNPI6G++mrN249kkFkuChIQmG5Fgj//p0AyAJypzxyi5T6P76
QKPRuHz9cUu6IrlZuVYcx75rXXpGYFw6nhdcBqnrXnqeZVhGEtihH65Id7NKWEoPZb8iVfc/FDQt
owztMixN0yltozQNZ3V7ncZkbxrAXZE8vFkZK2g66msJchLIjyuoiWQmvvyApGUY+JsJKPGLkrIF
IHcFALVJNXtTWsl9adMDPlAtQ1AZME0XvoFsShDz5McVn0J6y2z5ceTHlB9pnEltheQVEllIXiGR
z7Ifz6Cz4c2h6XkDLTDUFlkOQYuk/5EUimTnIykUyc5HoeTJjlHVMgWPwifv++Yf6/XLy8uVadlp
Sbs8zml/FfMKAKA4Z2WzLuqEHa/yviqBCEZXJJXeUzC25c2rcIhAEM1LyzBt8jtvtp8+kUee9i+0
ZWTL2+aKkLuyJJ8R2pHPrGPtV5ZcgRIXNVLoF6vh62tpkToy9LIzexnxvRydWEY8lhGPZRxjGccY
IDBEezkMsZSLlRZLtmQQYhm8WCBvr2lAMhFVyDqPpKDmPy1Pi5KtSGaM4Xrlh/aFRV3Rs3Uj+VdN
3rw/QJ9u3v3xuPv8DhSsUw/+ocHtdeKRDNz0wF4GfjpesJrM+CQDx5ACHtFkHdG6Zq159dxkQLPO
jxFa8Ucl7hsl1l9Sss5518vRPa/Ovupe0r+i7q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ge11srEIg0nPaJwPQNVc8usV
X0/J2NuO0a5ImI4UVwuPv3z89enu8+ffvpCvFKYgFJq2nXyc2LiG3l7H+R6yH/9PjGGw0LfTyLGN
0Ehdzw4DM6apZaXM8pMA/+NMfLNyXc+IAwcgIaNBEDhmStPYMSPT9izHSMA5QCUA8tMoCFxGHWpa
phn5lhVYNAlTljoueiItwy8ft7f/Bw==
Client to server data is XORed with a mask (included in the dataframe). Some people suggest this is in order to throw off bad caching mechanisms responding to new websocket requests with server messages from older sessions. The masking makes sure that even messages containing identical data will appear differently to applications that do not understand websockets.
Also note that there are many different size options for the headers themselves.
Refer to RFC 6455 Section 5 which defines the masking/unmasking process for payloads sent from the client to the server.
https://www.rfc-editor.org/rfc/rfc6455
If you find any freeware VBA code to do the job of forming packets let me know! :-)

Scapy Sends Malformed Packets

I'm sending out probe requests using scapy. It works perfectly fine on my desktop but when I send it out from scapy, using the exact same code, the packets arrive malformed. I'm watching them in wireshark.
The malformed one has a Logical-Link Control layer and the bits are all just out of order. I don't really know how else to put it. The source and destination mac addresses are both offset by a few bits. The packet is twice as large, I'm just really baffled.
For example
in scapy, my destination address is "aa:bb:cc:dd:ee:ff"
In the packet capture, the destination is "00:00:00:aa:bb:cc"
EDIT:
The packets show up fine on my laptop in wireshark, but in wireshark on my desktop is where there is an issue.
sendp(Dot11(addr1=dest,
addr2=source,
addr3=source)/
Dot11ProbeReq()/
Dot11Elt(ID="SSID",info='test')/
Dot11Elt(ID="Rates", info='\x02\x04\x0b\x16\x0c\x12\x18$')/
Dot11Elt(ID="ESRates", info='0H`l')/
Dot11Elt(ID="DSset", info='\x06'),
iface='wlan0', count=3)
EDIT: I believe the issue is because scapy is sending the wrong type/subtype.
The packet should have
Type/subtype: Probe Request (0x04)
but the packet in wireshark displays
Type/subtype: Data (0x20)
Monitor mode was not initiated correctly. The packets became malformed when not sent over a monitor interface.
try
sendp(RadioTap()/
Dot11(addr1=dest,
addr2=source,
addr3=source)/
Dot11ProbeReq()/
Dot11Elt(ID="SSID",info='test')/
Dot11Elt(ID="Rates", info='\x02\x04\x0b\x16\x0c\x12\x18$')/
Dot11Elt(ID="ESRates", info='0H`l')/
Dot11Elt(ID="DSset", info='\x06'),
iface='wlan0', count=3)

Test Harness for a Clojure TCP Client

I'm writing a TCP client to connect, send messages to, and read responses from nodes on a busy network. I do not have access to code running on those nodes, so I have to sculpt the TCP messages I send out over the wire very carefully.
I've decided that I'm going to gather a bunch of live TCP data off of the network and use that as the basis for testing my client - given these input parameters, make sure that the transmitted binary renders into the hex I expect to see.
At this point I'm about to either fire up Wireshark and figure out how to filter for the packets that I'm sending or implement a really simple TCP server that waits for connections, spits the transmitted binary to disk (maybe processed to hex) and then test that I'm sending what I expect to send; neither of which feel like robust or professional solutions.
So: how would you recommend setting up a test harness for a TCP client to verify the right hex messages are coming out?
You should check out aleph for tcp and gloss for protocols. here is a tutorial and an example.
a simple tcp echo-server could be:
(defn echo-handler [ch client-info]
(siphon ch ch)) ;; a simple echo handler
(defn start-server [] (start-tcp-server
echo-handler
{:port 9997}))
(start-server)
gloss will be very useful at spitting the received messages either as hex dump or as values if you encode your protocol in it and use "decode".

TComPort and Modbus-RTU?

It is possible to read and send data with TComPort for modbus RTU protocol?
I have read wiki http://en.wikipedia.org/wiki/Modbus for modbus, but what mean start and end with 3.5c idle?
I use C++Builder2009
Of course it's possible.
In MODBUS ASCII it is easy to determine end of message since 2 bytes are used for single byte transmitted over communication line (byte is transmitted as it's ASCII hexadecimal representation), but in MODBUS RTU you have 1 byte used for single byte transmitted which means that they had to know somehow that messages has ended. So, bytes are added to a new message as long as pause between them is less then 3.5 characters. When pause is greater then 3.5, you have an end of a message and you can parse the message, process it, and get ready for new one. This idle time is measured in characters since that is the only constant. Time period of 1 character transmitted over 9600 and over 115200 is not the same, and it is also not the same for 9600-8N1 and for 9600-8E2, so you have to adjust that time based on COM port settings.
yes its possible to send data with comport using modbus protocol.
There are various packages for that like RXTXcomm.jar,comm.jar which provide functions to communicate with slave device using com port