Virtual Temperature Sensor Kernel Module

This Linux kernel module simulates a virtual temperature sensor as a character device. It generates random temperature readings between 0 and 40 degrees Celsius.

Files

  • virtual_temp_sensor.c: The kernel module that simulates the temperature sensor. It tested in Ubuntu 22.04
  • virtual_temp_sensor_driver.c: A user-space application to interact with the virtual temperature sensor using system calls.

Module Description

1. Header Files and Definitions

#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)

2. Global Variables and Structures

static int gMajorNumber;
static struct cdev sVirtualCharDevice;
static struct class *sVirtualCharDeviceClass;
static struct device *sVirtualCharDeviceDev;

3. Function Prototypes and File Operations

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
};

4. Module Initialization Function

static int __init fVirtualSensorInit(void) {
    // Code
}

5. Module Exit Function

static void __exit fVirtualSensorExit(void) {
    // Code
}

6. Module Meta Information and Initialization/Exit Functions

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);

Installation and Usage

Prerequisites

  • Linux kernel headers
  • Build-essential package

Install the necessary tools:

sudo apt update
sudo apt install build-essential linux-headers-$(uname -r)

Compiling the Module

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

Loading the Module

Load the kernel module:

sudo insmod virtual_temp_sensor.ko

Check the kernel log:

dmesg

Unloading the Module

Unload the module:

sudo rmmod virtual_temp_sensor

Compiling and Running the Driver

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

Output Example

Current Temperature : 32 Celsius, 89 Fahrenheit

Unit Testing

Running the Tests

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);
}  

Network Sniffer in C

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.

Files

  • network_sniffer.c: The main program file for the network sniffer.

Requirements

  • libpcap library

Compilation

To compile the network sniffer, use the following command:

gcc -o network_sniffer network_sniffer.c -lpcap

Usage

Run the compiled program as root (or with sufficient privileges):

sudo ./network_sniffer

Output Example

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

Program Explanation

  • 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.

Functionality

  • 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.

Notes

  • 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.