Turn ON/OFF Wireless Card

Hi all,

In my program, I am trying to use ioctl to turn on/off the wireless card, using SIOCSIFFLAGS (I am new to it but somebody said it is the most traditional way). However, it seems that I didn't set the flag correctly since the wireless is always on (I have root permission). If any one could tell me how you do that or check my code to see where is the problem I will appreciate it. The codes are:

static int powerOff()      
{
  struct ifreq wrq;
  char ifname[]="wlan0";
  /* Set dev name */
  strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
  if(ioctl(skfd, SIOCGIFFLAGS, &wrq) < 0)
  {
    fprintf(stderr, "SIOCGIFFLAGS: %s\n", strerror(errno));
    return(-1);
  }
  wrq.ifr_flags |= (-IFF_UP);
  if(ioctl(skfd, SIOCSIFFLAGS, &wrq) < 0)
  {
    fprintf(stderr, "SIOCSIFFLAGS,: %s\n", strerror(errno));
    return (-1);
  }
  return(0);
}

Thanks for any help!

wrq.ifr_flags |= (-IFF_UP);

If this is a bitfield, this won't turn off the IFF_UP flag -- it will turn on nearly every possible flag except IFF_UP. Try:

wrq.ifr_flags &= ~IFF_UP;

Thanks! It is working now.