
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * dhcpsd - service helper
 * Copyright (c) 2025-2026 Roy Marples <roy@marples.name>
 * All rights reserved

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef SERVICE_H
#define SERVICE_H

#include <sys/uio.h>

#include "dhcpsd.h"

struct plugin;

struct srv_result {
	ssize_t sr_result;
	int sr_errno;
	void *sr_data;
	size_t sr_datalen;
};

struct srv_ctx {
	struct ctx *srv_ctx;
	int srv_fd;
	void *srv_buf;
	size_t srv_buflen;
	struct srv_result srv_result;
	ssize_t (*srv_dispatch)(struct srv_ctx *, struct plugin *, unsigned int,
	    const void *, size_t);
};

struct srv_ctx *srv_init(struct ctx *, const char *,
    ssize_t (*dispatch)(struct srv_ctx *, struct plugin *, unsigned int,
	const void *, size_t));
void srv_free(struct srv_ctx *);
#define srv_dropperms dhcpsd_dropperms
ssize_t srv_send(struct srv_ctx *, struct plugin *, unsigned int, ssize_t,
    const void *, size_t);
ssize_t srv_sendv(struct srv_ctx *, struct plugin *, unsigned int, ssize_t,
    struct iovec *, int);
int srv_run(struct srv_ctx *, struct plugin *, unsigned int, const void *,
    size_t, ssize_t *, void **, size_t *);
int srv_runv(struct srv_ctx *, struct plugin *, unsigned int, struct iovec *,
    int, ssize_t *, void **, size_t *);

#endif
