summaryrefslogtreecommitdiffstats
path: root/pickles/ip.pk
blob: 91608c4ca8581e5747475a3bd837e62e4fc902fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

/* Copyright (C) 2020-2021, Mohammad-Reza Nabipoor */
/* SPDX-License-Identifier: GPL-3.0-or-later */

/* WIP networking formats */

/* set_endian (ENDIAN_BIG); */

/* IPv4
 *
 * https://en.wikipedia.org/wiki/IPv4
 * - Updated Specification of the IPv4 ID Field
 *   https://tools.ietf.org/html/rfc6864
 */

type IPv4_Header =
  struct
  {
    uint<4> version = 4;
    offset<uint<4>,32> ihl : ihl >= 20#B; /* Internet Header Length */
    uint<6> dscp;  /* Differentiated Services Code Point */
    uint<2> ecn;   /* Explicit Congestion Notification */
    offset<uint16,B> total_length : /* including header and data */
      total_length >= 20#B;
    uint16   identification;
    uint<3>  flags;
    uint<13> fragment_offset;
    uint8    ttl; /* Time To Live */
    uint8    protocol;  /* RFC 790 */
    uint16   header_checksum;
    struct uint32
    {
      byte b0;
      byte b1;
      byte b2;
      byte b3;
    } src;  /* Source IP Address */
    struct uint32
    {
      byte b0;
      byte b1;
      byte b2;
      byte b3;
    } dst;  /* Destination IP Address */

    byte[ihl - 20#B] options; // FIXME
  };

type IPv4_Packet =
  struct
  {
    IPv4_Header header;

    var body_offset = OFFSET;
    var body_length = header.total_length - header.ihl;

    byte[0] @ body_offset + body_length; /* End-of-packet marker */

    method get_body_offset = offset<uint16,B>: { return body_offset; }
    method get_body_length = offset<uint16,B>: { return body_length; }
    method get_body = byte[]:
      {
        return byte[body_length] @ body_offset;
      }
  };

/* UDP (User Datagram Protocol) transport layer (IETF RFC 768)
 *
 * https://tools.ietf.org/html/rfc768
 */

type UDP_Header =
  struct
  {
    uint16 src_port;  /* source port */
    uint16 dst_port;  /* destination port */
    offset<uint16,B> length;
    uint16 checksum;
  };

assert ((UDP_Header {})'size == 8#B);

type UDP_Packet =
  struct
  {
    UDP_Header header;

    var body_offset = OFFSET;
    var body_length = header.length - header'size;

    byte[0] @ body_offset + body_length; /* End-of-packet marker */

    method get_body_offset = offset<uint16,B>: { return body_offset; }
    method get_body_length = offset<uint16,B>: { return body_length; }
    method get_body = byte[]:
      {
        return byte[body_length] @ body_offset;
      }
  };

type RTP_ExtensionHeader =
  struct
  {
    uint16 id;
    offset<uint16,32> len;  /* length of the extension in 32-bit units,
                             * excluding the 32 bits of the this header
                             */
    byte[len] data;
  };

type RTP_Header =
  struct
  {
    uint<2> version;
    uint<1> padding;
    uint<1> extension;
    uint<4> csrc_count;
    uint<1> marker;
    uint<7> payload_type;
    uint16  seq_num;          /* Sequence number */
    uint32  timestamp;
    uint32  ssrc;             /* Synchronization source identifier */
    uint32[csrc_count] csrc;  /* Contributing source identifiers */
    RTP_ExtensionHeader ext_header if extension;
  };

type RTP =
  struct
  {
    RTP_Header header;
    // FIXME
  };
Generated by cgit on ageinghacker.net.
I am Luca Saiu. If you have reason to request commit access to one of these repositories please contact me. You may also send me patches by email.