我找到了
SO_MARK
插座(7)手册页中的插座选项:
SO_MARK (since Linux 2.6.25)
Set the mark for each packet sent through this socket (similar
to the netfilter MARK target but socket-based). Changing the
mark can be used for mark-based routing without netfilter or
for packet filtering. Setting this option requires the
CAP_NET_ADMIN capability.
正如我最初问的那样,不是每包都适合我的目的。你可以用
setsockopt()
int fwmark;
//fwmark = <some value>;
if (setsockopt(sockfd, SOL_SOCKET, SO_MARK, &fwmark, sizeof fwmark) == -1)
perror("failed setting mark for socket packets");