# ioctl returns 22 - invalid argument when compiled with clang compiler for FreeBSD



## arishbaig (Oct 25, 2018)

Problem : I have an issue with a sample program to get the ip address which is working fine when I am compiling it with FreeBSD built in gcc. But when I am using cross compiler clang version 3.7.1 , I am getting invalid argument error in ioctl call. 
Here is the sample program --

```
#include <stdio.h>
#include <unistd.h>
#include <string.h> /* for strncpy */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <errno.h>

int
main()
{
 int fd;
 struct ifreq ifr;

 fd = socket(AF_INET, SOCK_DGRAM, 0);

 /* I want to get an IPv4 IP address */
 ifr.ifr_addr.sa_family = AF_INET;

 /* I want IP address attached to "eth0"/em0 */
 strncpy(ifr.ifr_name, "em0", IFNAMSIZ-1);

 ioctl(fd, SIOCGIFADDR, &ifr);
printf("\n ioctl retrun : %d",errno);

 close(fd);

 /* display result */
 printf("\n%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

 return 0;
}
```


----------



## SirDice (Oct 25, 2018)

arishbaig said:


> I have an issue with a sample program to get the ip address which is working fine when I am compiling it with FreeBSD built in gcc.


What version of FreeBSD? GCC was removed from the base OS quite some time ago, so there is no built in GCC any more. Clang 3.7.1 seems old too, 11.2-RELEASE and 12.0-BETA1 have Clang 6.0.1.


----------



## arishbaig (Oct 26, 2018)

I am trying on FreeBSD10 - gcc version 6.4.0 (FreeBSD Ports Collection)


----------

