Tuesday, September 29, 2015

Digest for comp.lang.c++@googlegroups.com - 5 updates in 1 topic

dakupoto@gmail.com: Sep 28 11:19PM -0700

Could some C++ guru please help ? May be
my question is stupid, so pardon me.
 
Can STL have a built-in method to handle
ANY user defined data type ? To be specific,
suppose I want to model a TCP/IP packet
as a struct. As the memebers of a TCP/IP
packet are in reality are byte arrays,
I can visualize the entire packet as a
large(1500) array of bytes, with the first
so many bytes making up the source address
and so on. So, I can use a vector of bytes
to model a TCP/IP packet. But now the
question is, can I just go ahead and use
the struct as template type(like for example
a built-in type as float, int etc.,) instead
of first taking out the contents of the same
TCP/IP packet and putting them into a byte
vector ?
 
Any hints/suggestions would be greatly
appreciated -thanks in advance for your help.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Sep 29 07:30AM

> my question is stupid, so pardon me.
 
> Can STL have a built-in method to handle
> ANY user defined data type ?
 
I don't understand that.
 
> packet are in reality are byte arrays,
> I can visualize the entire packet as a
> large(1500)
 
It can be larger than that, and if you reassemble fragments
so you're looking at IP /datagrams/ rather than /packets/,
it can reach 65535 octets.
 
> so many bytes making up the source address
> and so on. So, I can use a vector of bytes
> to model a TCP/IP packet.
 
Yes, that seems like a fair way to start.
 
> of first taking out the contents of the same
> TCP/IP packet and putting them into a byte
> vector ?
 
I don't understand what you're trying to say here. Which struct, and
what do you mean by a "template type"? Please explain better. It
seems like it could be an interesting question, once I understand it!
 
If you mean this:
 
std::vector<uint8_t> packet = something();
const struct ip_hdr* hdr = (struct ip_hdr*)&packet[0];
 
then I personally recommend against it. I always implement it as
functions which read octet by octet from the buffer:
 
struct in_addr src = src_of(packet);
 
That avoids endianness and alignment bugs, and forces you to consider
damaged packets and fragments.
 
> Any hints/suggestions would be greatly
> appreciated -thanks in advance for your help.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Robert Wessel <robertwessel2@yahoo.com>: Sep 29 03:19AM -0500

On 29 Sep 2015 07:30:41 GMT, Jorgen Grahn <grahn+nntp@snipabacken.se>
wrote:
 
 
>It can be larger than that, and if you reassemble fragments
>so you're looking at IP /datagrams/ rather than /packets/,
>it can reach 65535 octets.
 
 
IPv6 jumbograms can be 4GiB.
Juha Nieminen <nospam@thanks.invalid>: Sep 29 08:24AM

> But now the
> question is, can I just go ahead and use
> the struct as template type
 
Why would you think you can't use a struct as a template type?
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
scott@slp53.sl.home (Scott Lurndal): Sep 29 01:33PM

>of first taking out the contents of the same
>TCP/IP packet and putting them into a byte
>vector ?
 
namespace net {
 
/**
* Layer 2 (Ethernet) Header
*/
struct s_ethernet {
uint8_t e_dest_mac[6];
uint8_t e_source_mac[6];
uint16_t e_ethertype;
 
uint16_t get_ethertype(void) { return be16toh(e_ethertype); }
} PACKED;
 
/**
* Layer 2 (Ethernet) Header with 802.1Q VLAN tag.
*/
struct s_802_1q_ethernet {
uint8_t e_dest_mac[6];
uint8_t e_source_mac[6];
uint32_t e_802_1q_tag;
uint16_t e_ethertype;
 
uint16_t get_ethertype(void) { return be16toh(e_ethertype); }
} PACKED;
 
/**
* Layer 3 IPv4 Header
*/
struct s_ipv4 {
#if __BYTE_ORDER == __BIG_ENDIAN
uint8_t i_version:4,
i_ihl:4;
#else
uint8_t i_ihl:4,
i_version:4;

No comments: