Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inverse in_addr_t 1

Status
Not open for further replies.

shlomyb

Programmer
Nov 14, 2006
32
IL
I use - inet_addr() and for some reason I need a way to inverse in_addr_t .
Such as :

Before:

10.11.12.13

After:
13.12.11.10

Thanks!!!

#include <arpa/inet.h>

in_addr_t inet_addr(const char *cp);
 
Try
Code:
struct in_addr a;
unsigned w;
a.S_addr = inet_addr(/* a.b.c.d */);
w = a.S_un_b.s_b1; /* swap b1 and b4 */
a.S_un_b.s_b1 = a.S_un_b.s_b4;
a.S_un_b.s_b4 = w;
w = a.S_un_b.s_b2 /* swap b2 and b3 */
a.S_un_b.s_b2= a.S_un_b.s_b3
a.S_un_b.s_b3= w;
/* Now you have inverted addr via a.S_addr */
 
Thanks.
What do you mean by :
w = a.S_un_b.s_b1;

I get an error :
cmObject.cpp:284: `struct in_addr' has no member named `s_un_b'
 
Excuse me, please. Try this (insert S_un union selector):
Code:
struct in_addr a;
unsigned w;
a.S_un.S_addr = inet_addr("1.2.3.4"/* a.b.c.d */);
w = a.S_un.S_un_b.s_b1; /* swap b1 and b4 */
a.S_un.S_un_b.s_b1 = a.S_un.S_un_b.s_b4;
a.S_un.S_un_b.s_b4 = w;
w = a.S_un.S_un_b.s_b2; /* swap b2 and b3 */
a.S_un.S_un_b.s_b2= a.S_un.S_un_b.s_b3;
a.S_un.S_un_b.s_b3= w;
/* Now you have inverted addr via a.S_addr */
See struct in_addr declaration in your system socket header file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top