环回接口上的原始套接字可能吗?

发布于 2024-09-11 14:10:46 字数 685 浏览 11 评论 0原文

我们正在尝试通过原始套接字与侦听 Linux 环回接口的服务器进行通信,但服务器似乎没有从我们那里收到任何数据包。我们发送的数据包在 Wireshark 中可见。

环回上的原始套接字是否可能? (请不要问我们为什么需要它:这里解释起来太复杂了)

编辑:这就是我们打开它的方式

_I_RawSocket = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))

memset( &ifr, 0, sizeof( ifr ) );
strcpy( ifr.ifr_ifrn.ifrn_name, _InterfaceName);

ioctl( _I_RawSocket, SIOCGIFINDEX, &ifr )

memset( &sll, 0, sizeof( sll ) );
sll.sll_family   = AF_PACKET;
sll.sll_ifindex  = ifr.ifr_ifindex;
sll.sll_protocol = htons( ETH_P_ALL );

bind( _I_RawSocket, (struct sockaddr *) &sll, sizeof( sll ))

服务器是lighttpd,可以通过本地主机上的普通套接字访问。 netstat --raw 打印空表,但我绝对确定我们在普通 eth 设备上有两个功能性原始套接字。

We are trying to communicate with the server listening on Linux loopback interface via raw socket and it seems like the server does not get a single packet from us. The packets we send are visible in Wireshark.

Is raw socket on loopback possible at all? (Please, don't ask why we need it: it's too complicated to explain here)

EDIT: this is how we open it

_I_RawSocket = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))

memset( &ifr, 0, sizeof( ifr ) );
strcpy( ifr.ifr_ifrn.ifrn_name, _InterfaceName);

ioctl( _I_RawSocket, SIOCGIFINDEX, &ifr )

memset( &sll, 0, sizeof( sll ) );
sll.sll_family   = AF_PACKET;
sll.sll_ifindex  = ifr.ifr_ifindex;
sll.sll_protocol = htons( ETH_P_ALL );

bind( _I_RawSocket, (struct sockaddr *) &sll, sizeof( sll ))

The server is lighttpd and it's reachable via normal socket on localhost.
netstat --raw prints empty table but I'm absolutely sure we have two functional raw sockets on normal eth devices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2024-09-18 14:10:47

请确保绑定到 if_index

if (ioctl(sock, SIOCGIFINDEX, &stEthReq ) < 0 )
{
    printf( "failed to get IF index!" );
    return -1;
}
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sll_family   = AF_PACKET;
client_addr.sll_ifindex  = stEthReq.ifr_ifru.ifru_ivalue;
client_addr.sll_protocol = VOS_HTONS(usEthType);
ret = bind(sock,(struct sockaddr *)(&client_addr), sizeof(client_addr));

Please make sure bind to if_index

if (ioctl(sock, SIOCGIFINDEX, &stEthReq ) < 0 )
{
    printf( "failed to get IF index!" );
    return -1;
}
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sll_family   = AF_PACKET;
client_addr.sll_ifindex  = stEthReq.ifr_ifru.ifru_ivalue;
client_addr.sll_protocol = VOS_HTONS(usEthType);
ret = bind(sock,(struct sockaddr *)(&client_addr), sizeof(client_addr));
好多鱼好多余 2024-09-18 14:10:46

原始套接字在使用 bind() 和 connect() 时表现得特别混乱,但我无法确认您的问题在于它们。我建议您采用更直接的方法:

发送方

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define DEST "127.0.0.1"

int main(int argc, char **argv)
{

 int s;
 struct sockaddr_in dst_addr;
 char packet[50];

 struct iphdr *ip = (struct iphdr *)packet;  

 if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  perror("error:");
  exit(EXIT_FAILURE);
 }

 dst_addr.sin_family = AF_INET;
 dst_addr.sin_port = 0; /* not needed in SOCK_RAW */
 inet_pton(AF_INET, DEST, (struct in_addr *)&dst_addr.sin_addr.s_addr);
 memset(dst_addr.sin_zero, 0, sizeof(dst_addr.sin_zero));

 memset(packet, 'A', sizeof(packet));   /* payload will be all As */

 ip->ihl = 5;
 ip->version = 4;
 ip->tos = 0;
 ip->tot_len = htons(40);
 ip->frag_off = 0;  /* NF */
 ip->ttl = 64;
 ip->protocol = IPPROTO_RAW; /* this has to be IPPROTO_RAW */
 ip->check = 0;
 ip->saddr = dst_addr.sin_addr.s_addr;
 ip->daddr = dst_addr.sin_addr.s_addr;

 while(42) {
  sleep(5);
  if (sendto(s, packet, sizeof(packet), 0, 
   (struct sockaddr *)&dst_addr, (socklen_t)sizeof(dst_addr)) < 0)
   perror("uh oh:");
 }
 return(0);
}

接收方

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char **argv)
{
 int s;
 struct sockaddr_in src_addr;
 char packet[50];

 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  perror("error:");
  exit(EXIT_FAILURE);
 }

 memset(packet, 0, sizeof(packet));
 socklen_t *len = (socklen_t *)sizeof(src_addr);
 int fromlen = sizeof(src_addr);

 while(42) {
  if (recvfrom(s, &packet, sizeof(packet), 0,
   (struct sockaddr *)&src_addr, &fromlen) < 0)
   perror("uh oh:");

  int i = sizeof(struct iphdr); /* print the payload */
  for(; i < sizeof(packet); i++) {
   printf("%c", packet[i]);
  }
  printf("\n");
 }
 return(0);
}

我希望它们的行为完全符合您的要求。请阅读 man 7 raw 了解其工作原理的详细信息,更重要的是如果您想扩展它,请阅读 man 7 packet。另外,请注意 IPPROTO_RAW 意味着 IP_HDRINCL 套接字选项,这就是我们自己构建 ip 标头的原因 - 尽管 IP 校验和和总长度仍然由内核计算和填充。

编辑:此外,如果您想要一个原始套接字来将有效数据发送到 lighttpd 等应用程序,则必须将 protocol 参数与 socket() 相匹配以及为 IP 标头字段提供有效值。正确的以太网标头不是强制性的 - 唯一重要的字段将由内核堆栈为您填充。

Raw sockets behave particularly fizzy with bind() and connect(), but I can't confirm that your issue lies with them. I suggest you follow a more straightforward approach:

Sender

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define DEST "127.0.0.1"

int main(int argc, char **argv)
{

 int s;
 struct sockaddr_in dst_addr;
 char packet[50];

 struct iphdr *ip = (struct iphdr *)packet;  

 if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  perror("error:");
  exit(EXIT_FAILURE);
 }

 dst_addr.sin_family = AF_INET;
 dst_addr.sin_port = 0; /* not needed in SOCK_RAW */
 inet_pton(AF_INET, DEST, (struct in_addr *)&dst_addr.sin_addr.s_addr);
 memset(dst_addr.sin_zero, 0, sizeof(dst_addr.sin_zero));

 memset(packet, 'A', sizeof(packet));   /* payload will be all As */

 ip->ihl = 5;
 ip->version = 4;
 ip->tos = 0;
 ip->tot_len = htons(40);
 ip->frag_off = 0;  /* NF */
 ip->ttl = 64;
 ip->protocol = IPPROTO_RAW; /* this has to be IPPROTO_RAW */
 ip->check = 0;
 ip->saddr = dst_addr.sin_addr.s_addr;
 ip->daddr = dst_addr.sin_addr.s_addr;

 while(42) {
  sleep(5);
  if (sendto(s, packet, sizeof(packet), 0, 
   (struct sockaddr *)&dst_addr, (socklen_t)sizeof(dst_addr)) < 0)
   perror("uh oh:");
 }
 return(0);
}

Receiver

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char **argv)
{
 int s;
 struct sockaddr_in src_addr;
 char packet[50];

 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  perror("error:");
  exit(EXIT_FAILURE);
 }

 memset(packet, 0, sizeof(packet));
 socklen_t *len = (socklen_t *)sizeof(src_addr);
 int fromlen = sizeof(src_addr);

 while(42) {
  if (recvfrom(s, &packet, sizeof(packet), 0,
   (struct sockaddr *)&src_addr, &fromlen) < 0)
   perror("uh oh:");

  int i = sizeof(struct iphdr); /* print the payload */
  for(; i < sizeof(packet); i++) {
   printf("%c", packet[i]);
  }
  printf("\n");
 }
 return(0);
}

I hope these behave exactly like you want them to. Read man 7 raw for the gory details of why this works and more importantly man 7 packet if you want to extend it. Also, take note that IPPROTO_RAW implies the IP_HDRINCL socket option, which is why we're constructing the ip header ourselves - although the IP checksum and total length are computed and filled in by the kernel, still.

edit: In addition, if you wanted a raw socket with which to send valid data to an application like lighttpd, you'd have to match the protocol argument to socket() as well as provide valid values for the IP header fields. A proper ethernet header is not mandatory - the only important field will be filled for you by the kernel stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文