5 #include <sys/socket.h>
9 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
11 #define DEBUGP(x, args...)
15 parse_bindings(const char *optarg, struct ipt_set_info *info)
17 char *saved = strdup(optarg);
18 char *ptr, *tmp = saved;
21 while (i < (IP_SET_MAX_BINDINGS - 1) && tmp != NULL) {
22 ptr = strsep(&tmp, ",");
23 if (strncmp(ptr, "src", 3) == 0)
24 info->flags[i++] |= IPSET_SRC;
25 else if (strncmp(ptr, "dst", 3) == 0)
26 info->flags[i++] |= IPSET_DST;
28 exit_error(PARAMETER_PROBLEM,
29 "You must spefify (the comma separated list of) 'src' or 'dst'.");
33 exit_error(PARAMETER_PROBLEM,
34 "Can't follow bindings deeper than %i.",
35 IP_SET_MAX_BINDINGS - 1);
40 static int get_set_getsockopt(void *data, size_t * size)
43 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
45 exit_error(OTHER_PROBLEM,
46 "Can't open socket to ipset.\n");
48 return getsockopt(sockfd, SOL_IP, SO_IP_SET, data, size);
51 static void get_set_byname(const char *setname, struct ipt_set_info *info)
53 struct ip_set_req_get_set req;
54 int size = sizeof(struct ip_set_req_get_set);
57 req.op = IP_SET_OP_GET_BYNAME;
58 req.version = IP_SET_PROTOCOL_VERSION;
59 strncpy(req.set.name, setname, IP_SET_MAXNAMELEN);
60 req.set.name[IP_SET_MAXNAMELEN - 1] = '\0';
61 res = get_set_getsockopt(&req, &size);
63 exit_error(OTHER_PROBLEM,
64 "Problem when communicating with ipset, errno=%d.\n",
66 if (size != sizeof(struct ip_set_req_get_set))
67 exit_error(OTHER_PROBLEM,
68 "Incorrect return size from kernel during ipset lookup, "
69 "(want %d, got %d)\n",
70 sizeof(struct ip_set_req_get_set), size);
71 if (req.set.index == IP_SET_INVALID_ID)
72 exit_error(PARAMETER_PROBLEM,
73 "Set %s doesn't exist.\n", setname);
75 info->index = req.set.index;
78 static void get_set_byid(char * setname, ip_set_id_t index)
80 struct ip_set_req_get_set req;
81 int size = sizeof(struct ip_set_req_get_set);
84 req.op = IP_SET_OP_GET_BYINDEX;
85 req.version = IP_SET_PROTOCOL_VERSION;
86 req.set.index = index;
87 res = get_set_getsockopt(&req, &size);
89 exit_error(OTHER_PROBLEM,
90 "Problem when communicating with ipset, errno=%d.\n",
92 if (size != sizeof(struct ip_set_req_get_set))
93 exit_error(OTHER_PROBLEM,
94 "Incorrect return size from kernel during ipset lookup, "
95 "(want %d, got %d)\n",
96 sizeof(struct ip_set_req_get_set), size);
97 if (req.set.name[0] == '\0')
98 exit_error(PARAMETER_PROBLEM,
99 "Set id %i in kernel doesn't exist.\n", index);
101 strncpy(setname, req.set.name, IP_SET_MAXNAMELEN);
104 #endif /*_LIBIPT_SET_H*/