2017-04-26 12:25:05 +00:00
|
|
|
/**************************
|
|
|
|
* Network Common Depend. *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct in_addr* GET_LOCAL_IP(){
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
|
|
|
struct ifaddrs *list; // list of interfaces
|
|
|
|
struct ifaddrs *it; // interface iterator
|
|
|
|
struct sockaddr_in *info; // interface info
|
|
|
|
struct in_addr *result; // ip trouvée
|
|
|
|
result = malloc( sizeof(struct in_addr) );
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] On récupère la liste des interfaces
|
|
|
|
=========================================================*/
|
|
|
|
getifaddrs(&list);
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Parcourt des interfaces
|
|
|
|
=========================================================*/
|
|
|
|
for( it = list ; it ; it = it->ifa_next ){
|
|
|
|
|
|
|
|
/* 1. Si "lo" -> suivant */
|
|
|
|
if( strcmp(it->ifa_name, "lo") == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* 2. Si pas IPV4 -> suivant */
|
|
|
|
if( it->ifa_addr->sa_family != AF_INET )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* 3. Si ok, on retourne l'ip */
|
|
|
|
info = (struct sockaddr_in*) it->ifa_addr;
|
|
|
|
memcpy(result, &info->sin_addr, sizeof(struct in_addr));
|
|
|
|
freeifaddrs(list);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Cas échéant -> aucun résultat
|
|
|
|
=========================================================*/
|
|
|
|
freeifaddrs(list);
|
|
|
|
return NULL;
|
2017-04-28 12:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int setTimeout(int pSocket, const time_t pSec, char pFlags){
|
|
|
|
/* [1] Initialisation des variables
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. Création de la variable */
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
/* 2. On définit le temps du timeout */
|
|
|
|
timeout.tv_sec = pSec;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On applique à la socket
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. Timeout de reception */
|
|
|
|
if( pFlags&TIMEOUT_RECV ){
|
|
|
|
|
|
|
|
if( setsockopt(pSocket, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
|
|
|
close(pSocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Timeout d'envoi */
|
|
|
|
if( pFlags&TIMEOUT_SEND ){
|
|
|
|
|
|
|
|
if( setsockopt(pSocket, SOL_SOCKET, SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
|
|
|
close(pSocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 3. Résultat de succès */
|
|
|
|
return 0;
|
2017-04-26 12:25:05 +00:00
|
|
|
}
|