Question1. Suppose computers A and B have IP addresses 10.105.1.113 and 10.105.1.91 respectively and they both use the same net mask N. Which of the values of N given below should not be used if A and B should belong to the same network?
255.255.255.0
255.255.255.128
255.255.255.192
255.255.255.224
Question2. While opening a TCP connection, the initial sequence number is to be derived using a time-of-day (ToD) clock that keeps running even when the host is down. The low order 32 bits of the counter of the ToD clock is to be used for the initial sequence numbers. The clock counters increments once per millisecond. The maximum packet lifetime is given to be 64s. Which one of the choices given below is closest to the minimum permissible rate at which sequence numbers used for packets of a connection can increase?
0.015/s
0.064/s
0.135/s
0.327/s
During an interview in company interviewer ask me these questions. How to solve these question. Please help me.
Thank you.
Really you should ask only one question per post...
For question 1, after masking the IP addresses have to look the same. Masking is a bitwise AND operation, so you need to write down the numbers in question in binary. Now the first three groups don't matter, since 255 == 11111111 and you will not change anything. Let's focus on the last number only:
113 = 0111 0001
91 = 0101 1011
And for the mask:
0 = 0000 0000
128 = 1000 0000
192 = 1100 0000
224 = 1110 0000
Now for the masking:
Example:
1110 0000
0111 0001
========= AND
0110 0000
Since 0 AND 1 == 0, but 1 AND 1 == 1
Applying this mask to the two addresses, we get
113 91
0 0000 0000 0000 0000
128 0000 0000 0000 0000
192 0100 0000 0100 0000
224 0110 0000 0100 0000 **** when this mask is applied to the two IP addresses, the result is different
We conclude that the two addresses would end up on different subnets.
Conclusion: you can't use 255.255.255.224 as the mask if you want these two IP addresses on the same subnet. For more information you can go to https://en.wikipedia.org/wiki/Subnetwork for example.
As for question 2, it is one of those badly phrased questions. Is a "minimum rate" the lowest number, or the highest number? When you say "this is the maximum rate" you typically mean "the lowest number" but it's open for interpretation. I think in this case they are asking about the "maximum rate" (the smallest number), since the literal interpretation of the question makes no sense. Still I am struggling to understand what they are asking. When two computers communicate, they increase the sequence number on each packet. So what is "permissible"? I don't know. But 0.015/s is close to 1/64s - if I were a betting man, that's where I'd put my money but I can't explain it. I hope the answer to your first question at least is useful... and maybe that the rambling for the second spurs some good discussion and an actual answer.
Related
I am given a list of IPs, some of them have a netmask, other no.
I got the idea of how to create a mask but i'm not understanding why there are cases where an IP doesn't have a mask.
Like the following:
121.34.56.64–121.34.56.128 > Don't have mask
128.131.9.0–128.131.9.192 > Don't have mask
93.20.10.0–93.20.11.0 > Don't have mask
While the following actually have mask address:
67.56.34.64-67.56.34.79 > Prefix notation: 67.56.34.64/28 | Address/Mask: 67.56.34.64/255.255.255.240 and so on.
For example if I look at the third one of the addresses without mask i do the following:
93.20.10.0–93.20.11.0
93.20. 0000 101|0 . 0000 0000
93.20. 0000 101|1 . 0000 0000
NetMask: 255.255.254.0, that is not correct.
Can somebody explain me why and how to understand when a mask exists?
All IP addresses have a subnet mask and through Variable Length Subnet Masking (VLSM) they can change subnet masks depending on how we carve up the larger address pool.
Most likely your addresses are slightly off but based on the ranges given we can figure out what the masks should be.
Using your list:
121.34.56.64–121.34.56.128 if this range is inclusive then there are addresses in 2 subnets listed here and the actual range is probably 121.34.56.64-121.34.56.127 so the mask would be 255.255.255.192 or a /26.
128.131.9.0–128.131.9.192 if this range is inclusive then there are addresses in multiple subnets listed here and the actual range is probably 128.131.9.0-128.131.9.191. This is tricky since there isn't a mask that allows 192 addresses so it is likely that this isn't a single subnet but an aggregation of multipe subnets that could be 3 /26s, 6 /27s, 12 /28s, 24 /29s, 48, /30s, 96 /31s, or 192 /32s, or some combination of those. For example it could be 2 /26s and 2 /27s.
93.20.10.0–93.20.11.0 if this range is inclusive then there are addresses in 2 subnets listed here and the actual range is probably 92.20.10.0-92.20.10.255 so the mask would be 255.255.255.0 or a /24.
So you can deduce what some of the subnet masks might be but for the ones that don't fall on the subnet boundaries more information will be required.
I have two shields which conveniently (ie no pin clash) share a port and I need to be able to manipulate just SOME pins on the port. But I cannot be sure if I am manipulating pins on or off, I just want to set them arbitrarily as the need arrises, ie, in one operation I may be turning some pins on and some off.
I do know:
PORTX |= B11110000 // turns on bits 4-7
PORTX &= B11000011 // turns off bits 2-5
PORTX ^= B00111111 // toggles bits 0-5
My challenge has been to turn on AND off only some pins, leaving others unchanged.
I have achieved the desired result, and in as far as I think I have done it in a safe way, I want to confirm it is in fact SAFE, and have I gone about it the right (best) way, or can I achieve this a much simpler way.
First, I am using PORTD, pins 4-7. I set those pins as outputs and then set them all as low to ensure my program starts with them (4x relays) all off.
void initRelays(){
RELAYDDR |= B11110000;
RELAYPORT &= ~RELAYDDR;
}
I believe tis will set pins 4-7 off without modifying the lower bits due to the bitwise AND with ZERO. I believe this will leave bit 0-3 as they were previously set.
Inverting this value and ANDing it with the existing port value, will ensure those pins are off and leaves the other bits unchanged. I'm sure this line is not required, I am having it here for safety sake :)
I have left the comments in the below code in order for you to try and understand what I am doing.
void relayPush(byte stack){
// stack has bit 1 to relay 1 (pin 4), thru bit 4 to relay 4 (pin 7)
// take stack and isolate the four bottom bits (the information we want to convert)
stack &= B00001111; // (1) I think this line is probably not required
// now shift to the position we need
stack <<= 4; // (2)
// OR the new stack with the PORT
// (this turns on any relays set in stack)
RELAYPORT |= stack; // (3)
// we need to NOT modify the bottom bits of the port
// mark those with a '1' so as to not turn them off
// bottom of stack mask = 0x0f
// XOR stack and mask
stack ^= 0x0f; // (4)
// AND new stack and port to turn off appropriate relays
RELAYPORT &= stack; // (5)
}
I know I have done it in two PORT operations, and I could make this one by using a temp variable, that's not of a major concern since it's only turning everything required to be on in the first instance and then turning everything off thats required in the second instance.
Have a missed a simpler way of doing this?
edit: I have had a look at what #Ignacio has said about changing the final operations and this is what I've come up with:
0011 0011 current port assignment
xxxx 1010 current stack assignment (we only want the lower nibble)
1010 0011 desired result
0011 0011 current port
xxxx 1010 current stack
0000 1111 step 1 - apply this mask to stack
0000 1010 resultant stack
1010 0000 step 2 - stack << 4
0011 0011 PORT
1010 0000 STACK
1010 0011 step 3 - resultant PORT (port OR stack)
0000 1111 (MASK for step 4)
1010 0000 stack at step 4
1010 1111 step 4 - resultant stack (mask XOR stack)
1010 0011 port from step 3
1010 1111 stack from step 4
1010 0011 port AND stack (desired result)
/// changing steps 4 and 5 to drop XOR, and applying complement
1010 0000 stack prior to step 4
0101 1111 ~stack
1010 0011 port from step 3
0000 0011 stack AND port (not the desired result)
summary:
XOR is needed to populate the bottom nibble to B00001111 and leaving top nibble unchanged. Since we know the bottom nibble is ZERO (from earlier shift), we could simply add 0x0F. XOR achieves the same thing.
For the final AND operation, we need to switch off top nibble ZEROs. Hence, no complement.
New idea from my comment to #Ignacio:
0011 0011 current port
xxxx 1010 current stack
1010 0000 shifted stack
0000 0011 temp = port AND 0x0F
1010 0011 stack OR temp (desired result)
Sorry for the long post, but I think that is a better solution, although it does use another variable.
Your and operation with RELAYPORT clears the upper 4 bits. You should not perform the earlier xor operation and instead should just and it with the complement.
RELAYPORT &= ~stack;
Some thoughts ... based on my experience with megaAVR's (esp. AT90USB1287)
When you split a port and operate some bits as input and some bits as output, I recommend to take extra care when writing the whole output port. There are good BIT instructions in the AVR. If you want to write a complete port, keep in mind that writing to bits configured as input does have an effect, namely to activate (PORTxy<-1) or deactivate (PORTxy<-0) internal pullup resistors - so depending on your hardware you have to choose what to use for the "unwanted" bits (with another dependency on MCUCR(PUD). In other words, the (to-be-ignored) input bits in a register you write out can't contain any random values but exactly the ones that support the configuration of the internal pullups. Insofar it's not usefull to read in a PINx before writing (parts) back to the port. (This was used on older processors with port hardware less elaborated than the AVR processors)
When writing out a PORTx insert a NOP before reading back PINx (due to the internal latch).
In initRelays() I'd use a constant, because it compiles faster (single instruction) rather than a function of RELAYDDR which involves reading back RELAYDDR into a register and writing the register to RELAYPORT.
This is my first post and I absolutely <3 this site! So much great content!
So, I have the following TCPDump command I want to understand what it is asking (in plain English).
tcpdump 'tcp[12] & 80 !=0'
Is it asking to grab all TCP packets on byte offset 12 (TCP Header length and Reserved bits) with values at least 80 that is true? I believe I am wrong.
If the above is true, can someone write out the possible binaries for it?
80 gives 0101 0000. My mentor also wrote down: 1111 0000 and 0111 0000. But I don't know why...
If it's at least 80, the binary combo for that could be countless...
Is it asking to grab all TCP packets on byte offset 12 (TCP Header length and Reserved bits) with values at least 80 that is true
No. 80 in decimal is 50 in hexadecimal, so it's equivalent to tcp[12] & 0x50 !=0, which tests whether either the 0100 0000 bit or the 0001 0000 bit in the 12th byte of the TCP header are set. That's true of 0101 0000, but is also true of 1111 0000 and 0111 0000, as well as 0100 0000 and 0001 0000 and 0100 1111 and....
If you want to test the uppermost bit of that byte, you'd use tcp[12] & 0x80 !=0. That would, in effect, match all values >= 0x80.
Question in some competition exam:
The subnet mask for a particular network is 255.255.31.0. Which of the following pairs of IP addressed could belong to this network?
1: 172.57.88.62 & 172.56.87.23
2: 10.35.28.2 & 10.35.29.4
3: 191.203.31.87 & 192.234.31.88
4: 128.8.129.43 & 128.8.161.55
Now I am having a confusion in this question that i have read that a subnet mask is of pattern:
1111 1111. 1111 1111. 1111 1
I mean 1's start from left side but in this subnet mask:
1111 1111. 1111 1111. 0001 1111. 0000 0000
So what is the actual principle behind. please explain. i am totally confused.
Thanks in advance :)
That's a discontiguous subnet mask, which is no longer supported on most routers. The principle is the same: convert the dotted-quad IP addresses and mask to 32-bit unsigned integers and AND each address with the mask. If the results are the same, they're in the same subnet.
This type of subnet mask is kind of theoretical rather than practical. We use subnet mask of all 1's from most significant bit and all 0's at the end just to ensure that hosts in a particular subnet gets contiguous IPs. But if we have a subnet like this : 255.255.31.0
We would not be able to allocate contiguous IPs to host in a particular subnet.
But still it is a possibility (theoretically). This kind of subnet masks are barely used in practice.
Coming to the question,
The idea to figure out the IPs belonging to same network is same as in the normal subnet mask scenario.
Just take 'AND' of bits of given IPs with subnet mask (needless to say, resultant would be network id)and check which turns out to be the same for the pair.
options-
(a). This is incorrect in the first place.
Net IDs : 172.57.(something).0 and 172.56.(something).0
second octet is different
(b). Net IDs : 10.35.28.0 and 10.35.29.0
This is incorrect since third octet is different.
(c). Net IDs : 191.203.31.0 and 192.234.31.0
Clearly, this is incorrect since second and third octet are different.
(d). This will be answer (Because rest three are incorrect).
let's check it,
128.8.129.43 AND 255.255.31.0 => 128.8.1.0
128.8.161.55 AND 255.255.31.0 => 128.8.1.0
BINGO !
So, network id turns out to be the same in both cases. So, (d) will be answer.
"There are 10 types of people in this world - one who understand ternary, one who don't and others who just thought that this is gonna be a 'binary' joke."
Ans: option (d)
Explanation:
Here is the question is: Which of the following pair of IP addresses belongs to the given network. Applying a subnet mask to an IP address separates network address from host address.
So you have to find the network-id from the IP address using the given subnet mask. Below shows the example of how to find a network-id from given Subnet mask and IP address
The network bits are represented by the 1's in the subnet mask, and the host bits are represented by 0's. Performing a bitwise logical AND operation on the IP address with the subnet mask produces the network address. For example, applying the Class C subnet mask to our IP address 216.3.128.12 produces the following network address:
IP: 1101 1000 . 0000 0011 . 1000 0000 . 0000 1100 (216.003.128.012)
Mask: 1111 1111 . 1111 1111 . 1111 1111 . 0000 0000 (255.255.255.000)
------------------------------------------------------
1101 1000 . 0000 0011 . 1000 0000 . 0000 0000 (216.003.128.000)
Therefore the network-id is: 216.003.128.000
Hence doing the above for all the options, you will find that option (d) belongs to the same network.
I'm trying to add CRC16 error detection to a Motorola HCS08 microcontroller application. My checksums don't match, though. One online CRC calculator provides both the result I see in my PC program and the result I see on the micro.
It calls the micro's result "XModem" and the PC's result "Kermit."
What is the difference between the way those two ancient protocols specify the use of CRC16?
you can implement 16 bit IBM, CCITT, XModem, Kermit, and CCITT 1D0F using the same basic code base. see http://www.acooke.org/cute/16bitCRCAl0.html which uses code from http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
the following table shows how they differ:
name polynomial initial val reverse byte? reverse result? swap result?
CCITT 1021 ffff no no no
XModem 1021 0000 no no no
Kermit 1021 0000 yes yes yes
CCITT 1D0F 1021 1d0f no no no
IBM 8005 0000 yes yes no
where 'reverse byte' means that each byte is bit-reversed before processing; 'reverse result' means that the 16 bit result is bit-reversed after processing; 'swap result' means that the two bytes in the result are swapped after processing.
all the above was validated with test vectors against http://www.lammertbies.nl/comm/info/crc-calculation.html (if that is wrong, we are all lost...).
so, in your particular case, you can convert code for XModem to Kermit by bit-reversing each byte, bit reversing the final result, and then swapping the two bytes in the result.
[i believe, but haven't checked or worked out the details, that reversing each byte is equivalent to reversing the polynomial (plus some extra details). which is why you'll see very different explanations in different places for what is basically the same algorithm.
also, the approach above is not efficient, but is good for testing. if you want efficient the best thing to do is translate the above to lookup-tables.]
edit what i have called CCITT above is documented in the RevEng catalogue as CCITT-FALSE. for more info, see the update to my blog post at the link above.
My recollection (I used to do modem stuff way back when) is that Kermit processes the bits in each byte of the data using the least significant bit first.
Most software CRC implementations (Xmodem, probably) run through the data bytes most significant bit first.
When looking at the library source (download it from http://www.lammertbies.nl/comm/software/index.html) used for the CRC Calculation page you linked to, you'll see that XModem uses CRC16-CCITT, the polynomial for which is:
x^16 + x^12 + x^5 + 1 /* the '^' character here represents exponentition, not xor */
The polynomial is represented by the bitmap (note that bit 16 is implied)
0x1021 == 0001 0000 0010 0001 binary
The Kermit implementation uses:
0x8408 == 1000 0100 0000 1000 binary
which is the same bitmap as XModem's, only reversed.
The text file that accompanies the library also mentions the following difference for Kermit:
Only for CRC-Kermit and CRC-SICK: After all input processing, the one's complement of the CRC is calculated and the two bytes of the CRC are swapped.
So it should probably be easy to modify your CRC routine to match the PC result. Note that the source in the CRC library seems to have a pretty liberal license - it might make sense to use it more or less as is (at least the portions that apply for your application).
X-Modem 1K CRC16.
Process for bytewise CRC-16 using input data {0x01, 0x02} and polynomial 0x1021
Init crc = 0
Handle first input byte 0x01:
2.1 'Xor-in' first input byte 0x01 into MSB(!) of crc:
0000 0000 0000 0000 (crc)
0000 0001 0000 0000 (input byte 0x01 left-shifted by 8)
0000 0001 0000 0000 = 0x0100
The MSB of this result is our current divident: MSB(0x100) = 0x01.
2.2 So 0x01 is the divident. Get the remainder for divident from our table: crctable16[0x01] = 0x1021. (Well this value is famila from the manual computation above.)
Remember the current crc value is 0x0000. Shift out the MSB of current crc and xor it with the current remainder to get the new CRC:
0001 0000 0010 0001 (0x1021)
0000 0000 0000 0000 (CRC 0x0000 left-shifted by 8 = 0x0000)
0001 0000 0010 0001 = 0x1021 = intermediate crc.
Handle next input byte 0x02:
Currently we have intermediate crc = 0x1021 = 0001 0000 0010 0001.
3.1 'Xor-in' input byte 0x02 into MSB(!) of crc:
0001 0000 0010 0001 (crc 0x1021)
0000 0010 0000 0000 (input byte 0x02 left-shifted by 8)
0001 0010 0010 0001 = 0x1221
The MSB of this result is our current divident: MSB(0x1221) = 0x12.
3.2 So 0x12 is the divident. Get the remainder for divident from our table: crctable16[0x12] = 0x3273.
Remember the current crc value is 0x1021. Shift out the MSB of current crc and xor it with the current remainder to get the new CRC:
0011 0010 0111 0011 (0x3273)
0010 0001 0000 0000 (CRC 0x1021 left-shifted by 8 = 0x2100)
0001 0011 0111 0011 = 0x1373 = final crc.