Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:41:44+00:00 2026-05-13T12:41:44+00:00

I’m still struggling to get my sample code working on a 64 bit machine.

  • 0

I’m still struggling to get my sample code working on a 64 bit machine. My previous problem was solved because of missing/outdated headers/libraries.

I compile this code as follow: gcc -Wall -g -o server server.c and gcc -Wall -g -o client client.c
I run them on 2 linux machines(both 32bit) and it works fine. When I recompile this code on 64 bit machines then it seems that no packets go through from the client to the server. Putting tcpdump on the client it seems that it is sending malformed headers that gets rejected all the time. Can anybody enlighten me?

#include "client.h"
#include "util.h"

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include <asm/types.h>

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>

#define BUF_SIZE ETH_FRAME_TOTALLEN
#define NUMBER_OF_MESUREMENTS_PER_AMOUNT_OF_DATA 100000 /*how often to measure travelling time with one certain amount of data*/

int s = 0; /*Socketdescriptor*/
void* buffer = NULL;
long total_sent_packets = 0;

int main(int argc, char* argv[]) 
{
    buffer = (void*)malloc(BUF_SIZE);   /*Buffer for ethernet frame*/
    unsigned char* etherhead = buffer;  /*Pointer to ethenet header*/
    unsigned char* data = buffer + 14;  /*Userdata in ethernet frame*/
    struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to ethernet header*/

    unsigned char src_mac[6];       /*our MAC address */
    unsigned char dest_mac[6] = {0x00, 0x1E, 0x4F, 0xB1, 0xCB, 0x43};   /*MAC address, hardcoded...... :-(*/ 

    struct ifreq ifr;
    struct sockaddr_ll socket_address;
    int ifindex = 0;            /*Ethernet Interface index*/
    int i,j,k;
    int length;             /*length of received packet*/
    int sent;               /*length of sent packet*/

    /*stuff for time measuring: */
    struct timeval begin;
    struct timeval end;
    struct timeval result;
    unsigned long long allovertime;

    printf("Client started, entering initialiation phase...\n");

    /*open socket*/
    s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (s == -1) {
        perror("socket():");
            exit(1);
    }
    printf("Successfully opened socket: %i\n", s);

    /*retrieve ethernet interface index*/
    strncpy(ifr.ifr_name, ETH0, IFNAMSIZ);
    if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
        perror("SIOCGIFINDEX");
        exit(1);
    }
    ifindex = ifr.ifr_ifindex;
    printf("Successfully got interface index: %i\n", ifindex);

    /*retrieve corresponding MAC*/
    if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
        perror("SIOCGIFINDEX");
        exit(1);
    }
        for (i = 0; i < 6; i++) {
        src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
    }
    printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", 
            src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);

    /*prepare sockaddr_ll*/
    socket_address.sll_family   = PF_PACKET;
    socket_address.sll_protocol = htons(ETH_P_IP);
    socket_address.sll_ifindex  = ifindex;
    socket_address.sll_hatype   = ARPHRD_ETHER;
    socket_address.sll_pkttype  = PACKET_OTHERHOST;
    socket_address.sll_halen    = ETH_ALEN;
    socket_address.sll_addr[0]  = dest_mac[0];
    socket_address.sll_addr[1]  = dest_mac[1];
        socket_address.sll_addr[2]  = dest_mac[2];
        socket_address.sll_addr[3]  = dest_mac[3];
        socket_address.sll_addr[4]  = dest_mac[4];
        socket_address.sll_addr[5]  = dest_mac[5];
    socket_address.sll_addr[6]  = 0x00; 
    socket_address.sll_addr[7]  = 0x00;


    /*establish signal handler*/
    signal(SIGINT, sigint);
    printf("Successfully established signal handler for SIGINT\n");

    /*init random number generator*/
    srand(time(NULL));

    printf("We are in production state, sending packets to: %02X:%02X:%02X:%02X:%02X:%02X\n",
            dest_mac[0],dest_mac[1],dest_mac[2],dest_mac[3],dest_mac[4],dest_mac[5]);

    for (i = 50; i <= 1500; i += 50) {

        allovertime = 0;

        for (k = 0; k < NUMBER_OF_MESUREMENTS_PER_AMOUNT_OF_DATA; k++) {
            /*prepare buffer*/
            memcpy((void*)buffer, (void*)dest_mac, ETH_MAC_LEN);
            memcpy((void*)(buffer+ETH_MAC_LEN), (void*)src_mac, ETH_MAC_LEN);
            eh->h_proto = ETH_P_NULL;
            /*fill it with random data....*/
            for (j = 0; j < i; j++) {
                data[j] = (unsigned char)((int) (256.0*rand()/(RAND_MAX+1.0)));
            }

            /*clear the timers:*/
            timerclear(&begin);
            timerclear(&end);

            /*get time before sending.....*/
             gettimeofday(&begin,NULL);


            /*send packet*/
            sent = sendto(s, buffer, i+ETH_HEADER_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
            if (sent == -1) {
                perror("sendto():");
                exit(1);
            }

            /*Wait for incoming packet...*/
            length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
            if (length == -1) {
                perror("recvfrom():");
                exit(1);
            }

            /*get time after sending.....*/
            gettimeofday(&end,NULL);
            /*...and calculate difference.........*/
            timersub(&end,&begin,&result); 

                allovertime += ((result.tv_sec * 1000000 ) + result.tv_usec );

            total_sent_packets++;
        }

        printf("Sending %i bytes takes %lld microseconds in average\n",i ,allovertime/NUMBER_OF_MESUREMENTS_PER_AMOUNT_OF_DATA);
    }
    return (0);
}

void sigint(int signum) {
    /*Clean up.......*/

    struct ifreq ifr;

        if (s == -1)
            return;

    strncpy(ifr.ifr_name, ETH0, IFNAMSIZ);
    ioctl(s, SIOCGIFFLAGS, &ifr);
    ifr.ifr_flags &= ~IFF_PROMISC;
    ioctl(s, SIOCSIFFLAGS, &ifr);
    close(s);

    free(buffer);

    printf("Client terminating....\n");

    printf("Totally sent: %ld packets\n", total_sent_packets);
    exit(0);
}

——————server.c———————

#include "server.h"
#include "util.h"

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include <asm/types.h>

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>

#define BUF_SIZE ETH_FRAME_TOTALLEN

int s = 0; /*Socketdescriptor*/
void* buffer = NULL;
long total_packets = 0;
long answered_packets = 0;

int main(int argc, char* argv[]) 
{
    buffer = (void*)malloc(BUF_SIZE);   /*Buffer for Ethernet Frame*/
    unsigned char* etherhead = buffer;  /*Pointer to Ethenet Header*/
    struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to ethernet header*/

    unsigned char src_mac[6];       /*our MAC address*/

    struct ifreq ifr;
    struct sockaddr_ll socket_address;
    int ifindex = 0;            /*Ethernet Interface index*/
    int i;
    int length;             /*length of received packet*/
    int sent;

    printf("Server started, entering initialiation phase...\n");

    /*open socket*/
    s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (s == -1) {
        perror("socket():");
            exit(1);
    }
    printf("Successfully opened socket: %i\n", s);

    /*retrieve ethernet interface index*/
    strncpy(ifr.ifr_name, ETH0, IFNAMSIZ);
    if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
        perror("SIOCGIFINDEX");
        exit(1);
    }
    ifindex = ifr.ifr_ifindex;
    printf("Successfully got interface index: %i\n", ifindex);

    /*retrieve corresponding MAC*/
    if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
        perror("SIOCGIFINDEX");
        exit(1);
    }
        for (i = 0; i < 6; i++) {
        src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
    }
    printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", 
            src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);

    /*prepare sockaddr_ll*/
    socket_address.sll_family   = PF_PACKET;
    socket_address.sll_protocol = htons(ETH_P_IP);
    socket_address.sll_ifindex  = ifindex;
    socket_address.sll_hatype   = ARPHRD_ETHER;
    socket_address.sll_pkttype  = PACKET_OTHERHOST;
    socket_address.sll_halen    = ETH_ALEN;
    socket_address.sll_addr[6]  = 0x00; 
    socket_address.sll_addr[7]  = 0x00;


    /*establish signal handler (keyboard)*/
    signal(SIGINT, sigint);
    printf("Successfully established signal handler for SIGINT\n");

    printf("We are in production state, waiting for incoming packets....\n");

    while (1) {
        /*Wait for incoming packet...*/ 
        length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
        if (length == -1) {
            perror("recvfrom():");
            exit(1);
        }
        /*See if we should answer (Ethertype == 0x0 && destination address == our MAC)*/
        if (eh->h_proto == ETH_P_NULL && memcmp( (const void*)eh->h_dest, (const void*)src_mac, ETH_MAC_LEN) == 1 ) 
        {
            /*exchange addresses in buffer*/
            memcpy( (void*)etherhead, (const void*)(etherhead+ETH_MAC_LEN), ETH_MAC_LEN);
            memcpy( (void*)(etherhead+ETH_MAC_LEN), (const void*)src_mac, ETH_MAC_LEN);

            /*prepare sockaddr_ll*/
            socket_address.sll_addr[0]  = eh->h_dest[0];
            socket_address.sll_addr[1]  = eh->h_dest[1];
            socket_address.sll_addr[2]  = eh->h_dest[2];
            socket_address.sll_addr[3]  = eh->h_dest[3];
            socket_address.sll_addr[4]  = eh->h_dest[4];
            socket_address.sll_addr[5]  = eh->h_dest[5];

            /*send answer*/
            sent = sendto(s, buffer, length-4, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
            if (sent == -1) {
                perror("sendto():");
                exit(1);
            }

            answered_packets++;
        }

        total_packets++;
    }
}

void sigint(int signum) {
    /*Clean up.......*/

    struct ifreq ifr;

        if (s == -1)
            return;

    strncpy(ifr.ifr_name, ETH0, IFNAMSIZ);
    ioctl(s, SIOCGIFFLAGS, &ifr);
    ifr.ifr_flags &= ~IFF_PROMISC;
    ioctl(s, SIOCSIFFLAGS, &ifr);
    close(s);

    free(buffer);

    printf("Server terminating....\n");

    printf("Totally received: %ld packets\n", total_packets);
    printf("Answered %ld packets\n", answered_packets);
    exit(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:41:44+00:00Added an answer on May 13, 2026 at 12:41 pm

    Sorry for all the code, but I didn’t know how to make it shorter as i wasn’t sure where the error was. Anyway, I found the problem, so apologies for the “wall of code” guys!

    It turns out the problem was how our switches are configured. After I got 2 machines on the same subnet the code worked beautiful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are right that IndexOf is a more general operation… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer If you are on OSX, might as well install the… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer Use an SqlDepedency. This can be done in WinForms as… May 14, 2026 at 8:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.