3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2009-2012 Jardel Weyrich
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution
16 int ipfw_exec(int optname
, void * optval
, uintptr_t optlen
) {
23 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
);
25 syslog(LOG_ERR
, "socket(SOCK_RAW): %m");
36 result
= setsockopt(sock
, IPPROTO_IP
, optname
, optval
, optlen
);
38 syslog(LOG_ERR
, "setsockopt(): %m");
43 result
= getsockopt(sock
, IPPROTO_IP
, optname
, optval
, (socklen_t
*)optlen
);
45 syslog(LOG_ERR
, "getsockopt(): %m");
50 syslog(LOG_ERR
, "unhandled option");
57 void ipfw_free_ruleset(struct ip_fw
** rules
) {
58 if (rules
== NULL
|| *rules
== NULL
)
64 int ipfw_fetch_ruleset(struct ip_fw
** rules
, int * total_fetched
, int count
) {
68 if (rules
== NULL
|| *total_fetched
< 0 || count
< 1)
71 size
= sizeof(struct ip_fw
) * (*total_fetched
+ count
);
72 *rules
= (struct ip_fw
*)realloc(*rules
, size
);
74 syslog(LOG_ERR
, "realloc(): %m");
78 (*rules
)->version
= IP_FW_CURRENT_API_VERSION
;
79 if (ipfw_exec(IP_FW_GET
, *rules
, (uintptr_t)&size
) < 0)
81 fetched
= *total_fetched
;
82 *total_fetched
= size
/ sizeof(struct ip_fw
);
84 return *total_fetched
- fetched
;
87 int ipfw_validate_protocol(int value
) {
93 syslog(LOG_ERR
, "invalid protocol");
99 int ipfw_validate_ifname(const char * const value
) {
100 int len
= strlen(value
);
101 if (len
< 2 || len
> FW_IFNLEN
) {
102 syslog(LOG_ERR
, "invalid interface name");