Solved

subnet calculator

Posted on 1998-12-04
11
637 Views
Last Modified: 2012-05-04
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
Comment
Question by:coko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254845
So what you need to know then is how to convert an unsigned int into a string?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254846
#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
 

Author Comment

by:coko
ID: 1254847
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1254848
I know...that is what my program does (attempts to do).
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254849
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254850
My code snippet assumes little endianness by the way...
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1254851
BTW: atoi _is_ part of ANSI C.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1254852
Sorry, i think you must have meant itoa - so my comment is pretty
unnecessary :)
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1254853
BTW again: inet_ntoa converts an 'in_addr' back to a dotted
string - i think this is what you need, coko?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254854
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
 
LVL 3

Accepted Solution

by:
mliberi earned 40 total points
ID: 1254855
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question