"Fossies" - the Fresh Open Source Software Archive

Member "open-fcoe-3.19/fcoe-utils/include/net_types.h" (15 Apr 2015, 11737 Bytes) of package /linux/misc/open-fcoe-3.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "net_types.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2008, Intel Corporation.
    3  *
    4  * This program is free software; you can redistribute it and/or modify it
    5  * under the terms and conditions of the GNU Lesser General Public License,
    6  * version 2.1, as published by the Free Software Foundation.
    7  *
    8  * This program is distributed in the hope it will be useful, but WITHOUT
    9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   11  * for more details.
   12  *
   13  * You should have received a copy of the GNU Lesser General Public License
   14  * along with this program; if not, write to the Free Software Foundation, Inc.,
   15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
   16  *
   17  */
   18 
   19 #ifndef _LIBSA_NET_TYPES_H_
   20 #define _LIBSA_NET_TYPES_H_
   21 
   22 #if !defined(NTOHL)
   23 #include <netinet/in.h>
   24 #endif /* NTOHL */
   25 
   26 /*
   27  * Type definitions for network order fields in protocol packets.
   28  * The access functions below do gets and puts on these structures.
   29  */
   30 typedef unsigned char net8_t;       /* direct use and assignment allowed */
   31 
   32 /*
   33  * Aligned network order types.
   34  */
   35 typedef struct {
   36     u_int16_t   net_data;
   37 } net16_t;
   38 
   39 typedef struct {
   40     u_int32_t   net_data;
   41 } net32_t;
   42 
   43 /*
   44  * The 64-bit type only requires 32-bit alignment.
   45  */
   46 typedef struct {
   47     u_int32_t   net_data[2];    /* most significant word first */
   48 } net64_t;
   49 
   50 /*
   51  * 24-bit type.  Byte aligned, in spite of the name.
   52  */
   53 typedef struct {
   54     unsigned char   net_data[3];
   55 } net24_t;
   56 
   57 /*
   58  * 48-bit type.  Byte aligned.
   59  */
   60 typedef struct {
   61     unsigned char   net_data[6];
   62 } net48_t;
   63 
   64 /*
   65  * Unaligned network order types.
   66  * Any of these structures can be byte aligned.  No padding is implied.
   67  */
   68 typedef struct {
   69     unsigned char   net_data[2];
   70 } ua_net16_t;
   71 
   72 typedef struct {
   73     unsigned char   net_data[4];
   74 } ua_net32_t;
   75 
   76 typedef struct {
   77     unsigned char   net_data[8];
   78 } ua_net64_t;
   79 
   80 /*
   81  * Accessor functions.
   82  */
   83 
   84 /**
   85  * net8_get(net) - fetch from a network-order 8-bit field.
   86  *
   87  * @param net pointer to network-order 8-bit data.
   88  * @return the host-order value.
   89  */
   90 static inline u_int8_t net8_get(const net8_t * net)
   91 {
   92     return *net;
   93 }
   94 
   95 /**
   96  * net8_put(net, val) - store to a network-order 8-bit field.
   97  *
   98  * @param net pointer to network-order 8-bit data.
   99  * @param val host-order value to be stored at net.
  100  */
  101 static inline void net8_put(net8_t * net, u_int8_t val)
  102 {
  103     *net = val;
  104 }
  105 
  106 /**
  107  * net16_get(net) - fetch from a network-order 16-bit field.
  108  *
  109  * @param net pointer to type net16_t, network-order 16-bit data.
  110  * @return the host-order value.
  111  */
  112 static inline u_int16_t net16_get(const net16_t * net)
  113 {
  114     return ntohs(net->net_data);
  115 }
  116 
  117 /**
  118  * net16_put(net, val) - store to a network-order 16-bit field.
  119  *
  120  * @param net pointer to a net16_t, network-order 16-bit data.
  121  * @param val host-order value to be stored at net.
  122  */
  123 static inline void net16_put(net16_t * net, u_int16_t val)
  124 {
  125     net->net_data = htons(val);
  126 }
  127 
  128 /**
  129  * ua_net16_get(net) - fetch from an unaligned network-order 16-bit field.
  130  *
  131  * @param net pointer to type ua_net16_t, unaligned, network-order 16-bit data.
  132  * @return the host-order value.
  133  */
  134 static inline u_int16_t ua_net16_get(const ua_net16_t * net)
  135 {
  136     return (net->net_data[0] << 8) | net->net_data[1];
  137 }
  138 
  139 /**
  140  * ua_net16_put(net, val) - store to a network-order 16-bit field.
  141  *
  142  * @param net pointer to a ua_net16_t, network-order 16-bit data.
  143  * @param val host-order value to be stored at net.
  144  */
  145 static inline void ua_net16_put(ua_net16_t * net, u_int16_t val)
  146 {
  147     net->net_data[0] = (u_int8_t)((val >> 8) & 0xFF);
  148     net->net_data[1] = (u_int8_t)(val & 0xFF);
  149 }
  150 
  151 /**
  152  * net24_get(net) - fetch from a network-order 24-bit field.
  153  *
  154  * @param net pointer to type net24_t, network-order 24-bit data.
  155  * @return the host-order value.
  156  */
  157 static inline u_int32_t net24_get(const net24_t * net)
  158 {
  159     return (net->net_data[0] << 16) |
  160         (net->net_data[1] << 8) | net->net_data[2];
  161 }
  162 
  163 /**
  164  * net24_put(net, val) - store to a network-order 24-bit field.
  165  *
  166  * @param net pointer to a net24_t, network-order 24-bit data.
  167  * @param val host-order value to be stored at net.
  168  */
  169 static inline void net24_put(net24_t * net, u_int32_t val)
  170 {
  171     net->net_data[0] = (u_int8_t)((val >> 16) & 0xFF);
  172     net->net_data[1] = (u_int8_t)((val >> 8) & 0xFF);
  173     net->net_data[2] = (u_int8_t)(val & 0xFF);
  174 }
  175 
  176 /**
  177  * net32_get(net) - fetch from a network-order 32-bit field.
  178  *
  179  * @param net pointer to type net32_t, network-order 32-bit data.
  180  * @return the host-order value.
  181  */
  182 static inline u_int32_t net32_get(const net32_t * net)
  183 {
  184     return ntohl(net->net_data);
  185 }
  186 
  187 /**
  188  * net32_put(net, val) - store to a network-order 32-bit field.
  189  *
  190  * @param net pointer to a net32_t, network-order 32-bit data.
  191  * @param val host-order value to be stored at net.
  192  */
  193 static inline void net32_put(net32_t * net, u_int32_t val)
  194 {
  195     net->net_data = htonl(val);
  196 }
  197 
  198 /**
  199  * ua_net32_get(net) - fetch from an unaligned network-order 32-bit field.
  200  *
  201  * @param net pointer to type ua_net32_t, unaligned, network-order 32-bit data.
  202  * @return the host-order value.
  203  */
  204 static inline u_int32_t ua_net32_get(const ua_net32_t * net)
  205 {
  206     return (net->net_data[0] << 24) | (net->net_data[1] << 16) |
  207         (net->net_data[2] << 8) | net->net_data[3];
  208 }
  209 
  210 /**
  211  * ua_net32_put(net, val) - store to a network-order 32-bit field.
  212  *
  213  * @param net pointer to a ua_net32_t, network-order 32-bit data.
  214  * @param val host-order value to be stored at net.
  215  */
  216 static inline void ua_net32_put(ua_net32_t * net, u_int32_t val)
  217 {
  218     net->net_data[0] = (u_int8_t)((val >> 24) & 0xFF);
  219     net->net_data[1] = (u_int8_t)((val >> 16) & 0xFF);
  220     net->net_data[2] = (u_int8_t)((val >> 8) & 0xFF);
  221     net->net_data[3] = (u_int8_t)(val & 0xFF);
  222 }
  223 
  224 /**
  225  * net48_get(net) - fetch from a network-order 48-bit field.
  226  *
  227  * @param net pointer to type net48_t, network-order 48-bit data.
  228  * @return the host-order value.
  229  */
  230 static inline u_int64_t net48_get(const net48_t * net)
  231 {
  232     return ((u_int64_t) net->net_data[0] << 40) |
  233         ((u_int64_t) net->net_data[1] << 32) |
  234         ((u_int64_t) net->net_data[2] << 24) |
  235         ((u_int64_t) net->net_data[3] << 16) |
  236         ((u_int64_t) net->net_data[4] << 8) |
  237         (u_int64_t) net->net_data[5];
  238 }
  239 
  240 /**
  241  * net48_put(net, val) - store to a network-order 48-bit field.
  242  *
  243  * @param net pointer to a net48_t, network-order 48-bit data.
  244  * @param val host-order value to be stored at net.
  245  */
  246 static inline void net48_put(net48_t * net, u_int64_t val)
  247 {
  248     net->net_data[0] = (u_int8_t)((val >> 40) & 0xFF);
  249     net->net_data[1] = (u_int8_t)((val >> 32) & 0xFF);
  250     net->net_data[2] = (u_int8_t)((val >> 24) & 0xFF);
  251     net->net_data[3] = (u_int8_t)((val >> 16) & 0xFF);
  252     net->net_data[4] = (u_int8_t)((val >> 8) & 0xFF);
  253     net->net_data[5] = (u_int8_t)(val & 0xFF);
  254 }
  255 
  256 /**
  257  * net64_get(net) - fetch from a network-order 64-bit field.
  258  *
  259  * @param net pointer to type net64_t, network-order 64-bit data.
  260  * @return the host-order value.
  261  */
  262 static inline u_int64_t net64_get(const net64_t * net)
  263 {
  264     return ((u_int64_t) ntohl(net->net_data[0]) << 32) |
  265         ntohl(net->net_data[1]);
  266 }
  267 
  268 /**
  269  * net64_put(net, val) - store to a network-order 64-bit field.
  270  *
  271  * @param net pointer to a net64_t, network-order 64-bit data.
  272  * @param val host-order value to be stored at net.
  273  */
  274 static inline void net64_put(net64_t * net, u_int64_t val)
  275 {
  276     net->net_data[0] = (u_int32_t)htonl(val >> 32);
  277     net->net_data[1] = (u_int32_t)htonl((u_int32_t) val);
  278 }
  279 
  280 /**
  281  * ua_net64_get(net) - fetch from an unaligned network-order 64-bit field.
  282  *
  283  * @param net pointer to type ua_net64_t, unaligned, network-order 64-bit data.
  284  * @return the host-order value.
  285  */
  286 static inline u_int64_t ua_net64_get(const ua_net64_t * net)
  287 {
  288     return ((u_int64_t) net->net_data[0] << 56) |
  289         ((u_int64_t) net->net_data[1] << 48) |
  290         ((u_int64_t) net->net_data[2] << 40) |
  291         ((u_int64_t) net->net_data[3] << 32) |
  292         ((u_int64_t) net->net_data[4] << 24) |
  293         ((u_int64_t) net->net_data[5] << 16) |
  294         ((u_int64_t) net->net_data[6] << 8) |
  295         (u_int64_t) net->net_data[7];
  296 }
  297 
  298 /**
  299  * ua_net64_put(net, val) - store to a network-order 64-bit field.
  300  *
  301  * @param net pointer to a ua_net64_t, network-order 64-bit data.
  302  * @param val host-order value to be stored at net.
  303  */
  304 static inline void ua_net64_put(ua_net64_t * net, u_int64_t val)
  305 {
  306     net->net_data[0] = (u_int8_t)((val >> 56) & 0xFF);
  307     net->net_data[1] = (u_int8_t)((val >> 48) & 0xFF);
  308     net->net_data[2] = (u_int8_t)((val >> 40) & 0xFF);
  309     net->net_data[3] = (u_int8_t)((val >> 32) & 0xFF);
  310     net->net_data[4] = (u_int8_t)((val >> 24) & 0xFF);
  311     net->net_data[5] = (u_int8_t)((val >> 16) & 0xFF);
  312     net->net_data[6] = (u_int8_t)((val >> 8) & 0xFF);
  313     net->net_data[7] = (u_int8_t)(val & 0xFF);
  314 }
  315 
  316 /*
  317  * Compile-time initializers for the network-order type structures.
  318  * Note that the upper byte of these values is not masked so the
  319  * compiler will catch initializers that don't fit in the field.
  320  */
  321 
  322 /**
  323  * NET8_INIT(_val) - initialize a net8_t type.
  324  *
  325  * @param _val 8-bit value.
  326  * @return net8_t network-order value.
  327  */
  328 #define NET8_INIT(_val)     (_val)
  329 
  330 /**
  331  * NET24_INIT(_val) - initialize a net24_t type.
  332  *
  333  * @param _val host-order value.
  334  * @return net24_t network-order value.
  335  */
  336 #define NET24_INIT(_val)    { {                 \
  337                 ((_val) >> 16),         \
  338                 ((_val) >> 8) & 0xff,       \
  339                 ((_val) >> 0) & 0xff        \
  340                 } }
  341 
  342 /**
  343  * NET48_INIT(_val) - initialize a net48_t type.
  344  *
  345  * @param _val host-order value.
  346  * @return net48_t network-order value.
  347  */
  348 #define NET48_INIT(_val)    { {                 \
  349                 ((_val) >> 40),         \
  350                 ((_val) >> 32) & 0xff,      \
  351                 ((_val) >> 24) & 0xff,      \
  352                 ((_val) >> 16) & 0xff,      \
  353                 ((_val) >> 8) & 0xff,       \
  354                 ((_val) >> 0) & 0xff        \
  355                 } }
  356 
  357 /**
  358  * NET16_INIT(_val) - initialize a net16_t type.
  359  *
  360  * @param _val host-order value.
  361  * @return net16_t network-order value.
  362  */
  363 #define NET16_INIT(_val)    {   htons(_val) }
  364 
  365 /**
  366  * UA_NET16_INIT(_val) - initialize an unaligned 16-bit type.
  367  *
  368  * @param _val host-order value.
  369  * @return ua_net24_t network-order value.
  370  */
  371 #define UA_NET16_INIT(_val) { {                 \
  372                 ((_val) >> 8),          \
  373                 ((_val) >> 0) & 0xff        \
  374                 } }
  375 
  376 /**
  377  * NET32_INIT(_val) - initialize a 32-bit type.
  378  *
  379  * @param _val host-order value.
  380  * @return net32_t network-order value.
  381  */
  382 #define NET32_INIT(_val)    {   htonl(_val) }
  383 
  384 /**
  385  * UA_NET32_INIT(_val) - initialize an unaligned 32-bit type.
  386  *
  387  * @param _val host-order value.
  388  * @return ua_net32_t network-order value.
  389  */
  390 #define UA_NET32_INIT(_val) { {                 \
  391                 ((_val) >> 24),         \
  392                 ((_val) >> 16) & 0xff,      \
  393                 ((_val) >> 8) & 0xff,       \
  394                 ((_val) >> 0) & 0xff        \
  395                 } }
  396 
  397 /**
  398  * UA_NET48_INIT(_val) - initialize an unaligned 48-bit type.
  399  *
  400  * @param _val host-order value.
  401  * @return ua_net48_t network-order value.
  402  */
  403 #define UA_NET48_INIT(_val) { {                 \
  404                 ((_val) >> 40),         \
  405                 ((_val) >> 32) & 0xff,      \
  406                 ((_val) >> 24) & 0xff,      \
  407                 ((_val) >> 16) & 0xff,      \
  408                 ((_val) >> 8) & 0xff,       \
  409                 ((_val) >> 0) & 0xff        \
  410                 } }
  411 
  412 /**
  413  * NET64_INIT(_val) - initialize an unaligned 64-bit type.
  414  *
  415  * @param _val host-order value.
  416  * @return ua_net64_t network-order value.
  417  */
  418 #define NET64_INIT(_val)    { {                 \
  419                 htonl((_val) >> 32),        \
  420                 htonl((_val) & 0xffffffff)  \
  421                 } }
  422 
  423 /**
  424  * UA_NET64_INIT(_val) - initialize a 64-bit type.
  425  *
  426  * @param _val host-order value.
  427  * @return net64_t network-order value.
  428  */
  429 #define UA_NET64_INIT(_val) { {                  \
  430                 ((_val) >> 56),         \
  431                 ((_val) >> 48) & 0xff,      \
  432                 ((_val) >> 40) & 0xff,      \
  433                 ((_val) >> 32) & 0xff,      \
  434                 ((_val) >> 24) & 0xff,      \
  435                 ((_val) >> 16) & 0xff,      \
  436                 ((_val) >> 8) & 0xff,       \
  437                 ((_val) >> 0) & 0xff        \
  438                 } }
  439 
  440 #endif /* _LIBSA_NET_TYPES_H_ */