When I use the below code to try to use the TUN/TAP driver the read(.. call blocks and never returns from the read...The code is as follows:

Code:
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/ioctl.h>	
#include <linux/fcntl.h> //for O_RDWR
	
int tun_alloc&#40;char *dev&#41;; //declare that there will be such a function
//const O_RDWR=2;

int main&#40;int argc, char **argv&#41;
&#123;
	char *buf&#91;2048&#93;, dev&#91;10&#93;="tun0\0";
    int fd, len;
	
	fd = tun_alloc&#40;dev&#41;;
	printf&#40;"\r\n%s\r\n",dev&#41;;

	while&#40;1&#41;
	&#123;
		len = read&#40;fd,buf,sizeof&#40;buf&#41;&#41;; //locks up here
		if&#40;len>=0&#41;
		     printf&#40;"\r\nreceived %d Bytes",len&#41;;
		else
			printf&#40;"\rfailed"&#41;;
	&#125;		
	return 0;
&#125;

int tun_alloc&#40;char *dev&#41;
&#123;
	struct ifreq ifr;
    int fd, err ,r;
	char buf&#91;2048&#93;;

    if&#40; &#40;fd = open&#40;"/dev/net/tun", O_RDWR&#41;&#41; < 0 &#41;
	  &#123;
		  printf&#40;"\r\n Error opening tun device\r\n"&#41;;
		  return -1;
         //return tun_alloc_old&#40;dev&#41;;
	  &#125;
	  else
		  printf&#40;"\r\n Successfully opened TUN device.\r\n"&#41;;

      memset&#40;&ifr, 0, sizeof&#40;ifr&#41;&#41;;

      /* Flags&#58; IFF_TUN   - TUN device &#40;no Ethernet headers&#41; 
       *        IFF_TAP   - TAP device  
       *
       *        IFF_NO_PI - Do not provide packet information  
       */ 
      ifr.ifr_flags = IFF_TAP; 
      if&#40; *dev &#41;
         strncpy&#40;ifr.ifr_name, dev, IFNAMSIZ&#41;;
	  
      if&#40;&#40;err = ioctl&#40;fd, TUNSETIFF, &#40;void *&#41; &ifr&#41;&#41; < 0 &#41;
	  &#123;
         close&#40;fd&#41;;
		  printf&#40;"\r\n Errot ioctl tun device\r\n"&#41;;
         return err;
      &#125;
	  else
		  printf&#40;"\r\n ioctl successful\r\n"&#41;;
      strcpy&#40;dev, ifr.ifr_name&#41;;

      return fd;
&#125;

I have been stuck here for a while now. All help and any advice would greatly be appreciated. My project is coming to a halt due to this issue.

Thanks in advance,

Jordan