• Sábado 18 de Mayo de 2024, 11:07

Autor Tema:  Obtener El Puerto Por Donde Entra El Paquete  (Leído 867 veces)

odp77

  • Miembro activo
  • **
  • Mensajes: 25
    • Ver Perfil
Obtener El Puerto Por Donde Entra El Paquete
« en: Viernes 19 de Mayo de 2006, 19:08 »
0
Estoy realizando este sniffer con la libreria pcap.h, pero tengo un problema al obtener el puerto origen y destino del paquete:

Para obtener dicha informacion, uso las librerias tcp.h y udp.h respectivamente. Quisiera enfocarme en tcp, ya que si se resuelve este, el otro sera igual. en esta libreria existen dos estructuras con el mismo nombre: "struct tcphdr". a continuacion las pondre para ver los campos que poseen:

Citar
struct tcphdr
  {
    u_int16_t th_sport;  /* source port */
    u_int16_t th_dport;  /* destination port */
    tcp_seq th_seq;  /* sequence number */
    tcp_seq th_ack;  /* acknowledgement number */
#  if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int8_t th_x2:4;  /* (unused) */
    u_int8_t th_off:4;  /* data offset */
#  endif
#  if __BYTE_ORDER == __BIG_ENDIAN
    u_int8_t th_off:4;  /* data offset */
    u_int8_t th_x2:4;  /* (unused) */
#  endif
    u_int8_t th_flags;
#  define TH_FIN   0x01
#  define TH_SYN   0x02
#  define TH_RST   0x04
#  define TH_PUSH   0x08
#  define TH_ACK   0x10
#  define TH_URG   0x20
    u_int16_t th_win;  /* window */
    u_int16_t th_sum;  /* checksum */
    u_int16_t th_urp;  /* urgent pointer */
};

# else /* !__FAVOR_BSD */
struct tcphdr
  {
    u_int16_t source;
    u_int16_t dest;
    u_int32_t seq;
    u_int32_t ack_seq;
#  if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int16_t res1:4;
    u_int16_t doff:4;
    u_int16_t fin:1;
    u_int16_t syn:1;
    u_int16_t rst:1;
    u_int16_t psh:1;
    u_int16_t ack:1;
    u_int16_t urg:1;
    u_int16_t res2:2;
#  elif __BYTE_ORDER == __BIG_ENDIAN
    u_int16_t doff:4;
    u_int16_t res1:4;
    u_int16_t res2:2;
    u_int16_t urg:1;
    u_int16_t ack:1;
    u_int16_t psh:1;
    u_int16_t rst:1;
    u_int16_t syn:1;
    u_int16_t fin:1;
#  else
#   error "Adjust your <bits/endian.h> defines"
#  endif
    u_int16_t window;
    u_int16_t check;
    u_int16_t urg_ptr;
};
# endif /* __FAVOR_BSD */

Notese que en la de arriba existe un campo llamado "th_sport". pero no me deja usarlo en el programa, me toca usar "source". Pero, total cuando uso este campo:

Citar
struct tcphdr *tcpc;
tcpc=packet + sizeof(struct ether_header);
printf("Puerto origen:%d \n",tcpc->source);

siempre sale que el puerto es el 69, cuando no lo es.

Que pena quitarles tanto tiemp pero es que necesito esa ayudita.

gracias y saludos.