簡體   English   中英

嘗試寫入struct ip時發生錯誤“將指針解引用為不完整類型”

[英]Error “dereferencing pointer to incomplete type” when trying to write to a struct ip

我試圖使用原始套接字在C for Linux中編寫一個簡短的自己的traceroute程序,但是在編譯時出現錯誤消息“將指針解引用為不完整類型” struct ip

這些是我包含的標題:

#include <netinet/ip.h>         
#include <netinet/ip_icmp.h>        
#include <sys/socket.h>         
#include <stdlib.h>             
#include <stdio.h>              
#include <sys/types.h>
#include <errno.h>
#include <netdb.h> 
#include <arpa/inet.h>
#include <ifaddrs.h>

然后我如何使用IP標頭

struct ip *myIpHeader = (struct ip*)buffr;

然后是一堆東西...然后:

myIpHeader->ip_v = 4;                           
myIpHeader->ip_hl = 5;                      
myIpHeader->ip_tos = 0;                                             
myIpHeader->ip_len = 20+8;                  
myIpHeader->ip_off = 0;                     
myIpHeader->ip_p = IPPROTO_ICMP;                
inet_pton(AF_INET, argv[1], &(myIpHeader->ip_dst)); 
inet_pton(AF_INET, ownip->h_name, &(myIpHeader->ip_src));
myIpHeader->ip_sum = 0;                      
myIpHeader->ip_id = htonl(12345);                    
myIpHeader->ip_ttl = ttl;   

然后我用它來發送:

sendto(mysock, buffr, sizeof myIpHeader + sizeof myicmphead, 0, (struct sockaddr*)&cliAddr, sizeof cliAddr);

將評論轉換為答案。

在Mac上,標頭<netinet/ip.h>確實包含一個struct ip —但這不是POSIX標准化的標頭。 它也在Linux(Ubuntu 16.04)中存在。 因此,似乎在進行解引用時,沒有包含標頭,或者標頭的內容“不可見”。

您正在使用-std=gnu11-std=c11編譯嗎? 如果是后者,則可能需要啟用POSIX(或GNU)定義。 通過使用-std=gnu11最輕松地解決-std=gnu11 或者,在命令行上使用-D_GNU_SOURCE-D_XOPEN_SOURCE=700或類似的命令,或在源代碼中使用等效的#define

posixver.h好壞,我使用自制的頭文件posixver.h

/*
@(#)File:           $RCSfile: posixver.h,v $
@(#)Version:        $Revision: 1.4 $
@(#)Last changed:   $Date: 2017/06/18 00:15:42 $
@(#)Purpose:        Request appropriate POSIX and X/Open Support
@(#)Author:         J Leffler
@(#)Copyright:      (C) JLSS 2010-2017
*/

/*TABSTOP=4*/

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2008 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if defined(__cplusplus)
#define _XOPEN_SOURCE 700   /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#elif __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 700   /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

您可以根據需要使用該屬性,無論該屬性有無歸因。 您可以在https://github.com/jleffler/soq/tree/master/src/libsoq中找到該文件的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM