Coding a website in C? - c

I was just reading the http://www.meebo.com/ About Us page, and read this line :
"plus, we're one of the few still around using C!"
Considering that meebo is an online chat client, how do they work with C? How can they use C for the backend? How does it interact with the frontend? For example, let's say a user creates a new account, and new directory is to be made, how does the information go from the front end to the back end?
I'm sorry if it's an invalid question.
Thank you
Edit 1: The Intro Tutorial to CGI was great. Any good books I can pick up from my library regarding this?
Thanks a lot for the quick response guys!

I don't know how meebo does it, but given that it's chat software they probably have a custom server written in C to handle the actual message traffic.
However, Apache and most other HTTP servers have always been able to call C programs just as they can call PHP, CGI and other languages for certain requests. Some websites are even written in Lisp.
The backend has to be compiled each time, unlike an interpreted language, but that happens at rollout and is part of the build/production scripts.
The permissions given and user account that the C program runs under must be carefully chosen, and of course a C website suffers from the same issues any other C program can fall for, such as buffer overrun, segfault, stackoverflow, etc. As long as you run it with reduced permissions you are better protected, and it's no worse than any other language/platform/architecture.
For servers, however, it's still used widely - the gold standard, I suppose. You can find plenty of servers written in Java, C++, and every other language, but C just seems to stick around.
-Adam

I've rolled non-blocking HTTP 1.1 servers in as little as 50 lines of code (sparse) or a few hundred (better), up to about 5k (safe). The servers would load dynamic shared objects as modules to handle specific kinds of requests.
The parent code would handle connection tracking, keep alives, GET/POST/HEAD requests and feed them off to handlers that were loaded on start up. I did this when I was working with VERY little elbow room on embedded devices that had to have some kind of web based control panel .. specifically a device that controlled power outlets.
The entry point to each DSO was defined by the URL and method used (i.e. /foo behaved differently depending on the type of request it was serving).
My little server did quite well, could handle about 150 clients without forks or threads and even had a nice little template system so the UI folks could modify pages without needing hand-holding.
I would most decidedly not use this kind of setup on any kind of production site, even your basic hello world home page with a guest book.
Now, if all I have to do is listen on port 80/443, accept requests with a small POST payload, sanitize them and forward them along to other clients ... its a little different.But that's a task specific server that pretends to be a web server, its not using C to generate dynamic pages.

Meebo uses a custom Lighttpd module called mod_meebo. It doesn't fully answer your question, but I thought you might be interested.

A lot of server-side programs can be done in C, not to mention CGI programming. They could also be Using C with MySQL, which is very possible. But without access to their source code, we have no way of knowing just how much C they are using.
Claiming that they are "one of the few around still using C" was probably just a joke. With stats like this at least I would hope so.
-John

You can see a good example of a web site in C with source code: fossil.
It uses SQLite for the back end.

Related

embedding a headless browser in C

Is any of env-js, phantom JS, slimmerJS or any of the headless browsers embeddable in a C application? This is what I need to do: I have a C application that connects to a couple of servers through HTTP & HTTPS. Up until now, I got every page, figured out what it did, (mainly Javascript), extracted the parts of code that I needed (I also implemented a very simple string parser/extracter) and implemented the flow by sending the HTML code through a (secure)Socket and reading back the response. That part still works smoothly.
That was until I bumped into a set of very complex (read: obfuscated and looong) javascript pages linked one after the other, with several scripts included, and server side programming, and then I realized that I wouldn't be able to get a "maintainable" program with the procedure I've taken.
So I have spent the last several days looking for an embeddable Javascript interpreter. Bumped into spider monkey which is embeddable in C, but since I don't have any control over the scripts received, its lack of the DOM implementation makes it unsuitable. I also considered implementing the DOM interface, but honestly is a way too long distraction from my main proyect.
Then I considered the headless browsers mentioned above. Have read all the information I found about them, looked for some sort of library to interface with them, and in abscence of such librarys, have considedered hacking the code, only to realize that even trying to hack phantomJS to embedded it in my C system would be even crazier than implementing the DOM interface in spider monkey.
The system currently works in Windows and I'm using MinGW to develop it, but its final target is to be implemented in a Raspberry PI, so the more I can have plain straight C source code, the easier it will be to move the system to its final destination. With this I mean: I can use windows libraries in the meantime if they are pre-built, but can't miss the need that they must be compilable and generated in a plain compiler. I don't have yet the Raspberry PI, but I'm not expecting any fancy development tool set (I might be wrong on this)
Lastly, for the curious inside, the system is a stock screener, generates graphics with indicators which are put in a web server and generates alerts (to send notifications of price conditions) through Yahoo Messenger (this choice was mainly due to portability and availability of source code).
I will really appreciate your help in find a way to implement/embed in C *any* Javascript interpreter that has a DOM interface implemented.
Regards.
Alfredo Meraz

Getting started with http media streaming in C

Alrighty so, I've been wanting to learn C for awhile, and now I have a project idea that's actually relevant to a website I want to build, but I have a few initial questions on how to get started. This isn't really a "how to program" question or anything, I can get started with C programming fine, I know how to read and communicate with various APIs and protocols as long as I have documentation, etc. I'm just looking for a starting point, I guess.
The program would be something similar to ice or shoutcast, so basically audio streaming. Does anyone think they could give a brief, high-level overview of what would be required? As I said the end product would be a url you pop in a .pls file and you can stream it to w/e client you want. What protocols, libraries, and documentation should I be looking at?
If you want this to be a toy for learning, you might want to do all the work yourself; it's a complicated problem, and getting it right is definitely going to be educational. A copy of Advanced Programming the Unix Environment, 2nd edition or TCP/IP Illustrated, Vol 1 would be helpful but not strictly necessary.
If you want this to be useful too, I'd suggest starting with libev or libevent. libevent has some built-in HTTP handling, which might be nice, but there are reports that libevents HTTP handling isn't perfect. libev doesn't provide built-in HTTP handling, but it should be easier to write with libev than performing all the work by hand. Using these pre-written event-based libraries will improve the stability and reliability of your program compared to writing the entire thing by hand, though they aren't doing anything that you can't do yourself.

How can I use data discovered via a memory scanner in an external program?

So, some background: I'm using a memory scanner called cheat engine to get real-time values for game stats (e.g. hp, mana, exp) in a non open-source video game that I'm trying to make a bot for.
For my bot to effectively use this information, I need to get it from cheat engine (or any memory scanner with similar functionality) to my bot code in a timely manner.
Now, one obvious way to do that would be to save all of the information to a file and then load the file in my bot code, but since this data needs updating about every half second or so, that isn't a real solution.
What I really need is either a terribly convenient memory scanner that allows you to use the information you uncover as a set of variables in some programming language (preferably java, c, or matlab), or a way to access the memory addresses found in one of the above languages.
This latter option should hopefully be doable, as cheat engine gives the memory address, controlling process ID, and data type.
This question doesn't have an easy answer. As far as I can tell you are very new to this area, so what you really need is a proper introduction to the subject, and for that I recommend reading Exploiting Online Games: Cheating Massively Distributed Systems.
It's an awesome book and it shows in a detailed manner how game hacks work, and it dedicates an entire chapter on how to build bots.
If you want to write an application to read/write data to those memory addresses you need to investigate functions like ReadProcessMemory() and WriteProcessMemory(). Whatever language you to choose to implement your bot needs to provide access to the Windows API. This is needed because you have to manipulate another process' memory space.
There are lots of tutorials out there that shows how to do this using C and C++, since they are the preferred languages to do this kind of stuff. Another option is to use a macro tool if you want something simple to play the game for you.
Modern computer games implement their own anti-cheat mechanisms to make it a little more difficult for people like you (and me). And since this book presents attack and defense techniques I recommend it to anyone interested on how to exploit computer games. The book is fully loaded with code examples.
I'm sorry for not providing more information but I was criticized once in the past for helping people with your curiosity and I would also never do a better job than the authors of the book explaining how to do this stuff.
Try using the Lua interface to get what you need.
Here's an example (I have not tried it, but I'm assuming it works....)
http://forum.cheatengine.org/viewtopic.php?t=530047
You can probably use COM with a script in Lua (with LuaCOM) on one side and Matlab or C on the other
You would need to use a debugging library to do that. You would set a watchpoint on your variable's location, and when it triggers, you would get its value.
scanmem does that for Linux.
Unfortunately, many closed-source games go to great lengths to avoid the use of debuggers, so this won't probably work on your game.
Have you tried Visual VM?
http://visualvm.java.net/download.html
Cheat Engine is open source, so what you do is look in the source code of Cheat Engine, and look how the memory dump thing works.
However, it is highly non-trivial to monitor a live process that you are not controlling, so unless you are a black hat code wizard, level 11, I suspect that it won't work.
Even if You say You want to look at a few integers that you can guess looking at memory dump, it is a lot harder to find that area programmatically, consistently, while possibly retracking every so often as the data may be copied or moved when the state of the program changes.
Also read this encouraging citation from Cheat Engines FAQ:
Q:Will Cheat Engine work on online games?
A:Most of the time, no
But anyway, try it - it sounds fun and I am sure you will learn something, and there is always a chance that you'll make it work :-)

How can I best deploy a web application written in C?

Say I have fancy new algorithm written in C,
int addone(int a) {
return a + 1;
}
And I want to deploy as a web application, for example at
http://example.com/addone?a=5
which responds with,
Content-Type: text/plain
6
What is the best way to host something like this? I have an existing setup using Python mod_wsgi on Apache2, and for testing I've just built a binary from C and call as a subprocess using Python's os.popen2.
I want this to be very fast and not waste overhead (ie I don't need this other Python stuff at all). I can dedicate the whole server to it, re-compile anything needed, etc.
I'm thinking about looking into Apache C modules. Is that useful? Or I may build SWIG wrappers to call directly from Python, but again that seems wasteful if I'm not using Python at all. Any tips?
The easiest way should be to write this program as a CGI app (http://en.wikipedia.org/wiki/.cgi). It would run with any webserver that supports the Common Gateway Interface.
The output format needs to follow the CGI rules.
If you want to take full advantage of the web server capabilities then you can write an Apache module in C. That needs a bit more preparation but gives you full control.
Maybe this tiny dynamic webserver in C to be used with C language can help you.. it should be easy to use and self-contained.
Probably the fastest solution you can adopt according to the benchmarks shown on their homepage!
This article from yesterday has a good discussion on why not to use C as a web framework. I think an easy solution for you would be to use ctypes, it's certainly faster than starting a subprocess. You should make sure that your method is thread safe and that you check your input argument.
from ctypes import *
libcompute = CDLL("libcompute.so")
libcompute.addone(int(a))
I'm not convinced that you're existing general approach might not be the best one. I'm not saying that Apache/Python is necessarily the correct one but there is something compelling about separating the concerns in your architecture being composed of highly focused elements that are specialists in their functions within the overall system.
Having your C-based algorithm server being decoupled from the HTTP server may give you access to things like HTTP scalability and caching facilities that might otherwise have to be in-engineered (or reinvented) within your algorithm component if things are too tightly coupled.
I don't think performance concerns in of themselves are always the best or only reasons when designing an architecture. For example the a YAWS deployment with a C-based driver could be a very performant option.
I have just setup a web service using libmicrohttpd and have had amazing results. On a quad core I've been handling 20400 requests a second and the CPU is running only at 58%. This is probably going to be deployed on a server with 8 cores, so I'm expecting much better results. A very simple C service will be even faster!
I have tried GWAN, it is very good, but it's closed, and doesn't play well with virtual environments. I will give #Gil kudos being good at supporting it here though. We just had a few issues and found LibMicroHttpd works better for our needs.
If you go here, you may need to update your openssl if you're using CentOs from axivo
rpm -ivh --nosignature http://rpm.axivo.com/redhat/axivo-release-6-1.noarch.rpm
yum --disablerepo=* --enablerepo=axivo update openssl-devel
You can try Duda I/O, it only requires a Linux host: http://duda.io

How can I make an AJAX server side script in C?

I am looking into AJAX for the first time and I would like to know if it's possible to make the requests from a server side CGI application written in C?
Will the C application just use printf for the data, similar to this .asp example?
If I were you, I would stay away from C for server-side stuff. There are so many other languages that are better suited for this, but if you insist, you could use a library like cgic. Basically, you would just use the CGI handler from a server like Apache, but please, PLEASE use something other than C. It's very dangerous in the wrong hands, especially via CGI.
Use something like PHP or Perl to keep yourself sane. PHP is perfect for someone just starting out, and you won't have to futz around with compilation and making your CGI handler work/be secure.
ASP does some magic, such as outputting the appropriate response headers, but other than that it really is that simple. The server-side of AJAX is just responding to requests. Output the right data in the expected format and you're done. Stick with REST principles and this becomes easy.
You can do scripting using C.
Take a look at http://bellard.org/tcc/ - this is a small C99 compiler for Windows and UNIX.
It has a unique feature: "-run" option. With this option, tinycc compiles the code into memory and executes it without creating an intermediate binary. Thus all you need to do is to create your CGI files like this:
#!path-to-tinycc-executable -run
// your C code goes below
....
However, I have to agree with previous commenters that C is not great for server side CGI.
I am looking into AJAX for the first time and I would like to know if it's possible to make the requests from a server side CGI application written in C?
I'll hop on the bandwagon, and say that it's usually easier to just use another language. However, I also understand that sometimes you have to use C - i.e., for embedded servers with limited resources. If that's the strongly suggest you use cgic, or heck, maybe even a framework like klone. I'm a bit biased towards the latter, though. It provides a nice interface to getting request objects, almost like the scripting variety of frameworks.

Resources