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.
Related
I'm working on a project, implementing everything in C language. As a part of the project, we need to be able to control and configure firewalld, firewall of the current system.
firewalld is implemented in Python and an interface is available. However, we don't want to make Python calls from C or vice versa.
There are command line tools to configure firewalld (e.g. firewall-cmd) but we don't want to make such calls from C either.
I recently started working on firewalld, I don't know much about its internals. I've read that it uses D-Bus, I also don't know much about D-Bus.
There is a C library developed by Thomas Woerner: libfirewall.
However, it's been more than a year and a half since the last commit so it's not maintained. Other than libfirewall, I don't know any firewalld interface in C.
I gave libfirewall a shot. It got me some problems when both compiling and running the examples and I still have problems to resolve. Is it worth to continue with libfirewall? Should I use it?
Is there any other interface that I'm not aware of?
Possibly naive question due to lack of understanding of D-Bus: I thought, maybe, with a D-Bus interface, I can issue commands to firewalld. Can it be done? (i.e. Does D-Bus work like that?) Can we write a program that mimics, say firewall-cmd, and interacts with D-Bus in the same way and at the end allows us to control firewalld?
If this is possible, how to do it and what to use? libdbus and GDBus have relatively good documentation although libdbus requires good deal of effort. They even said "If you use this low-level API directly, you're signing up for some pain." in the documentation. In any case I'll be in need of examples or any kind of text demonstrating their usage.
How should I approach this problem?
Yes, you can issue commands to firewalld via D-Bus. I haven't checked but expect that firewall-cmd is itself implemented as a D-Bus client.
The D-Bus API is extensively documented: https://firewalld.org/documentation/man-pages/firewalld.dbus.html. The documentation should give you a rough idea what can be accomplished through the API. You could try the D-Bus debugger d-feet to interact with firewalld without any code.
GDBus is definitely the easiest way use D-Bus from C but it's still not trivial and firewalld is a fairly complex API: Using it may require some expertise (completely depending on what you need to do).
I am looking for a NIO type of library for C. I want to implement a multi threaded UDP network server that will have a lot of clients connecting to it.
Instead of attempting to code my own program to handle packets and 'connections'. I thought I would have a look if there is not already an existing library that has been tested and build for scalability and high performance.
I have found a few for Java but none for C. such as Apache Mina.
I am hoping that some one out there knows of a good one that may assist me.
Thaks
It sounds like you want something to abstract select(), poll(), or whatever the most efficient mechanism is for your platform.
Have you looked at libevent and libev? There is a nice writeup here.
First of all, C has no classes. Secondly, C provides you with everything you need to implement a scalable and high performance solution. It's more low level than java's NIO, but there are good tutorials out there in google.
And if you want a library - try boosts' asio. It is C++, but perhaps you can use it.
If you are using Linux I strongly recommend you to use the POSIX API. It gives you resources for multithreading and networking acrosss any Linux box.
GNU C library
I am interested in making a simple HTTP server in C to play with (of course I'm only expecting to learn more about how a server works and gain some knowledge from there). I saw some topics regarding the same question as well as providing code but that didnt help. Are there any books for what I'm looking for?
First, there are many HTTP server C code out there. At least, have a look into some of them.
There are also several libraries for adding HTTP server abilities to your software. I do like the onion library, but there are others (libmicrohttpd, Wt for C++, etc...).
You need to be fluent with
advanced unix programming
unix networking programming
HTTP protocol
The C10K problem
FastCGI perhaps
I'm not sure it is worth the effort to re-implement a simple HTTP server. You probably won't do better than existing stuff in a reasonable time.
You might learn more by studying and improving existing code.
At last, your question is perhaps off-topic here. Did you consider asking on programmers's forum?
Actually, since HTTP is a complex protocol (above TCP/IP which is itself complex, implemented e.g. on operating systems like Posix or Linux which are complex), you won't be able to make a simple but standard compliant implementation (if you want it robust enough, which is required by HTTP specs). It has to be complex!
You definitely will learn a lot by implementing something simple. Just read a little bit about HTTP (e.g. wikipedia's page on HTTP, and start implementing some simple GET and HEAD requests); but you should have some knowledge about e.g. basic Linux network programming. However, you'll then implement only a subset of HTTP.
Almost the same question as this one here:
What's the easiest way to grab a web page in C?
however the conditions have changed and I need to connect via https, this is a bit more tricky, anyone got any snippets?
I am on a qnx platform, building and compiling additional libraries and rolling it out onto our product is very, very hard given the contraints. So things like libcurl are not possible.
Results:
It turns out I had to install libcurl on QNX after all. This involved installing perl and openSSL to build libcurl, but once that was built it was good to go. This was the least desirable option but it ended up being worth it.
libcurl should be able to handle anything you need to do.
If you're not able to use a library, then I guess you're either forced to cheat, as in "call out to a shell or some other environment that already has this capability". I'm not very familiar with QNX or the environments where it's typically run, not enough to dicount this possibility on my own anyway.
By the way, before skipping this: libcurl is known to build on QNX, so try that before even reading further.
Failing that, taking the question literally, I guess you need to implement the relevant parts of the HTTP protocol yourself. Since you now need secure access too, you're in a world of hurt. You just don't want to implement that type of code on your own, it is a lot of work, many many wheels to re-invent.
At the very least, I'd recommend taking a hard look around to see if any of the things you need to do this are already implemented. This page implies that OpenSSH is available for the QNX platform, which is encouraging.
I was away when you posted this followup question.
I've now posted an SSL-capable example program at http://pastebin.com/f1cd08b33
This needs to be linked against OpenSSL (-lssl) but doesn't need libcurl at all.
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.