IP头,OpenBSD上的C中的原始套接字

时间:2013-10-16 05:28:39

标签: c sockets header ip openbsd

为什么这不适用于openBSD。 我收到了这个错误:

错误:在非结构或联合的

中请求成员'ip_hl'

错误:在非结构或联合的情况下请求成员'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");    


(.....)

1 个答案:

答案 0 :(得分:3)

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语法。