Skip to content

Instantly share code, notes, and snippets.

@DRiKE
Created July 16, 2020 18:28

Revisions

  1. DRiKE created this gist Jul 16, 2020.
    32 changes: 32 additions & 0 deletions xdpblog1-3-parse_dname.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    static __always_inline
    uint8_t *parse_dname(struct cursor *c, uint8_t *pkt)
    {
    uint8_t *dname = c->pos;

    int i;
    for (i = 0; i < 128; i++) { /* Maximum 128 labels */
    uint8_t o;

    if (c->pos + 1 > c->end)
    return 0;

    o = *(uint8_t *)c->pos;
    if ((o & 0xC0) == 0xC0) {
    /* Compression label, Only back references! */
    if ((o | 0x3F) >= (dname - pkt))
    return 0;

    /* Compression label is the last label of a dname. */
    c->pos += 1;
    break;

    } else if (o & 0xC0)
    /* Unknown label type */
    return 0;

    c->pos += o + 1;
    if (!o)
    break;
    }
    return dname;
    }