I've go t a small but very important problem... please help me

Brecht Vermeulen Brecht.Vermeulen at rug.ac.be
Wed Jan 15 14:06:55 EST 2003



> The problem is that inside the code of the element (written in C++) I need
> to access to the ip destination address and destination port that are inside
> the IP header of the IP packet ....


well, you can get a pointer to the IP header and then you can access all
fields (as in a struct), have a look at the elements in elements/ip

e.g. setipdscp
inline Packet *
SetIPDSCP::smaction(Packet *p_in)
{
  WritablePacket *p = p_in->uniqueify();
  click_ip *ip = p->ip_header();
  assert(ip);

  unsigned short old_hw = ((unsigned short *)ip)[0];
  ip->ip_tos = (ip->ip_tos & 0x3) | _dscp;
  unsigned short new_hw = ((unsigned short *)ip)[0];

  // 19.Aug.1999 - incrementally update IP checksum according to RFC1624.
  // new_sum = ~(~old_sum + ~old_halfword + new_halfword)
  unsigned sum = (~ip->ip_sum & 0xFFFF) + (~old_hw & 0xFFFF) + new_hw;
  sum = (sum & 0xFFFF) + (sum >> 16);
  ip->ip_sum = ~(sum + (sum >> 16));

  return p;
}


you see the use of ip->ip_tos,

The header as used, can be found in include/click and is:

struct click_ip {
#if CLICK_BYTE_ORDER == CLICK_LITTLE_ENDIAN
    uint8_t     ip_hl : 4;              /* 0     header length */
    uint8_t     ip_v : 4;               /*       version == 4 */
#endif
#if CLICK_BYTE_ORDER == CLICK_BIG_ENDIAN
    uint8_t     ip_v : 4;               /* 0     version == 4 */
    uint8_t     ip_hl : 4;              /*       header length */
#endif
    uint8_t     ip_tos;                 /* 1     type of service */
#define IP_DSCPMASK 0xFC                /*         diffserv code point */
#define IP_ECNMASK 0x03                 /*         ECN code point */
#define IP_ECN_NOT_ECT 0x00             /*         not ECN capable
transport */
#define IP_ECN_ECT1 0x01                /*         ECN capable transport
*/
#define IP_ECN_ECT2 0x02                /*         ECN capable transport
*/
#define IP_ECN_CE 0x03                  /*         ECN congestion exp'd */
    uint16_t    ip_len;                 /* 2-3   total length */
    uint16_t    ip_id;                  /* 4-5   identification */
    uint16_t    ip_off;                 /* 6-7   fragment offset field */
#define IP_RF 0x8000                    /*         reserved fragment flag
*/
#define IP_DF 0x4000                    /*         don't fragment flag */
#define IP_MF 0x2000                    /*         more fragments flag */
#define IP_OFFMASK 0x1fff               /*         mask for fragmenting
bits */
    uint8_t     ip_ttl;                 /* 8     time to live */
    uint8_t     ip_p;                   /* 9     protocol */
    uint16_t    ip_sum;                 /* 10-11 checksum */
    struct in_addr ip_src;              /* 12-15 source address */
    struct in_addr ip_dst;              /* 16-19 destination address */
};


so, the ip_dst is the destination ip field.

same story for tcp header and udp header (where you need the port), have a
look into click_tcp.h

here you would use something as
click_tcp *tcp = p->tcp_header();

This is as far as I see, I have never used the tcp_header thing however,
so please test and find yourself some things. Maybe there are still other
possibilities also ?

regards,
Brecht




More information about the click mailing list