xref: /openbsd/lib/libradius/radius_local.h (revision 834c20a7)
1*834c20a7Syasuoka /*	$OpenBSD: radius_local.h,v 1.2 2024/07/24 08:19:16 yasuoka Exp $ */
20eaf192dSyasuoka 
30eaf192dSyasuoka /*-
40eaf192dSyasuoka  * Copyright (c) 2009 Internet Initiative Japan Inc.
50eaf192dSyasuoka  * All rights reserved.
60eaf192dSyasuoka  *
70eaf192dSyasuoka  * Redistribution and use in source and binary forms, with or without
80eaf192dSyasuoka  * modification, are permitted provided that the following conditions
90eaf192dSyasuoka  * are met:
100eaf192dSyasuoka  * 1. Redistributions of source code must retain the above copyright
110eaf192dSyasuoka  *    notice, this list of conditions and the following disclaimer.
120eaf192dSyasuoka  * 2. Redistributions in binary form must reproduce the above copyright
130eaf192dSyasuoka  *    notice, this list of conditions and the following disclaimer in the
140eaf192dSyasuoka  *    documentation and/or other materials provided with the distribution.
150eaf192dSyasuoka  *
160eaf192dSyasuoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170eaf192dSyasuoka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180eaf192dSyasuoka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190eaf192dSyasuoka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200eaf192dSyasuoka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210eaf192dSyasuoka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220eaf192dSyasuoka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230eaf192dSyasuoka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240eaf192dSyasuoka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250eaf192dSyasuoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260eaf192dSyasuoka  * SUCH DAMAGE.
270eaf192dSyasuoka  */
280eaf192dSyasuoka 
290eaf192dSyasuoka #ifndef RADIUS_LOCAL_H
300eaf192dSyasuoka #define RADIUS_LOCAL_H
310eaf192dSyasuoka 
320eaf192dSyasuoka #ifndef countof
330eaf192dSyasuoka #define countof(x) (sizeof(x)/sizeof((x)[0]))
340eaf192dSyasuoka #endif
350eaf192dSyasuoka 
360eaf192dSyasuoka typedef struct _RADIUS_PACKET_DATA {
370eaf192dSyasuoka 	uint8_t code;
380eaf192dSyasuoka 	uint8_t id;
390eaf192dSyasuoka 	uint16_t length;
400eaf192dSyasuoka 	uint8_t authenticator[16];
410eaf192dSyasuoka 	char attributes[0];
420eaf192dSyasuoka }                   RADIUS_PACKET_DATA;
430eaf192dSyasuoka #pragma pack(1)
440eaf192dSyasuoka typedef struct _RADIUS_ATTRIBUTE {
450eaf192dSyasuoka 	uint8_t type;
460eaf192dSyasuoka 	uint8_t length;
470eaf192dSyasuoka 	char data[0];
480eaf192dSyasuoka 	uint32_t vendor;
490eaf192dSyasuoka 	uint8_t vtype;
500eaf192dSyasuoka 	uint8_t vlength;
510eaf192dSyasuoka 	char vdata[0];
520eaf192dSyasuoka }                 RADIUS_ATTRIBUTE;
530eaf192dSyasuoka #pragma pack()
540eaf192dSyasuoka 
550eaf192dSyasuoka struct _RADIUS_PACKET {
560eaf192dSyasuoka 	RADIUS_PACKET_DATA *pdata;
570eaf192dSyasuoka 	size_t capacity;
580eaf192dSyasuoka 	const RADIUS_PACKET *request;
590eaf192dSyasuoka };
600eaf192dSyasuoka #define RADIUS_PACKET_CAPACITY_INITIAL   64
610eaf192dSyasuoka #define RADIUS_PACKET_CAPACITY_INCREMENT 64
620eaf192dSyasuoka 
630eaf192dSyasuoka #define ATTRS_BEGIN(pdata) ((RADIUS_ATTRIBUTE*)pdata->attributes)
640eaf192dSyasuoka 
650eaf192dSyasuoka #define ATTRS_END(pdata) \
660eaf192dSyasuoka     ((RADIUS_ATTRIBUTE*)(((char*)pdata) + ntohs(pdata->length)))
670eaf192dSyasuoka 
680eaf192dSyasuoka #define ATTRS_NEXT(x) ((RADIUS_ATTRIBUTE*)(((char*)x) + x->length))
690eaf192dSyasuoka 
700eaf192dSyasuoka /*
710eaf192dSyasuoka  * must be expression rather than statement
720eaf192dSyasuoka  * to be used in third expression of for statement.
730eaf192dSyasuoka  */
740eaf192dSyasuoka #define ATTRS_ADVANCE(x) (x = ATTRS_NEXT(x))
750eaf192dSyasuoka 
760eaf192dSyasuoka int radius_ensure_add_capacity(RADIUS_PACKET * packet, size_t capacity);
77*834c20a7Syasuoka int radius_unshift_raw_attr(RADIUS_PACKET * packet, uint8_t type,
78*834c20a7Syasuoka     const void *buf, size_t length);
790eaf192dSyasuoka 
800eaf192dSyasuoka #define ROUNDUP(a, b)	((((a) + (b) - 1) / (b)) * (b))
810eaf192dSyasuoka #define	MINIMUM(a, b)	(((a) < (b))? (a) : (b))
820eaf192dSyasuoka 
830eaf192dSyasuoka #endif				/* RADIUS_LOCAL_H */
84