File: util.h

package info (click to toggle)
ladvd 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,240 kB
  • sloc: ansic: 11,169; sh: 189; makefile: 184
file content (109 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * $Id$
 *
 * Copyright (c) 2008, 2009
 *      Sten Spans <sten@blinkenlights.nl>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _util_h
#define _util_h

#include <pwd.h>

#define CRIT	0
#define WARN	1
#define INFO	2
#define DEBUG	3

extern int8_t loglevel;

#define my_log(p, ...)	    __my_log(__func__, p, 0, __VA_ARGS__)
#define my_loge(p, ...)	    __my_log(__func__, p, errno, __VA_ARGS__)
#define my_fatal(...)	    __my_fatal(__func__, 0, __VA_ARGS__)
#define my_fatale(...)	    __my_fatal(__func__, errno, __VA_ARGS__)
void __my_log(const char *func, int8_t prio, int err, const char *fmt, ...);
void __my_fatal(const char *func, int err, const char *fmt, ...) __noreturn;

void *my_malloc(size_t size);
void *my_calloc(size_t, size_t);
char *my_strdup(const char *str);
int my_socket(int af, int type, int proto);
void my_socketpair(int spair[]);
int my_nonblock(int s);

void my_chroot(const char *path) __nonnull();
void my_drop_privs(struct passwd *pwd) __nonnull();
void my_rlimit_child();

int read_line(const char *path, char *line, uint16_t len) __nonnull();
int write_line(const char *path, char *line, uint16_t len) __nonnull();
uint16_t my_chksum(const void *data, size_t length, int cisco) __nonnull();

ssize_t my_mreq(struct parent_req *mreq);

struct netif *netif_iter(struct netif *netif, struct nhead *);
struct netif *subif_iter(struct netif *subif, struct netif *netif);
int netif_excluded(struct netif *netif, struct ehead *);
void netif_protos(struct netif *netif, struct mhead *mqueue);
void netif_descr(struct netif *netif, struct mhead *mqueue);
void portname_abbr(char *);

static inline
struct netif *netif_byindex(struct nhead *netifs, uint32_t index) {
    struct netif *netif = NULL;

    assert(netifs);

    TAILQ_FOREACH(netif, netifs, entries) {
	if (netif->index == index)
	    break;
    }
    return(netif);
}

static inline
struct netif *netif_byname(struct nhead *netifs, char *name) {
    struct netif *netif;

    assert((netifs != NULL) && (name != NULL));

    TAILQ_FOREACH(netif, netifs, entries) {
	if (strcmp(netif->name, name) == 0)
	    break;
    }
    return(netif);
}

static inline
struct netif *netif_byaddr(struct nhead *netifs, uint8_t *hwaddr) {
    struct netif *netif;

    assert((netifs != NULL) && (hwaddr != NULL));

    TAILQ_FOREACH(netif, netifs, entries) {
	if (memcmp(netif->hwaddr, hwaddr, ETHER_ADDR_LEN) == 0)
	    break;
    }
    return(netif);
}

#define PCAP_MAGIC       0xa1b2c3d4
#define PCAP_PKTHDR_SIZ  16

void my_pcap_init(int fd);
void my_pcap_write(struct parent_msg *);
void my_pcap_close();

#endif /* _util_h */