Added return code to setipaddress and removed fprintfs

Added return code to setipaddress and removed fprintfs
This commit is contained in:
MassimilianoPinto
2014-02-14 12:27:33 +01:00
parent 93fc31d9d9
commit badb2e3f2c
2 changed files with 38 additions and 13 deletions

View File

@ -33,6 +33,7 @@
* from gw_read_gwbuff() * from gw_read_gwbuff()
* 25-09-2013 Massimiliano Pinto setipaddress uses getaddrinfo * 25-09-2013 Massimiliano Pinto setipaddress uses getaddrinfo
* 06-02-2014 Mark Riddoch Added parse_bindconfig * 06-02-2014 Mark Riddoch Added parse_bindconfig
* 10-02-2014 Massimiliano Pinto Added return code to setipaddress
* *
*@endverbatim *@endverbatim
*/ */
@ -53,8 +54,9 @@ extern int lm_enabled_logfiles_bitmask;
* *
* @param a Pointer to a struct in_addr into which the address is written * @param a Pointer to a struct in_addr into which the address is written
* @param p The hostname to lookup * @param p The hostname to lookup
* @return 1 on success, 0 on failure
*/ */
void int
setipaddress(struct in_addr *a, char *p) { setipaddress(struct in_addr *a, char *p) {
#ifdef __USE_POSIX #ifdef __USE_POSIX
struct addrinfo *ai = NULL, hint; struct addrinfo *ai = NULL, hint;
@ -73,14 +75,26 @@ setipaddress(struct in_addr *a, char *p) {
hint.ai_flags = AI_PASSIVE; hint.ai_flags = AI_PASSIVE;
hint.ai_family = AF_UNSPEC; hint.ai_family = AF_UNSPEC;
if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) { if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) {
fprintf(stderr, "getaddrinfo(%s) failed with %s", p, gai_strerror(rc)); LOGIF(LE, (skygw_log_write_flush(
LOGFILE_ERROR,
"Error : getaddrinfo failed for [%s] due [%s]",
p,
gai_strerror(rc))));
return 0;
} }
} else { } else {
hint.ai_flags = AI_CANONNAME; hint.ai_flags = AI_CANONNAME;
hint.ai_family = AF_INET; hint.ai_family = AF_INET;
if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) { if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) {
fprintf(stderr, "getaddrinfo(%s) failed with %s", p, gai_strerror(rc)); LOGIF(LE, (skygw_log_write_flush(
LOGFILE_ERROR,
"Error : getaddrinfo failed for [%s] due [%s]",
p,
gai_strerror(rc))));
return 0;
} }
} }
@ -88,9 +102,11 @@ setipaddress(struct in_addr *a, char *p) {
if (ai != NULL) { if (ai != NULL) {
res_addr = (struct sockaddr_in *)(ai->ai_addr); res_addr = (struct sockaddr_in *)(ai->ai_addr);
memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr)); memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr));
}
freeaddrinfo(ai); freeaddrinfo(ai);
return 1;
}
#else #else
struct hostent *h; struct hostent *h;
@ -100,11 +116,18 @@ setipaddress(struct in_addr *a, char *p) {
if (h == NULL) { if (h == NULL) {
if ((a->s_addr = inet_addr(p)) == -1) { if ((a->s_addr = inet_addr(p)) == -1) {
fprintf(stderr, "unknown or invalid address [%s]\n", p); LOGIF(LE, (skygw_log_write_flush(
LOGFILE_ERROR,
"Error : gethostbyname failed for [%s]",
p)));
return 0;
} }
} else { } else {
/* take the first one */ /* take the first one */
memcpy(a, h->h_addr, h->h_length); memcpy(a, h->h_addr, h->h_length);
return 1;
} }
#endif #endif
} }

View File

@ -28,6 +28,8 @@
* 04-07-2013 Massimiliano Pinto Added new MySQL protocol status for asynchronous connection * 04-07-2013 Massimiliano Pinto Added new MySQL protocol status for asynchronous connection
* Added authentication reply status * Added authentication reply status
* 12-07-2013 Massimiliano Pinto Added routines for change_user * 12-07-2013 Massimiliano Pinto Added routines for change_user
* 14-02-2014 Massimiliano Pinto setipaddress returns int
*
*/ */
#include <stdio.h> #include <stdio.h>
@ -294,5 +296,5 @@ int gw_hex2bin(uint8_t *out, const char *in, unsigned int len);
int gw_generate_random_str(char *output, int len); int gw_generate_random_str(char *output, int len);
char *gw_strend(register const char *s); char *gw_strend(register const char *s);
int setnonblocking(int fd); int setnonblocking(int fd);
void setipaddress(struct in_addr *a, char *p); int setipaddress(struct in_addr *a, char *p);
int gw_read_gwbuff(DCB *dcb, GWBUF **head, int b); int gw_read_gwbuff(DCB *dcb, GWBUF **head, int b);