static void* datagram_client_blocking_thread(void* arg) { int iloop; test_datagram_arg_t* darg = arg; socket_t* sock = darg->sock; network_address_t* target = darg->target; const network_address_t* address; char buffer[1024] = {0}; size_t send = 973; size_t recv; log_debugf(HASH_NETWORK, STRING_CONST("IO start on socket 0x%llx"), sock); for (iloop = 0; iloop < 512; ++iloop) { log_infof(HASH_NETWORK, STRING_CONST("UDP read/write pass %d"), iloop); EXPECT_EQ(udp_socket_sendto(sock, buffer, send, target), send); recv = udp_socket_recvfrom(sock, buffer, send, &address); EXPECT_EQ(recv, send); EXPECT_TRUE(network_address_equal(target, address)); thread_yield(); } log_infof(HASH_NETWORK, STRING_CONST("IO complete on socket 0x%llx"), sock); return 0; }
void send_udp(char* data) { uip_ipaddr_t ipaddr; char* ipEnd; char* portEnd; uint16_t port; int i; char found; // Extract IP address ipEnd = strchr((const char*) data, ' '); ipEnd[0]=0; uiplib_ipaddrconv(data, &ipaddr); portEnd = strchr((const char*) ipEnd + 1, ' '); portEnd[0]=0; port = atoi(ipEnd + 1); //printf("Port target : %i\n",port); //uip_ip6addr(&ipaddr,0xFE80,0000,0000,0000,0x3E97,0x0EFF,0xFE00,0x0001 ); int res = udp_socket_sendto(&client_sock, portEnd+1, strlen(portEnd+1), &ipaddr, port); }
static int blast_client_send_handshake(blast_client_t* client, blast_reader_t* reader) { int iaddr, addrsize = 0; socket_t** socks; const network_address_t** addrarr; packet_handshake_t packet; packet.type = PACKET_HANDSHAKE; packet.token = 0; packet.timestamp = blast_timestamp(client->begin_send); packet.datasize = reader->size; packet.seq = blast_seq(client->seq++); packet.namesize = (uint32_t)string_copy(packet.name, sizeof(packet.name), STRING_ARGS(reader->name)).length; if (client->target) { addrarr = &client->target; socks = &client->sock; addrsize = 1; } else { addrarr = (const network_address_t**)client->address; socks = client->socks; addrsize = array_size(addrarr); } for (iaddr = 0; iaddr < addrsize; ++iaddr) { udp_socket_sendto(socks[iaddr], &packet, sizeof(packet_handshake_t) - (PACKET_NAME_MAXSIZE - packet.namesize) + 1, addrarr[iaddr]); #if BUILD_ENABLE_LOG { char buffer[NETWORK_ADDRESS_NUMERIC_MAX_LENGTH]; string_t addr = network_address_to_string(buffer, sizeof(buffer), addrarr[iaddr], true); log_infof(HASH_BLAST, STRING_CONST("Sent handshake to %.*s (seq %lld, timestamp %lld)"), STRING_FORMAT(addr), packet.seq, (tick_t)packet.timestamp); } #endif } client->last_send = time_current(); return 0; }
static void* datagram_server_blocking_thread(void* arg) { int iloop; const network_address_t* from; socket_t* sock = (socket_t*)arg; char buffer[1024]; for (iloop = 0; iloop < 512 * 4; ++iloop) { log_infof(HASH_NETWORK, STRING_CONST("UDP mirror pass %d"), iloop); size_t read = udp_socket_recvfrom(sock, buffer, sizeof(buffer), &from); EXPECT_EQ(read, 973); EXPECT_EQ(udp_socket_sendto(sock, buffer, read, from), read); thread_yield(); } log_infof(HASH_NETWORK, STRING_CONST("IO complete on socket 0x%llx"), sock); return 0; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(multicast_example_process, ev, data) { PROCESS_BEGIN(); /* Create a linkl-local multicast addresses. */ uip_ip6addr(&addr, 0xff02, 0, 0, 0, 0, 0, 0x1337, 0x0001); /* Join local group. */ if(uip_ds6_maddr_add(&addr) == NULL) { printf("Error: could not join local multicast group.\n"); } /* Register UDP socket callback */ udp_socket_register(&s, NULL, receiver); /* Bind UDP socket to local port */ udp_socket_bind(&s, PORT); /* Connect UDP socket to remote port */ udp_socket_connect(&s, NULL, PORT); while(1) { /* Set up two timers, one for keeping track of the send interval, which is periodic, and one for setting up a randomized send time within that interval. */ etimer_set(&periodic_timer, SEND_INTERVAL); etimer_set(&send_timer, (random_rand() % SEND_INTERVAL)); PROCESS_WAIT_UNTIL(etimer_expired(&send_timer)); printf("Sending multicast\n"); udp_socket_sendto(&s, "hello", 6, &addr, PORT); PROCESS_WAIT_UNTIL(etimer_expired(&periodic_timer)); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void send(void *ptr) { struct propple_socket *s = ptr; struct propple_msg m; uip_ipaddr_t addr; m.h.seqno = s->seqno; m.h.datalen = s->datalen; memcpy(m.data, s->data, s->datalen); uip_create_linklocal_allnodes_mcast(&addr); printf("propple-socket: send sending\n"); if(udp_socket_sendto(&s->s, &m, sizeof(struct propple_hdr) + s->datalen, &addr, s->port) == 0) { printf("propple-socket: send: failed\n"); } else { printf("propple-socket: send: sending %d bytes\n", sizeof(struct propple_hdr) + s->datalen); } }
static int blast_client_send_data_chunk(blast_client_t* client, uint64_t seq) { packet_payload_t packet; void* data; uint64_t res; packet.type = PACKET_PAYLOAD; packet.token = client->token; packet.timestamp = blast_timestamp(client->begin_send); packet.seq = seq; data = client->readers[client->current]->map(client->readers[client->current], packet.seq * PACKET_CHUNK_SIZE, PACKET_CHUNK_SIZE); if (!data) { log_errorf(HASH_BLAST, ERROR_SYSTEM_CALL_FAIL, STRING_CONST("Unable to map source segment at offset %lld"), packet.seq * PACKET_CHUNK_SIZE); return BLAST_ERROR_UNABLE_TO_READ_FILE; } memcpy(packet.data, data, PACKET_CHUNK_SIZE); client->readers[client->current]->unmap(client->readers[client->current], data, packet.seq * PACKET_CHUNK_SIZE, PACKET_CHUNK_SIZE); /* #if BUILD_ENABLE_LOG char* addr = network_address_to_string( client->target, true ); log_infof( HASH_BLAST, "Send payload to %s (seq %lld, timestamp %lld) token %d (file %d/%d)", addr, packet.seq, (tick_t)packet.timestamp, packet.token, client->current + 1, array_size( client->readers ) ); string_deallocate( addr ); #endif */ size_t payload_size = sizeof(packet_payload_t); res = udp_socket_sendto(client->sock, &packet, payload_size, client->target); return (res == payload_size ? 0 : -1); }
static void rxtx_loop() { int ret, send_ret; struct sockaddr_in peer_addr; char peer_ip[32]; uint16_t peer_port; while (!the_working_paras.need_exit) { ret=fd_readable(the_working_paras.sockfd, 0, 10000); if (ret<=0) { if (ret<0 && !the_working_paras.no_verbose) ERR_DBG_PRINT("test fd failed"); continue; } ret=udp_socket_recvfrom(the_working_paras.sockfd ,the_working_paras.buf ,sizeof(the_working_paras.buf) ,&peer_addr); INC_STAT(the_stat_data.rx_times_total); if (ret>0) { INC_STAT(the_stat_data.rx_pkts); INC_STAT_VALUE(the_stat_data.rx_bytes, ret); if (!the_working_paras.no_verbose) { resolve_sockaddr(&peer_addr, peer_ip, sizeof(peer_ip), &peer_port); printf("[%"PRIu64"]got %d bytes from %s:%d\n" ,the_stat_data.rx_pkts ,ret ,peer_ip ,(int)peer_port); if (!the_working_paras.no_prt_pkt) { printf("the data contents is:\n"); print_mem(the_working_paras.buf, ret); } } if (!the_working_paras.only_recv) { if (!the_working_paras.no_verbose) { printf("try send back %d bytes to %s:%d\n" ,ret ,peer_ip ,(int)peer_port); } send_ret=udp_socket_sendto(the_working_paras.sockfd, the_working_paras.buf, ret, &peer_addr); INC_STAT(the_stat_data.tx_pkts_total); INC_STAT_VALUE(the_stat_data.tx_bytes_total, ret); if (send_ret==ret) { INC_STAT(the_stat_data.tx_pkts_succ); INC_STAT_VALUE(the_stat_data.tx_bytes_succ, ret); } else if (send_ret<=0) { INC_STAT(the_stat_data.tx_pkts_fail); INC_STAT_VALUE(the_stat_data.tx_bytes_fail, ret); if (!the_working_paras.no_verbose) ERR_DBG_PRINT("tx failed"); } else DBG_PRINT_QUIT("abnormal event: udp packet was partly sent!"); } } else { INC_STAT(the_stat_data.rx_times_fail); if (!the_working_paras.no_verbose) ERR_DBG_PRINT("rx failed"); } printf("\n\n"); continue; } }