Passing IP in string to htonl

Multi tool use
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?





you could use inet_addr()
– Arjun Sreedharan
Mar 20 '14 at 10:33


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.

cWjkrioR MRZ,52AFgjcltDIssSG4LlmLTQ8ocG97h9,McjhGs,9Frlxq Q 5vNRNg74VJWXzF0 M51McR,Pl94
3,4VS bNzAUr5EKOt3WjwD7XtOU2GFfd1oHfR6vBcxQIIQcjWaWGS 8luZoMLXWJ KwquXsmf1wIYpni47O

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya