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


  long int sleep_time;
  int server_port;
  char server_ip[] = "000.000.000.000"; 

  
  LOG_OPEN();
  
  if (argc != 5)
  {
    print_help();
    exit(EXIT_SUCCESS);
  }
  else
  {
    // to do: user input validation!
    sensor_id = atoi(argv[1]);
    sleep_time = atoi(argv[2]);
    strncpy(server_ip, argv[3],strlen(server_ip));
    server_port = atoi(argv[4]);
    //printf("%d %ld %s %d\n", sensor_id, sleep_time, server_ip, server_port);
  }
  
  srand48( time(NULL) );
  
  // open TCP connection to the server; server is listening to SERVER_IP and PORT
  client = tcp_active_open( server_port, server_ip );
  
 
   int i=LOOPS;
   signal(SIGINT, my_handler);
  
  
  while(running) 
  {
    temperature = temperature + TEMP_DEV * ((drand48() - 0.5)/10); 
    time(&timestamp);
    // send data to server in this order (!!): <sensor_id><temperature><timestamp>
    // remark: don't send as a struct!
    tcp_send( client, (void *)&sensor_id, sizeof(sensor_id));
    tcp_send( client, (void *)&temperature, sizeof(temperature));
    tcp_send( client, (void *)&timestamp, sizeof(timestamp));
    LOG_PRINTF(sensor_id,temperature,timestamp);
    sleep(sleep_time);
    UPDATE(i);
  }
  
  i = 0;
  
  tcp_close( &client );
  
  LOG_CLOSE();
  
  exit(EXIT_SUCCESS);
}
void my_handler(){
    signal(SIGINT,SIG_DFL);
    printf("sensor_node %d was closed\n",sensor_id);
    running = 0;
    double k = 100.001;
    tcp_send( client, (void *)&sensor_id, sizeof(sensor_id));
    tcp_send( client, &k, sizeof(temperature));
    tcp_send( client, (void *)&timestamp, sizeof(timestamp));
}
Esempio n. 3
0
int QwtPlotWidget::sendJpeg2clipboard()
{
  char buf[80];
  QByteArray bytes;
  QBuffer qb_buffer(&bytes);
  qb_buffer.open(QIODevice::WriteOnly);
  canvas()->paintCache()->save(&qb_buffer, "JPG"); // writes pixmap into bytes in JPG format
  sprintf(buf,"@clipboard(%d,%d)\n", id, (int) qb_buffer.size());
  tcp_send(s,buf,strlen(buf));
  return tcp_send(s, qb_buffer.data(), qb_buffer.size());
}
Esempio n. 4
0
void PvGLWidget::mouseMoveEvent(QMouseEvent *event)
{
  char buf[100];
  sprintf( buf, "QPlotMouseMoved(%d,%d,%d)\n",id, event->x(), event->y());
  tcp_send(s,buf,strlen(buf));
  QOpenGLWidget::mouseMoveEvent(event);
}
Esempio n. 5
0
void www_server_reply_send() {
  // Make sure a packet ends with two newlines to mark end of data
  www_server_reply_add_p(newline);
  www_server_reply_add_p(newline);
  // Send packet
  tcp_send(rlength);
}
Esempio n. 6
0
// ---------------------------------------------------------------------------
// Enviar mensaje por puerto TCP con informacion de configuracion
//
UINT8 TCP_SendCfgBuff(void)
{
  UINT8 i;
  UINT16 len;
  UINT8 * pOutCfgMsg;
  pOutCfgMsg = (UINT8 *)(&CfgMsgFrame);
	 	   
//  while(tcp_checksend(TcpConfigSoch) < 0);
  if (tcp_checksend(TcpConfigSoch) < 0){
    return 0x01;
  }
  
  len = 0;
  for( i=0 ; i < MIN_CFGFRAME_LEN ; i++ ){
	  net_buf[TCP_APP_OFFSET+len] = *pOutCfgMsg;
	  pOutCfgMsg++;
	  len++;
	}

  for( i=0 ; i < CfgMsgFrame.Len ; i++ ){
	  net_buf[TCP_APP_OFFSET+len] = *pOutCfgMsg;
	  pOutCfgMsg++;
	  len++;
	}
    	
  if(tcp_send(TcpConfigSoch, &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. 7
0
/* Send an fast path data PDU */
void
iso_fp_send(rdpIso * iso, STREAM s, uint32 flags)
{
	int fp_flags;
	int len;
	int index;

	fp_flags = (1 << 2) | 0;	/* one event, fast path */
	if (flags & SEC_ENCRYPT)
	{
		fp_flags |= 2 << 6;	/* FASTPATH_INPUT_ENCRYPTED */
	}
	s_pop_layer(s, iso_hdr);
	len = (int) (s->end - s->p);
	out_uint8(s, fp_flags);
	if (len >= 128)
	{
		out_uint16_be(s, len | 0x8000);
	}
	else
	{
		/* copy the bits up to pack and save 1 byte */
		for (index = 3; index < len; index++)
		{
			s->data[index - 1] = s->data[index];
		}
		len--;
		s->end--;
		out_uint8(s, len);
	}

	tcp_send(iso->tcp, s);
}
Esempio n. 8
0
/* Send req and get response */
int http_transaction(http_t *client, http_trans_t *trans)
{
	int rc = 0;

	ASSERT(client);
	ASSERT(trans);

	if (!client->initialized)
		return RC_HTTP_OBJECT_NOT_INITIALIZED;

	trans->rsp_len = 0;
	do {
#ifdef ENABLE_SSL
		if (client->ssl_enabled) {
			TRY(ssl_send(client, trans->p_req, trans->req_len));
			TRY(ssl_recv(client, trans->p_rsp, trans->max_rsp_len, &trans->rsp_len));
		}
		else
#endif
		{
			TRY(tcp_send(&client->tcp, trans->p_req, trans->req_len));
			TRY(tcp_recv(&client->tcp, trans->p_rsp, trans->max_rsp_len, &trans->rsp_len));
		}
	}
	while (0);

	trans->p_rsp[trans->rsp_len] = 0;
	http_response_parse(trans);

	return rc;
}
Esempio n. 9
0
void QwtPlotWidget::enterEvent(QEvent *event)
{
  char buf[100];
  sprintf(buf, "mouseEnterLeave(%d,1)\n",id);
  tcp_send(s,buf,strlen(buf));
  QwtPlot::enterEvent(event);
}
Esempio n. 10
0
static void tcp_estab_handler(void *arg)
{
	struct tcpconn *tc = arg;
	struct le *le = list_head(&tc->ql);
	int err = 0;

	DEBUG_INFO("connection (%J) established\n", &tc->srv);

	while (le) {
		struct dns_query *q = le->data;

		le = le->next;

		q->mb.pos = 0;
		err = tcp_send(tc->conn, &q->mb);
		if (err)
			break;

		DEBUG_INFO("tcp send %J\n", &tc->srv);
	}

	if (err) {
		tcpconn_close(tc, err);
		return;
	}

	tmr_start(&tc->tmr, tc->dnsc->conf.idle_timeout,
		  tcpconn_timeout_handler, tc);
	tc->connected = true;
}
Esempio n. 11
0
/*****************************************************************************
** Function name:		U16 tcpSend (void)
**
** Descriptions:		Envia dados TCP
*****************************************************************************/
U16 tcpSend (char *p, U8 sz,U8 soc,U8 send)
{
	U8 *sendbuf,ip[4];

	switch (tcp_get_state (soc))
	{
		case TCP_STATE_FREE:
		case TCP_STATE_CLOSED:
			if(send == CMD_SEND_TO_SERVER)
			{
				inet_aton((U8*)cfg.tcp.ip_serv_rem,ip);
				tcp_connect(soc,ip,atoi(cfg.tcp.port_serv_rem),atoi(cfg.tcp.port_serv_loc));
			}
			break;

		case TCP_STATE_CONNECT:			
			/* We are connected, send command to remote peer. */
			if(tcp_check_send (soc))
			{
				sendbuf = tcp_get_buf (sz);
				strncpy((char *)sendbuf,(const char *)p,sz);
				if(tcp_send (soc, sendbuf, sz))
					return __TRUE; 	/*Packet Sent*/
				else
					return __FALSE;	/*Send Error*/
			}
		break;
	}
	return __TRUE;
}
Esempio n. 12
0
// ------------------------------------------------
// Function:        smtp_from()
// ------------------------------------------------
// Input:           Sender email address
// Output:          TRUE if succesful
// ------------------------------------------------
// Description:     Adds one MAIL FROM clause
// ------------------------------------------------
BOOL smtp_from(char *s)
{
    PPBUF buf;
    BOOL res;

    if(smtp_state != SMTP_FROM) return FALSE;

    // ----------------------
    // send MAIL FROM command
    // ----------------------
    buf = tcp_new(SOCKET_SMTP);
    if(buf == NULL) return FALSE;

    write_string(buf, "MAIL FROM:<");
    write_string(buf, s);
    write_string(buf, ">\r\n");
    res = tcp_send(SOCKET_SMTP, buf);
    release_buffer(buf);

    if(!res) return FALSE;

    if(!smtp_ok()) return FALSE;
    smtp_state = SMTP_RCPT;
    return TRUE;
}	
Esempio n. 13
0
static void tcp_announce(const char *command, ContextId ctx, const char *arg)
{
	char *msg;
	char *j;
	char *arg2 = strdup(arg);

	for (j = arg2; *j; ++j)
		if ((*j == '\t') || (*j == '\n'))
			*j = ' ';

	if (asprintf(&msg, "%s\t%d:%llu\t%s\n", command, ctx.plugin, ctx.connid, arg2) < 0) {
		free(arg2);
		return;
	}

	printf("%s\n", msg);
	
	LinkedNode *i = tcp_clients()->first;

	while (i) {
		tcp_send(i->element, msg);
		i = i->next;
	}

	free(arg2);
	free(msg);
}
Esempio n. 14
0
int main(int argc, char *argv[])
{
	int s = tcp_listen("0.0.0.0", 445);
	select_group_t *sg = select_group_create();

	if(s < 0)
		return 0;

	select_group_add_socket(sg, s, SOCKET_TYPE_LISTEN, NULL);
	select_set_listen(sg, s, listener);

	/* We start by receiving a header. */
	state = RECV_STATE_HEADER;

	while(1)
	{
		select_group_do_select(sg, 3, 0);
	}
#if 0
	/* Do 'session setup' */
	length = smb_get_length(smb);
	data = safe_malloc(length);
	smb_get(smb, data, length);
	tcp_send(s, data, length);


	while(1)
	{
		select_group_do_select(sg, -1, -1);
	}

	select_group_destroy(sg);
#endif
	return 0;
}
Esempio n. 15
0
static void tcp_estab_handler(void *arg)
{
	struct sip_conn *conn = arg;
	struct le *le;
	int err;

	conn->established = true;

	le = list_head(&conn->ql);

	while (le) {

		struct sip_connqent *qent = le->data;
		le = le->next;

		if (qent->qentp) {
			*qent->qentp = NULL;
			qent->qentp = NULL;
		}

		err = tcp_send(conn->tc, qent->mb);
		if (err)
			qent->transph(err, qent->arg);

		list_unlink(&qent->le);
		mem_deref(qent);
	}
}
Esempio n. 16
0
bool
tcp_test(int argc, char **argv)
{
    int sockfd;
    int port = 80;
    int len = 0;

    char *host = "localhost";
    char *buffer = "GET / HTTP/1.0\r\n\r\n";
    char *getbuf = NULL;

    if(!tcp_connect(host, port, &sockfd))
        return false;


    if(!tcp_send(sockfd, buffer, 5000, &len))
        return false;
    
    printf("sent %d bytes\n", len);

    len=0;

    if((getbuf=tcp_recv(sockfd, 5000, 9000, &len)) == NULL)
        return false;

    return true;
}
Esempio n. 17
0
void senddata(struct socket_h *hp, int balance)
{
	buffer[namelen] = ':';
	sprintf(buffer + namelen + 1, "%d\0", balance);
	printf("about to send:%s\n", buffer);
	tcp_send(hp, buffer);
}
Esempio n. 18
0
static BOOL
iso_send_connection_request(RDPCLIENT * This, char *cookie)
{
	STREAM s;
	int cookielen = (int)strlen(cookie);
	int length = 11 + cookielen;

	s = tcp_init(This, length);

	if(s == NULL)
		return False;

	out_uint8(s, 3);	/* version */
	out_uint8(s, 0);	/* reserved */
	out_uint16_be(s, length);	/* length */

	out_uint8(s, length - 5);	/* hdrlen */
	out_uint8(s, ISO_PDU_CR);
	out_uint16(s, 0);	/* dst_ref */
	out_uint16(s, 0);	/* src_ref */
	out_uint8(s, 0);	/* class */

	out_uint8p(s, cookie, cookielen);

	s_mark_end(s);
	return tcp_send(This, s);
}
Esempio n. 19
0
static void estab_handler(void *arg)
{
	struct rst *rst = arg;
	struct mbuf *mb;
	int err;

	re_printf("rst: connection established\n");

	mb = mbuf_alloc(512);
	if (!mb) {
		err = ENOMEM;
		goto out;
	}

	err = mbuf_printf(mb,
			  "GET %s HTTP/1.0\r\n"
			  "Icy-MetaData: 1\r\n"
			  "\r\n",
			  rst->path);
	if (err)
		goto out;

	mb->pos = 0;

	err = tcp_send(rst->tc, mb);
	if (err)
		goto out;

 out:
	if (err) {
		re_printf("rst: error sending HTTP request: %m\n", err);
	}

	mem_deref(mb);
}
Esempio n. 20
0
/* Send req and get response */
RC_TYPE http_client_transaction(HTTP_CLIENT *p_self, HTTP_TRANSACTION *p_tr )
{
	RC_TYPE rc;
	if (p_self == NULL || p_tr == NULL)
	{
		return RC_INVALID_POINTER;
	}

	if (!p_self->initialized)
	{
		return RC_HTTP_OBJECT_NOT_INITIALIZED;
	}

	do
	{		
		rc = tcp_send(&p_self->super, p_tr->p_req, p_tr->req_len);
		if (rc != RC_OK)
		{
			break;
		}

		rc = tcp_recv(&p_self->super, p_tr->p_rsp, p_tr->max_rsp_len, &p_tr->rsp_len);
	}
	while(0);

	return rc;
}
Esempio n. 21
0
File: iso.c Progetto: RPG-7/reactos
static void
iso_send_connection_request(char *username)
{
	STREAM s;
	int length = 30 + strlen(username);

	s = tcp_init(length);

	out_uint8(s, 3);	/* version */
	out_uint8(s, 0);	/* reserved */
	out_uint16_be(s, length);	/* length */

	out_uint8(s, length - 5);	/* hdrlen */
	out_uint8(s, ISO_PDU_CR);
	out_uint16(s, 0);	/* dst_ref */
	out_uint16(s, 0);	/* src_ref */
	out_uint8(s, 0);	/* class */

	out_uint8p(s, "Cookie: mstshash=", strlen("Cookie: mstshash="));
	out_uint8p(s, username, strlen(username));

	out_uint8(s, 0x0d);	/* Unknown */
	out_uint8(s, 0x0a);	/* Unknown */

	s_mark_end(s);
	tcp_send(s);
}
Esempio n. 22
0
void smtpc_sendhelo (void)
{
	INT8 i;
	UINT8* buf;

	/* Fill TCP Tx buffer with "HELO " and use callback function	*/
	/* smtp_getdomain in order to get domain from systems and send	*/
	/* that combined "HELO domainnname" to SMTP server				*/
	
	buf = &net_buf[TCP_APP_OFFSET];
	
	*buf++ = 'H';
	*buf++ = 'E';
	*buf++ = 'L';
	*buf++ = 'O';
	*buf++ = ' '; 
	
	i = smtpc_getdomain(buf);
	
	if(i < 0)
		return;
	
	buf += i;	
	
	/* Insert CRLF	*/
	
	*buf++ = '\r';
	*buf = '\n';
		

	(void)tcp_send(smtp_client.sochandle, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, i + 7);

}
Esempio n. 23
0
int http_stream_send(struct request* request, struct mbuf*mb)
{
    if(request->state == STREAM)
        return tcp_send(request->tcp, mb);

    return -EINVAL;
}
Esempio n. 24
0
bool SendKaypad(JNIEnv* env, jobject thiz, int keyval)
{
	ALI_MSG msg;
	bool ret = false;
    unsigned int msg_snd = 0;
	pthread_mutex_lock(&pplay->m_lock);
	msg.type = (short)ALI_MSG_KEY;
	msg.key = keyval;
	memcpy(&msg_snd, &msg, 4);
	unsigned int send_msg = htonl(msg_snd);
   if(tcp_send(pplay->curstbsock, (char *)&send_msg, sizeof(send_msg), NORMAL_DELAY) == sizeof(send_msg))

	__android_log_print(ANDROID_LOG_INFO, JNIDEC_TAG, "SendKaypad keyval %d\n", keyval);
//	if(tcp_send(pplay->curstbsock, (char *)&msg, sizeof(ALI_MSG), NORMAL_DELAY) == sizeof(ALI_MSG))
	{
		ret=true;
//		if(tcp_recv(pplay->curstbsock, (char *)&msg, 4, NORMAL_DELAY) == 4)
//		{
//			ret = true;
//		}
	}
	pthread_mutex_unlock(&pplay->m_lock);
	__android_log_print(ANDROID_LOG_INFO, JNIDEC_TAG, "SendKaypad %d\n", ret);
	return ret;
}
Esempio n. 25
0
File: cons.c Progetto: GGGO/baresip
static int output_handler(const char *str)
{
	struct mbuf *mb;
	int err = 0;

	if (!str)
		return EINVAL;

	mb = mbuf_alloc(256);
	if (!mb)
		return ENOMEM;

	mbuf_write_str(mb, str);

	if (sa_isset(&cons->udp_peer, SA_ALL)) {
		mb->pos = 0;
		err |= udp_send(cons->us, &cons->udp_peer, mb);
	}

	if (cons->tc) {
		mb->pos = 0;
		err |= tcp_send(cons->tc, mb);
	}

	mem_deref(mb);

	return err;
}
Esempio n. 26
0
void QImageWidget::mousePressEvent( QMouseEvent *e)
{
char buf[80];

  if(e->button() == Qt::LeftButton)
  {
    sprintf(buf,"QPushButton(%d) -xy=%d,%d\n",id, e->x(), e->y());
    tcp_send(s,buf,strlen(buf));
  }
  else if(e->button() == Qt::RightButton)
  {
    sprintf(buf,"QMouseRight(%d)\n",id);
    tcp_send(s,buf,strlen(buf));
  }
  QWidget::mousePressEvent(e);
}
Esempio n. 27
0
void QImageWidget::leaveEvent(QEvent *event)
{
  char buf[100];
  sprintf(buf, "mouseEnterLeave(%d,0)\n",id);
  tcp_send(s,buf,strlen(buf));
  QWidget::leaveEvent(event);
}
Esempio n. 28
0
int telnet_svc_write(struct telnet_svc * tn, 
					 const void * buf, unsigned int len)
{
	struct tcp_pcb * tp = tn->tp;
	char * cp;
	int cnt;
	int rem;
	int pos;

	DCC_LOG1(LOG_MSG, "len=%d", len);

	if (tn->tp == NULL) {
		/* discard if we are not connected */
		return len;
	}

	cp = (char *)buf;
	rem = len;
	cnt = 0;

	/* search for IAC */
	for (pos = 0; (pos < rem) && (cp[pos] != IAC); pos++);

	while (pos < rem) {
		if (cp[pos] == IAC) {
			/* send data until IAC, inclusive */
			if (tcp_send(tp, cp, pos + 1, 0) <= 0)
				return cnt;
		} 
		cnt += pos;
		cp += pos;
		rem -= pos;
		pos = 1;
		/* search for next IAC */
		for (; (pos < rem) && (cp[pos] != IAC); pos++);
	};

	/* send the last chunk */
	if (pos) {
		if (tcp_send(tp, cp, pos, TCP_SEND_NOWAIT) <= 0)
			return cnt;
	}

	cnt += pos;

	return len;
}
Esempio n. 29
0
void send_data(int soc, int event, int len) {
    int i;
    __builtin_quad tmp;
    static int tx_index;
    static char buffer[4][TX_BUF_SIZE];
    
    switch (event) {
        
        case TCP_EVENT_DATA:
            while (link_available() && (tx_index < TX_BUF_SIZE - 128)) {
                link_recv(&tmp);
                if (tx_index < 0 || tx_index >= TX_BUF_SIZE) {
                    tx_index = 0;
                    tx_error_counter++;
                }
                
                link_quad2char(&tmp, 1, &txbuf[tx_index]);
                tx_index += 16;

                if (tx_index >= TX_BUF_SIZE) {
                    tx_index = 0;
                    tx_error_counter++;
                }
            }
            break;
            
        case TCP_EVENT_REGENERATE:
            puts("regenerate");
            if ((tcp_checksend(socket[soc]) != -1) && (len > 0) && (len < TX_BUF_SIZE)) {
                memcpy(&net_buf[TCP_APP_OFFSET], buffer[soc], len);
                tcp_send(socket[soc], &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, len);		
            }
            break;
    }
    
    if (tx_index > 0) {
        for (i = 0; i < 4; ++i) {
            if (tcp_checksend(socket[i]) != -1) {
                memcpy(&net_buf[TCP_APP_OFFSET], txbuf, tx_index);
                memcpy(buffer[i], txbuf, tx_index);
                tcp_send(socket[i], &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, tx_index);		
                tx_index = 0;
                return;
            }
        }
    }
}
Esempio n. 30
0
int syscall_handler(int num, struct syscall_arguments *args)
{
    interrupt_unmask();
    
    switch (num)
    {
        case ADDPROCESS:
            interrupt_mask();
                process_add((void (*)())args->arg1);
            interrupt_unmask();
        break;
        case YIELD:
            interrupt_sleepinsyscall();
        break;
        
        case UDPSEND:
            return udp_output(args->arg1, (struct sockaddr*)args->arg2, 
                (char *)args->arg3, args->arg4);
        break;
        case UDPRECV:
            return udp_recvfrom(args->arg1, (struct sockaddr*)args->arg2, 
                (char *)args->arg3, args->arg4);
        break;
        case UDPSOCKET: 
            return udp_socket();
        break;
        case UDPBIND:
            return udp_bind(args->arg1, (struct sockaddr*)args->arg2);
        break;
        case UDPCLOSE:
            return udp_close(args->arg1);
        break;
        
        case TCPCONNECT:
            return tcp_connect(args->arg1, (struct sockaddr*)args->arg2);
        break;
        
        case TCPSEND:
            return tcp_send(args->arg1, (char *)args->arg2, args->arg3);
        break;
        case TCPRECV:
            return tcp_recv(args->arg1, (char *)args->arg2, args->arg3);
        break;
        case TCPSOCKET: 
            return tcp_socket();
        break;
        case TCPBIND:
            return tcp_bind(args->arg1, (struct sockaddr*)args->arg2);
        break;
        case TCPCLOSE:
            return tcp_close(args->arg1);
        break;
        case TCPLISTEN:
            return tcp_listen(args->arg1);
        break;
    }
    
    return 0;
}