I want to use two BulkSendApplications to send from node_0 to node_1 and from node_2 to node_3.
My code for creating the applications looks the following:
// node_0 to node_1
uint16_t rcv_port = 50000;
PacketSinkHelper sink1 ("ns3::TcpSocketFactory",
Address (InetSocketAddress (Ipv4Address::GetAny (), rcv_port)));
ApplicationContainer sinkApps1 = sink1.Install (node_1);
sinkApps1.Start (MilliSeconds (0));
sinkApps1.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
BulkSendHelper source1 (
"ns3::TcpSocketFactory",
Address (InetSocketAddress (node_1, rcv_port)));
ApplicationContainer sourceApps1 = source1.Install (node_0);
source.SetAttribute ("MaxBytes", UintegerValue (0));
sourceApps1.Start (MilliSeconds (start_sending_traffic));
sourceApps1.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
// node_2 to node_3
uint16_t rcv_port2 = 50001;
PacketSinkHelper sink2 ("ns3::TcpSocketFactory",
Address (InetSocketAddress (Ipv4Address::GetAny (), rcv_port2)));
ApplicationContainer sinkApps2 = sink2.Install (node_3);
sinkApps2.Start (MilliSeconds (0));
sinkApps2.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
BulkSendHelper source2 (
"ns3::TcpSocketFactory",
Address (InetSocketAddress (node_3, rcv_port2)));
ApplicationContainer sourceApps2 = source2.Install (node_2);
source2.SetAttribute ("MaxBytes", UintegerValue (0));
sourceApps2.Start (MilliSeconds (start_sending_traffic));
sourceApps2.Stop (MilliSeconds (start_sending_traffic + traffic_duration));
The code is the same for both, just with the correct nodes. If I run this code, node_0->node_1 is transmitting the correct amount of traffic while node_2->node_3 is not transmitting anything.
I am checking the amount of bits received with the following code snippet:
Ptr<PacketSink> s1 = DynamicCast<PacketSink> (sinkApps1.Get (0));
std::cout << "Total Bits Received: " << s1->GetTotalRx () * 8 << std::endl;
Ptr<PacketSink> s2 = DynamicCast<PacketSink> (sinkApps2.Get (0));
std::cout << "Total Bits Received: " << s2->GetTotalRx () * 8 << std::endl;
Does anyone know what is going wrong here? Am I using the constructors the wrong way?
Related
I have an IP address 10.0.2.0
The next IP after a block of 64 (10.0.2.0 to 10.0.2.63) is 10.0.2.64
After that (10.0.2.64 to 10.0.2.127 ) 10.0.2.128 etc
How do I calculate the nth?
I had assumed roughly
a = (n*64) mod 256
b = 255/n
10.0.2+b.a
Here's the final solution (in JavaScript):
function incrementIp(ip,nips){
var input = ip.split(".");
var ip = (input[0] << 24) | (input[1] << 16) | (input[2] << 8) | (input[3] << 0);
ip+=nips;
return (ip>>24 & 0xff )+ "." + (ip>>16 &0xff) + "." +( ip>>8 &0xff) + "." + (ip & 0xff);
}
I wrote a simple code where I add hexadecimal values multiplied by 0x1, 0x100 and so on together.
uid = (nuidPICC[0] * 0x1000000);
uid = uid + (nuidPICC[1] * 0x10000);
uid = uid + (nuidPICC[2] * 0x100);
uid = uid + nuidPICC[3];
when I pass numbers D1,55,BF,2D result is D154BF2D but on some numbers combination it is working well, I am using Arduino IDE 1.8.5, can you explain?
The code you have is working perfectly, so there's rather little to explain, if you assume that it is doing what you want it to do. If it's not doing what you want it to do, then we can guess what you want it to do, but then it could be that you wanted it to make you a nice cup of tea, and in that case this won't be much help.
Since you don't give the definitions of the variables, I will assume you have something like:
int8_t nuidPICC[] = { 0xD1,0x55,0xBF,0x2D };
printf("nuidPICC = { %d, %d, %d, %d }\n", nuidPICC[0], nuidPICC[1], nuidPICC[2], nuidPICC[3]);
int32_t uid = (nuidPICC[0] * 0x1000000);
uid = uid + (nuidPICC[1] * 0x10000);
uid = uid + (nuidPICC[2] * 0x100);
uid = uid + nuidPICC[3];
printf("uid = %d * %d + %d * %d + %d * %d + %d = %d\n",
nuidPICC[0], 0x1000000,
nuidPICC[1], 0x10000,
nuidPICC[2], 0x100,
nuidPICC[3], uid);
printf("%d in hex is %08x\n",uid,uid);
which outputs
nuidPICC = { -47, 85, -65, 45 }
uid = -47 * 16777216 + 85 * 65536 + -65 * 256 + 45 = -782975187
-782975187 in hex is d154bf2d
And you can verify that it does exactly what you asked it to.
However, given the values you're multiplying by, you seem to be trying to make a mask from four signed bytes.
Multiplying a int8_t by an integer literal causes it to be extended to an int, and so
int32_t x = int8_t(0xbf) * 0x100;
printf("0xbf * 0x100 = %d or 0x%08x\n",x,x);
0xbf * 0x100 = -16640 or 0xffffbf00
Those leading 0xffff are called 'sign extension' and are causing the next higher byte value to differ from what you would get if you were just shifting and combining the bits.
If you want to combine signed bytes, you need to mask off the sign extension:
uid = (nuidPICC[0] << 24);
uid = uid | (nuidPICC[1] << 16) & 0xff0000;
uid = uid | (nuidPICC[2] << 8) & 0xff00;
uid = uid | nuidPICC[3] & 0xff;
or
uid = ( 0xffffffd1 << 24)
| ( ( 0x00000055 << 16 ) & 0xff0000 )
| ( ( 0xffffffbf << 8 ) & 0xff00 )
| ( 0x0000002d & 0xff )
= 0xd155bf2d
but usually it's easier to use unsigned bytes for bit masks as they don't have sign extension:
uint8_t nuidPICC[] = { 0xD1,0x55,0xBF,0x2D };
uint32_t uid = (nuidPICC[0] << 24);
uid = uid | (nuidPICC[1] << 16);
uid = uid | (nuidPICC[2] << 8);
uid = uid | nuidPICC[3];
printf("uid = ( 0x%x << %d) | ( 0x%x << %d ) | ( 0x%x << %d ) | 0x%x = 0x%x\n",
nuidPICC[0], 24,
nuidPICC[1], 16,
nuidPICC[2], 8,
nuidPICC[3], uid);
uid = ( 0x000000d1 << 24)
| ( 0x00000055 << 16 )
| ( 0x000000bf << 8 )
| 0x0000002d
= 0xd155bf2d
I'm using non-OS SDK v2.0 for esp8266.
I join a multicast group.
I then receive a message (that was sent to multicast).
I want to respond to the sender, but in struct espconn, udp.remote_ip is the address of the multicast group, not sender.
How do I get the ip address of the sender?
Edit:
Receive function has an void* arg argument that is casted to struct espconn.
The old method doesn't do the job anymore, but I found a way to find remote ip and port.
New code:
struct espconn* udp_ch;
remot_info *premot = NULL;
udp_ch = arg;
if (espconn_get_connection_info(udp_ch,&premot,0) == ESPCONN_OK){
os_printf("%d.%d.%d.%d:%d\n", premot->remote_ip[0], premot->remote_ip[1], premot->remote_ip[2], premot->remote_ip[3], premot->remote_port);
}
else{
os_printf("Get info fail\n");
}
This is exactly what I was searching for before. For now, as far as I can see, it works well.
OLD:
I found a way to find the ip, but I don't think it should be done this way. Until I find better, I will use this.
The first thing I did was print first 256 hex values, from void* arg.
I noticed that my address was occurring before a bunch of zeros.
On unicast, the starting position of 0s was 128.
I'm currently using this function:
uint32_t udp_get_addr(void* arg){
uint32_t adr = 0;
uint16_t pos;
uint8_t* data = (uint8_t*) arg;
//unicast?
for(pos = 128; pos<144; pos++){
if(data[pos] != 0){
adr = 1;
break;
}
}
//multicast
if(adr == 1)
pos = 172;
else
pos = 124;
adr = data[pos]<<24 | data[pos+1]<<16 | data[pos+2]<<8 | data[pos+3];
return adr;
}
I know this method is bad, and there is a number of things that can be changed for the better, but for now, this will do.
Edit2:
I needed the source port also. It is located 4 bytes before address. New functions that I currently use:
#define SRC_ADDR_U 120
#define SRC_ADDR_M 168
uint32_t udp_src_addr(void* arg, uint8_t isMulticast){
uint32_t res;
uint8_t* tmp = (uint8_t*) arg;
uint16_t pos;
if(isMulticast) pos = SRC_ADDR_M+4;
else pos = SRC_ADDR_U+4;
res = (tmp[pos+3] << 24) | (tmp[pos+2] << 16) | (tmp[pos+1] << 8) | tmp[pos];
return res;
}
uint16_t udp_src_port(void* arg, uint8_t isMulticast){
uint32_t res;
uint8_t* tmp = (uint8_t*) arg;
uint16_t pos;
if(isMulticast) pos = SRC_ADDR_M;
else pos = SRC_ADDR_U;
res = (tmp[pos+1] << 8) | tmp[pos];
return res;
}
I am a little surprised by the output of the following code:
double array[] = {4, 5, 6, 8, 10, 20};
double* p = array + 3;
//Print array address
cout << (unsigned long)(array) << endl; //This prints 1768104
cout << (unsigned long)(p) << endl; //This prints 1768128
//print p - array
cout << (unsigned long)(p - array) << endl; // This prints 3
I am surprised that the last line prints 3. Shouldn't it print 24 = 3 * 8 bytes? Also, as expected,
the address of p is the address of array + 3 * 8 bytes. This seems inconsistent.
In fact, it is not even a legal assignment to write:
p = p - array; // can't assign an int to type double* No idea, why this is an int.
Pointer arithmetic works in multiples of the size being operated on. p is 3 double sizes greater than array, so that's why you get that response. It's the same reason your p = array + 3 line worked.
If you want the 24, do your casting differently to operate on byte-sized values:
cout << (char *)p - (char *)array;
Your statement p = p - array is meaningless - you can't assign an integer (the difference between pointers) to a pointer variable.
This is how pointer arithmetic works.
You may try like this:-
cout << (char *)p - (char *)array;
I'm very interested in cryptography, and since I like programming too, I decided to make a little program to encrypt files using XTEA encryption algorithm.
I got inspired from Wikipedia, and so I wrote this function to do the encryption (To save space, I won't post the deciphering function, as it is almost the same):
void encipher(long *v, long *k)
{
long v0 = v[0], v1 = v[1];
long sum = 0;
long delta = 0x9e3779b9;
short rounds = 32;
for(uint32 i = 0; i<rounds; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
}
v[0] = v1;
v[1] = v1;
}
Now when I want to use it, I wrote this code:
long data[2]; // v0 and v1, 64bits
data[0] = 1;
data[1] = 1;
long key[4]; // 4 * 4 bytes = 16bytes = 128bits
*key = 123; // sets the key
cout << "READ: \t\t" << data[0] << endl << "\t\t" << data[1] << endl;
encipher(data, key);
cout << "ENCIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
decipher(data, key);
cout << "DECIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
I always get either run-time crash or wrong decipher text:
I do understand the basics of the program, but I don't really know what is wrong with my code. Why is the enciphered data[0] and data1 the same? And why is deciphered data completely different from the starting data? Am I using the types wrong?
I hope you can help me solving my problem :) .
Jan
The problem is here:
v[0] = v1; // should be v[0] = v0
v[1] = v1;
Also, you only set the first 4 bytes of the key. The remaining 12 bytes are uninitialized.
Try something like this:
key[0] = 0x12345678;
key[1] = 0x90ABCDEF;
key[2] = 0xFEDCBA09;
key[3] = 0x87654321;
The fixed code gives me this output:
READ: 1
1
ENCIPHERED: -303182565
-1255815002
DECIPHERED: 1
1