Skip to content
Snippets Groups Projects
Commit 05c1d01a authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa
Browse files

Add packet header encoding functions

parent 8e20bd8d
No related merge requests found
......@@ -33,10 +33,12 @@ lib_LTLIBRARIES = libngtcp2.la
OBJECTS = \
ngtcp2_pkt.c \
ngtcp2_conv.c
ngtcp2_conv.c \
ngtcp2_str.c
HFILES = \
ngtcp2_pkt.h \
ngtcp2_conv.h
ngtcp2_conv.h \
ngtcp2_str.h
libngtcp2_la_SOURCES = $(HFILES) $(OBJECTS)
libngtcp2_la_LDFLAGS = -no-undefined \
......
......@@ -30,6 +30,8 @@
#include <arpa/inet.h>
#endif /* HAVE_ARPA_INET_H */
#include "ngtcp2_str.h"
#ifdef WORDS_BIGENDIAN
#define bwap64(N) (N)
#else /* !WORDS_BIGENDIAN */
......@@ -54,3 +56,18 @@ uint16_t ngtcp2_get_uint16(const uint8_t *p) {
memcpy(&n, p, 2);
return ntohs(n);
}
uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n) {
n = bswap64(n);
return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
}
uint8_t *ngtcp2_put_uint32be(uint8_t *p, uint32_t n) {
n = htonl(n);
return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
}
uint8_t *ngtcp2_put_uint16be(uint8_t *p, uint16_t n) {
n = htons(n);
return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
}
......@@ -52,4 +52,25 @@ uint32_t ngtcp2_get_uint32(const uint8_t *p);
*/
uint16_t ngtcp2_get_uint16(const uint8_t *p);
/*
* ngtcp2_put_uint64be writes |n| in host byte order in |p| in network
* byte order. It returns the one beyond of the last written
* position.
*/
uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n);
/*
* ngtcp2_put_uint32be writes |n| in host byte order in |p| in network
* byte order. It returns the one beyond of the last written
* position.
*/
uint8_t *ngtcp2_put_uint32be(uint8_t *p, uint32_t n);
/*
* ngtcp2_put_uint16be writes |n| in host byte order in |p| in network
* byte order. It returns the one beyond of the last written
* position.
*/
uint8_t *ngtcp2_put_uint16be(uint8_t *p, uint16_t n);
#endif /* NGTCP2_CONV_H */
......@@ -23,6 +23,9 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ngtcp2_pkt.h"
#include <assert.h>
#include "ngtcp2_conv.h"
ssize_t ngtcp2_pkt_decode_hd(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
......@@ -140,3 +143,74 @@ ssize_t ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
return (ssize_t)len;
}
ssize_t ngtcp2_pkt_encode_hd_long(uint8_t *out, size_t outlen,
const ngtcp2_pkt_hd *hd) {
uint8_t *p;
if (outlen < 17) {
return NGTCP2_ERR_INVALID_ARGUMENT;
}
p = out;
*p++ = NGTCP2_HEADER_FORM_MASK | hd->type;
p = ngtcp2_put_uint64be(p, hd->conn_id);
p = ngtcp2_put_uint32be(p, hd->pkt_num);
p = ngtcp2_put_uint32be(p, hd->version);
assert(p - out == 17);
return 17;
}
ssize_t ngtcp2_pkt_encode_hd_short(uint8_t *out, size_t outlen,
const ngtcp2_pkt_hd *hd) {
uint8_t *p;
size_t len = 1;
int need_conn_id;
if (hd->flags & NGTCP2_PKT_FLAG_CONN_ID) {
need_conn_id = 1;
len += 8;
}
len += hd->type;
if (outlen < len) {
return NGTCP2_ERR_INVALID_ARGUMENT;
}
p = out;
*p = hd->type;
if (need_conn_id) {
*p |= NGTCP2_CONN_ID_MASK;
}
if (hd->flags & NGTCP2_PKT_FLAG_KEY_PHASE) {
*p |= NGTCP2_KEY_PHASE_MASK;
}
++p;
if (need_conn_id) {
p = ngtcp2_put_uint64be(p, hd->conn_id);
}
switch (hd->type) {
case 1:
*p++ = (uint8_t)hd->pkt_num;
break;
case 2:
p = ngtcp2_put_uint16be(p, (uint16_t)hd->pkt_num);
break;
case 3:
p = ngtcp2_put_uint32be(p, hd->pkt_num);
break;
default:
assert(0);
}
assert((size_t)(p - out) == len);
return p - out;
}
......@@ -66,4 +66,28 @@ ssize_t ngtcp2_pkt_decode_hd_long(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
ssize_t ngtcp2_pkt_decode_hd_short(ngtcp2_pkt_hd *dest, const uint8_t *pkt,
size_t pktlen);
/*
* ngtcp2_pkt_encode_hd_long encodes |hd| as QUIC long header into
* |out| which has length |outlen|. It returns the number of bytes
* written into |outlen| if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_INVALID_ARGUMENT
* Buffer is too short
*/
ssize_t ngtcp2_pkt_encode_hd_long(uint8_t *out, size_t outlen,
const ngtcp2_pkt_hd *hd);
/*
* ngtcp2_pkt_encode_hd_short encodes |hd| as QUIC short header into
* |out| which has length |outlen|. It returns the number of bytes
* written into |outlen| if it succeeds, or one of the following
* negative error codes:
*
* NGTCP2_ERR_INVALID_ARGUMENT
* Buffer is too short
*/
ssize_t ngtcp2_pkt_encode_hd_short(uint8_t *out, size_t outlen,
const ngtcp2_pkt_hd *hd);
#endif /* NGTCP2_PKT_H */
/*
* ngtcp2
*
* Copyright (c) 2017 ngtcp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ngtcp2_str.h"
#include <string.h>
uint8_t *ngtcp2_cpymem(uint8_t *dest, const uint8_t *src, size_t n) {
memcpy(dest, src, n);
return dest + n;
}
/*
* ngtcp2
*
* Copyright (c) 2017 ngtcp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NGTCP2_STR_H
#define NGTCP2_STR_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <ngtcp2/ngtcp2.h>
uint8_t *ngtcp2_cpymem(uint8_t *dest, const uint8_t *src, size_t n);
#endif /* NGTCP2_STR_H */
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment