int main(int argc, char *argv[]) {

    /* TODO install signalhandler clean_up for clean termination */
    
    set_up_server();

    print_ip_and_port();   /* inform user of where we are running */

    server_loop();

}
Beispiel #2
0
int _cdecl
main(int argc, char *argv[])
{

  int	c;
  int   not_inetd = 0;
#ifdef WIN32
  BOOL  child = FALSE;
#endif
  char arg1[BUFSIZ], arg2[BUFSIZ];
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
  char FileName[PATH_MAX];   /* for opening the debug log file */

  struct sockaddr name;
  netperf_socklen_t namelen = sizeof(name);
  

#ifdef WIN32
	WSADATA	wsa_data ;

	/* Initialize the winsock lib ( version 2.2 ) */
	if ( WSAStartup(MAKEWORD(2,2), &wsa_data) == SOCKET_ERROR ){
		printf("WSAStartup() failed : %d\n", GetLastError()) ;
		return 1 ;
	}
#endif /* WIN32 */

	/* Save away the program name */
	program = (char *)malloc(strlen(argv[0]) + 1);
	if (program == NULL) {
		printf("malloc(%d) failed!\n", strlen(argv[0]) + 1);
		return 1 ;
	}
	strcpy(program, argv[0]);

  netlib_init();
  
  /* Scan the command line to see if we are supposed to set-up our own */
  /* listen socket instead of relying on inetd. */

  /* first set a copy of initial values */
  strncpy(local_host_name,"0.0.0.0",sizeof(local_host_name));
  local_address_family = AF_UNSPEC;
  strncpy(listen_port,TEST_PORT,sizeof(listen_port));

  while ((c = getopt(argc, argv, SERVER_ARGS)) != EOF) {
    switch (c) {
    case '?':
    case 'h':
      print_netserver_usage();
      exit(1);
    case 'd':
      /* we want to set the debug file name sometime */
      debug++;
      break;
    case 'L':
      not_inetd = 1;
      break_args_explicit(optarg,arg1,arg2);
      if (arg1[0]) {
	strncpy(local_host_name,arg1,sizeof(local_host_name));
      }
      if (arg2[0]) {
	local_address_family = parse_address_family(arg2);
	/* if only the address family was set, we may need to set the
	   local_host_name accordingly. since our defaults are IPv4
	   this should only be necessary if we have IPv6 support raj
	   2005-02-07 */  
#if defined (AF_INET6)
	if (!arg1[0]) {
	  strncpy(local_host_name,"::0",sizeof(local_host_name));
	}
#endif
      }
      break;
    case 'n':
      shell_num_cpus = atoi(optarg);
      if (shell_num_cpus > MAXCPUS) {
	fprintf(stderr,
		"netserver: This version can only support %d CPUs. Please",
		MAXCPUS);
	fprintf(stderr,
		"           increase MAXCPUS in netlib.h and recompile.\n");
	fflush(stderr);
	exit(1);
      }
      break;
    case 'p':
      /* we want to open a listen socket at a */
      /* specified port number */
      strncpy(listen_port,optarg,sizeof(listen_port));
      not_inetd = 1;
      break;
    case '4':
      local_address_family = AF_INET;
      break;
    case '6':
#if defined(AF_INET6)
      local_address_family = AF_INET6;
      strncpy(local_host_name,"::0",sizeof(local_host_name));
#else
      local_address_family = AF_UNSPEC;
#endif
      break;
    case 'v':
      /* say how much to say */
      verbosity = atoi(optarg);
      break;
    case 'V':
      printf("Netperf version %s\n",NETPERF_VERSION);
      exit(0);
      break;
#ifdef WIN32
/*+*+SAF */
	case 'I':
		child = TRUE;
		/* This is the handle we expect to inherit. */
		/*+*+SAF server_sock = (HANDLE)atoi(optarg); */
		break;
	case 'i':
		/* This is a handle we should NOT inherit. */
		/*+*+SAF CloseHandle((HANDLE)atoi(optarg)); */
		break;
#endif

    }
  }

  /* +*+SAF I need a better way to find inherited handles I should close! */
  /* +*+SAF Use DuplicateHandle to force inheritable attribute (or reset it)? */

/*  unlink(DEBUG_LOG_FILE); */

  strcpy(FileName, DEBUG_LOG_FILE);
    
#ifndef WIN32
  snprintf(&FileName[strlen(FileName)], sizeof(FileName) - strlen(FileName), "_%d", getpid());
  if ((where = fopen(FileName, "w")) == NULL) {
    perror("netserver: debug file");
    exit(1);
  }
#else
  {
    
    if (child) {
      snprintf(&FileName[strlen(FileName)], sizeof(FileName) - strlen(FileName), "_%x", getpid());
    }
    
    /* Hopefully, by closing stdout & stderr, the subsequent
       fopen calls will get mapped to the correct std handles. */
    fclose(stdout);
    
    if ((where = fopen(FileName, "w")) == NULL) {
      perror("netserver: fopen of debug file as new stdout failed!");
      exit(1);
    }
    
    fclose(stderr);
    
    if ((where = fopen(FileName, "w")) == NULL) {
      fprintf(stdout, "fopen of debug file as new stderr failed!\n");
      exit(1);
    }
  }
#endif
 
#ifndef WIN32 
  chmod(DEBUG_LOG_FILE,0644);
#endif
  
#if WIN32
  if (child) {
	  server_sock = (SOCKET)GetStdHandle(STD_INPUT_HANDLE);
  }
#endif

  /* if we are not a child of an inetd or the like, then we should
   open a socket and hang listens off of it. otherwise, we should go
   straight into processing requests. the do_listen() routine will sit
   in an infinite loop accepting connections and forking child
   processes. the child processes will call process_requests */
  
  /* If fd 0 is not a socket then assume we're not being called */
  /* from inetd and start server socket on the default port. */
  /* this enhancement comes from [email protected] (Von Welch) */
  if (not_inetd) {
    /* the user specified a port number on the command line */
    set_up_server(local_host_name,listen_port,local_address_family);
  }
#ifdef WIN32
  /* OK, with Win2003 WinNT's POSIX subsystem is gone, and hence so is */
  /* fork.  But hopefully the kernel support will continue to exist */
  /* for some time.  We are not counting on the address space */
  /* copy_on_write support, since it isn't exposed except through the */
  /* NT native APIs (which are not public).  We will try to use the */
  /* InheritHandles flag in CreateProcess though since this is public */
  /* and is used for more than just POSIX so hopefully it won't go */
  /* away. */
  else if (TRUE) {
    if (child) {
      process_requests();
    } else {
      strncpy(listen_port,TEST_PORT,sizeof(listen_port));
      set_up_server(local_host_name,listen_port,local_address_family);
    }
  }
#endif
#if !defined(__VMS)
  else if (getsockname(0, &name, &namelen) == SOCKET_ERROR) {
    /* we may not be a child of inetd */
    if (errno == ENOTSOCK) {
      strncpy(listen_port,TEST_PORT,sizeof(listen_port));
      set_up_server(local_host_name,listen_port,local_address_family);
    }
  }
#endif /* !defined(__VMS) */
  else {
    /* we are probably a child of inetd, or are being invoked via the
       VMS auxilliarly server mechanism */
#if !defined(__VMS)
    server_sock = 0;
#else
    if ( (server_sock = socket(TCPIP$C_AUXS, SOCK_STREAM, 0)) == INVALID_SOCKET ) 
    { 
      perror("Failed to grab aux server socket" ); 
      exit(1); 
    } 
  
#endif /* !defined(__VMS) */
    process_requests();
  }
#ifdef WIN32
	/* Cleanup the winsock lib */
	WSACleanup();
#endif

  return(0);
}