This Linux kernel module simulates a virtual temperature sensor as a character device. It generates random temperature readings between 0 and 40 degrees Celsius.
virtual_temp_sensor.c
: The kernel module that simulates the temperature sensor. It tested in Ubuntu 22.04virtual_temp_sensor_driver.c
: A user-space application to interact with the virtual temperature sensor using system calls.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/random.h>
#include <linux/device.h>
#define mDeviceName "virtual_temp_sensor"
#define mCommandFahrenheit _IOR('k', 1, int)
static int gMajorNumber;
static struct cdev sVirtualCharDevice;
static struct class *sVirtualCharDeviceClass;
static struct device *sVirtualCharDeviceDev;
static int fDeviceOpen(struct inode *, struct file *);
static ssize_t fDeviceRead(struct file *, char *, size_t, loff_t *);
static int fDeviceRelease(struct inode *, struct file *);
static long fDeviceIoctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param);
static struct file_operations sFileOperation = {
.owner = THIS_MODULE,
.unlocked_ioctl = fDeviceIoctl,
.open = fDeviceOpen,
.read = fDeviceRead,
.release = fDeviceRelease
};
static int __init fVirtualSensorInit(void) {
// Code
}
static void __exit fVirtualSensorExit(void) {
// Code
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oguzhan Caglar");
MODULE_DESCRIPTION("A virtual temperature sensor Linux kernel module");
MODULE_VERSION("0.1");
module_init(fVirtualSensorInit);
module_exit(fVirtualSensorExit);
- Linux kernel headers
- Build-essential package
Install the necessary tools:
sudo apt update
sudo apt install build-essential linux-headers-$(uname -r)
Create a Makefile
:
obj-m += virtual_temp_sensor.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Compile the module:
make
Load the kernel module:
sudo insmod virtual_temp_sensor.ko
Check the kernel log:
dmesg
Unload the module:
sudo rmmod virtual_temp_sensor
Compile the driver:
gcc -o virtual_temp_sensor_driver virtual_temp_sensor_driver.c
Run the driver to get temperature readings:
./virtual_temp_sensor_driver
Current Temperature : 32 Celsius, 89 Fahrenheit
We can test whether the number which generated by sensor is in 0-40 range or not. The following program can be a sample:
void test_fDeviceRead_random_temp_range(void) {
char buffer[sizeof(int)];
ssize_t result = fDeviceRead(NULL, buffer, sizeof(buffer), NULL);
int temp_value;
memcpy(&temp_value, buffer, sizeof(int));
// check the temperature sensor
assert(temp_value >= 0 && temp_value <= 40);
}
This program is a simple network sniffer developed in C using the pcap
library. It captures network traffic, displaying both MAC and IP addresses of each packet.
network_sniffer.c
: The main program file for the network sniffer.
- libpcap library
To compile the network sniffer, use the following command:
gcc -o network_sniffer network_sniffer.c -lpcap
Run the compiled program as root (or with sufficient privileges):
sudo ./network_sniffer
MAC Addresses: 00:0c:29:55:c4:1b -> 00:50:56:f6:ae:6f, IP Addresses: 192.168.244.129 -> 192.168.244.129
MAC Addresses: 00:50:56:f6:ae:6f -> 00:0c:29:55:c4:1b, IP Addresses: 8.8.8.8 -> 8.8.8.8
- Header Files:
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
- Packet Handler Function: This function is called by
pcap_loop
for each captured packet. It extracts and prints MAC and IP addresses.
void fPacketHandler(u_char *user_data, const struct pcap_pkthdr *pkthdr, const u_char *packet)
- Main Function: Sets up the pcap session and starts the packet capturing loop.
- Device Selection: The first available network device is selected for packet capturing.
- Packet Capturing: Captures packets in real-time and calls
fPacketHandler
for each packet. - Information Display: Displays the source and destination MAC and IP addresses for each packet.
- This program requires root privileges to capture network packets.
- Ensure that the libpcap library is installed on your system.
- Use this program responsibly and only on networks where you have permission to capture traffic.