Hi all, I got sort of a task to do. I have to write in C "simulated network driver". What does it mean?
- It has to run on all network adapters
- It has to be as simple as it can be
- It has to run on all linux distributions (or at least most of them)
- It does not have to run a network service or any network-connected things
- after ifconfig up - it has to run a task - whatever (e.g. show some text, or light a 'caps lock' diode, or something else)
I have never written drivers on linux, but I'm very good at C programming.
Could you give me some hints how to do that?
Regards.
Have you ever compiled a kernel before? The work involved is going to be very similar.
Linux already has a dummy network driver which you could derive yours from. In the linux source code, look in drivers/net/dummy.c You might also find drivers/net/loopback.c useful.
First thing to try for would be just printing a message into the dmesg buffer, that's not difficult, I think it's just kprintf... After that you might want to have it create a file under /proc/ somewhere for things to read from; when traffic or whatever happens, and someone's opened that file, they can read from it. That's the proper way to interface since the kernel's not supposed to reach into the guts of userspace and do things without being asked.
I have only written some servers on linux, but it was rather general things like sockets and some algorythmics - no linux-only things. So I am brand new at linux.
Is Netbeans going to be sufficient environment? I got Ubuntu 9.10 distribution.
Thank you for your post - it is priceless to me to have some points I can start.
netbeans? Isn't that java?
You're going to need to install gcc and your kernel source. I think you're going to be doing your work in a terminal, not any sort of IDE. Compiling code for the kernel is very different from compiling code for your system.
Following this tutorial I was able to create a do-nothing character driver, but had to modify their instructions slightly:
# from magazine
make -C /usr/src/kernel-source-2.6.8 M=pwd modules
# what I needed
make -C /usr/src/linux M=${pwd} modules
...what this is doing is telling make to use the makefile that came with your kernel (in my case stored in /usr/src/linux/Makefile) to compile your source, which spares you a lot of headaches. That M variable tells it where to find your own makefile once it's set up.
I think pwd vs ${pwd} was a typo in the magazine itself, but /usr/src/linux is just where my system keeps its kernel sources. Yours may be something else, or probably not even installed right now.
Note that a network driver is not a character driver. But I'd think they get built it the same way.
Well, I got this tutorial done.
So for now - is this network driver to be a module like that ones from tutorial? Or something else?
What do you use to edit code in terminal? I have been doing tutorial using pico but I guess it is not the best linux C-code editor ;).
Now i try to finish:
linuxgazette.net/issue93/bhaskaran.html
tutorial but unfortunatelly I see no way to compile that source. Trying:
cc -I/usr/src/linux-2.4/include/ -Wall -c rtl8139.c
has very long output with lots of errors.
Trying:
make -C /usr/src/linux M=${pwd} modules
got 3 errors:
struct net_device has no member named open
struct net_device has no member named stop
struct net_device has no member named hard_start_xmit
Any ideas?