*** empty log message ***
[mmondor.git] / tests / bktr_httpd / net.h
CommitLineData
23ab503c 1/* $Id: net.h,v 1.5 2008/01/23 01:33:15 mmondor Exp $ */
fa6ec601
MM
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
dacf6158 14#include <sys/types.h>
cad5aaab 15#include <sys/socket.h>
dacf6158
MM
16#include <netinet/in.h>
17
e3a154c0 18#include <mmhash.h>
dacf6158 19
fa6ec601
MM
20
21/* Address family independent system */
22
23struct server_sockaddr {
24 union {
25 struct sockaddr sockaddr;
26 struct sockaddr_in sockaddr_in;
27 struct sockaddr_in6 sockaddr_in6;
28 } u;
29};
30#define ss_family u.sockaddr.sa_family
31
32struct server_af_info {
33 size_t sock_length;
34 size_t addr_length;
35 size_t port_offset;
36 size_t addr_offset;
37};
38
39extern struct server_af_info *server_afi[1];
40
41/*
42 * Note that the following macros should only be used after calling
43 * net_init(), since they require the server_afi array to previously
44 * have been initialized.
45 */
46
47/* Returns pointer to family of address */
48#define SERVER_SOCKADDR_FAMILY(s) \
49 ((sa_family_t *)(&(s)->ss_family))
50/* Returns length of sockaddr-style address */
51#define SERVER_SOCKADDR_SOCKLEN(s) \
52 (server_afi[(int)(s)->ss_family]->sock_length)
53/* Returns length of internal address */
54#define SERVER_SOCKADDR_ADDRLEN(s) \
55 (server_afi[(int)(s)->ss_family]->addr_length)
56/* Returns pointer to port of address */
57#define SERVER_SOCKADDR_PORT(s) \
58 ((in_port_t *)(((char *)s) + \
59 server_afi[(int)(s)->ss_family]->port_offset))
60/* Returns pointer to family-specific address */
61#define SERVER_SOCKADDR_ADDRESS(s) \
62 ((void *)(((char *)s) + \
63 server_afi[(int)(s)->ss_family]->addr_offset))
64/* Returns struct sockaddr family-independent pointer */
65#define SERVER_SOCKADDR(s) \
66 (&((s)->u.sockaddr))
67
68
69typedef struct netaddr {
e3a154c0 70 hashnode_t node;
fa6ec601
MM
71 struct server_sockaddr saddr;
72 int count;
23ab503c 73 bool bktr; /* To limit bktr streams per addr */
e3a154c0 74 char address[64];
fa6ec601
MM
75} netaddr_t;
76
77
78extern bool net_init(void);
79extern netaddr_t *netaddr_new(struct server_sockaddr *);
80extern void netaddr_destroy(netaddr_t *);
cad5aaab 81extern bool net_listen(const char *, int, int);
fa6ec601
MM
82
83
84#endif