/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * IPC process structure
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#ifndef OUROBOROS_IPCPD_IPCP_H
#define OUROBOROS_IPCPD_IPCP_H

#include <ouroboros/hash.h>
#include <ouroboros/ipcp.h>
#include <ouroboros/list.h>
#include <ouroboros/protobuf.h>
#include <ouroboros/qos.h>
#include <ouroboros/sockets.h>
#include <ouroboros/tpm.h>

#include <pthread.h>
#include <time.h>

#define ipcp_dir_hash_strlen() (ipcp_dir_hash_len() * 2)

struct ipcp_ops {
        int   (* ipcp_bootstrap)(struct ipcp_config * conf);

        int   (* ipcp_enroll)(const char *        dst,
                              struct layer_info * info);

        int   (* ipcp_connect)(const char * dst,
                               const char * component,
                               qosspec_t    qs);

        int   (* ipcp_disconnect)(const char * dst,
                                  const char * component);

        int   (* ipcp_reg)(const uint8_t * hash);

        int   (* ipcp_unreg)(const uint8_t * hash);

        int   (* ipcp_query)(const uint8_t * hash);

        int   (* ipcp_flow_alloc)(int              fd,
                                  const uint8_t *  dst,
                                  qosspec_t        qs,
                                  const buffer_t * data);

        int   (* ipcp_flow_join)(int             fd,
                                 const uint8_t * dst);

        int   (* ipcp_flow_alloc_resp)(int              fd,
                                       int              response,
                                       const buffer_t * data);

        int   (* ipcp_flow_dealloc)(int fd);
};

int             ipcp_init(int               argc,
                          char **           argv,
                          struct ipcp_ops * ops,
                          enum ipcp_type    type);

int             ipcp_start(void);

void            ipcp_sigwait(void);

void            ipcp_stop(void);

void            ipcp_fini(void);

enum ipcp_type  ipcp_get_type(void);

const char *    ipcp_get_name(void);

/* TODO: Only specify hash algorithm in directory policy */
void            ipcp_set_dir_hash_algo(enum hash_algo algo);

void            ipcp_set_state(enum ipcp_state state);

enum ipcp_state ipcp_get_state(void);

/* Helper functions to handle races during flow allocation */
int             ipcp_wait_flow_req_arr(const uint8_t *  dst,
                                       qosspec_t        qs,
                                       time_t           mpl,
                                       uint32_t         mtu,
                                       const buffer_t * data);

int             ipcp_wait_flow_resp(const int fd);


/* Helper functions for directory entries, could be moved */
size_t          ipcp_dir_hash_len(void);

int             ipcp_get_layer_name(char * layer);

uint8_t *       ipcp_hash_dup(const uint8_t * hash);

void            ipcp_hash_str(char            buf[],
                              const uint8_t * hash);

/* Helper function to lock a thread to a core. */
void            ipcp_lock_to_core(void);

#endif /* OUROBOROS_IPCPD_IPCP_H */
