Archive: Check subnet in user's ip address


Check subnet in user's ip address
I've used NSIS to create short, easy scripts but am faced with something more complex that I'm struggling with because I'm not a programmer. What I've found which might help doesn't make sense to me so I must ask for your tolerance with my lack of knowledge. The issue: The server the application connects to will depend on the user's subnet. I use the function getip in ip.dll to get the address and I can extract the ; from it so I have the IP in a variable. If the ip address falls into the 10.5.95 range, the application logs into Atlanta, if it is 10.38.1 it goes to Birmingham as an example. I think I've been trying to make this complicated but it probably doesn't need to be. Thanks TONS for your help and patience.


Does the range have to be the first 3 numbers or last 3 of the IP address.

-Stu


Time to go (I was hoping you were going to reply sometime within the last 20 minutes), but if you need to get the last 3 numbers then use this:


Function GetSubNetMask
Exch $R0 ;ip
Push $R1
Push $R2

StrCpy $R1 -1
IntOp $R1 $R1 + 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "" +5
StrCmp $R2 "." 0 -3

IntOp $R1 $R1 + 1
StrCpy $R0 $R0 "" $R1
Goto +2

StrCpy $R0 ""

Pop $R2
Pop $R1
Exch $R0
FunctionEnd


Use:
Push "0.1.2.3"
Call GetSubNetMask
Pop $R0

Otherwise I'm sure someone else can write you another function if this one doesn't do what you need.

-Stu

Stu-

Sorry, I wasn't here but it was my turn to cook. I can't thank you enough - I believe this will work great as it works in my test environment; I will test in the live environment next week. You're quick and skilled and I seriously want to improve my skills; should I focus on general programming skills or do you have a suggestion more specific?

Thanks again!!!


You don't have to be a programmer to do NSIS. In fact, when I started using NSIS back in 2001 I had no real programming experience, but since then I've learned dozens of different languages.

If you get a problem, you just need to ask here. I did the same when I started.

-Stu


Follow-up
Stu-

I have to make a change - I need the first 3 parts of the ip address, not the last 3. I can't figure out how to change it myself [great source of frustration].

-Mary



Function GetSubNetMaskA
Exch $R0 ;ip
Push $R1
Push $R2

StrCpy $R1 0
IntOp $R1 $R1 + 1
StrCpy $R2 $R0 1 -$R1
StrCmp $R2 "" +4
StrCmp $R2 "." 0 -3

StrCpy $R0 $R0 -$R1
Goto +2

StrCpy $R0 ""

Pop $R2
Pop $R1
Exch $R0
FunctionEnd


Now called GetSubNetMaskA

-Stu

Stu-

Thanks for modifying it! It works great now.

I'm hung up on understanding what is probably very simple: strcmp $R2 "" +4

What is the variable being compared to that's in ""?

Sorry if it's a stupid question, like I said I'm struggling.

Thank you!
-Mary


It's comparing $R2 to "" (empty string). If it is "", then it shall jump over the next three commands/instructions onto the 4th one.

-Stu


Stu-

Is that step good programming as precaution for reaching the end of the string or a space? The concept was to reverse the review of the string, character by character, and once the first period was reached from the right side, the number of characters to offset by was increased by 1. The string then became the original IP address offset by the number of characters from the right side of the address.

Why was the first script started with StrCpy $R1 -1 and the second with StrCpy $R1 0? Didn't the fist begin exactly on the first character while the second started one character in? If I got that right, why?

Thanks again.


When $R2 is "" we know that we have reached the end of the string. This isn't the best way to go about it - a better way would be to compare the value of the character counter ($R1) to the length of the string (or 0 if going reverse), however this requires a little more code and is less efficient in this case.

The first script goes from the beginning of the string to the end and the second goes from the end to the beginning.

For the first script we start at -1 ($R1) because the counter will increment it by 1 so it will really start off at 0 (hence we start at the start of the string).

The second script however starts at 0 because when we add 1 to it it becomes 1. In the main StrCpy call, we use -$R1 and so we start on the last character in the string.

-Stu


Stu-
Just to make sure...when reading a string from the left, 0 means the first character and when reading a string from the right, position 1 is the same as the last character.
Mary


-1 would be the last character (or 1 I 'spose yes).
It's just the way StrCpy works.

You can mess around with it to get used to the values ("" being one of them also). Also check out StrCpy in the manual.

-Stu


Thanks, all set!