summaryrefslogtreecommitdiff
path: root/src/libnet_build_lldp.c
blob: c49fb8fc0aab31e6e11af97f9db9c0d798fb5d0a (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "common.h"

LIBNET_API
libnet_ptag_t libnet_build_lldp_chassis(const uint8_t subtype,
const uint8_t *const value,const uint8_t value_s, libnet_t *l,
libnet_ptag_t ptag)
{
  assert(value != NULL && "Chassis ID string cannot be a NULL");
  assert((value_s != 0) && "Chassis ID string length must be greater or equal to 1");
  assert(LIBNET_LLDP_TLV_HDR_SIZE == 0x02 && "TLV header size must be a 2 bytes");
  assert(LIBNET_LLDP_SUBTYPE_SIZE == 0x01 && "Subtype field size must be 1 byte");

  uint32_t n, h;
  libnet_pblock_t *p;
  struct libnet_lldp_hdr hdr;
  memset(&hdr, 0, sizeof(struct libnet_lldp_hdr));

  if (l == NULL)
  {
    return (-1);
  }

  if(value == NULL)
  {
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
    "%s(): Chassis ID string is NULL", __func__);
    return (-1);
  }

  if(value_s == 0)
  {
      snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
      "%s(): Incorrect Chassis ID string length", __func__);
      return (-1);
  }

  /* size of memory block */
  n = h =  LIBNET_LLDP_TLV_HDR_SIZE + /* TLV Header size */
           LIBNET_LLDP_SUBTYPE_SIZE + /* Chassis ID subtype size */
           value_s;                   /* Chassis ID string length */

  LIBNET_LLDP_TLV_SET_TYPE(hdr.tlv_info, LIBNET_LLDP_CHASSIS_ID);
  LIBNET_LLDP_TLV_SET_LEN(hdr.tlv_info, value_s + LIBNET_LLDP_SUBTYPE_SIZE);

  /*
   *  Find the existing protocol block if a ptag is specified, or create
   *  a new one.
   */
  p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LLDP_CHASSIS_H);
  if (p == NULL)
  {
    return (-1);
  }

  const uint16_t type_and_len = htons(hdr.tlv_info);
  if (libnet_pblock_append(l, p, &type_and_len, sizeof(type_and_len)) == -1)
  {
    goto bad;
  }

  if (libnet_pblock_append(l, p, &subtype, sizeof(subtype)) == -1)
  {
    goto bad;
  }

  if (libnet_pblock_append(l, p, value, value_s) == -1)
  {
    goto bad;
  }

  return (ptag ? ptag
               : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_LLDP_CHASSIS_H));
bad:
  libnet_pblock_delete(l, p);
  return (-1);
}


LIBNET_API
libnet_ptag_t libnet_build_lldp_port(const uint8_t subtype,
const uint8_t *const value, const uint8_t value_s,
libnet_t *l, libnet_ptag_t ptag)
{
  assert(value != NULL && "Port ID string cannot be a NULL");
  assert((value_s != 0) && "Port ID string length must be greater or equal to 1");
  assert(LIBNET_LLDP_TLV_HDR_SIZE == 0x02 && "TLV header size must be a 2 bytes");
  assert(LIBNET_LLDP_SUBTYPE_SIZE == 0x01 && "Subtype field size must be 1 byte");

  uint32_t n, h;
  libnet_pblock_t *p;
  struct libnet_lldp_hdr hdr;
  memset(&hdr, 0, sizeof(struct libnet_lldp_hdr));

  if (l == NULL)
  {
    return (-1);
  }

  if(value == NULL)
  {
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
    "%s(): Port ID string is NULL", __func__);
    return (-1);
  }

  if(value_s == 0)
  {
      snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
      "%s(): Incorrect Port ID string length", __func__);
      return (-1);
  }

  /* size of memory block */
  n = h =  LIBNET_LLDP_TLV_HDR_SIZE + /* TLV Header size */
           LIBNET_LLDP_SUBTYPE_SIZE + /* Port ID subtype size */
           value_s;                   /* Port ID string length */

  LIBNET_LLDP_TLV_SET_TYPE(hdr.tlv_info, LIBNET_LLDP_PORT_ID);
  LIBNET_LLDP_TLV_SET_LEN(hdr.tlv_info, value_s + LIBNET_LLDP_SUBTYPE_SIZE);

  /*
   *  Find the existing protocol block if a ptag is specified, or create
   *  a new one.
   */
  p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LLDP_PORT_H);
  if (p == NULL)
  {
    return (-1);
  }

  const uint16_t type_and_len = htons(hdr.tlv_info);
  if (libnet_pblock_append(l, p, &type_and_len, sizeof(type_and_len)) == -1)
  {
    goto bad;
  }

  if (libnet_pblock_append(l, p, &subtype, sizeof(subtype)) == -1)
  {
    goto bad;
  }

  if (libnet_pblock_append(l, p, value, value_s) == -1)
  {
    goto bad;
  }

  return (ptag ? ptag
               : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_LLDP_PORT_H));
bad:
  libnet_pblock_delete(l, p);
  return (-1);
}

LIBNET_API
libnet_ptag_t libnet_build_lldp_ttl(const uint16_t ttl,
libnet_t *l, libnet_ptag_t ptag)
{
  assert(ttl <= UINT16_MAX && "Incorrect value of ttl");
  assert(LIBNET_LLDP_TLV_HDR_SIZE == 0x02 && "TLV header size must be a 2 bytes");
  assert(LIBNET_LLDP_SUBTYPE_SIZE == 0x01 && "Subtype field size must be 1 byte");

  uint32_t n, h;
  libnet_pblock_t *p;
  struct libnet_lldp_hdr hdr;
  memset(&hdr, 0, sizeof(struct libnet_lldp_hdr));

  if (l == NULL)
  {
    return (-1);
  }

  if(ttl > UINT16_MAX)
  {
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
    "%s(): Incorrect value of ttl", __func__);
    return (-1);
  }

  /* size of memory block */
  n = h =  LIBNET_LLDP_TLV_HDR_SIZE + /* TLV Header size */
           sizeof(uint16_t);          /* Size of 2 octets */

  LIBNET_LLDP_TLV_SET_TYPE(hdr.tlv_info, LIBNET_LLDP_TTL);
  LIBNET_LLDP_TLV_SET_LEN(hdr.tlv_info, sizeof(uint16_t)); /* Size is 2 octets */

  /*
   *  Find the existing protocol block if a ptag is specified, or create
   *  a new one.
   */
  p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LLDP_TTL_H);
  if (p == NULL)
  {
    return (-1);
  }

  const uint16_t type_and_len = htons(hdr.tlv_info);
  if (libnet_pblock_append(l, p, &type_and_len, sizeof(type_and_len)) == -1)
  {
    goto bad;
  }

  if (libnet_pblock_append(l, p, &ttl, sizeof(ttl)) == -1)
  {
    goto bad;
  }

  return (ptag ? ptag
               : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_LLDP_TTL_H));
bad:
  libnet_pblock_delete(l, p);
  return (-1);
}


LIBNET_API
libnet_ptag_t libnet_build_lldp_end(libnet_t *l, libnet_ptag_t ptag)
{
  assert(LIBNET_LLDP_TLV_HDR_SIZE == 0x02 && "TLV header size must be a 2 bytes");
  assert(LIBNET_LLDP_SUBTYPE_SIZE == 0x01 && "Subtype field size must be 1 byte");

  uint32_t n, h;
  libnet_pblock_t *p;
  struct libnet_lldp_hdr hdr;
  memset(&hdr, 0, sizeof(struct libnet_lldp_hdr));

  if (l == NULL)
  {
    return (-1);
  }

  /* size of memory block */
  n = h =  LIBNET_LLDP_TLV_HDR_SIZE; /* TLV Header size */

  LIBNET_LLDP_TLV_SET_TYPE(hdr.tlv_info, LIBNET_LLDP_END_LLDPDU);
  LIBNET_LLDP_TLV_SET_LEN(hdr.tlv_info, 0);

  /*
   *  Find the existing protocol block if a ptag is specified, or create
   *  a new one.
   */
  p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LLDP_TTL_H);
  if (p == NULL)
  {
    return (-1);
  }

  const uint16_t type_and_len = htons(hdr.tlv_info);
  if (libnet_pblock_append(l, p, &type_and_len, sizeof(type_and_len)) == -1)
  {
    goto bad;
  }

  return (ptag ? ptag
               : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_LLDP_TTL_H));
bad:
  libnet_pblock_delete(l, p);
  return (-1);
}

/**
 * Local Variables:
 *  indent-tabs-mode: nil
 *  c-file-style: "stroustrup"
 * End:
 */