Ejemplo n.º 1
0
void attacker_send_split(havege_state *havege_state, void* socket)
  //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker);
  //@ ensures  attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
{
  int temp;
  int size1;
  int size2;
  char buffer[MAX_MESSAGE_SIZE];

  //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
  size1 = net_recv(socket, buffer, MAX_MESSAGE_SIZE);
  if (size1 <= 0)
  {
    //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
    return;
  }
  //@ assert chars(buffer, size1, ?cs);

  //@ close_havege_util(pub, pred, attacker);
  r_int_with_bounds(havege_state, &temp, 0, size1);
  //@ open_havege_util(pub, pred, attacker);
  size2 = temp;
  net_send(socket, buffer, (unsigned int) (size2));
  net_send(socket, (void*) buffer + size2,
            (unsigned int) (size1 - size2));

  //@ chars_join(buffer);
  //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
}
Ejemplo n.º 2
0
Archivo: dll.c Proyecto: icharge/cncnet
int WINAPI fake_sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
{
#ifdef _DEBUG
    printf("sendto(s=%d, buf=%p, len=%d, flags=%08X, to=%p, tolen=%d)\n", s, buf, len, flags, to, tolen);
#endif

    if (to->sa_family == AF_IPX)
    {
        struct sockaddr_in to_in;

        if (dedicated)
        {
            if (is_ipx_broadcast((struct sockaddr_ipx *)to))
            {
                net_write_int8(my_p2p ? 1 : 0);
                net_write_int32(0xFFFFFFFF);
                net_write_int16(0xFFFF);
                net_write_data((void *)buf, len);
                net_send(&server);
            }
            else
            {
                ipx2in((struct sockaddr_ipx *)to, &to_in);

                /* use p2p only if both clients are in p2p mode */
                if (to_in.sin_zero[0] && my_p2p) {
                    net_write_data((void *)buf, len);
                    net_send(&to_in);
                } else {
                    net_write_int8(my_p2p ? 1 : 0);
                    net_write_int32(to_in.sin_addr.s_addr);
                    net_write_int16(to_in.sin_port);
                    net_write_data((void *)buf, len);
                    net_send(&server);
                }
            }

            return len;
        }

        ipx2in((struct sockaddr_ipx *)to, &to_in);
        to_in.sin_port = htons(5000); // forcing the destination port
        net_write_data((void *)buf, len);

        /* check if it's a broadcast */
        if (is_ipx_broadcast((struct sockaddr_ipx *)to))
        {
            net_send(&server);
            return len;
        }
        else
        {
            return net_send(&to_in);
        }
    }

    return sendto(s, buf, len, flags, to, tolen);
}
Ejemplo n.º 3
0
void net_showoutput(mDBF *mDB, char *s) {
    if (netsocket) {
        if (mDB) {
            char	buf[551];
            sprintf(buf,"%lu|%s",mDB->recno,s);
            net_send(netsocket,buf);
        } else
            net_send(netsocket,s);
    }
}
Ejemplo n.º 4
0
Archivo: client.c Proyecto: rsenn/tichu
/* -------------------------------------------------------------------------- *
 * Nimmt Eingaben eines Input Widgets entgegen und sendet sie an den Server.  *
 * Es kann sich dabei um eine Nachricht an das aktuelle Spiel, an einen Mit-  *
 * spieler oder um einen Befehl handeln. Letzterer beginnt mit /              *
 * -------------------------------------------------------------------------- */
void client_message(char *msg)
{
  if(msg[0] == '/')
  {
    net_send("%s", &msg[1]);
  }
  else
  {
    net_send("msg %s :%s", client_target, msg);
  }
}
Ejemplo n.º 5
0
int main(int argc, char ** argv)
{

    SDLS_init("test");
    net_init("127.0.0.1",5000,5001);
    TTF_Font * ttfFont2= SDLS_loadFont("fonts\\arial.ttf",15);
    SDL_Texture *texture_text3= SDLS_loadText(ttfFont2,"utiliser les flèches du clavier...", 0,0,0);
    int quit=false;
    SDL_Event event;
    // handle events
    while (!quit)
    {
        SDL_WaitEvent(&event); // c'est bloquant...

        switch (event.type)
        {
        case SDL_QUIT:
            quit = true;
            break;
         case SDL_KEYDOWN:       // sur l'utilisation du clavier on bouge la forme
            switch (event.key.keysym.sym)
            {
            case SDLK_LEFT:
				net_send("L");
                break;
            case SDLK_RIGHT:
				net_send("R");
                break;
            case SDLK_UP:
				net_send("U");
                break;
            case SDLK_DOWN:
				net_send("D");
                break;
            }
        }

// couleur du fond en blanc
        SDLS_eraseWithBackgroundColor(255,255,255);
        SDLS_copyTexture(texture_text3,SDLS_getScreenWidth()-200,SDLS_getScreenHeight()-30);
// test de modification des pixels, ici on assombrit toute l'image.
        // sombre();
//affichage du rendeder
        SDLS_displayAll();
    }
    // cleanup SDL
    SDL_DestroyTexture(texture_text3);
//Remove timer in case the call back was not called
    SDLS_cleanup();
    return 0;
}
Ejemplo n.º 6
0
static void 
_proc_brd_isreg(struct msg_ac_brd_t *msg, int len, int proto)
{
	if(!__uuid_equ(&msg->header.acuuid[0], &sysstat.acuuid[0])) {
		if(__uuid_equ(&msg->takeover[0], &sysstat.acuuid[0])) {
			if(sysstat.sock >= 0) {
				close(sysstat.sock);
				sysstat.sock = -1;
			}
			_proc_brd(msg, len, proto);
		} else {
			/* tell the broadcast ac, ap have reg in other ac */
			struct msg_ap_resp_t *resp = 
				malloc(sizeof(struct msg_ap_resp_t));
			if(resp == NULL) {
				sys_warn("Malloc for response failed:%s\n", 
					strerror(errno));
				return;
			}
			fill_msg_header(resp, MSG_AP_RESP, 
				msg->header.acuuid, 
				new_random(msg->header.mac));

			/* calculate chap */
			chap_fill_msg_md5(resp, sizeof(*resp), 
				msg->header.random);
			net_send(proto, -1, &msg->header.mac[0], 
				(void *)resp, sizeof(struct msg_ap_resp_t));
			free(resp);
		}
	}
}
Ejemplo n.º 7
0
void handle_client(FILE *fp, int sockfd, char *cmd)
{
   int n;
   char sendline[MAXLINE];
   char recvline[MAXLINE+1];
   int quit = 0;

   while (!quit) {
      if (cmd) {
	 strcpy(sendline, cmd);       /* one shot command */
	 quit = 1;
      } else if (fgets(sendline, MAXLINE, fp) == NULL) {
	 break;
      }
      n = strlen(sendline);
      if (net_send(sockfd, sendline, n) != n)
         error_abort("handle_client: write error on socket");

      while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
	  recvline[n] = 0;
	  fputs(recvline, stdout);
      }
      if (n < 0) {
	 char msg[200];
         sprintf(msg, "handle_client: net_recv error: %s\n", strerror(-n));
	 error_abort(msg);
     }
   }
}
Ejemplo n.º 8
0
int handle_input(const char* buffer, int rcvlen, int fd, int is_conn)
{
	int len, v;
	const char *policy   = "<policy-file-request/>";
	const char *response = "<?xml version=\"1.0\"?>"
				"<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">"
				"<cross-domain-policy>"
				"<site-control permitted-cross-domain-policies=\"all\"/>"
				"<allow-access-from domain=\"*\" to-ports=\"*\" />"
				"</cross-domain-policy>";

	if (rcvlen < 4) return 0;
	v = *(uint32_t *)buffer;
	if (is_conn) {
		if (rcvlen == 23 && !memcmp (buffer, policy, 23)) {
			net_send (fd, (uint8_t*)response, strlen(response) + 1, 1);
			epi.fds[fd].cb.recvlen = 0;

			TRACE_LOG("Policy Req [%s] Received, Rsp [%s] Sent", policy, response);
			return 0;
		}
		len = ntohl (v);
		v = sizeof(protocol_t);
	} else {
		len = v;
		v = sizeof (server_proto_t);
	}

	if ((len > RCVBUFSZ) || (len < v)) {
		return -1;
	}

	return len;
}
Ejemplo n.º 9
0
void send_trans_multi_result_to_php(trans_multi_t* atm)
{
	char buff[10240];
	int j = sizeof(protocol_t);
	//PKG_H_UINT32(buff, atm->key_albumid, j);
	PKG_H_UINT32(buff, atm->new_key_albumid, j);
	PKG_H_UINT32(buff, atm->file_cnt, j);
	int loop;
	for (loop = 0; loop < atm->file_cnt; loop++) {
		file_info_t* lfi = &atm->fi[loop];
		if (!lfi->ret) {
			create_lloccode(lfi->lloccode, lfi->sth[0].new_thumb_id, lfi->sth[1].new_thumb_id, 
				lfi->sth[2].new_thumb_id, lfi->sth[3].new_thumb_id);
		}
		PKG_STR(buff, lfi->lloccode, j, LLOCCODE_LEN);
		PKG_H_UINT32(buff, lfi->ret, j);
		PKG_H_UINT32(buff, lfi->thumb_cnt, j);
		
		DEBUG_LOG("TRAN FILE\t[%u %u %s %u]", loop, lfi->ret, lfi->lloccode, lfi->ret);
		int lp;
		for (lp = 0; lp < lfi->thumb_cnt; lp++) {
			DEBUG_LOG("TRAN THUMB\t[%u %u %u]", lp, lfi->sth[lp].thumb_id, lfi->sth[lp].new_thumb_id);
			if (lfi->ret)
				PKG_H_UINT32(buff, lfi->sth[lp].thumb_id, j);
			else 
				PKG_H_UINT32(buff, lfi->sth[lp].new_thumb_id, j);
		}
	}
	DEBUG_LOG("TRANS MULTI OK\t[%u %u %u]", atm->userid, atm->new_key_albumid, atm->file_cnt);
	init_proto_head(buff, atm->userid, atm->cmd, j);
	net_send(atm->fd, buff, j);
}
Ejemplo n.º 10
0
void attacker_send_random(havege_state *havege_state, void* socket)
  //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker);
  //@ ensures  attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
{
  int temp;
  int size;
  char buffer[MAX_MESSAGE_SIZE];

  //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
  //@ close_havege_util(pub, pred, attacker);
  r_int_with_bounds(havege_state, &temp, MIN_RANDOM_SIZE, MAX_MESSAGE_SIZE);
  size = temp;
  r_int_with_bounds(havege_state, &temp, 0, INT_MAX);
  //@ open_havege_util(pub, pred, attacker);
  //@ close random_request(attacker, temp, false);
  if (havege_random(havege_state, buffer, (unsigned int) size) == 0)
  {
    //@ assert cryptogram(buffer, size, ?ccs, ?cg);
    //@ assert is_bad_nonce_is_public(?proof, pub, pred);
    //@ proof(cg);
    //@ public_cryptogram(buffer, cg);
    net_send(socket, buffer, (unsigned int) size);
  }
  //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
}
Ejemplo n.º 11
0
	void Connection::sendout()
	{
		assert(this->send_buffer.current >= 0 && this->send_buffer.size >= this->send_buffer.current);

	    while(this->send_buffer.current > 0)
	    {
			printf("send %lu",send_buffer.current);
		    ssize_t n = net_send(this, &this->send_buffer.data[0], this->send_buffer.current);
		    if (n == SOCKET_ERROR)
		    {
			    this->disable();
			    break;
		    }

		    size_t size = (size_t)n;

		    if (size == 0)
		    {
			    /* would block */
			    break;
		    }

		    if (this->send_buffer.current > size)
		    {
			    memmove(&this->send_buffer.data[0], &this->send_buffer.data[size], this->send_buffer.current - size);
		    }

		    this->send_buffer.current -= size;
	    }
	}
Ejemplo n.º 12
0
void send()
{
  uint32_t sleep_time = 1000;
  uint8_t port;
  net_event_t *event;

#ifdef PLATFORM_MICA2
  com_ioctl_IFACE_RADIO(CC1000_TX_POWER, 0x00);
#endif
#ifdef PLATFORM_TELOSB
   com_ioctl_IFACE_RADIO(CC2420_TX_POWER, 0x00);
#endif

  event = (net_event_t *)mybuf.data;
  event->from =  mos_node_id_get();
  event->to = 0;
  event->event = 9;
  port = 40;
  
  while (1) {
     mybuf.size=6;
     net_send(&mybuf, MST_PROTO_ID, port, true, MST_DATA, 0);
     mos_led_toggle (1);
     mos_thread_sleep (sleep_time);
  }
}
Ejemplo n.º 13
0
int send_data(uint8_t* buf, int len, int flag)
{
	int ret_of_net_send;
	//struct timeval start,end;
	uint32_t wait_secs = 1;
	while(db_server_set[0].fd == -1)
	{
		sleep(wait_secs);
		db_server_set[0].fd = connect_to_svr(
			db_server_set[0].ip, db_server_set[0].port, 8*1024, 1);
		wait_secs = (wait_secs > 128) ?1 :wait_secs*2 ;
	}
	if (db_server_set[0].fd == -1)
		ERROR_RETURN(("cant connect to db server\t"), -1);
	if (flag >= 0x1214 && flag <= 0x1215)
		return 0;
	if (flag >= 0x1211 && flag <= 0x1213)
		DEBUG_LOG("\tSEND IDC:%lu",++send_idc);
	else if (flag == 0x1001 || flag ==0x1002 || flag == 0x1018)
		DEBUG_LOG("\tSEND TMP:%lu",++send_tmp);

	pthread_spin_lock(&lock_net_send);

   total_send ++;
	ret_of_net_send = net_send(db_server_set[0].fd, buf, len);

	pthread_spin_unlock(&lock_net_send);
    
	if( ret_of_net_send == 0)
		return 0;
	else
		ERROR_RETURN(("send data failed\t"), -1);
}
Ejemplo n.º 14
0
int main(int argc, char *argv[]) {
#ifdef TCP
 int s = net_connect_tcp(NULL,"2334",0);
#else
 int s = net_connect_udp("2334",0);
#endif
 if (s < 0) {
  printf("Failed to connect (%d)\n",s);
  return 0;
 }
 printf("Connected to %d\nSending: %s\n",s,"Hello World");
 int r;
 char *msg = "Hello World";
 if (r = net_send(s,msg,strlen(msg)+1) < 0) {
  printf("Send error (%d, %d)\n",r,errno);
  return 0;
 }
 char *str;
 if ((str = net_receive(s)) == NULL)
  printf("Receive error (%d)\n",errno);
 else
  printf("Received: %s\n",str);
 closesocket(s);
 printf("Session terminated\n");
}
Ejemplo n.º 15
0
extern int net_send_packet(int sock, t_packet const * packet, unsigned int * currsize)
{
    unsigned int size;
    int          addlen;

    if (!packet) {
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL packet (closing connection)",sock);
	return -1;
    }

    if (!currsize) {
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL currsize (closing connection)",sock);
	return -1;
    }

    if ((size = packet_get_size(packet))<1) {
	eventlog(eventlog_level_error,__FUNCTION__,"[%d] packet to send is empty (skipping it)",sock);
	*currsize = 0;
	return 1;
    }

    addlen = net_send(sock,packet_get_raw_data_const(packet,*currsize),size-*currsize);

    if (addlen <= 0) return addlen;

    *currsize += addlen;
    /* sent all data in this packet? */
    if (size==*currsize)
    {
	*currsize = 0;
	return 1;
    }

    return 0;
}
Ejemplo n.º 16
0
static inline bool send_packet(const char *name,
			       struct net_context *ctx,
			       int ipsum_len,
			       int pos)
{
	struct net_buf *buf;
	bool fail = false;

	buf = ip_buf_get_tx(ctx);
	if (buf) {
		uint8_t *ptr;
		int sending_len = ipsum_len - pos;

		ptr = net_buf_add(buf, sending_len);
		memcpy(ptr, lorem_ipsum + pos, sending_len);
		sending_len = buf->len;

		if (net_send(buf) < 0) {
			PRINT("%s: sending %d bytes failed\n",
			      __func__, sending_len);
			ip_buf_unref(buf);
			fail = true;
			goto out;
		} else {
			PRINT("%s: sent %d bytes\n", __func__,
			      sending_len);
		}
	}

out:
	return fail;
}
Ejemplo n.º 17
0
void attacker_send_concatenation(havege_state *havege_state, void* socket)
  //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker);
  //@ ensures  attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
{
  int size1;
  int size2;
  char buffer1[MAX_MESSAGE_SIZE];
  char buffer2[MAX_MESSAGE_SIZE];
  char buffer3[MAX_MESSAGE_SIZE];

  //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker);

  size1 = net_recv(socket, buffer1, MAX_MESSAGE_SIZE);
  size2 = net_recv(socket, buffer2, MAX_MESSAGE_SIZE);
  if (size1 <= 0 || size2 <= 0 || MAX_MESSAGE_SIZE - size1 <= size2)
  {
    //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
    return;
  }

  //@ chars_to_crypto_chars(buffer1, size1);
  memcpy(buffer3, buffer1, (unsigned int) size1);
  //@ chars_to_crypto_chars(buffer2, size2);
  memcpy((char*) buffer3 + size1, buffer2, (unsigned int) size2);
  //@ crypto_chars_join(buffer3);
  //@ crypto_chars_to_chars(buffer3, size1 + size2);
  net_send(socket, buffer3, (unsigned int) (size1 + size2));
  //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
  //@ crypto_chars_to_chars(buffer1, size1);
  //@ crypto_chars_to_chars(buffer2, size2);
}
Ejemplo n.º 18
0
void attacker_send_hash(havege_state *havege_state, void* socket)
  //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker);
  //@ ensures  attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
{
  int temp;
  int size;
  char buffer[MAX_MESSAGE_SIZE];

  //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker);

  size = net_recv(socket, buffer, MAX_MESSAGE_SIZE);
  if (size < MINIMAL_STRING_SIZE)
  {
    //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
    return;
  }
  //@ assert chars(buffer, size, ?pay);

  char hash[64];
  //@ chars_to_crypto_chars(buffer, size);
  //@ HASH_PUB_PAYLOAD(pay)
  sha512(buffer, (unsigned int) size, hash, 0);
  //@ assert cryptogram(hash, 64, ?h_ccs, ?h_cg);
  //@ assert h_cg == cg_hash(cs_to_ccs(pay));
  //@ assert is_hash_is_public(?proof, pub, pred);
  //@ crypto_chars_to_chars(buffer, size);
  //@ public_chars(buffer, size);
  //@ proof(h_cg);
  //@ public_cryptogram(hash, h_cg);
  net_send(socket, hash, 64);

  //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker);
}
Ejemplo n.º 19
0
int main(int argc, char** argv)
{
    net_init();
    net_send();
    net_recv();
    return 0;
}
Ejemplo n.º 20
0
int
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	net_connection_t nc;
	struct torrent *t;
	char *buf;
	int ret = -1;

	buf = cbuf2buf(cbid, sz);
	if (!buf)           return -EINVAL;
	if (tor_isnull(td)) return -EINVAL;

	NET_LOCK_TAKE();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	assert(t->data);
	nc = (net_connection_t)t->data;
	ret = net_send(spdid, nc, buf, sz);
done:
	NET_LOCK_RELEASE();
	assert(lock_contested(&net_lock) != cos_get_thd_id());
	return ret;
}
Ejemplo n.º 21
0
static size_t mcp_send_packet(mcp_packet_t packet) {
	if (invoke_packet_handlers(MCP_SENT, &packet) == BLOCK_PACKET) {
		return packet.len; // pretend we sent it
	}

	if (setting("Verbose")->b_var) {
		ui_console_lock();

		print("[MCP] sent:\n\n");

		mcp_dump_packet(packet);

		print("\n");

		ui_console_unlock();
	}

	byte buf[packet.len];

	net_set_data(buf, packet.len, 0, word);
	net_set_data(buf, packet.id, sizeof(word), byte);
	memcpy(buf + sizeof(word) + sizeof(byte), packet.data, packet.len
			- MCP_HEADER_SIZE);

	size_t sent = net_send(mcp_socket, buf, packet.len);

	/*if (sent == packet.len) {
		invoke_packet_handlers(MCP_SENT, &packet);
	}*/

	return sent;
}
Ejemplo n.º 22
0
Archivo: plasma.c Proyecto: LEDPi/LEDPi
void MsgPlasma_Change(void *sender, uint16_t fc, uint16_t len, uint8_t data[])
{
	if(!plasma_initialized) return;

	if(len==0)
	{
		//Send current plasma settings to client

		uint8_t data_to_send[16];

		//flag byte

		uint8_t flags = 0; 

		if(plasma_invert) flags += 128;
		flags += (plasma_method<<2);
		flags += plasma_color;
		
		data_to_send[0] = flags;

		float tmp;
		
		WRITE(&data_to_send[1],plasma_concentric_scale);
		WRITE(&data_to_send[5],plasma_concentric_speed);
		WRITE(&data_to_send[9],plasma_period);
		WRITE(&data_to_send[13],plasma_step_size);

		net_send(sender,fc|0x8000,16,data_to_send);
	}
	else if(len==16)
	{
		//Set new plasma settings
		
		//flag byte
		if(data[0]>=0x80) plasma_invert = true;
		else plasma_invert = false;

		plasma_method = (plasma_method_t)((data[0]&0x1C)>>2);
		plasma_color = (plasma_color_t)(data[0]&0x03);

		float tmp;

		//concentric scale
		memcpy((void*) &tmp, &data[1],4);
		plasma_concentric_scale = (plasma_var_t) tmp;
		
		// concentric speed
		memcpy((void*) &tmp, &data[5],4);
		plasma_concentric_speed = (plasma_var_t) tmp;

		//period
		memcpy((void*) &tmp, &data[9],4);
		plasma_period = (plasma_var_t) tmp;
		
		//speed
		memcpy((void*) &tmp, &data[13],4);
		plasma_step_size = (plasma_var_t) tmp;
	}
	else
	{
Ejemplo n.º 23
0
int WifiGecko_Send(const char * data, int datasize)
{
	if(WifiGecko_Connect() < 0)
		return connection;

	int ret = 0, done = 0, blocksize = 1024;

	while (done < datasize)
	{
		if(blocksize > datasize-done)
			blocksize = datasize-done;

		ret = net_send(connection, data+done, blocksize, 0);
		if (ret < 0)
		{
			WifiGecko_Close();
			return ret;
		}
		else if(ret == 0)
		{
			break;
		}

		done += ret;
		usleep (1000);
	}

	return ret;
}
Ejemplo n.º 24
0
int socket_send(unsigned char *buff, unsigned int bytes) {
	int sent = net_send(sock.sock, buff, bytes);
	if (sent != bytes) {
		warning("%s: failed for TCP connection to host '%s' on port %d:\n\t%s\n", sock.host, sock.port, strerror(errno));
	}
	return sent;
}
Ejemplo n.º 25
0
/* connect:
 *  Set the target address and send the first connection request.  This
 *  might not get through of course; later we can just repeat the send
 *  statement because the target address is already set.  No need to store
 *  it anywhere.
 */
static int connect (NET_CONN *conn, const char *target)
{
	int id;
	static int next_id = 0;
	struct conn_data_t *data = conn->data;

	/* The id is a compound of the current time (with 1 second 
	 * granularity), and an increasing counter.  The time is needed
	 * because the counter resets when you restart the program, and 
	 * the counter is needed because of the granularity of the time. */

	/* Strictly, this line needs a mutex, but there's nowhere around 
	 * here we can call `MUTEX_CREATE'. */
	id = (next_id++ << 16) + (time(NULL) & 0xffff);

	strcpy (data->connect_string, "connect");
	data->connect_string[8] = (id >> 24) & 0xff;
	data->connect_string[9] = (id >> 16) & 0xff;
	data->connect_string[10] = (id >> 8) & 0xff;
	data->connect_string[11] = id & 0xff;
	
	if (net_assigntarget (data->chan, target)) return 1;
	if (net_send (data->chan, data->connect_string, 12)) return 2;
	data->connect_timestamp = __libnet_timer_func();
	return 0;
}
Ejemplo n.º 26
0
int send_request_to_db(userid_t id, uint16_t cmd, const void* body_buf, int body_len)
{
	assert(body_len >= 0);

	static uint8_t dbbuf[pkg_size];

	if (proxysvr_fd == -1) {
		proxysvr_fd = connect_to_service(config_get_strval("dbproxy_ip"), 0, 65535, 1);
	}

	uint32_t len = sizeof(svr_proto_t) + body_len;
	if ((proxysvr_fd == -1) || 	(len > sizeof(dbbuf))) {
		ERROR_LOG("send to dbproxy failed: fd=%d len=%d", proxysvr_fd, len);
		return -1;
	}

	svr_proto_t* pkg = reinterpret_cast<svr_proto_t*>(dbbuf);
	pkg->len = len;
	pkg->seq = 0;
	pkg->cmd = cmd;
	pkg->ret = 0;
	pkg->id  = id;
	memcpy(pkg->body, body_buf, body_len);

	return net_send(proxysvr_fd, dbbuf, len);
}
Ejemplo n.º 27
0
/*---------------------------------------------------------------------------*/
static int coap_context_send(coap_context_t *ctx, struct net_buf *buf)
{
  int max_data_len, ret;

  max_data_len = IP_BUF_MAX_DATA - UIP_IPUDPH_LEN;

  PRINTF("%s: send to peer data %p len %d\n", __FUNCTION__,
	 ip_buf_appdata(buf), ip_buf_appdatalen(buf));

  if (ip_buf_appdatalen(buf) > max_data_len) {
    PRINTF("%s: too much (%d bytes) data to send (max %d bytes)\n",
	   __FUNCTION__, ip_buf_appdatalen(buf), max_data_len);
    ip_buf_unref(buf);
    ret = -EINVAL;
    goto out;
  }

  ret = net_send(buf);
  if (ret < 0) {
    PRINT("%s: sending %d bytes failed\n", __FUNCTION__,
	  ip_buf_appdatalen(buf));
    ip_buf_unref(buf);
  }

out:
  return ret;
}
Ejemplo n.º 28
0
static void send_data(const char *taskname, struct net_context *ctx)
{
	int len = strlen(lorem_ipsum);
	struct net_buf *buf;

	buf = ip_buf_get_tx(ctx);
	if (buf) {
		uint8_t *ptr;
		uint16_t sent_len;

		ptr = net_buf_add(buf, 0);
		memcpy(ptr, lorem_ipsum, len);
		ptr = net_buf_add(buf, len);
		ptr = net_buf_add(buf, 1); /* add \0 */
		*ptr = '\0';
		sent_len = buf->len;

		if (net_send(buf) < 0) {
			PRINT("%s: %s(): sending %d bytes failed\n",
			      taskname, __func__, len);
			ip_buf_unref(buf);
		} else {
			PRINT("%s: %s(): sent %d bytes\n", taskname,
			      __func__, sent_len);
		}
	}
}
Ejemplo n.º 29
0
int main(int argc, char ** argv)
{
	struct board_cfg * cfg = (struct board_cfg *)(CFG_ADDR);
	uint8_t addr = 1;
	int i;

	if (cfg->magic == CFG_MAGIC)
		addr = cfg->mstp_addr;
	else
		addr = 0;

	stdio_init();

	supervisor_init();

	INF("Starting RS485 test");

	net_start(addr);
	net_probe(true);

	for (i = 0;; ++i) {
		(void)i;
		thinkos_sleep(100);
		net_send(0, "U U U U ", 8);
	}

	return 0;
}
Ejemplo n.º 30
0
//static uint32_t actual_data_len;
int recv_data(char *buf, uint32_t proto, uint32_t special_flag, uint32_t len)
{
	int tag = g_buffer_tag;

	if ((memory_size -(g_buffer_ptr[tag] - g_buffer[tag])) < sizeof(aresult))
		ERROR_RETURN(("MEMORY IS NOT ENOUGH"), 0);

	proto_data_len = len- sizeof(protocol_t);
	//actual_data_len = proto_data_len + sizeof(aresult.type) + sizeof(aresult.special_flag);

	aresult.type = proto;
	aresult.special_flag = special_flag;
	memcpy(&(aresult.project_number), buf, proto_data_len);
	memcpy(g_buffer_ptr[tag], &aresult, sizeof(aresult));

	sum_data_in_mem[tag] ++;
	g_buffer_ptr[tag] += sizeof(aresult);//actual_data_len;

	if (data_forward) {
		if (forward_fd == -1)
			forward_fd = connect_to_svr(forward_ip, forward_port, 1024, 1);
		if (forward_fd != -1) {
			init_proto_head(forward_buffer, len, proto, 0, 0, 0);
			char *ptr = forward_buffer + sizeof(protocol_t);
			memcpy(ptr, buf, proto_data_len);
			net_send(forward_fd, forward_buffer, len);
		}
	}

	return 0;
}