Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"‘sockaddr_in’ undeclared (first use in this function)" error despite including the requisite headers

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>#include <net/if.h>
#include <stdarg.h>

#define BACKLOG 10

void * get_in_addr(struct sockaddr *sa){
    if(sa->sa_family == AF_INET){
            return &((sockaddr_in *)sa)->sin_addr;
    }
    else if(sa->sa_family == AF_INET6){
            return &((sockaddr_in6 *)sa)->sin6_addr;
    }
}

I am using the sockaddr_in struct in my code to chekc whether an incoming connection is an IPv4 or an IPV6 address. I get the error "‘sockaddr_in’ undeclared (first use in this function)" here despite including the netinet/in.h header in my code. Is there something that I am not seeing here?

like image 802
user1295872 Avatar asked May 17 '13 10:05

user1295872


1 Answers

sockaddr_in is not typedef, so try using it with struct like following

(struct sockaddr_in *)
like image 115
Dayal rai Avatar answered Nov 10 '22 01:11

Dayal rai