示例#1
0
static void init(void)
{
   init_winsock();
   create_io_completion_port();
   create_listening_socket();
   bind_listening_socket();
   start_listening();
   load_accept_ex();
   start_accepting();
}
示例#2
0
ns_net_udp :: ns_net_udp ()
{
#ifdef OS_MINGW
	init_winsock();
#endif
	sockAddrSize = sizeof(sockaddr_in);
	if ((sFd = socket(AF_INET, SOCK_DGRAM, 0/*IPPROTO_UDP*/)) <= 0) {

		perror("can't open socket:");
	}
}
示例#3
0
int wget_main(int argc UNUSED_PARAM, char **argv)
{
	char buf[512];
	struct host_info server, target;
	len_and_sockaddr *lsa;
	unsigned opt;
	int redir_limit;
	char *proxy = NULL;
	char *dir_prefix = NULL;
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
	char *post_data;
	char *extra_headers = NULL;
	llist_t *headers_llist = NULL;
#endif
	FILE *sfp;                      /* socket to web/ftp server         */
	FILE *dfp;                      /* socket to ftp server (data)      */
	char *fname_out;                /* where to direct output (-O)      */
	int output_fd = -1;
	bool use_proxy;                 /* Use proxies if env vars are set  */
	const char *proxy_flag = "on";  /* Use proxies if env vars are set  */
	const char *user_agent = "Wget";/* "User-Agent" header field        */

	static const char keywords[] ALIGN1 =
		"content-length\0""transfer-encoding\0""chunked\0""location\0";
	enum {
		KEY_content_length = 1, KEY_transfer_encoding, KEY_chunked, KEY_location
	};
#if ENABLE_FEATURE_WGET_LONG_OPTIONS
	static const char wget_longopts[] ALIGN1 =
		/* name, has_arg, val */
		"continue\0"         No_argument       "c"
		"spider\0"           No_argument       "s"
		"quiet\0"            No_argument       "q"
		"output-document\0"  Required_argument "O"
		"directory-prefix\0" Required_argument "P"
		"proxy\0"            Required_argument "Y"
		"user-agent\0"       Required_argument "U"
#if ENABLE_FEATURE_WGET_TIMEOUT
		"timeout\0"          Required_argument "T"
#endif
		/* Ignored: */
		// "tries\0"            Required_argument "t"
		/* Ignored (we always use PASV): */
		"passive-ftp\0"      No_argument       "\xff"
		"header\0"           Required_argument "\xfe"
		"post-data\0"        Required_argument "\xfd"
		/* Ignored (we don't do ssl) */
		"no-check-certificate\0" No_argument   "\xfc"
		;
#endif

	INIT_G();
	IF_WIN32_NET(init_winsock();)
示例#4
0
static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD timeout)
{
    int result;
    ULONG flag;
    DWORD res;

    init_winsock();

    assert(server->addr_len);
    result = netconn->socket = socket(server->addr.ss_family, SOCK_STREAM, 0);
    if(result != -1) {
        set_socket_blocking(netconn, FALSE);
        result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len);
        if(result == -1)
        {
            res = WSAGetLastError();
            if (res == WSAEINPROGRESS || res == WSAEWOULDBLOCK) {
                FD_SET set;
                int res;
                socklen_t len = sizeof(res);
                TIMEVAL timeout_timeval = {0, timeout*1000};

                FD_ZERO(&set);
                FD_SET(netconn->socket, &set);
                res = select(netconn->socket+1, NULL, &set, NULL, &timeout_timeval);
                if(!res || res == SOCKET_ERROR) {
                    closesocket(netconn->socket);
                    netconn->socket = -1;
                    return ERROR_INTERNET_CANNOT_CONNECT;
                }
                if (!getsockopt(netconn->socket, SOL_SOCKET, SO_ERROR, (void *)&res, &len) && !res)
                    result = 0;
            }
        }
        if(result == -1)
        {
            closesocket(netconn->socket);
            netconn->socket = -1;
        }
    }
    if(result == -1)
        return ERROR_INTERNET_CANNOT_CONNECT;

    flag = 1;
    result = setsockopt(netconn->socket, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
    if(result < 0)
        WARN("setsockopt(TCP_NODELAY) failed\n");

    return ERROR_SUCCESS;
}
示例#5
0
文件: client.c 项目: Jactry/wine
static HRESULT WINAPI lobbyclient_Initialize(IDirectPlay8LobbyClient *iface, void *context,
                                             PFNDPNMESSAGEHANDLER msghandler, DWORD flags)
{
    IDirectPlay8LobbyClientImpl *This = impl_from_IDirectPlay8LobbyClient(iface);

    TRACE("(%p):(%p,%p,%x)\n", This, context, msghandler, flags);

    if(!msghandler)
        return E_POINTER;

    This->usercontext = context;
    This->msghandler = msghandler;
    This->flags = flags;

    init_winsock();

    return DPN_OK;
}
示例#6
0
文件: client.c 项目: Jactry/wine
static HRESULT WINAPI IDirectPlay8ClientImpl_Initialize(IDirectPlay8Client *iface,
        void * const pvUserContext, const PFNDPNMESSAGEHANDLER pfn, const DWORD dwFlags)
{
    IDirectPlay8ClientImpl *This = impl_from_IDirectPlay8Client(iface);

    TRACE("(%p):(%p,%p,%x)\n", This, pvUserContext, pfn, dwFlags);

    if(!pfn)
        return DPNERR_INVALIDPARAM;

    This->usercontext = pvUserContext;
    This->msghandler = pfn;
    This->flags = dwFlags;

    init_winsock();

    return DPN_OK;
}
示例#7
0
文件: usbip.c 项目: Shmuma/usbip
int
main(int argc, char *argv[])
{
	int cmd;

	if(init_winsock()){
		err("can't init winsock");
		return 0;
	}
	cmd = parse_opt(argc, argv);

	switch(cmd) {
		case CMD_ATTACH:
			if (optind == argc - 2)
				attach_device(argv[optind], argv[optind+1]);
			else
				show_help(argv[0]);
			break;
		case CMD_DETACH:
			while (optind < argc)
				detach_port(argv[optind++]);
			break;
		case CMD_PORT:
			show_port_status();
			break;
		case CMD_LIST:
			while (optind < argc)
				show_exported_devices(argv[optind++]);
			break;
		case CMD_ATTACHALL:
			while(optind < argc)
				attach_devices_all(argv[optind++]);
			break;
		case CMD_VERSION:
			printf("%s\n", version);
			break;
		case CMD_HELP:
			show_help(argv[0]);
			break;
		default:
			show_help(argv[0]);
	}
	return 0;
}
示例#8
0
int wcplg_open_sftp_session(char *userhost, char *user, char *pass,
                            int portnumber)
{
  wcplg_set_last_error_msg(NULL);

  if (ProgressProc("", "", 0) == 1) {
    wcplg_set_last_error_msg("cancel by user");
    return RESULT_ERR;
  }

  console_password = dupstr(pass);
  flags = 0;                    //FLAG_STDERR | FLAG_INTERACTIVE;
  cmdline_tooltype = TOOLTYPE_FILETRANSFER;
  ssh_get_line = &console_get_line;

  do_defaults(NULL, &cfg);

  init_winsock();
  sk_init();

  back = NULL;

  if (userhost) {
    if (psftp_connect(userhost, user, portnumber)) {
      return RESULT_ERR;
    }

    if (do_sftp_init()) {
      return RESULT_ERR;
    }
  } else {
    printf
      ("psftp: no hostname specified; use \"open host.name\" to connect\n");
    return RESULT_ERR;
  }

  ISinitT = 1;

  return RESULT_OK;
}
示例#9
0
int main() {
	int result = 0;

	if (!init_winsock()) {
		printf("Cannot initialize winsock\n");
		result = 1;
		goto cleanup;
	}

	SOCKET server_socket = create_server();
	if (server_socket == INVALID_SOCKET) {
		printf("Socket error\n");
		result = 1;
		goto cleanup;
	}

	run(server_socket);

cleanup:
	free_winsock();
	return result;
}
示例#10
0
DWORD WINAPI TCP_server_thread(LPVOID param)
{
	if (FAILED(init_winsock()))
		return -1;

	// init and listen
	SOCKADDR_IN server_addr;
	int tmp_socket = socket(PF_INET, SOCK_STREAM, 0);

	if (tmp_socket == -1)
		return -1;

	printf("Server Starting @ %d....", (int)server_port);

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons((int)server_port);
	server_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
	memset(&(server_addr.sin_zero), 0, 8);
	if (bind(tmp_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)) == -1)
	{
		printf("FAIL!\n");
		server_socket = -1;
		closesocket(tmp_socket);
		return -1;
	}
	if (listen(tmp_socket, MAX_CONNECTION) == -1 ) 
	{
		server_socket = -1;
		printf("FAIL!\n");
		closesocket(tmp_socket);
		return -1;
	}
	printf("OK\n");

	int sock_size = sizeof(SOCKADDR_IN);
	SOCKADDR_IN user_socket;

	// wait for connection
	server_socket = tmp_socket;

	while(!server_stopping)
	{
		int acc_socket = accept(tmp_socket, (SOCKADDR*)&user_socket, &sock_size);

		if (server_stopping)
			break;

		if (acc_socket == -1)
			continue;

		DWORD ip = user_socket.sin_addr.S_un.S_addr;
		DWORD *para = new DWORD[2];
		para[0] = acc_socket;
		para[1] = ip;
		CreateThread(NULL, NULL, handler_thread, para, NULL, NULL);
	}

	printf("DWindow Network Server Stopping\n");

	server_socket = -1;
	return 0;
}
示例#11
0
文件: psftp.c 项目: rdebath/sgt
/*
 * Main program. Parse arguments etc.
 */
int main(int argc, char *argv[])
{
    int i;
    int portnumber = 0;
    char *userhost, *user;
    int mode = 0;
    int modeflags = 0;
    char *batchfile = NULL;

    flags = FLAG_STDERR | FLAG_INTERACTIVE;
    ssh_get_line = &console_get_line;
    init_winsock();
    sk_init();

    userhost = user = NULL;

    for (i = 1; i < argc; i++) {
	if (argv[i][0] != '-') {
	    if (userhost)
		usage();
	    else
		userhost = dupstr(argv[i]);
	} else if (strcmp(argv[i], "-v") == 0) {
	    verbose = 1, flags |= FLAG_VERBOSE;
	} else if (strcmp(argv[i], "-h") == 0 ||
		   strcmp(argv[i], "-?") == 0) {
	    usage();
	} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
	    user = argv[++i];
	} else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
	    portnumber = atoi(argv[++i]);
	} else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc) {
	    console_password = argv[++i];
	} else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) {
	    mode = 1;
	    batchfile = argv[++i];
	} else if (strcmp(argv[i], "-bc") == 0) {
	    modeflags = modeflags | 1;
	} else if (strcmp(argv[i], "-batch") == 0) {
	    console_batch_mode = TRUE;
	} else if (strcmp(argv[i], "-be") == 0) {
	    modeflags = modeflags | 2;
	} else if (strcmp(argv[i], "--") == 0) {
	    i++;
	    break;
	} else {
	    usage();
	}
    }
    argc -= i;
    argv += i;
    back = NULL;

    /*
     * If a user@host string has already been provided, connect to
     * it now.
     */
    if (userhost) {
	if (psftp_connect(userhost, user, portnumber))
	    return 1;
	do_sftp_init();
    } else {
	printf("psftp: no hostname specified; use \"open host.name\""
	    " to connect\n");
    }

    do_sftp(mode, modeflags, batchfile);

    if (back != NULL && back->socket() != NULL) {
	char ch;
	back->special(TS_EOF);
	sftp_recvdata(&ch, 1);
    }
    WSACleanup();
    random_save_seed();

    return 0;
}
示例#12
0
文件: simi.c 项目: TronicLabs/sdcc
void openSimulator (char **args, int nargs)
{
    struct sockaddr_in sin;
    int retry = 0;
    int i;
    u_long iMode;
    int iResult;
    int fh;

  init_winsock();

  Dprintf(D_simi, ("simi: openSimulator\n"));
#ifdef SDCDB_DEBUG
  if (D_simi & sdcdbDebug)
    {
      printf("simi: openSimulator: ");
      for (i=0; i < nargs; i++ )
        {
          printf("arg%d: %s ", i, args[i]);
        }
      printf("\n");
    }
#endif
  invalidateCache(XMEM_CACHE);
  invalidateCache(IMEM_CACHE);
  invalidateCache(SREG_CACHE);

  if (INVALID_SOCKET == (sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0)))
    {
      fprintf(stderr, "cannot create socket: %d\n", WSAGetLastError());
      exit(1);
    }

  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  sin.sin_port = htons(9756);

 try_connect:
  /* connect to the simulator */
  if (SOCKET_ERROR == connect(sock, (struct sockaddr *)&sin, sizeof(sin)))
    {
      /* if failed then wait 1 second & try again
         do this for 10 secs only */
      if (retry < 10)
        {
          if ( !retry )
              simPid = execSimulator(args, nargs);
          retry ++;
          Sleep(1000);
          goto try_connect;
        }
      perror("connect failed :");
      exit(1);
    }

  iMode = 1; /* set non-blocking mode */
  iResult = ioctlsocket(sock, FIONBIO, &iMode);
  if (iResult != NO_ERROR)
    {
      perror("ioctlsocket failed");
      exit(1);
    }

  fh = _open_osfhandle(sock, _O_TEXT);
  if (-1 == fh)
    {
      perror("cannot _open_osfhandle");
      exit(1);
    }

  /* got the socket now turn it into a file handle */
  if (!(simin = fdopen(fh, "r")))
    {
      perror("cannot open socket for read");
      exit(1);
    }

  fh = _open_osfhandle(sock, _O_TEXT);
  if (-1 == fh)
    {
      perror("cannot _open_osfhandle");
      exit(1);
    }

  if (!(simout = fdopen(fh, "w")))
    {
      perror("cannot open socket for write");
      exit(1);
    }
  /* now that we have opened, wait for the prompt */
  waitForSim(200, NULL);
  simactive = 1;
}
示例#13
0
文件: scp.c 项目: rdebath/sgt
/*
 *  Main program (no, really?)
 */
int main(int argc, char *argv[])
{
    int i;
    int list = 0;

    init_winsock();

    for (i = 1; i < argc; i++) {
	if (argv[i][0] != '-')
	    break;
	if (strcmp(argv[i], "-v") == 0)
	    verbose = 1;
	else if (strcmp(argv[i], "-r") == 0)
	    recursive = 1;
	else if (strcmp(argv[i], "-p") == 0)
	    preserve = 1;
	else if (strcmp(argv[i], "-q") == 0)
	    statistics = 0;
	else if (strcmp(argv[i], "-h") == 0 ||
		 strcmp(argv[i], "-?") == 0)
	    usage();
	else if (strcmp(argv[i], "-P") == 0 && i+1 < argc)
	    portnumber = atoi(argv[++i]);
	else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc)
	    password = argv[++i];
	else if (strcmp(argv[i], "-ls") == 0)
	    list = 1;
	else if (strcmp(argv[i], "--") == 0)
	{ i++; break; }
	else
	    usage();
    }
    argc -= i;
    argv += i;

    if (list) {
	if (argc != 1)
	    usage();
	get_dir_list(argc, argv);

    } else {

	if (argc < 2)
	    usage();
	if (argc > 2)
	    targetshouldbedirectory = 1;

	if (colon(argv[argc-1]) != NULL)
	    toremote(argc, argv);
	else
	    tolocal(argc, argv);
    }

    if (connection_open) {
	char ch;
	ssh_send_eof();
	ssh_recv(&ch, 1);
    }
    WSACleanup();
    random_save_seed();

    return (errs == 0 ? 0 : 1);
}
示例#14
0
/*------------------------------------------------------------------------
 * Set the Usage information for the user...
 */
int main(int argc, char **argv)
{
FILE			*ofp = 0;
const char		*outfile = 0;
SOCKET			sockfd;
int			c;
char			errbuf[256];
struct sockaddr_in	myaddr;
int			have_targets = FALSE;
#ifdef _WIN32
int			winsock_level = 2;
#endif

	/* quick hack */
	if ( argv[1]  &&  strcmp(argv[1], "--version") == 0 )
	{
		puts(Version);
		exit(EXIT_SUCCESS);
	}

	while ( (c = getopt(argc, argv, "CHT:nfp:bt:vw:mVO:1P")) != EOF )
	{
		switch (c)
		{
		  default:	Usage(argv[0]);

		  case 'P':
#ifdef ENABLE_PERL
				gen_Perl = TRUE;
#else
				die("Sorry, -P perl support not compiled");
#endif
				break;

		  case 'C':
#ifdef SUPPORT_CIFS
				dest_portno = 445;
				break;
#else
				die("Sorry, -C not supported yet");
#endif

		  case '1':
#ifdef _WIN32
				winsock_level = 1;
#endif
				break;

		  case 'H':	gen_HTTP = TRUE;
				break;
		
		  case 'O':	outfile = optarg;
				break;

		  case 't':	set_tries( atoi(optarg) );
				break;

		  case 'V':
				puts(Version);
				exit(EXIT_SUCCESS);

		  case 'm':	show_mac_address = TRUE;
				break;

		  case 'f':	full_nbtstat = TRUE;
				break;

	 	  case 'p':	bind_portno = (unsigned short)atoi(optarg);
				break;

		  case 'b':
#if SUPPORT_BROADCAST
				broadcast = TRUE;
				break;
#else
				die("Sorry, -b broadcast not yet supported");
#endif

		  case 'v':	verbose++;
				break;

		  case 'T':	timeout_secs = atoi(optarg);
				break;

		  case 'w':	write_sleep_msecs = atoi(optarg);
				break;

		  case 'n':	no_inverse_lookup = TRUE;
				break;
		}
	}

	if ( outfile != 0 )
		ofp = fopen(outfile, "w");
	if ( ofp == 0 )
		ofp = stdout;

	if ( gen_HTTP )
		printf("Content-type: text/plain\n\n");

#if _WIN32
	init_winsock(verbose, winsock_level);
#endif

	/*----------------------------------------------------------------
	 * Run through the rest of the command line parsing the various
	 * target specifications. Each one specifies one or more targets
	 * that we're to scan, and each one gets linked into the end of
	 * the current chain.
	 */
	while ( optind < argc )
	{
	const char		*arg = argv[optind++];

		if ( ! parse_target_range(arg, errbuf) )
			die("ERROR: invalid target specification: %s", errbuf);

		have_targets++;
	}

	if ( ! have_targets )
		Usage(argv[0]);

	sockfd = socket(PF_INET, SOCK_DGRAM, 0);

	if ( ! SOCKET_IS_VALID(sockfd) )
	{
		die("ERROR: cannot create socket [%s]", NATIVE_ERROR);
	}

	/*----------------------------------------------------------------
	 * Some systems require that we specifically enable broadcast on
	 * the local network: do so if requested. It doesn't seem to work
	 * very well.
	 */
#if ENABLE_BROADCAST
	if ( broadcast )
	{
	int	b = 1;
	int	rc;

		rc = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
				(char *)&b,
				sizeof b);

		if ( rc != 0 )
		{
			die("ERROR: can't set SO_BROADCAST [%s]", NATIVE_ERROR);
		}
	}
#endif

	/*----------------------------------------------------------------
	 * Bind the local endpoint to receive our responses. If we use a
	 * zero, the system will pick one for us, or we can pick our own
	 * if we wish to make it easier to get past our firewall.
	 */
	memset(&myaddr, 0, sizeof myaddr);

	myaddr.sin_family      = AF_INET;
	myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	myaddr.sin_port        = htons(bind_portno);

	if ( bind_in(sockfd, &myaddr) != 0 )
#ifdef _WIN32
		die("ERROR: cannot bind to local socket [%ld]",
			WSAGetLastError());
#else
		die("ERROR: cannot bind to local socket [%s]", strerror(errno));
#endif

	if ( verbose )
	{
		fprintf(ofp, "Bound to %s.%d\n", inet_ntoa( myaddr.sin_addr),
						ntohs( myaddr.sin_port));
	}

#ifdef ENABLE_PERL
	if ( gen_Perl ) start_perl(ofp, argv);
#endif

	query_names(ofp, sockfd);

#ifdef ENABLE_PERL
	if ( gen_Perl ) end_perl(ofp);
#endif

#if _WIN32
	WSACleanup();
#endif

	return 0;
}