c - IP header ,OpenBSD 上 C 中的原始套接字

标签 c sockets header ip openbsd

为什么这在 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/

相关文章:

c - 从文件的最后一行读取到 C 中的第一行

java - 在java中使用AES/CBC/PKCS5Padding加密后的文件大小是多少

php - 如何在 php 脚本完成之前导致重定向发生?

html - 在页脚上居中 Ul

c - 在 Arduino 中使用带有 LED 的循环的二进制计数器

c - 在 C 中使用 scanf 在 EOF 处退出 while 循环

c - 带描述符的 free() 函数

http - HTTP header 设置问题

c - 我的密码会编译但不会打印——我犯了什么错误?

从bind()收到的C++ errno 22