Hello everyone in C Boards! I'm here to look again for answer or hints to help me accomplish what I need to do...
I'm writing a server with sockets (actually, a repository of files), and I have a code (see down). The code shouldn't care about IPv4 and IPv6. When a IPv6 interface connects to the server, everything is printed fine and the IP appears ok. Anyway, when a IPv4 connect to the repository the IP appears in a IPv6 style...
I know that they are the same address expressed in different ways, but I need it to print the IP as a IPv4 without the ::fffff: prefix...
Example:
Aceptada conexión de ::ffff:127.0.0.1
should print
Aceptada conexión de 127.0.0.1
The code:
Code:
printf("Esperando conexiones por el puerto %s...\n",puerto);
// Manejo de conexiones, etc
while(1){
sizeClient = sizeof(struct sockaddr_storage);
socketClient = accept(socketId, (struct sockaddr *)&infoClient, &sizeClient);
if(socketClient==-1){
perror("aceppt");
continue;
}
char s[INET6_ADDRSTRLEN];
inet_ntop(infoClient.ss_family,
(void*)direccionIn((struct sockaddr_in *)&infoClient),
s, sizeof(s));
printf("Aceptada conexión de %s\n", s);
// Todo el código aqui
// All the code here
printf("Cerrando conexión de %s\n", s);
close(socketClient);
}
And the direccionIn:
Code:
void *direccionIn(struct sockaddr *sa) {
// Si es IPv4
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
// Si es IPv6
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}