//zzc:
#ifndef __CYGWIN__
/*
	skyeye_net_vnet.c - vnet net device file support functions
	Copyright (C) 2003 Skyeye Develop Group
	for help please send mail to <skyeye-developer@lists.sf.linuxforum.net> 
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
*/

/*
 * 04/27/2003 	initial version
 *				chenyu <chenyu@hpclab.cs.tsinghua.edu.cn>
 */
#include <fcntl.h> 
#include <net/if.h>
//#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include "armdefs.h"
//chy: make sure that if_vnet.h is as same as vnet/if_vnet.h
#include "if_vnet.h"

#define DEBUG 1
#if DEBUG
#define DBG_PRINT(a...) fprintf(stderr, ##a)
#else
#define DBG_PRINT(a...)
#endif


//chy:2003-04-26 open&ioctl net dev file, and return file handler 
// set ne2k mac addr is in nic_reset function
// no use hostip
int vnetif_init(int index,unsigned char *macaddr, unsigned char *hostip)
{
  int vnetif_fd, vnet_fd; 
  static int firsttime=0;
  struct ifreq ifr;
  int flag;
  unsigned char maddr[6];
  char buf[128];

  firsttime++;
  DBG_PRINT("vnetif_init begin, hostip[0][1][2][3] %d.%d.%d.%d\n",hostip[0],hostip[1],hostip[2],hostip[3]);
  //chy: should change !!! will support more virtual hubs in the future
if(firsttime==1){
  if(hostip[0]==0) //means don't do hostif ......
	goto vnet_done;	
/*
  if(vnet_fd=open("/var/vnet_lock",O_CREAT|O_EXCL|O_RDONLY)<0)
	goto vnet_done;
  if(unlink("/var/vnet_lock")<0){
	perror("vnetif_init: unlink error");
	exit(-1);
  }
*/

  vnetif_fd = open("/dev/net/vnet", 2);
  DBG_PRINT("vnetif_init:for hostif fd %d\n", vnetif_fd);
  if(vnetif_fd == -1) {
    perror("vnetif_init:for hostif open");
    printf("-----------------------------------------------------------\n");
    printf("NOTICE: Now the net simulation function is disable!!!\n");
    printf("NOTICE: 1. you should be root at first !!!\n");
    printf("NOTICE: 2. if you don't make device node, you should do commands:\n");
    printf("NOTICE:      mkdir /dev/net; mknod /dev/net/vnet c 10 201\n");
    printf("NOTICE: 3. you should inmod linux kernel net driver vnet.o!!!\n");
    printf("NOTICE: Please read README in SkyEye package and try again!!!\n");
    printf("-----------------------------------------------------------\n");
    exit(-1);
  }

  memset(&ifr, 0, sizeof(ifr));
  //If you don't  set name, then it will be set in vnet_hostif_create
  //If you want more vnet, then you should set in here
  //maybe should be defineed in skyeye.conf 
  //now, I only use vnet0.   chy
  //IFNAMSIZ=16
  strncpy(ifr.ifr_name, "vnet0", IFNAMSIZ);
  ifr.ifr_flags = IFF_VNET;
  if (ioctl(vnetif_fd, VNETSETIFF, (void *) &ifr) < 0) {
    perror("ioctl VNETSETIFF error");
    exit(-1);
  }
  DBG_PRINT("ioctl VNETSETIFF ok\n");
  snprintf(buf, sizeof(buf), "ifconfig vnet0 inet %d.%d.%d.%d",
           hostip[0],hostip[1],hostip[2],hostip[3]);
  DBG_PRINT("tapif_init: system(\"%s\");\n", buf);
  system(buf);
}

vnet_done: 
  //the scond time, open dev file for guestif
  vnetif_fd = open("/dev/net/vnet", 2);
  DBG_PRINT("vnetif_init:for guestif fd %d\n", vnetif_fd);
  if(vnetif_fd == -1) {
    perror("vnetif_init:for guestif open");
    exit(-1);
  }

  //set mac addr for vnet
  memcpy(maddr,macaddr,6);
  if (ioctl(vnetif_fd, SIOCSIFADDR, (void *) maddr) < 0) {
     	printf("vnetif_init: set mac addr error\n");
     	exit(-1);
  }
  DBG_PRINT("vnetif_init: vnetif_init: set mac addr ok\n");
  //set ifflag for vnet
  flag=IFF_UP|IFF_RUNNING|IFF_BROADCAST;
  if (ioctl(vnetif_fd, SIOCSIFFLAGS, (void *) &flag) < 0) {
       printf("vnetif_init: ioctl vnet set ifflag error\n");
        exit(-1);
  }
  DBG_PRINT("vnetif_init: ioctl vnet set ifflag ok.\n");
  
  DBG_PRINT("vnetif_init end\n");
  return vnetif_fd;
 }

//zzc: #endif __CYGWIN__
#endif
