title: ioctl IOCGIWSCAN : invalid arguments,
body: <p>Hi everybody i'm trying to get information so does iwlist command. 
  I got a invalid arguments errno, and I don't understand why, I'm fellowing a good paper with deals about that : 
  <a href="http://blog.listincomprehension.com/2011/03/wireless-scanning-with-erlang.html" rel="nofollow">ioctl &amp; iwreq</a>
  .
  My code is the fellowing :</p>
  
  <pre><code>#include &lt;sys/ioctl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;netpacket/packet.h&gt;
  #include &lt;netinet/if_ether.h&gt;
  #include "ioctlcmd.h"
  #include &lt;arpa/inet.h&gt;
  
  /*  SIOCGIWNAME     0x8B01  power
      SIOCGIWTXPOW    0x8B27  
      SIOCSIWFREQ     0x8B04        set channel/frequency (Hz)   
  */
  
  
  
  int check_ifname_exist(char * ifname) {
  
      int sock = socket(AF_PACKET,SOCK_RAW,0) ; 
      if (sock == RET_ERR) {
        printf("Error while checking ifname [ERRNO : %d], [STRERROR : %s]\n",errno,strerror(errno)); 
        return RET_ERR ; 
      }   
  
      struct ifreq ifr ; 
      strncpy(ifr.ifr_name,ifname,IFNAMSIZ) ;
  
      int ret = ioctl(sock,SIOCGIFINDEX,&amp;ifr) ; 
      if (ret == RET_ERR) {
        printf("Error while checking ifname [ERRNO : %d], [STRERROR : %s]\n",errno,strerror(errno)); 
        return RET_ERR ; 
      }
  
     return RET_SUCCES ; 
  }
  
  int get_channel_fq(char * essid, char * ifname) {
  
    int sock = socket(AF_PACKET,SOCK_RAW,0) ; 
    if (sock == RET_ERR) {
      fprintf(stderr,"Error while opening socket [ERRNO : %d] [STRERROR : %s]\n",errno,strerror(errno)); 
      return RET_ERR ;    
    }
  
    if (bind_socket(ifname,sock) == RET_ERR)
      return RET_ERR ; 
    printf("coucou\n") ; //DEBUG 
    struct iwreq iwr ; 
    memset(&amp;iwr,0,sizeof(struct iwreq)) ; 
    memcpy(iwr.ifr_ifrn.ifrn_name,ifname,IFNAMSIZ) ;
    printf("%s\n",iwr.ifr_ifrn.ifrn_name) ;   
    iwr.u.data.pointer = essid ; 
    iwr.u.data.length = strlen(essid)+1 ; 
    iwr.u.data.flags |= IW_SCAN_THIS_ESSID ; 
  
    if ((sock =ioctl(sock,SIOCSIWSCAN,&amp;iwr)) == RET_ERR) {
      fprintf(stderr,"Error while getting the freq of the card. [ERRNO : %d] [STRERROR : %s]\n",errno,strerror(errno)) ; 
      return RET_ERR ; 
    }
    printf("coucou\n") ;  // DEBUG
    int bufsize = 4096 ;
    struct iwreq iwr2 ={0} ; 
    //memset(&amp;iwr2,0,sizeof(struct iwreq)) ; 
    char  *p = NULL ; 
    p=calloc(bufsize,1) ; 
    iwr2.u.data.pointer = p ; 
    iwr2.u.data.length = bufsize ; 
    iwr2.u.data.flags = 0 ;
    memcpy(iwr2.ifr_ifrn.ifrn_name,ifname,IFNAMSIZ) ;
    if (ioctl(sock,SIOCGIWSCAN,&amp;iwr2) == RET_ERR) { // ERROR 
           fprintf(stderr,"Error while getting the freq of the card. [ERRNO : %d] [STRERROR : %s]\n",errno,strerror(errno)) ; // ERROR IS HERE
           return RET_ERR ;
    }
  
     return RET_SUCCES ; 
  }
  
  int bind_socket(char * ifname,int sock_fd) {
  
    struct ifreq ifr ; 
    struct sockaddr_ll sall ; 
    memset(&amp;sall,0,sizeof(struct sockaddr_ll)) ; 
    strncpy(ifr.ifr_name,ifname,IFNAMSIZ) ; 
    ifr.ifr_addr.sa_family = AF_INET ; 
  
    if (ioctl(sock_fd,SIOCGIFINDEX,&amp;ifr) == RET_ERR) 
      goto ret_err ; 
    if (ifr.ifr_ifindex &lt; 0) 
       goto ret_err ; 
  
    sall.sll_ifindex = ifr.ifr_ifindex  ; 
    sall.sll_family = AF_PACKET ; 
    sall.sll_protocol = htons(ETH_P_ALL) ; 
  
    if (bind(sock_fd,(struct sockaddr *)&amp;sall,sizeof(sall)) == RET_ERR) 
      goto ret_err ;
  
    return sock_fd ; 
  
  ret_err : 
  
     fprintf(stderr,"Error while binding socket. [ERRNO = %d] [STDERROR = %s]\n",errno,strerror(errno)) ; 
     return RET_ERR ; 
  }
  
  int main() {
  
     get_channel_fq("ditwifi","wlan0") ; 
  }
  </code></pre>
  
  <p>In the code above, the two printf("coucou") are printed, so I'm sure that the first ioctl()
       ioctl(sock,IOCSIWSCAN,&amp;iwr)
  works fine. </p>
  
  <p>I think I miss something, but I did't find.</p>
  
  <p>Thanks a lot. </p>