*** empty log message ***
[mmondor.git] / tests / bktr_httpd / net.h
CommitLineData
fa6ec601
MM
1/* $Id: net.h,v 1.1 2008/01/19 04:22:28 mmondor Exp $ */
2
3/*
4 * Copyright (c) 2008, Matthew Mondor
5 * ALL RIGHTS RESERVED.
6 */
7
8#ifndef _NET_H_
9#define _NET_H_
10
11
12#include <stdbool.h>
13
14
15/* Address family independent system */
16
17struct server_sockaddr {
18 union {
19 struct sockaddr sockaddr;
20 struct sockaddr_in sockaddr_in;
21 struct sockaddr_in6 sockaddr_in6;
22 } u;
23};
24#define ss_family u.sockaddr.sa_family
25
26struct server_af_info {
27 size_t sock_length;
28 size_t addr_length;
29 size_t port_offset;
30 size_t addr_offset;
31};
32
33extern struct server_af_info *server_afi[1];
34
35/*
36 * Note that the following macros should only be used after calling
37 * net_init(), since they require the server_afi array to previously
38 * have been initialized.
39 */
40
41/* Returns pointer to family of address */
42#define SERVER_SOCKADDR_FAMILY(s) \
43 ((sa_family_t *)(&(s)->ss_family))
44/* Returns length of sockaddr-style address */
45#define SERVER_SOCKADDR_SOCKLEN(s) \
46 (server_afi[(int)(s)->ss_family]->sock_length)
47/* Returns length of internal address */
48#define SERVER_SOCKADDR_ADDRLEN(s) \
49 (server_afi[(int)(s)->ss_family]->addr_length)
50/* Returns pointer to port of address */
51#define SERVER_SOCKADDR_PORT(s) \
52 ((in_port_t *)(((char *)s) + \
53 server_afi[(int)(s)->ss_family]->port_offset))
54/* Returns pointer to family-specific address */
55#define SERVER_SOCKADDR_ADDRESS(s) \
56 ((void *)(((char *)s) + \
57 server_afi[(int)(s)->ss_family]->addr_offset))
58/* Returns struct sockaddr family-independent pointer */
59#define SERVER_SOCKADDR(s) \
60 (&((s)->u.sockaddr))
61
62
63typedef struct netaddr {
64 pnode_t node;
65 struct server_sockaddr saddr;
66 int count;
67} netaddr_t;
68
69
70extern bool net_init(void);
71extern netaddr_t *netaddr_new(struct server_sockaddr *);
72extern void netaddr_destroy(netaddr_t *);
73
74
75#endif