FastCGI application - how to get the listening socket handle? - c++

All examples for the subject are based on some C/C++ libraries like fcgi_stdio and similar. But I want to make a FastCGI script, using assembly language and there is no such libraries.
Almost everything about the protocol and communication is clear, but I still can't understand how the program gets the handle to the listening socket, passed by the web server?
My findings:
I installed the lighttpd server and tried to configure it in order to get fastCGI to work with my programs.
But I was confused, that the configuration file needs "host" and "port" - Is this means the FastCGI should create the listening socket by itself? I mean using socket/bind/listen/accept functions? Then how my application will know these host and port parameters. They are in the web server config file?
The other way lighttpd allows is to specify some named socket path - for example "/tmp/myapp.socket". How my application have to handle this? And how it will know this path?
I tried to make some small program using accept(0, ...) as #user58697 specified in his answer (and this answer is in accordance with the official FastCGI specification!) Unfortunately nothing happened - the call to accept fails miserably!
So, is the communication is server specific?

According to the spec, from the application point of view FCGI_LISTENSOCK_FILENO equals to STDIN_FILENO, which is defined as 0. That means that you shall accept(STDIN_FILENO, ...) or even accept(0, ...).

Related

gRPC IPC between Go and C++ on Windows

I am failing to allow ipc between Go and C++ over sockets on Windows.
I have a C++ app and a Go app on Windows (10/11) where I need IPC between the apps. I have been trying out gRPC and it worked over a localhost channel perfectly but the firewall popup is not ideal. It looks like grpc++ doesn't support named pipes but finding out if I can switch to sockets on the C++ side is a little less clear.
Trying TestClient client(grpc::CreateChannel("unix:grpctest", grpc::InsecureChannelCredentials()));
just results in:
DNS resolution failed for unix:grpctest: C-ares status is not ARES_SUCCESS qtype=A name=unix is_balancer=0: Domain name not found
Does anybody know if this is possible?

Get one clojure project to "communicate" through a local server

I have a project which generates a puzzle and solves it in separate classes using the REPL. Is there any way to separate those classes into separate projects so that I can create a server that is on the same machine and can find puzzles already generated and return them to the calling client (puzzle solver) without the use of web servers?
Update: initially misunderstood what I was trying to do and reworded my question
Definitely you can. In broad strokes, you probably want:
A single project with clear client, server and shared namespaces (eg. logic about solving puzzles that might be shared between client and server). There's nothing intrinsically wrong about bundling all the code together at first (later you can ship client+shared and server+shared separately)
A library to make client/server communication simpler. A common use case is exposing the server with a web API because it's also simpler to test with web browsers, CLI clients (such as cURL), but it's OK if you want to use plain sockets and exchange messages in whatever format (EDN strings?) makes sense to you.
An example of a library for sockets would be https://github.com/atroche/clj-sockets , if you don't want to use Java interop and use classes from the java.net package directly.
Also note that web servers are actually socket servers too that listen for connections on fixed ports (eg. 80, 443 or 8080) for HTTP messages using certain conventions (eg. GET /api/puzzles/1)
Finally, you'll need at least two namespaces that export a -main method: one to launch the server and another to launch a client.

How do applications handle ports if they are not open?

I am trying to write an application in c++ using winsock.
I need to handle a case where default port used by the app is not open to use then what is fallback mechanism?
Server or client?
In the server, if the requested port is already taken, you log an error and stop. This is why ports are often stored in configuration.
For the client, if you cannot connect to the specified port, you log an error too. Its not much different from not being able to connect to any other network resource, like a URL in your browser.
That completely depends on you.
There is a mechanism called port knocking: The application just tries a range of ports until it finds one it can bind to. Obviously server AND client have to do this to find each other if the default port did not work out.
You could also just display an error message and let the user decide on how to deal with this.
Or you specify a fallback port in a config file...
Just to give you an idea.

How to connect deamon with web server (e.g. via FastCGI)?

I'm writing a program in C++ that will run all the time in the background to do different tasks - a deamon.
Apart from it's usual tasks a web application should communicate with it (AJAX requests in doing a COMET pattern = lots of open but sleeping connections).
So the question is: how should I connect it to the web server (apache and lighttpd are relevant)?
Of what I've read FastCGI would be very interesting for that task, but all references I've read were talking about the web server starting the FastCGI application when necessary. This wouldn't work here as the deamon would already be running...
I've also read that the web server would talk via socets with the FastCGI application - so that could be an entry point for me, the deamon would "only" need to talk to such a socket.
But are there good libraries available for that?
Looking at the features of http://cppcms.com/wikipp/en/page/main it looks very interesting for me - but could that work in my case? And could it be stripped down - offering even a SQL connection is far too heavy for my case...
So what advice can you give me?
PS: Performance wise I recon a single threaded but asynchronous implementation would work for the deamon <-> web server glue.
FastCGI is the standard protocol to communicate with the web server. All mentioned web servers can communicate with the remote deamon application via fastcgi.
http://cppcms.com/wikipp/en/page/cppcms_1x_tut_web_server_config
Also if you are looking for Comet support, that what CppCMS provides you natively:
http://blog.cppcms.com/post/107
And could it be stripped down - offering even a SQL connection is far too heavy for my case...
CppCMS library is very small also it allows to reduce its size:
http://cppcms.com/wikipp/en/page/cppcms_1x_build#Build.Options
also SQL connectivity CppDB is independent part.
Performance wise I recon a single threaded but asynchronous implementation would work for the deamon <-> web server glue.
It is one of the standard CppCMS run mode - running asynchronous web applications.
Update:
Of what I've read FastCGI would be very interesting for that task, but all references I've read were talking about the web server starting the FastCGI application when necessary. This wouldn't work here as the deamon would already be running..
Indeed some web servers start the fast cgi applications but:
Lighttpd allows both to start FastCGI and SCGI application or connect to independent one
Cherokee (AFAIR) allows both to start FastCGI and SCGI application or connect to independent one
Apache:
mod_fascgi allows both to start FastCGI application or connect to independent one
mod_scgi connects to independent application - does not start application.
mod_fcgid always starts application - does not suite you
Also as general note, apache does not suit a pattern of working with many idle connections as it uses thread (or even process depending mpm) per connection.
Nginx - does not start applications at all, however for fastcgi currently not the best for comet streaming because of buffering, so SCGI would be better with nginx (assuming you are using latest nginx version)
Small note: SCGI is a protocol that is very similar to FastCGI but significantly simpler.

C++ redirect outgoing connections

Is there any way in C++ on windows to monitor a program and redirect any outgoing requests it makes on a specific port? I have a simple C++ http proxy and want it to be able to automatically redirect all browser requests on port 80 through itself.
The simple way to do it is to create a Windows kernel hook to trap socket requests and reroute them to your proxy.
Some useful documentation on this is:
http://www.internals.com/articles/apispy/apispy.htm
If you're using Windows Vista or better, consider Windows Filtering Platform (WFP):
http://www.microsoft.com/whdc/device/network/wfp.mspx
Also consider looking at Detours (commercial) and EasyHook (free). They significantly simplify the process of writing hooks and redirecting API calls (both Win32 and Application).
The program would have to be run with administrative privileges in kernel mode of the host OS.
While I don't have extensive experience with windows kernel hooks, in BSD and linux its trivial to install a kernel module that over-writes the system calls for creating sockets and could easily redirect all sockets to a proxy socket of choice.
If you mean [any destination port] to [one port] then you will have to rely on special drivers. The problem with windows is the inability to natively block [drop] packets. For example a common solution is winpcap. However, while you can monitor traffic, you cannot stop the traffic or modify it in a useful way.
On windows the only solution I've seen would be to use some open TUN/TAP adapter. With that, you would be able to modify every packet that leaves your system.
If you know beforehand the destination port you will be using then it gets rather simple. Simply write a passthrough c++ socket program that will only change the destination port.
If you want to redirect browser requests then you can simply edit the settings in your browser.