Is there any raw TCP library in C or C++? - c++

I going to create kernel mode driver level app that establish a TCP connection, here is my requirement:
I don’t want pass data to user-mode
I don’t want use winsocket and OS socket library
I need to just pass tcp packet to a library and
the library create simple TCP-client or TCP-Server connection for me. It should perform all TCP connection requirements such as tcp handshake, generate packet, calculate checksum, set TCP flags and acknowledgment then give the new packet to me so I can send the packet to my network adapter.
Do you know exiting TCP implementation that it does not use OS socket library?

I think the proper way to ask this question is this:
What is the proper way to do TCP sockets within kernel code?
And I'm not sure you want to do TCP just at the packet level, because you'll also likely want to handle TCP segmentation, IP fragmentaion, sending only when the remote window size permits it, and ACK generation. In other words, if you're doing a TCP server within kernel mode, you want the whole kernel TCP stack.
In any case, Bing or Google around for "kernel sockets" or "ksocket".
For Linux: http://ksocket.sourceforge.net/ Also, check out this example for UDP.
For Windows: Go to this page and downlaod both the HttpDisk and KHttpd samples. Both feature a windows device driver than makes use of a similar "ksocket" library. (Look for ksocket.c and ksocket.h in each)

For Linux, use the kernel_*() versions of the usual socket API, i.e. kernel_bind(), kernel_listen(), kernel_accept(), kernel_connect(). They're in #include <linux/net.h> and are used in ways very similar to "normal" sockets.
Solaris has very similar interfaces, there named ksocket_*(), see #include <sys/ksocket.h> for references.
For the *BSD UN*X flavours, Apple's Network Kernel Extensions Guide gives some details (also with references to the corresponding interfaces on Free/Net/OpenBSD).
Don't know about Windows.

Related

Reading a TCP header and managing a TCP connection with RAW_SOCKET

I'm trying to get the TCP header of a TCP connection in C++11. Reading through already existing StackOverflow questions (here, here, here and here) it seems like I have to open a RAW_SOCKET or to write a Linux Kernel Module (LKM) to have access to it.
From what I've understood, opening a raw socket means handling the whole TCP protocol (handshake, window size, etc...). Is there a way to obtain the TCP header and let the kernel manage the TCP protocol (either "by hand" or with some framework)?
I know I could use libpcap for capturing the packets, but this would mean for my application making somehow a match from the incoming packet in the TCP socket and the captured packet from libpcap. While this is a possible solution, it'd be a cumbersome one (and I wouldn't like to do that).
Any help is appreciated, thank you!
A "quick and dirty" approach might be using two connections, an external connection to the remote host and a pure internal one. Sure, this won't be the most efficient approach, but is easy (and fast) to implement (the core feature of QAD "solutions"...):
socket ext_raw ------- socket remote, TCP (likely, at least)
socket int_raw ---
| (loop back connection)
socket int_tcp ---
Any incoming messages at ext_raw and int_raw are just forwarded from one to the other (while incoming messages on ext_raw can be inspected for TCP headers), whereas all the normal TCP handling is done by the internal TCP socket. So in a way, you'll be tunneling the TCP connection through your two raw sockets...

Windows : Ask Socket to deliver all packets (even the corrupted)

My current job is to analyse the kind of lost in a real time transmission.
The C++ software is made for Windows(using Visual Studio) with a Raw UDP Socket, i use the socket lib winsock2.h.
I just want to ask the socket or the OS, to delivers all the packets even the corrupted/discarded one.
How can i achieve that ?
Thanks
I found a solution
The SIO_RCVALL control code enables a socket to receive all IPv4 or IPv6 packets passing through a network interface.
https://msdn.microsoft.com/en-us/library/windows/desktop/ee309610(v=vs.85).aspx

C++ Simple Chat client without boost

I want to attempt to write a peer to peer chat client that I can use with one of my friends, both of us are running fedora 17. After doing some reading I want to achieve this by sending the messages through TCP sockets, but I'm not sure what library/libraries I need to use to do this. Honestly I'm a little worn out from trying to get the boost libraries to compile and figuring out how to link them, the whole process just seems unnecessarily complicated to me, seeing that I use vim because I personal prefer not to use IDEs. Are there any standard C++ libraries that allow for packets of data to be sent from one device to another using tcp sockets?
You don't need any special libraries, it's all included in the standard C library (libc) that all applications are linked with.
The things you have to do are the usual for a client:
Find the IP address of the host (getaddrinfo)
Create the socket (socket)
Connect to the server (connect)
Send and receive data (write and read)
Close connection when done (close)
There are many tutorials on network programming on the Internet, almost all of them have examples showing char servers and clients.
I think you just need C++ and standard library.
you need to write a program binding to the concerned port and connectting to another computer by TCP .
Just Sending and receiving msgs through socket connection.

How to retrieve a data from a directory by sending a request through the serial port?

I have a controller which has a serial port and ethernet. I want to retrieve an event and the data associated with this event from the event directory through the serial or ethernet port of the controller. I do have a packet format (request packet data) for the specified event to be retrieved. Can anyone tell me how to retrieve the data by sending a request through the serial port? I am beginner and not that much well-versed in programming.
You will need to have some kind of a program running on your embedded platform, listening to the serial port and answering requests. This kind of program is usually called a "daemon" (pronounced the same as "demon"; just like "Caesar" rhymes with "sea star").
If you already have a daemon, you will need to figure out what format it uses. Since I have no idea what you might have I cannot even guess.
If you will be writing your own daemon, you will need to choose some sort of protocol. Personally I like the JSON format for a serial protocol; it is simple enough that you can extract data just using sscanf() from the C library if there is not a better library available, and of course it's easy to build JSON strings just using sprintf().
http://json.org/
What you want is the Serial Programming Guide for POSIX Operating Systems. If you are bound to Windows for some reason, you get POSIX through installing Cygwin. Expect to become familiar with man pages like termios and fcntl since you'll first have to set the serial port parameters to work with your device, though they're likely to be the standard 8-N-1 at some rate. Then it's a matter of reading and writing bytes to the port's file descriptor. You're more likely to be using the low level open(), close(), read(), and write(), which are a level below the stdio (printf, fopen, stdout) you're more likely to be used to as a newer programmer.
Computers these days often lack the RS232 serial port, so if you need one you can find a cheap USB adapter. Be aware that USB adapters don't necessarily implement some of the ancillary signals (RTS,CTS,etc.) in my experience.
Also look into libraries for your specific needs and situation.
You should specify the controller for more useful answers.
Your controller should support any data exchange protocol. You can find this info in documenttion. May be, it supports MODBUS or MODBUS TCP. In this case you can use any modbus compatible software.

How to implement icmp packet handler using winsock version 1.1?

I am working on an very old application right now. I need to make change in this application to listen for coming icmp request and decide to reply or drop the packet (kind of access control on ICMP). The application is Winsock version 1.1 based. I tried different ways to create a socket and capture icmp packet using the socket. But none of my efforts worked.
Can anyone help me out? Or is it totally impossible?
Thank you very much for your answer.
I don't think you will be able to intercept ICMP packets at the application (Winsock) level, since this is not an application function. You will probably need to write a network filter driver for whichever version of Windows your application runs on.
You say the application is old, but nothing about the OSes you're running it on. Unless you're running it on 16-bit Windows or NT 3.x, there's no good reason you can't just migrate to Winsock 2. You can download Winsock 2 for Win95, and it comes in all later Win9x OSes, as well as in Windows NT 4 and up.
If you really must run this app on Win16, it may be possible to dig up one of the third-party Winsock stacks from that era that did offer raw sockets support, but that sounds like a huge hassle, if you want to do it legally. Easier to just specify Win98 as a minimum OS version and move on.
EDIT: I'm assuming you can do what you want with raw sockets, which requires moving to Winsock 2 if you must use the built-in Winsock in MS operating systems. Changing your program to use Winsock 2 instead of Winsock 1.1 is easy; one library change, one header file change, and a change to the WSAStartup call, and there you are. Raw ICMP sockets let you construct any ICMP packet you want, and in newer OSes you may be able to listen for ICMP packets, too. If it turns out that the stack won't let you listen for the packets you need via sockets, you can do it with packet capturing techniques instead.