I'm building a client-server program that has the client and server connected using a single TCP connection.
Within the communication there are several data "channels", which I want to multiplex over my single TCP connection, while still having good flow-control between the channels (preventing starvation and so on...). Also, tunneling will be nice, but not a requirement.
I'm using C, Windows.
I thought of using ssh but I have several problems with it:
I had trouble finding a nice open-source ssh code for windows.
Most of the code in SSH handles security, which I have no need for.
SSH seems a little too complicated for my needs, with all the X11, pty, shells and so on.
It sounds like you'd want to send data in "segments", where each segment would have a header giving the channel and number of bytes of data to follow. That way, you could intersperse data for the various channels on a single connection. Does that help?
Related
I am new to C Socket Programming. I know how to write for TCP and UDP as different programs.
But only one server should handle both the clients.
Can anyone tell me how to write a sever that handles both TCP and UDP clients?
You cannot listen to TCP and UDP clients using 1 server socket. You can however create 2 server sockets (one TCP-server and one UDP-server). Note that it would not even make sense to have 1 server for both: UDP is connectionless so the first question arises when you try to do accept on your server socket (since it is a hypothetical hybrid version, what should accept do?).
Anyway, I assume you want two servers in the same event loop (if you don't know what it is, it is enough for you to think of it as your main-function). Since C sockets are blocking by default, you cannot run two servers right out of the box.
You can use select (Google it). If you don't know what it is, I would recommend to try it in Python first. In Python it's fairly straight forward and it will give you some insight of the concept. Basically what you do is: create multiple server sockets than "switch" between those sockets, see what sockets have read events (be it new connections or messages) and then process those events.
I can recommend libuv. It is a C library that is originally built for Node.js. Prior to libuv, they used platform-dependent event loop implementations (libev). Libuv originated as an effort to create a multi-platform library for non-blocking IO (TCP, UDP, fs, etc.). However, even if you don't want to write multi-platform code, it comes with a great API to create server sockets and listen to multiple sockets in the same event loop.
I have a weird problem that I can't figure out in SIMCOM900A, I am receiving downlink data of about 1080 bytes every 5 seconds, meanwhile doing other things like sending data to server. However, sometimes AT command response comes in between downlink data. Like below:- ## to ## is my downlink packet but I receive AT+CSQ response in between!
##10500000110483&A8813&B182&C1027&D~OTA_S|True|InProcess|182|YCrOK/Uei1R/CKZSAmV+EkjvShKck+ko7zPYP0y7vULiXaPY6H6FeJx47QtXM+0+vAInJ7svI9nQVljeKO9oaDmPU5Qp6p2Yp2GSGbMlvqJhshlTEpcH+6dG759Oev/YBfoNYPg76IG7Ufd1hl7msQshJvQLCdcvYuZKv1xfSFH0xcUR9EVU7UBQO8CYP0HIPQb98JhhvJ6XqrVHQph+6JSq6YcEmCU9YG7ANaaHpX7Q+CXq/C7W+dmFru6VytvXpyRQbnEoSZDA4jx6MbM4vZjFgb6laHEocUl9JbA4wy8Gjeil6MX1Ae/MeI9zWkItwWgxSOM2VuIWpzNBe3MnpOwocXvDcnH/QsI2xHvUsxeT4RK0pLSvGVvSJ2G+TuZ80L54xfa9mCRu0tt3Pf/doPMt2neo3h6dw2zX46m/NdvVmfVCS9muTL3tLtHbziEs0TzA8H28w2m9nCB1htF0H/RRsvcBWeuhTuC3XAn5A3eCZN6LQKNjLEGCdcHdSDmP1bOD/D5bF/QCaS8GnWCYNewnN1uLenUx2MCiVM0GXgo28LKw9cK7gA81rBcuM60XBlNOM64XRhCkBbIus1gvXBZhvXAptLYPpHDZuJ95qd76LblmuFRfM7SlP3/NcJlpzWCUM8lCTqs1w+nkbJl1w6X6usEs55msGy4zrRseMMjZPaqcoeuG08l6JmuHS/W1g1mSM1s7aOPcNl4b54kwzruE+Xqtx2u+XiL4el2gfWOp0ddrO16bv33CfL2O4618vT5gZ26N4OshdpPw2nhc89rw7nbh6/F40NeLmqtnH7jrf9a+Xh9dvr5h+x4DATuf7Xb0hTLw18EwxnclkHIDNe9K3MraThZt/yfI70bevmvJ0Nh+jD+FGgzzXsP6SNM190O6wQZd08+ka26knGbUNU8SpAWy+lnomn6WumbIzGvEWvZaGI99WlzHXKvrmOv
+CSQ: 10,0
OK
OQsf0M+kYo3xJFvKF65jI8rWMbrlW1y3XnYVu6WfS~##
Any suggestions as to why this is happening or how to fix it are welcome! Thanks in advance.
This is a configurational issue. Basically with SIMCOM900A you are limited to two serial interfaces if my memory serves me right. One for AT commands and modem control, the second for debugging.
As you have setup a data connection via the AT command interface it is effectively sharing the interface for incoming/outgoing data communication and modem control commands. What you are seeing with the +CSQ messages are unsolicited (URC) result codes.
You basically have two options:
1) Make use of multiplexing via GSM 07.10 which your modem supports. These then connects to your modem and configures using AT+CMUX so that you will have effectively two ways of connecting to the same serial device.
Thereafter you can use one for managing the modem and sending/receiving AT commands and responses. And the second for receiving/sending pure data (you can also execute AT commands in this channel but there are limitations).
2) Modify your host program/script to cope with this situation and make decisions on what to do based on whether it is data or AT command responses / unsolicited response codes.
NOTE: A word of warning regarding multiplexing. By multiplexing you will end up degrading the performance of communication between the modem and host.
NOTE: It makes real sense always to have a good read through of your modems manual regarding interfacing and AT commands. Manufacturers are renowned for implementing slightly differently compared to the GSM specs :)
On a Linux machine, you have a daemon that listens on TCP port A. However, it is usually stopped because it is rarely used and takes away a large amount of system resources. Instead, I want to do something like this:
Code an application that listens on port B and does the following as soon as a connection is established: If the daemon is stopped, start it and wait until it listens on port A. Now the difficult part: Connect the client to the daemon in a completely transparent way, i.e. without the client having to reconnect on port A. Also, but this is irrelevant for this question, the application will shut down the daemon when there are no connections for a certain amount of time.
Of course, I could have my application connect to the daemon and pipe all communication. I do not want that. I want some way to forward the established connection to the daemon and then get rid of the connected socket, while the client is now happily connected with the daemon. In some way, I want to give the daemon's process my already connected socket. Is there any way to do something like this?
I'm running Debian, if that's important. I would want to code the application in C/C++, and it's okay to have OS-specific solutions (i.e. use syscalls). Forgive me though, I am not much of a Linux coder, so I am not very familiar with Linux system programming. If there is some obvious way to do it, I simply didn't know.
Of course, I am open for any kind of suggestion.
This problem has a pre-existing standard solution, generically known as inetd. It has been around for a long time, first in Unix systems and then Linux.
The more modern implementation is xinetd
I am using libpcap library. I have made one packet sniffer C program using pcap.h. Now I want to block packets coming on port 23 on my computer via eth0 device. I tried pcap_filter function but it is not useful for blocking.
Please explain to me how to code this functionality using c program.
Libpcap is just used for packet capturing, i.e. making packets available for use in other programs. It does not perform any network setup, like blocking, opening ports. In this sense pcap is a purely passive monitoring tool.
I am not sure what you want to do. As far as I see it, there are two possibilities:
You actually want to block the packets, so that your computer will not process them in any way. You should use a firewall for that and just block this port. Any decent firewall should be able to do that fairly easy. But you should be aware, that this also means no one will be able to ssh into your system. So if you do that on a remote system, you have effectively locked out yourself.
You still want other programs (sshd) to listen on port 23 but all this traffic is annoying you in your application. Libpcap has a filtering function for that, that is quite powerful. With this function you can pass small scripts to libpcap and have it only report packets that fit. Look up filtering in the pcap documentation for more information. This will however not "block the traffic" just stop pcap from capturing it.
Actually using pcap you are not able to build firewall. This is because packets seen inside your sniffer (built using pcap) are just copy of packets which (with or without sniffer) are consumed by network stack.
In other words: using filters in pcap will cause that you will not see copies of original packets (as far as I know pcap compiles filters and add those to kernel so that on kernel level copy will not be done); anyway original packet will go to network stack anyway.
The solution of your problem most probably could be done by netfilter. You can register in NF_IP_PRE_ROUTING hook and there decide to drop or allow given traffic.
I wrote a program that creates a TCP and UDP socket in C and starts both servers up. The goal of the application is to monitor requests over the TCP socket as to what UDP packets to send it (i.e. monitor for something like "0x01 0x02" and if I see it, then have the UDP server parse the payload, and forward it over to the TCP server for processing). The problem is, the UDP server will be busy keeping another device up, literally sending thousands of packets back and forth with this device. So what is the best way to continuously monitor requests from the TCP server, but send it certain payloads from the UDP server when requested since the UDP server will be busy?
I looked into pthreads with semaphores and/or mutex (not sure all the socket operations are thread safe, though, and if this is the right way to approach it) as well as fork / pipe. Forking the UDP server off as a child process seems easy enough, but I don't see exactly how I would be passing the kind of data I need among both servers (need request data from TCP and payload data from the UDP).
Firstly, would it make sense to put these two servers into one program? If so, you won't have to communicate between processes, and the whole logic becomes substantially easier. You will have to think about doing asynchronous input and output, and the select() function is designed for just this. There will be many explanations around on how to do this, and a quick look finds this page.
However, if you must have two separate processes, then you will need to choose a mechanism for inter-process communication, of which there are several, and your choice will be affected by your operating system. A pipe, if available, might be suitable, as might a Unix named pipe. Or you could look into third-party message passing frameworks, or just use shared memory and/or semaphores (but be very careful!).
What you should look at is libevent, anything else you are reinventing the wheel writing this low level code yourself. Here is a Tutorial, Google, Krugle
Also you should use some predefined protocol between the servers. There are lots to choose from. Ranging from the extremely simple XDR to Protocol Buffers.
You could use pipes on Unix. See http://tldp.org/LDP/lpg/node11.html
Well, you certainly picked an interesting introduction to C!
You might try shared memory. What OS?