为什么这在 openBSD 上不起作用。 我收到了这个错误:
错误:在非结构或 union 中请求成员“ip_hl”
错误:在非结构或 union 中请求成员“ip_v”
等等……
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
int main(int argc, char *argv[]) {
int mahoosocket, mahoo;
char data[4096];
struct sockaddr_in sin;
struct tcphdr tcp;
struct ip *ip =(struct ip *)data;
if (argc != 2)
(....)
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = 0;
.(...)
ip.ip_hl=5;
ip.ip_v =4;
ip.ip_tos =0;
ip.ip_id = htonl (54321);
ip.ip_off=0;
ip.ip_ttl=255;
ip.ip_p=6;
ip.ip_sum=0;
ip.ip_src.s_addr= inet_addr ("127.0.0.1");
(.....)
最佳答案
ip
是一个指针:
struct ip *ip =(struct ip *)data;
当使用指向结构的指针访问成员时,您使用 ->
而不是 。
ip->ip_hl = 5;
ip->ip_v = 4;
ip->ip_src.s_addr = inet_addr ("127.0.0.1");
这是您应该熟悉的基本 C 语法。
关于c - IP header ,OpenBSD 上 C 中的原始套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395866/