Search code examples
clinux-device-driver

How to populate a sockaddr_in from char* in a Linux kernel module?


I am experimenting with sendng UDP packets from a Linux kernel module following the example code at "How to create a UDP Server in kernel-space"

I'm not that familiar with programming in kernel space.

In user space I can write:

struct sockaddr_in txAddr;
txAddr.sin_addr.s_addr = inet_addr("192.168.2.10");

But inet_addr does not appear available in the kernel.

How would I do it in the kernel space?


Solution

  • You do it manually:

    txAddr.sin_addr.s_addr = 0x0a02a8c0;
    

    Assuming your machine is little-endian, this will put the bytes in network byte order (i.e. big endian) in the field.