I am currently writing an FTP server and I need to parse the ip and port of a remote server from an input string buffer in the following format:
xxx,xxx,xxx,xxx,yyy,zzz
where:
xxx
stands for an ip address octet in decimalyyy
is round((remote port number) / 256)zzz
is (remote port number) % 256
For example: 127,0,0,1,123,64
means ip = 127.0.0.1
and port = 31552
.
I am currently using sscanf
to extract the following fields from the input string buffer:
sscanf(str, "%u,%u,%u,%u,%u,%u", ret_ip, &ip[0], &ip[1], &ip[2], &temp1, &temp2) == 6
where:
- str is the input buffer
- ret_ip is of type
uint32_t
- ip's are of type
uint32_t
- temp1 and temp2 are of type
unsigned short int
Example code:
#include <stdio.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
uint32_t ip[4];
unsigned short int temp, temp1;
if (sscanf("127,0,0,1,142,214", "%u,%u,%u,%u,%u,%u", &ip[0], &ip[1], &ip[2], &ip[3], &temp, &temp1) == 6)
{
printf("%u : %u", temp, temp1);
}
return (0);
}
My problem is that, for valid string, the value of temp1 is always 0 (zero), i.e. all the other variables are filled according to string except the temp1. I would appreciate any help.