• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

subnet calculator

I am trying to write a function that accepts two strings,
an ip address and a netmask, and returns another string,
which is the subnet calculated from the two input parameters.

first i convert the two strings to ip addresses
using inet_addr(ip) and inet_addr(subnet)

then i use the & operation to get the netmask in
an unsigned int format, but I don't know how to
convert that back to a string.

Is it possible to do that conversion, or am I completely
off topic here ???

Does anybody know of any code that does this successfully ?

Thanx,
      corneliu
0
coko
Asked:
coko
  • 4
  • 3
  • 2
  • +2
1 Solution
 
scrapdogCommented:
So what you need to know then is how to convert an unsigned int into a string?
0
 
scrapdogCommented:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


unsigned long int x=123456;
char *y,c;
char string[16], s[3];
int i,n;

void main()
{
  y = (char *)&x;
  for(i=3;i>=0;i--) {
       c=*(y+i);
       n=(int)c;
       if (n<0) {n=n+256;}
       itoa(n,s,10);
       strcat(string,s);
       if(i>0) strcat(string,"."); }
  printf("%s\n",string);
}
0
 
cokoAuthor Commented:
no, i need to know how to convert a subnet address
like 47.245.34 from unsigned it (which is represented
something like 3462356326) to the actual address in
a str
0
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

 
scrapdogCommented:
I know...that is what my program does (attempts to do).
0
 
ozoCommented:
Your progrem depends on the 'endian'ness of the integer representation.
(although, if you converted from a string the same way in the first place, and don't really care about the int value, then it may not matter)
atoi is not part of ANSI C, if your library doesn't have it, you can use sprintf instead
0
 
scrapdogCommented:
My code snippet assumes little endianness by the way...
0
 
snoeglerCommented:
BTW: atoi _is_ part of ANSI C.
0
 
snoeglerCommented:
Sorry, i think you must have meant itoa - so my comment is pretty
unnecessary :)
0
 
snoeglerCommented:
BTW again: inet_ntoa converts an 'in_addr' back to a dotted
string - i think this is what you need, coko?
0
 
ozoCommented:
You're right snoegler, atoi is part of ANSI C, I ment to say that itoa is not :)
nor is inet_ntoa part of ANSI C, but it is likely to be found in the same places as inet_addr
does
printf("%s\n",inet_ntoa(inet_makeaddr(inet_network("47.245.34.0")>>8,inet_addr("47.245.34.0"))));
do the job?
0
 
mliberiCommented:
char *Int2ipaddress(char *buffer, int x)
/* convert integer x to ip address */
/* fills buffer and return it      */
{ sprintf(buffer,"%d.%d.%d.%d",(x>>24)&0xff
                              ,(x>>16)&0xff
                              ,(x>> 8)&0xff
                              , x     &0xff
         );
  return buffer;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now