]> git.ipfire.org Git - thirdparty/strongswan.git/blob - lib/liblwres/lwpacket.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / lib / liblwres / lwpacket.c
1 /*
2 * Copyright (C) 2000, 2001 Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 /* $Id: lwpacket.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
19
20 #include <config.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <lwres/lwbuffer.h>
27 #include <lwres/lwpacket.h>
28 #include <lwres/result.h>
29
30 #include "assert_p.h"
31
32 #define LWPACKET_LENGTH \
33 (sizeof(lwres_uint16_t) * 4 + sizeof(lwres_uint32_t) * 5)
34
35 lwres_result_t
36 lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
37 REQUIRE(b != NULL);
38 REQUIRE(pkt != NULL);
39
40 if (!SPACE_OK(b, LWPACKET_LENGTH))
41 return (LWRES_R_UNEXPECTEDEND);
42
43 lwres_buffer_putuint32(b, pkt->length);
44 lwres_buffer_putuint16(b, pkt->version);
45 lwres_buffer_putuint16(b, pkt->pktflags);
46 lwres_buffer_putuint32(b, pkt->serial);
47 lwres_buffer_putuint32(b, pkt->opcode);
48 lwres_buffer_putuint32(b, pkt->result);
49 lwres_buffer_putuint32(b, pkt->recvlength);
50 lwres_buffer_putuint16(b, pkt->authtype);
51 lwres_buffer_putuint16(b, pkt->authlength);
52
53 return (LWRES_R_SUCCESS);
54 }
55
56 lwres_result_t
57 lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
58 lwres_uint32_t space;
59
60 REQUIRE(b != NULL);
61 REQUIRE(pkt != NULL);
62
63 space = LWRES_BUFFER_REMAINING(b);
64 if (space < LWPACKET_LENGTH)
65 return (LWRES_R_UNEXPECTEDEND);
66
67 pkt->length = lwres_buffer_getuint32(b);
68 /*
69 * XXXBEW/MLG Checking that the buffer is long enough probably
70 * shouldn't be done here, since this function is supposed to just
71 * parse the header.
72 */
73 if (pkt->length > space)
74 return (LWRES_R_UNEXPECTEDEND);
75 pkt->version = lwres_buffer_getuint16(b);
76 pkt->pktflags = lwres_buffer_getuint16(b);
77 pkt->serial = lwres_buffer_getuint32(b);
78 pkt->opcode = lwres_buffer_getuint32(b);
79 pkt->result = lwres_buffer_getuint32(b);
80 pkt->recvlength = lwres_buffer_getuint32(b);
81 pkt->authtype = lwres_buffer_getuint16(b);
82 pkt->authlength = lwres_buffer_getuint16(b);
83
84 return (LWRES_R_SUCCESS);
85 }