Everything2
Near Matches
Ignore Exact
Full Text
Everything2

IP Addresses and How to Deal With Them

created by iain

(thing) by iain (5.5 y) (print)   ?   (I like it!) 1 C! Fri Jul 21 2000 at 12:09:15

Taken from the original at http://www.ecst.csuchico.edu/~beej/guide/net/ ... see end of writeup for Copyright statement.

IP Addresses and How to Deal With Them

Fortunately for you, there are a bunch of functions that allow you to manipulate IP addresses. No need to figure them out by hand and stuff them in a long with the << operator.

First, let's say you have a struct sockaddr_in ina, and you have an IP address "132.241.5.10" that you want to store into it. The function you want to use, inet_addr(), converts an IP address in numbers-and-dots notation into an unsigned long. The assignment can be made as follows:

ina.sin_addr.s_addr = inet_addr("132.241.5.10");

Notice that inet_addr() returns the address in Network Byte Order already -- you don't have to call htonl(). Swell!

Now, the above code snippet isn't very robust because there is no error checking. See, inet_addr() returns -1 on error. Remember binary numbers? (unsigned) -1 just happens to correspond to the IP address 255.255.255.255! That's the broadcast address! Wrongo. Remember to do your error checking properly.

All right, now you can convert string IP addresses to longs. What about the other way around? What if you have a struct in_addr and you want to print it in numbers-and-dots notation? In this case, you'll want to use the function inet_ntoa() ("ntoa" means "network to ascii") like this:

printf("%s", inet_ntoa(ina.sin_addr));

That will print the IP address. Note that inet_ntoa() takes a struct in_addr as an argument, not a long. Also notice that it returns a pointer to a char. This points to a statically stored char array within inet_ntoa() so that each time you call inet_ntoa() it will overwrite the last IP address you asked for. For example:

char *a1, *a2;
 :
 :
a1 = inet_ntoa(ina1.sin_addr);  /* this is 198.92.129.1 */
a2 = inet_ntoa(ina2.sin_addr);  /* this is 132.241.5.10 */
printf("address 1: %s\n",a1);
printf("address 2: %s\n",a2);

will print:

address 1: 132.241.5.10
address 2: 132.241.5.10

If you need to save the address, strcpy() it to your own character array.

That's all on this topic for now. Later, you'll learn to convert a string like "whitehouse.gov" into its corresponding IP address (see DNS, below.)


Prev | Up | Next


Copyright © 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted in any medium provided that its content is not altered, it is presented in its entirety, and this copyright notice remains intact. Contact beej@ecst.csuchico.edu for more information.


printable version
chaos

Real hackers start their own IRC networks so that they can't be traced by the FBI Beej's Guide to Network Programming socket() 192.168.0.0/16
whitehouse.gov Class C IP address How to transfer your domain name to a new registrar IP subnetting
broadcast address IP address Decimal IP address How to beat someone at "pick a number"
Sharing an internet connection with Linux How 'bout them transparent dangling carrots? NAT Home Area Networks
IP masquerade routing table Whose Line is it Anyway? Dealing with foggy bathroom mirrors
August 23, 2007 How to get around censorware Thinking you know more about computers than the tech you just called Gay for pay
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
What you are reading:
All Along the Watchtower
down
Zulu
Marquis de Sade
The Kinsey Institute
Satan
An afternoon in Jamaica
Recommended reading
Japanese puns that are not funny but at least are puns
Babyklappe
Australia
On Menu Writing
methadone
New Writeups
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
stainedglass
1(fiction)
kalen
Three "T"s(idea)
octillion369
Undead(idea)
archiewood
Ico(fiction)
Heisenberg
Why I love Everything2(log)
octillion369
Death Knight(person)
XWiz
Are you hoping for a miracle?(review)
santo
The Host(review)
LostPsion
"Shut the Fuck Up" Theaters(idea)
beatrice
You've been slowly taking me over for nearly a year, do you know that?(idea)
Berek
YouTube(thing)
This page courtesy of The Everything Development Company