Sending raw Ethernet packets from a specific interface in C on Linux

Austin's blog

Lately I’ve been writing some code to send packets to a specific MAC address from a specific interface. I’m sure this will come in handy again so here is how it goes:

Includes:
(might not need all of these)

#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
#include <linux/if_packet.h>

Open the raw socket:

int sockfd;
...
/* Open RAW socket to send on */
if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
    perror("socket");
}

Get the index of the interface to send on:

struct ifreq if_idx;
...
memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, "eth0", IFNAMSIZ-1);
if (ioctl(sock, SIOCGIFINDEX, &if_idx) < 0)
    perror("SIOCGIFINDEX");

Get the MAC address of the interface to send on:

struct ifreq if_mac;
...
memset(&if_mac, 0, sizeof(struct ifreq));
strncpy(if_mac.ifr_name, "eth0", IFNAMSIZ-1);
if (ioctl(sock, SIOCGIFHWADDR, &if_mac) < 0)
    perror("SIOCGIFHWADDR");

Get the IP address of the interface to send on:

struct ifreq if_ip; …

View original post 437 more words

feof and fscanf …

What a shame. I have been stuck into this “bug” for several times.  In case of being awkward again, I decide to write it down this time.

If you have a file contains lines of numbers such as


1
2
3
4

Look at these C codes:


FILE *file;

file = fopen("text.txt", "r);

int val;

while (!feof(file))

{

    fscanf(file, "%d", &val);

    printf("%d\n", val);

}

fclose(file);

Surprise! The last line of the file was printed twice!

It’s because that the feof() return true if the last function failed, which means that when fscanf() touch the bottom of the file, the feof() still return 0 this time. It will return -1 when the next time fscanf() runs. Though fscanf() will fail, the value of “val” doesn’t change so that the last line

There are many ways to fix this.

I recommend that never use feof() again

For example


FILE *file;

file = fopen("text.txt", "r);

int val;

while (fscanf(file, "%d", &val) == 1)
{
    printf("%d\n", val);
}
fclose(file);

It’s done!

You can find more common C programming errors here

Build Own C/C++ Library in Linux

It’s pretty cool to see your own code become a library and run well in all linux PCs.

How does it happen?

I found 3 greats blogs on the Internet that teach you step by step.

1. C/C++ library programming on Linux – Part one: Static libraries

2. C/C++ library programming on Linux – Part two: Dynamic libraries

3. C/C++ library programming on Linux – Part three: Dynamic libraries using POSIX API

Briefly, If you want to build a static library, you can


gcc -c source1.c source2.c

ar rcs libname.a source1.o source2.o

gcc -o mainprog mainprog.c -Lpath_of_lib -lname

It work well on g++, too.

Similarly, you can build a dynamic library like this


gcc -fPIC -c source1.c source2.c

gcc -shared -Wl, -soname, libname.so.1 -o libname.so.1.0 source1.o source2.o

ln -sf libname.so.1.0 libname.so

ln -sf libname.so.1.0 libname.so.1

gcc -o mainprog mainprog.c -Lpath_of_lib -lname

#export the dir of lib

export  LD_LIBRARY_PATH=path_of_lib