LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error: dereferencing pointer to incomplete type in C (https://www.linuxquestions.org/questions/programming-9/error-dereferencing-pointer-to-incomplete-type-in-c-941772/)

Himalay 04-26-2012 12:22 AM

error: dereferencing pointer to incomplete type in C
 
I am getting following compilation errors in gcc. Anyone please help me in finding what is wrong?
$ gcc -c recv_v4.c
recv_v4.c: In function `recv_v4':
recv_v4.c:42: error: dereferencing pointer to incomplete type
recv_v4.c:42: error: `ICMP_TIMXCEED' undeclared (first use in this function)
recv_v4.c:42: error: (Each undeclared identifier is reported only once
recv_v4.c:42: error: for each function it appears in.)
recv_v4.c:42: error: dereferencing pointer to incomplete type
recv_v4.c:42: error: `ICMP_TIMXCEED_INTRANS' undeclared (first use in this function)
recv_v4.c:59: error: dereferencing pointer to incomplete type
recv_v4.c:59: error: `ICMP_UNREACH' undeclared (first use in this function)
recv_v4.c:72: error: dereferencing pointer to incomplete type
recv_v4.c:72: error: `ICMP_UNREACH_PORT' undeclared (first use in this function)
recv_v4.c:75: error: dereferencing pointer to incomplete type
recv_v4.c:82: error: dereferencing pointer to incomplete type
recv_v4.c:82: error: dereferencing pointer to incomplete type

Following is the code:

Code:

#include        "trace.h"
recv_v4.c
extern int gotalarm;

/*
 * Return: -3 on timeout
 *                  -2 on ICMP time exceeded in transit (caller keeps going)
 *                  -1 on ICMP port unreachable (caller is done)
 *                >= 0 return value is some other ICMP unreachable code
 */

int recv_v4(int seq, struct timeval *tv)
{
        int hlen1, hlen2, icmplen, ret;
        socklen_t len;
        ssize_t        n;
        struct ip *ip, *hip;
        struct icmp *icmp;
        struct udphdr *udp;

        gotalarm = 0;
        alarm(3);
        for ( ; ; ) {
                if (gotalarm)
                        return(-3);                /* alarm expired */
                len = pr->salen;
                n = recvfrom(recvfd, recvbuf, sizeof(recvbuf), 0, pr->sarecv, &len);
                if (n < 0) {
                        if (errno == EINTR)
                                continue;
                        else
                                err_sys("recvfrom error");
                }

                ip = (struct ip *) recvbuf;        /* start of IP header */
                hlen1 = ip->ip_hl << 2;                /* length of IP header */
       
                icmp = (struct icmp *) (recvbuf + hlen1); /* start of ICMP header */
                if ( (icmplen = n - hlen1) < 8)
                        continue;                                /* not enough to look at ICMP header */
       
                if (icmp->icmp_type == ICMP_TIMXCEED && icmp->icmp_code == ICMP_TIMXCEED_INTRANS) {
                        if (icmplen < 8 + sizeof(struct ip))
                                continue;                        /* not enough data to look at inner IP */

                        hip = (struct ip *) (recvbuf + hlen1 + 8);
                        hlen2 = hip->ip_hl << 2;
                        if (icmplen < 8 + hlen2 + 4)
                                continue;                        /* not enough data to look at UDP ports */

                        udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);
                        if (hip->ip_p == IPPROTO_UDP &&
                                udp->uh_sport == htons(sport) &&
                                udp->uh_dport == htons(dport + seq)) {
                                ret = -2;                /* we hit an intermediate router */
                                break;
                        }

                } else if (icmp->icmp_type == ICMP_UNREACH) {
                        if (icmplen < 8 + sizeof(struct ip))
                                continue;                        /* not enough data to look at inner IP */

                        hip = (struct ip *) (recvbuf + hlen1 + 8);
                        hlen2 = hip->ip_hl << 2;
                        if (icmplen < 8 + hlen2 + 4)
                                continue;                        /* not enough data to look at UDP ports */

                        udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);
                        if (hip->ip_p == IPPROTO_UDP &&
                                udp->uh_sport == htons(sport) &&
                                udp->uh_dport == htons(dport + seq)) {
                                if (icmp->icmp_code == ICMP_UNREACH_PORT)
                                        ret = -1;        /* have reached destination */
                                else
                                        ret = icmp->icmp_code;        /* 0, 1, 2, ... */
                                break;
                        }
                }
                if (verbose) {
                        printf(" (from %s: type = %d, code = %d)\n",
                                        Sock_ntop_host(pr->sarecv, pr->salen),
                                        icmp->icmp_type, icmp->icmp_code);
                }
                /* Some other ICMP error, recvfrom() again */
        }
        alarm(0);                                        /* don't leave alarm running */
        gettimeofday(tv, NULL);                /* get time of packet arrival */
        return(ret);
}

trace.h
Code:

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <unistd.h>
#include <stdio.h>


#ifdef        HAVE_SOCKADDR_DL_STRUCT
#include        <net/if_dl.h>
#endif


#define        BUFSIZE                1500

struct rec {                                        /* format of outgoing UDP data */
  u_short        rec_seq;                        /* sequence number */
  u_short        rec_ttl;                        /* TTL packet left with */
  struct timeval        rec_tv;                /* time packet left */
};

                        /* globals */
char        recvbuf[BUFSIZE];
char        sendbuf[BUFSIZE];

int                datalen;                        /* # bytes of data following ICMP header */
char        *host;
u_short        sport, dport;
int                nsent;                                /* add 1 for each sendto() */
pid_t        pid;                                /* our PID */
int                probe, nprobes;
int                sendfd, recvfd;        /* send on UDP sock, read on raw ICMP sock */
int                ttl, max_ttl;
int                verbose;

                        /* function prototypes */
const char        *icmpcode_v4(int);
const char        *icmpcode_v6(int);
int                recv_v4(int, struct timeval *);
int                recv_v6(int, struct timeval *);
void        sig_alrm(int);
void        traceloop(void);
void        tv_sub(struct timeval *, struct timeval *);

struct proto {
  const char        *(*icmpcode)(int);
  int        (*recv)(int, struct timeval *);
  struct sockaddr  *sasend;        /* sockaddr{} for send, from getaddrinfo */
  struct sockaddr  *sarecv;        /* sockaddr{} for receiving */
  struct sockaddr  *salast;        /* last sockaddr{} for receiving */
  struct sockaddr  *sabind;        /* sockaddr{} for binding source port */
  socklen_t                  salen;        /* length of sockaddr{}s */
  int                        icmpproto;        /* IPPROTO_xxx value for ICMP */
  int          ttllevel;                /* setsockopt() level to set TTL */
  int          ttloptname;                /* setsockopt() name to set TTL */
} *pr;

#ifdef        IPV6

#include        <netinet/ip6.h>
#include        <netinet/icmp6.h>

#endif


pan64 04-26-2012 01:41 AM

I have tried and worked for me. So I suggest you to type gcc -E rec_v4.c > rec_v4.i and look at that rec_v4.i file, maybe it will give you a tip where is it lost. I do not know what os you have, maybe a define solves it.

Himalay 04-26-2012 04:40 AM

I am using CYGWIN and Windows XP Professional OS.

ta0kira 04-26-2012 10:07 PM

In my <netinet/ip_icmp.h> you need to have __USE_BSD defined to get struct icmp and the macros you're using.
Kevin Barry

Himalay 04-27-2012 01:17 AM

I defined __USE_BSD in ip_icmp.h. But still having same errors.

pan64 04-27-2012 01:22 AM

Oh, no! Do not touch that file. gcc -D__USE_BSD -c recv_v4.c would be the way, but actually I cannot check if it solves your problem.

Himalay 04-27-2012 05:53 AM

thanks pan64.

ta0kira 04-27-2012 11:14 AM

Quote:

Originally Posted by pan64 (Post 4664190)
Oh, no! Do not touch that file. gcc -D__USE_BSD -c recv_v4.c would be the way, but actually I cannot check if it solves your problem.

Yes, I should have been more explicit.
Kevin Barry

joyce092130 07-07-2012 04:27 AM

error: dereferencing pointer to incomplete type
 
i am working on my client/server chat program and i get "error: dereferencing pointer to incomplete type" when i compile my client.c.
Here is my code:

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

int main(int argc,char *argv[])
{
struct sockaddr_in clientaddr;
pid_t pid;
int clientfd,sendbytes;
struct hostent *host;
char *buf,*buf_r;

if(argc < 4)
{
printf("usage:\n");
printf("%s host port name\n",argv[0]);
exit(1);
}

host = gethostbyname(argv[1]);
if((clientfd = socket (AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket\n");
exit(1);
}
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons((uint16_t)atoi(argv[2]));
clientaddr.sin_addr = *((struct in_addrr *) host->h_addr);
bzero(&(clientaddr.sin_zero),0);
if(connect(clientfd,(struct sockaddr *)&clientaddr,sizeof(struct sockaddr)) == -1)
{
perror("connect\n");
exit(1);
}

buf=(char *)malloc(120);
memset(buf,0,120);

buf_r=(char *)malloc(100);

if( recv(clientfd,buf,100,0) == -1)
{
perror("recv: ");
exit(1);
}
printf("\n%s\n",buf);

pid = fork();

while(1)
{
if(pid > 0)
{
//get_cur_time(time_str);

strcpy(buf,argv[3]);
strcat(buf,":");
memset(buf_r,0,100);
//gets(buf_r);
fgets(buf_r,100,stdin);
strncat(buf,buf_r,strlen(buf_r)-1);
//strcat(buf,time_str);
//printf("---%s\n",buf);
if((sendbytes = send(clientfd,buf,strlen(buf),0)) == -1)
{
perror("send\n");
exit(1);
}
}
else if(pid == 0)
{
memset(buf,0,1000);
if(recv(clientfd,buf,100,0) <=0)
{
perror("recv:");
close(clientfd);
raise(SIGSTOP);
exit(1);
}
printf("%s\n",buf);
}
else
perror("fork");
}
close(clientfd);
return 0;
}

Please help me find solution for this. Thank you.

pan64 07-07-2012 08:35 AM

please open a new thread for your problem and also please use [code]here comes your code[/code] to keep formatting, and post the full error message.
Actually the real error message was:
Quote:

gcc -o prog -Wall prog.c
prog.c: In function 'main':
prog.c:36: error: dereferencing pointer to incomplete type
you made a typo in line 36:
clientaddr.sin_addr = *((struct in_addrr *) host->h_addr);
so, just remove that r


All times are GMT -5. The time now is 01:20 AM.