Estructura ARP a buffer unsigned char *

Iniciado por ahkbar87, 2 Febrero 2012, 22:08 PM

0 Miembros y 1 Visitante están viendo este tema.

ahkbar87

Hola hackearianos maestros,
        Perdón la ausencia no he ingresado mucho por trabajo aunque no me cuesta nada, igual me escuso, trataré de ayudar en lo que pueda, aunque siempre que se algún tema alguien los contesta, ni que decir Ethernal Idol un mostro.

Esta está difícil, yo no comprendo que pasa el caso es el siguiente:

tengo una estructura declarada de la siguiente manera, que son los campos de ARP.

Código (cpp) [Seleccionar]

#ifndef __ARP__20111128
#define __ARP__20111128

#include <pcap/pcap.h>
#include <netinet/in.h>

//Opcodes
#define ARP_OP_REPLY    0x0002
#define ARP_OP_REQUEST  0x0001


namespace Crossover{
namespace Framework{
namespace Net{
namespace Protocols{


    struct ARP{
        u_short hrd;    // Hardware address space (e.g., Ethernet, Packet Radio Net.)
        u_short pro;    // Protocol address space.  For Ethernet hardware, this is from the set of type fields ether_typ$<protocol>.
        u_char  hln;    // byte length of each hardware address
        u_char  pln;    // byte length of each protocol address
        u_short op;     // opcode (ares_op$REQUEST | ares_op$REPLY)
        u_char  sha[6]; // Hardware address of sender of this packet, n from the ar$hln field.
        in_addr spa;    // Protocol address of sender of this packet, m from the ar$pln field.
        u_char  tha[6]; // Hardware address of target of this packet (if known).
        in_addr tpa;    // Protocol address of target.
    }; 

}}}};

#endif


y en el main tengo el siguiente código.
Código (cpp) [Seleccionar]


#include "Crossover/Crossover.h"

using namespace Crossover::Framework::Net::Protocols;
using namespace Crossover::Framework::Net;
using namespace std;

int main(){

    try
    {   

        ARP arp;
        memset(&arp, 0 , sizeof(arp));
        arp.spa.s_addr = 0xffffffff;
        unsigned char *szBuff = (unsigned char *)malloc(1024);

        memcpy( szBuff, &arp, sizeof(arp));
           


    }   
    catch(Exception ex)
    {   
        cout << ex.message() << " OS: " << ex.systemMessage() << endl;
    }   
    return 0;
}





El problema es que al momento de pasar esta linea.
memcpy( szBuff, &arp, sizeof(arp));

Los 0xffffffff asignados (  arp.spa.s_addr) no comienzan desde el indice 14 (szBuff[14]) como debería ser, si no que desde el 16.

de antemano gracias. saludos!


rir3760

En una estructura los campos de esta no necesariamente están colocados uno inmediatamente después del otro (como sucede en el caso de los arrays).

Si es necesario para cumplir con los requisitos de alineación algunos bytes no se utilizan (eso se conoce como "padding"). Casi te puedo asegurar que el tamaño del tipo "in_addr" dado por "sizeof(in_addr)" es cuatro y de ahí que la siguiente posición libre utilizada y que debe ser múltiplo de ese numero es 16.

Un saludo
C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.
--
Kernighan & Ritchie, The C programming language

ahkbar87

Gracias rir3760,
                         Estaba buscando el problema del padding en las estructuras, aún no se como solventar el problema, para que el padding no lo haga entre sha y spa:

u_char  sha[6]; // Aquí el compilador le agregar 2 bytes de padding.
in_addr spa;


¿Sabes cual podría ser la solución?

:)

ahkbar87

#3
Llegue a la solución gracias por leer.

http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/2009-July/006339.html

__attribute__ ((packed)) arp