Passing IP in string to htonl

Multi tool use
Passing IP in string to htonl
I am binding to socket in a kernel module. I get the IP in string format from another socket. How should I pass this IP to htonl(). I tried typecasting it to (long int). But, obviously it won't work.
How to achieve this?
inet_addr()
so it should be addr_send.sin_addr.s_addr = inet_addr(IP) ?
– mlemboy
Mar 20 '14 at 10:35
2 Answers
2
unsigned int inet_addr(char *str)
{
int a, b, c, d;
char arr[4];
sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d;
return *(unsigned int *)arr;
}
//use it as follows:
//inet_addr() returns address in Network Byte Order, so no need of htonl()
thesockaddr_in.sin_addr.s_addr = inet_addr(str);
what is header file in kernel module?
– mlemboy
Mar 20 '14 at 10:40
kernel libraries?
– mlemboy
Mar 20 '14 at 10:45
@mahesh have updated the ans. write your own inet_addr
– Arjun Sreedharan
Mar 20 '14 at 11:03
I've used that it returns 16777343 for 127.0.0.1. :(
– mlemboy
Mar 20 '14 at 11:13
Is the output right?
– mlemboy
Mar 20 '14 at 11:22
Yo can use something like this:
const char *IP = "62.4.36.125";
SOCKADDR_IN DestAddr;
DestAddr.sin_family = AF_INET;
DestAddr.sin_port = htons (PORTNUM);
DestAddr.sin_addr.s_addr = inet_addr(IP);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you could use
inet_addr()
– Arjun Sreedharan
Mar 20 '14 at 10:33