C++ 原始套接字SYN与数据包套接字SYN

C++ 原始套接字SYN与数据包套接字SYN,c++,linux,tcp,C++,Linux,Tcp,我制作了一个引发问题的示例: #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <stdlib.h> #include <errno.h> #include <netinet/tcp.h> #include <netinet/ip.h> #include <sys/socket.h> #include <

我制作了一个引发问题的示例:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <time.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/if_packet.h>

#include <iostream>

enum {
    IP_ERR
};

int main(int argc, char **argv) {
        int fd;
        sockaddr_ll device;
        // tcp and ip syn data
        unsigned char ipTcp[] = {0x45, 0x00, 0x00, 0x28, 0x23, 0xc6, 0x00, 0x00, 0x40, 0x06, 0x59, 0x08, 0x7f, 0x00, 0x00, 0x01,
                        0x7f, 0x00, 0x00, 0x01, 0x0f, 0xa1, 0x0b, 0xb8, 0x6b, 0x8b, 0x45, 0x67, 0x00, 0x00, 0x00, 0x00,
                        0x50, 0x02, 0xaa, 0xaa, 0x3a, 0xea, 0x00, 0x00};

    // if any argument, send syn frame using packet socket
    // otherwise, send using raw socket
        bool asPacket = argc > 1;

        if (asPacket) {
            // send as packet
            fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
            if (fd == -1) {
                throw IP_ERR;
            }

            memset(&device, 0, sizeof(device));
            if (!(device.sll_ifindex = if_nametoindex("lo"))) {
                throw IP_ERR;
            }
            device.sll_family = AF_PACKET;
            device.sll_protocol = htons(ETH_P_IP);

            std::cout << sendto(fd, ipTcp, 40, 0, (struct sockaddr *) &device, sizeof(device)) << std::endl;

        } else {

            // send as raw socket
            fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
            if (fd == -1) {
                throw IP_ERR;
            }

            sockaddr_in sin;
            memset(&sin, 0, sizeof(sin));
            sin.sin_family = AF_INET;
            sin.sin_port = htons(3000);
            sin.sin_addr.s_addr = htonl(0x7f000001);
            std::cout << sendto(fd, ipTcp, 40, 0, (struct sockaddr *) &sin, sizeof(sin)) << std::endl;

        }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
枚举{
IP_ERR
};
int main(int argc,字符**argv){
int-fd;
sockaddr\u ll装置;
//tcp和ip同步数据
无符号字符ipTcp[]={0x45、0x00、0x00、0x28、0x23、0xc6、0x00、0x00、0x40、0x06、0x59、0x08、0x7f、0x00、0x00、0x01,
0x7f、0x00、0x00、0x01、0x0f、0xa1、0x0b、0xb8、0x6b、0x8b、0x45、0x67、0x00、0x00、0x00、0x00、0x00、,
0x50、0x02、0xaa、0xaa、0x3a、0xea、0x00、0x00};
//如果有任何参数,请使用数据包套接字发送syn帧
//否则,使用原始套接字发送
bool-asPacket=argc>1;
如果(asPacket){
//作为数据包发送
fd=套接字(AF_数据包、SOCK_DGRAM、HTON(ETH_P_IP));
如果(fd==-1){
犯错误;
}
memset(&device,0,sizeof(device));
if(!(device.sll\u ifindex=if\u nametoindex(“lo”)){
犯错误;
}
device.sll_family=AF_数据包;
device.sll_协议=htons(ETH_P_IP);

std::cout可能这是对环回接口的优化。如果您非常确定自己的代码是正确的,那么unix.stackexchange.com可能是一个更好的提问场所。如果您发送到远程计算机而不是本地计算机,会发生什么情况?我在这里发布的只是一个简单的示例,我的完整代码可以远程连接并下载使用数据包套接字的Google.com首页,我只对localhost有这个问题(我想使用数据包套接字,因为它们是低级的,但我也想在localhost上开发)。问题是当端口上没有任何内容时。当您尝试连接到Google上未使用的端口时,是否获得了正确的RST?如果是,那么这听起来像是Linux为环回提供的某种快捷方式。整个RST只是一个症状(更复杂的是,谷歌不发送RST,但即使我通过netcat连接,他们也不会发送RST,这是出于设计)。不要管RST,问题更像是“数据包套接字不会触发本地主机上的任何操作,但它们可以远程工作”。