tcp_header.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00038 #ifndef NET_TCP_HEADER_H_
00039 #define NET_TCP_HEADER_H_
00040 
00041 #include <sys/types.h>
00042 
00044 #define TCP_HEADER_SIZE                         sizeof(tcp_header_t)
00045 
00049 #define TCP_HEADER_LENGTH(header)               (GET_TCP_HEADER_LENGTH(header) * 4U)
00050 
00054 #define TCP_COMPUTE_HEADER_LENGTH(length)       ((uint8_t) ((length) / 4U))
00055 
00059 typedef struct tcp_header tcp_header_t;
00060 
00064 typedef struct tcp_option tcp_option_t;
00065 
00067 typedef struct tcp_max_segment_size_option tcp_max_segment_size_option_t;
00068 
00070 struct tcp_header {
00071         uint16_t source_port;
00072         uint16_t destination_port;
00073         uint32_t sequence_number;
00074         uint32_t acknowledgement_number;
00075 
00076         uint8_t hlr; /* header length, reserved1 */
00077 
00078 #define GET_TCP_HEADER_LENGTH(header) \
00079         (((header)->hlr & 0xf0) >> 4)
00080 #define SET_TCP_HEADER_LENGTH(header, length) \
00081         ((header)->hlr = \
00082          ((length & 0x0f) << 4) | ((header)->hlr & 0x0f))
00083 
00084 #define GET_TCP_HEADER_RESERVED1(header) \
00085         ((header)->hlr & 0x0f)
00086 #define SET_TCP_HEADER_RESERVED1(header, reserved1) \
00087         ((header)->hlr = \
00088          (reserved1 & 0x0f) | ((header)->hlr & 0xf0))
00089 
00090         /* reserved2, urgent, acknowledge, push, reset, synchronize, finalize */
00091         uint8_t ruaprsf;  
00092 
00093 #define GET_TCP_HEADER_RESERVED2(header) \
00094         (((header)->ruaprsf & 0xc0) >> 6)
00095 #define SET_TCP_HEADER_RESERVED2(header, reserved2) \
00096         ((header)->ruaprsf = \
00097          ((reserved2 & 0x03) << 6) | ((header)->ruaprsf & 0x3f))
00098 
00099 #define GET_TCP_HEADER_URGENT(header) \
00100         (((header)->ruaprsf & 0x20) >> 5)
00101 #define SET_TCP_HEADER_URGENT(header, urgent) \
00102         ((header)->ruaprsf = \
00103          ((urgent & 0x01) << 5) | ((header)->ruaprsf & 0xdf))
00104 
00105 #define GET_TCP_HEADER_ACKNOWLEDGE(header) \
00106         (((header)->ruaprsf & 0x10) >> 4)
00107 #define SET_TCP_HEADER_ACKNOWLEDGE(header, acknowledge) \
00108         ((header)->ruaprsf = \
00109          ((acknowledge & 0x01) << 4) | ((header)->ruaprsf & 0xef))
00110 
00111 #define GET_TCP_HEADER_PUSH(header) \
00112         (((header)->ruaprsf & 0x08) >> 3)
00113 #define SET_TCP_HEADER_PUSH(header, push) \
00114         ((header)->ruaprsf = \
00115          ((push & 0x01) << 3) | ((header)->ruaprsf & 0xf7))
00116 
00117 #define GET_TCP_HEADER_RESET(header) \
00118         (((header)->ruaprsf & 0x04) >> 2)
00119 #define SET_TCP_HEADER_RESET(header, reset) \
00120         ((header)->ruaprsf = \
00121          ((reset & 0x01) << 2) | ((header)->ruaprsf & 0xfb))
00122 
00123 #define GET_TCP_HEADER_SYNCHRONIZE(header) \
00124         (((header)->ruaprsf & 0x02) >> 1)
00125 #define SET_TCP_HEADER_SYNCHRONIZE(header, synchronize) \
00126         ((header)->ruaprsf = \
00127          ((synchronize & 0x01) << 1) | ((header)->ruaprsf & 0xfd))
00128 
00129 #define GET_TCP_HEADER_FINALIZE(header) \
00130         ((header)->ruaprsf & 0x01)
00131 #define SET_TCP_HEADER_FINALIZE(header, finalize) \
00132         ((header)->ruaprsf = \
00133          (finalize & 0x01) | ((header)->ruaprsf & 0xfe))
00134 
00135         uint16_t window;
00136         uint16_t checksum;
00137         uint16_t urgent_pointer;
00138 } __attribute__ ((packed));
00139 
00141 struct tcp_option {
00143         uint8_t type;
00145         uint8_t length;
00146 };
00147 
00149 struct tcp_max_segment_size_option {
00154         tcp_option_t option;
00155         
00157         uint16_t max_segment_size;
00158 } __attribute__ ((packed));
00159 
00160 #endif
00161 

Generated on Thu Jun 2 07:45:50 2011 for HelenOS/USB by  doxygen 1.4.7