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