ip tcp udp mpeg4头结构的定义

IP包头结构浅析

                                      

 IP报头结构图

版本号(Version):长度4比特。标识目前采用的IP协议的版本号。一般的值为0100IPv4),IPv6的值(0110

 

IP包头长度(Header Length):长度4比特。这个字段的作用是为了描述IP包头的长度,因为在IP包头中有变长的可选部分。IP包头最小长度为20字节,由于变长的可选部分最大长度可能会变成24字节。

 

服务类型(Type of Service):长度8比特。这个子段可以拆分成两个部分:PrecedenceTOSTOS目前不太使用。而Precedence则用于QOS应用。(TOS字段的详细描述RFC 1340 1349

 

IP包总长(Total Length):长度16比特。IP包最大长度65535字节。

 

标识符(Identifier:长度16比特。该字段和FlagsFragment Offest字段联合使用,对大的上层数据包进行分段(fragment)操作。

 

标记(Flags):长度3比特。该字段第一位不使用。第二位是DF位,DF位设为1时表明路由器不能对该上层数据包分段。如果一个上层数据包无法在不分段的情况下进行转发,则路由器会丢弃该上层数据包并返回一个错误信息。第三位是MF位,当路由器对一个上层数据包分段,则路由器会在除了最后一个分段的IP包的包头中将MF位设为1

 

分段序号(Fragment Offset):长度13比特。该字段对包含分段的上层数据包的IP包赋予序号。由于IP包在网络上传送的时候不一定能按顺序到达,这个字段保证了目标路由器在接受到IP包之后能够还原分段的上层数据包。到某个包含分段的上层数据包的IP包在传送是丢失,则整个一系列包含分段的上层数据包的IP包都会被要求重传。

 

生存时间(TTL):长度8比特。当IP包进行传送时,先会对该字段赋予某个特定的值。当IP包经过每一个沿途的路由器的时候,每个沿途的路由器会将IP包的TTL值减少1。如果TTL减少为0,则该IP包会被丢弃。这个字段可以防止由于故障而导致IP包在网络中不停被转发。

 

协议(Protocol):长度8比特。标识了上层所使用的协议。

 

头部校验(Header Checksum):长度16位,由于IP包头是变长的,所以提供一个头部校验来保证IP包头中信息的正确性。

 

起源和目标地址(Source and Destination Addresses

):这两个地段都是32比特。标识了这个IP包的起源和目标地址。

 

可选项(Options):这是一个可变长的字段。该字段由起源设备根据需要改写。可选项目包含以下内容:

松散源路由(Loose source routing):给出一连串路由器接口的IP地址。IP包必须沿着这些IP地址传送,但是允许在相继的两个IP地址之间跳过多个路由器。

严格源路由(Strict source routing):给出一连串路由器接口的IP地址。IP包必须沿着这些IP地址传送,如果下一跳不在IP地址表中则表示发生错误。

路由记录(Record route):当IP包离开每个路由器的时候记录路由器的出站接口的IP地址。

时间戳(Timestamps):当IP包离开每个路由器的时候记录时间。


其他定义


// i386 is little_endian.
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN   (1)   //BYTE ORDER
#else
#error Redefine LITTLE_ORDER
#endif

//Mac头部,总长度14字节
typedef struct _eth_hdr
{
unsigned char dstmac[6]; //目标mac地址
unsigned char srcmac[6]; //源mac地址
unsigned short eth_type; //以太网类型
}eth_hdr;

//IP头部,总长度20字节
typedef struct _ip_hdr
{
#if LITTLE_ENDIAN
unsigned char ihl:4;   //首部长度
unsigned char version:4, //版本 
#else
unsigned char version:4, //版本
unsigned char ihl:4;   //首部长度
#endif
unsigned char tos;   //服务类型
unsigned short tot_len; //总长度
unsigned short id;    //标志
unsigned short frag_off; //分片偏移
unsigned char ttl;   //生存时间
unsigned char protocol; //协议
unsigned short chk_sum; //检验和
struct in_addr srcaddr; //源IP地址
struct in_addr dstaddr; //目的IP地址
}ip_hdr;

//TCP头部,总长度20字节
typedef struct _tcp_hdr
{
unsigned short src_port;   //源端口号
unsigned short dst_port;   //目的端口号
unsigned int seq_no;    //序列号
unsigned int ack_no;    //确认号
#if LITTLE_ENDIAN
unsigned char reserved_1:4; //保留6位中的4位首部长度
unsigned char thl:4;    //tcp头部长度
unsigned char flag:6;    //6位标志
unsigned char reseverd_2:2; //保留6位中的2位
#else
unsigned char thl:4;    //tcp头部长度
unsigned char reserved_1:4; //保留6位中的4位首部长度
unsigned char reseverd_2:2; //保留6位中的2位
unsigned char flag:6;    //6位标志 
#endif
unsigned short wnd_size;   //16位窗口大小
unsigned short chk_sum;   //16位TCP检验和
unsigned short urgt_p;    //16为紧急指针
}tcp_hdr;

//UDP头部,总长度8字节
typedef struct _udp_hdr
{
unsigned short src_port; //远端口号
unsigned short dst_port; //目的端口号
unsigned short uhl;   //udp头部长度
unsigned short chk_sum; //16位udp检验和
}udp_hdr;

//ICMP头部,总长度4字节
typedef struct _icmp_hdr
{
unsigned char icmp_type;   //类型
unsigned char code;    //代码
unsigned short chk_sum;   //16位检验和
}icmp_hdr;

//Mpeg4头部 

 typedef   struct   _mp4_header   { 

//   vol 
int   ident; 
int   random_accessible_vol; 
int   type_indication; 
int   is_object_layer_identifier; 
int   visual_object_layer_verid; 
int   visual_object_layer_priority; 
int   aspect_ratio_info; 
int   vol_control_parameters; 
int   chroma_format; 
int   low_delay; 
int   vbv_parameters; 
int   first_half_bit_rate; 
int   latter_half_bit_rate; 
int   first_half_vbv_buffer_size; 
int   latter_half_vbv_buffer_size; 
int   first_half_vbv_occupancy; 
int   latter_half_vbv_occupancy; 
int   shape; 
int   time_increment_resolution; 
int   fixed_vop_rate; 
int   fixed_vop_time_increment; 
int   width; 
int   height; 
int   interlaced; 
int   obmc_disable; 
int   sprite_usage; 
int   not_8_bit; 
int   quant_precision; 
int   bits_per_pixel; 
int   quant_type; 
int   load_intra_quant_matrix; 
int   load_nonintra_quant_matrix; 
int   quarter_pixel; 
int   complexity_estimation_disable; 
int   error_res_disable; 
int   data_partitioning; 
int   intra_acdc_pred_disable; 
int   scalability; 

//   gop 
int   time_code; 
int   closed_gov; 
int   broken_link; 

//   vop 
int   prediction_type; 
int   time_base; 
int   time_inc; 
int   vop_coded; 
int   rounding_type; 
int   hor_spat_ref; 
int   ver_spat_ref; 
int   change_CR_disable; 
int   constant_alpha; 
int   constant_alpha_value; 
int   intra_dc_vlc_thr; 
int   quantizer; 
int   fcode_for; 
int   shape_coding_type; 

//   macroblock 
int   not_coded; 
int   mcbpc; 
int   derived_mb_type; 
int   cbpc; 
int   ac_pred_flag; 
int   cbpy; 
int   dquant; 
int   cbp; 

//   extra/derived 
int   mba_size; 
int   mb_xsize; 
int   mb_ysize; 
int   picnum; 
int   mba; 
int   mb_xpos; 
int   mb_ypos; 
int   dc_scaler; 

int   intrablock_rescaled; 

}   mp4_header;

//RTP头结构
  /* RFC 3550.5.1 RTP Fixed Header Fields.
             
                The RTP header has the following format:

                0                   1                   2                   3
                0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
               |V=2|P|X|  CC   |M|     PT      |       sequence number         |
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
               |                           timestamp                           |
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
               |           synchronization source (SSRC) identifier            |
               +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
               |            contributing source (CSRC) identifiers             |
               |                             ....                              |
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             
             
               5.3. Available if X bit filled.
                0                   1                   2                   3
                0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
               |      defined by profile       |           length              |
               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
               |                        header extension                       |
               |                             ....                              |

            */
	//rtcp头结构
            /* RFC 3550 RTCP header.
                    0                   1                   2                   3
                    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
                   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            header |V=2|P|    XX   |   type        |             length            |
                   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            */
    public class RTCP_PacketType
    {
        /// <summary>
        /// Sender report.
        /// </summary>
        public const int SR = 200;

        /// <summary>
        /// Receiver report.
        /// </summary>
        public const int RR = 201;

        /// <summary>
        /// Session description.
        /// </summary>
        public const int SDES = 202;

        /// <summary>
        /// BYE.
        /// </summary>
        public const int BYE = 203;

        /// <summary>
        /// Application specifiec data.
        /// </summary>
        public const int APP = 204;
    }
/*++
V ― 版本。识别 RTP 版本。 
P ― 间隙(Padding)。设置时,数据包包含一个或多个附加间隙位组,其中这部分不属于有效载荷。 
X ― 扩展位。设置时,在固定头后面,根据指定格式设置一个扩展头。 
CSRC Count ― 包含 CSRC 标识符(在固定头后)的编号。 
M ― 标记。标记由 Profile 文件定义。允许重要事件如帧边界在数据包流中进行标记。 
Payload Type ― 识别 RTP 有效载荷的格式,并通过应用程序决定其解释。Profile 文件规定了从 Payload 编码到 Payload 格式的缺省静态映射。另外的 Payload Type 编码可能通过非 RTP 方法实现动态定义。 
Sequence Number ― 每发送一个 RTP 数据包,序列号增加1。接收方可以依次检测数据包的丢失并恢复数据包序列。 
Timestamp ― 反映 RTP 数据包中的第一个八位组的采样时间。采样时间必须通过时钟及时提供线性无变化增量获取,以支持同步和抖动计算。 
SSRC ― 同步源。该标识符随机选择,旨在确保在同一个 RTP 会话中不存在两个同步源具有相同的 SSRC 标识符。 
CSRC ― 贡献源标识符。识别该数据包中的有效载荷的贡献源。 
--*/
typedef struct _RTP_HEAD
{
    unsigned char    Version        : 2;
    unsigned char    Padding        : 1;
    unsigned char    Extension    : 1;
    unsigned char    Ccount        : 4;
    unsigned char    Marker        : 1;
    unsigned char    Ptype        : 7;
    WORD            Snumber;        //16bits
    DWORD            Timestamp;        //32
    DWORD            Ssrc;            //32
    DWORD            Csrc;            //32
}RTP_HEAD,*pRTP_HEAD;

/*++
Version ― 识别 RTP 版本。 RTP 数据包中的该值与 RTCP 数据包中的一样。 当前规范定义的版本值为 2 。 
P ― 间隙(Padding)。设置时, RTCP 数据包包含一些其它 padding 八位位组,它们不属于控制信息。 Padding 的最后八位是用于计算应该忽略多少间隙八位位组。一些加密算法中需要计算固定块大小时也可能需要使用 Padding 字段。在一个复合 RTCP 数据包中,只有最后的个别数据包中才需要使用 padding ,这是因为复合数据包是作为一个整体来加密的。 
RC ― 接收方报告计数。包含在该数据包中的接收方报告块的数量,有效值为 0 。 
Packet type ― 包括常量 200 ,识别这是一个 RTCP SR 数据包。 
Length ― RTCP 数据包的大小(32 位字减去 1),包含头和任意间隙 (偏移量的引入使得 0 成为有效值,并避免了扫描复合 RTCP 数据包过程中的无限循环现象,而采用 32 位字计数方法则避免了对 4 的倍数的有效性校验)。 
--*/
typedef struct _RTCP_HEAD
{
    unsigned char    Version        : 2;
    unsigned char    Padding        : 1;
    unsigned char    PCount        : 5;
    unsigned char    Ptype;            //8bits
    WORD            Length;            //16bits
}RTCP_HEAD,*pRTCP_HEAD;
posted @ 2009-08-21 09:41  vieri122  阅读(4025)  评论(2编辑  收藏  举报