File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / miniupnpd / ipfw / ipfwaux.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 00:32:35 2013 UTC (10 years, 11 months ago) by misho
Branches: elwix, MAIN
CVS tags: v1_8p0, v1_8, HEAD
1.8

    1: /*
    2:  * MiniUPnP project
    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
    7:  */
    8: 
    9: #include "ipfwaux.h"
   10: #include <stdio.h>
   11: #include <stdlib.h>
   12: #include <string.h>
   13: #include <syslog.h>
   14: #include <unistd.h>
   15: 
   16: int ipfw_exec(int optname, void * optval, uintptr_t optlen) {
   17: 	static int sock = -1;
   18: 	int result;
   19: 
   20: 	switch (optname) {
   21: 		case IP_FW_INIT:
   22: 			if (sock == -1)
   23: 				sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
   24: 			if (sock < 0) {
   25: 				syslog(LOG_ERR, "socket(SOCK_RAW): %m");
   26: 				return -1;
   27: 			}
   28: 			break;
   29: 		case IP_FW_TERM:
   30: 			if (sock != -1)
   31: 				close(sock);
   32: 			sock = -1;
   33: 			break;
   34: 		case IP_FW_ADD:
   35: 		case IP_FW_DEL:
   36: 			result = setsockopt(sock, IPPROTO_IP, optname, optval, optlen);
   37: 			if (result == -1) {
   38: 				syslog(LOG_ERR, "setsockopt(): %m");
   39: 				return -1;
   40: 			}
   41: 			break;
   42: 		case IP_FW_GET:
   43: 			result = getsockopt(sock, IPPROTO_IP, optname, optval, (socklen_t *)optlen);
   44: 			if (result == -1) {
   45: 				syslog(LOG_ERR, "getsockopt(): %m");
   46: 				return -1;
   47: 			}
   48: 			break;
   49: 		default:
   50: 			syslog(LOG_ERR, "unhandled option");
   51: 			return -1;
   52: 	}
   53: 
   54: 	return 0;
   55: }
   56: 
   57: void ipfw_free_ruleset(struct ip_fw ** rules) {
   58: 	if (rules == NULL || *rules == NULL)
   59: 		return;
   60: 	free(*rules);
   61: 	*rules = NULL;
   62: }
   63: 
   64: int ipfw_fetch_ruleset(struct ip_fw ** rules, int * total_fetched, int count) {
   65: 	int fetched;
   66: 	socklen_t size;
   67: 
   68: 	if (rules == NULL || *total_fetched < 0 || count < 1)
   69: 		return -1;
   70: 
   71: 	size = sizeof(struct ip_fw) * (*total_fetched + count);
   72: 	*rules = (struct ip_fw *)realloc(*rules, size);
   73: 	if (*rules == NULL) {
   74: 		syslog(LOG_ERR, "realloc(): %m");
   75: 		return -1;
   76: 	}
   77: 
   78: 	(*rules)->version = IP_FW_CURRENT_API_VERSION;
   79: 	if (ipfw_exec(IP_FW_GET, *rules, (uintptr_t)&size) < 0)
   80: 		return -1;
   81: 	fetched = *total_fetched;
   82: 	*total_fetched = size / sizeof(struct ip_fw);
   83: 
   84: 	return *total_fetched - fetched;
   85: }
   86: 
   87: int ipfw_validate_protocol(int value) {
   88: 	switch (value) {
   89: 		case IPPROTO_TCP:
   90: 		case IPPROTO_UDP:
   91: 			break;
   92: 		default:
   93: 			syslog(LOG_ERR, "invalid protocol");
   94: 			return -1;
   95: 	}
   96: 	return 0;
   97: }
   98: 
   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");
  103: 		return -1;
  104: 	}
  105: 	return 0;
  106: }
  107: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>