- 论坛徽章:
- 7
|
回复 178# Godbach
依葫芦画瓢跟iptables学使用TC_INIT函数去读内核内iptables信息,但只读出了表的大致信息(即SO_GET_INFO标志位返回的信息),后面继续读表的规则内容(即SO_GET_ENTRIES标志位返回)时报getsockopt的参数错误,一直找不到什么原因,郁闷!!!!
struct xtc_handle *
TC_INIT(const char *tablename)
{
struct xtc_handle *h;
STRUCT_GETINFO info;
unsigned int tmp;
socklen_t s;
int sockfd;
retry:
iptc_fn = TC_INIT;
if (strlen(tablename) >= TABLE_MAXNAMELEN) {
errno = EINVAL;
return NULL;
}
sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
if (sockfd < 0)
return NULL;
if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
fprintf(stderr, "Could not set close on exec: %s\n",
strerror(errno));
abort();
}
s = sizeof(info);
strcpy(info.name, tablename);
if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) { /*此处成功取到表的信息*/
close(sockfd);
return NULL;
}
DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
info.valid_hooks, info.num_entries, info.size);
if ((h = alloc_handle(info.name, info.size, info.num_entries))
== NULL) {
close(sockfd);
return NULL;
}
/* Initialize current state */
h->sockfd = sockfd;
h->info = info;
h->entries->size = h->info.size;
tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries, /*此处取规则信息报参数错误*/
&tmp) < 0)
goto error;
#ifdef IPTC_DEBUG2
{
int fd = open("/tmp/libiptc-so_get_entries.blob",
O_CREAT|O_WRONLY);
if (fd >= 0) {
write(fd, h->entries, tmp);
close(fd);
}
}
#endif
if (parse_table(h) < 0)
goto error;
CHECK(h);
return h;
error:
TC_FREE(h);
/* A different process changed the ruleset size, retry */
if (errno == EAGAIN)
goto retry;
return NULL;
} |
|