tox_new now sets error to TOX_ERR_NEW_PORT_ALLOC when binding to port fails.

This commit is contained in:
irungentoo 2015-03-12 13:03:14 -04:00
parent 4ee017078b
commit 7afab000f7
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
7 changed files with 53 additions and 12 deletions

View File

@ -337,7 +337,7 @@ int main(int argc, char *argv[])
/* IPv6 status from global define */ /* IPv6 status from global define */
Messenger_Options options = {0}; Messenger_Options options = {0};
options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
m = new_messenger(&options); m = new_messenger(&options, 0);
/* setup a default friend and friendnum */ /* setup a default friend and friendnum */
if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)

View File

@ -112,7 +112,7 @@ int main(int argc, char *argv[])
Messenger_Options options = {0}; Messenger_Options options = {0};
options.ipv6enabled = ipv6enabled; options.ipv6enabled = ipv6enabled;
m = new_messenger(&options); m = new_messenger(&options, 0);
if ( !m ) { if ( !m ) {
fputs("Failed to allocate messenger datastructure\n", stderr); fputs("Failed to allocate messenger datastructure\n", stderr);

View File

@ -1593,24 +1593,34 @@ static void LANdiscovery(Messenger *m)
} }
/* Run this at startup. */ /* Run this at startup. */
Messenger *new_messenger(Messenger_Options *options) Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
{ {
Messenger *m = calloc(1, sizeof(Messenger)); Messenger *m = calloc(1, sizeof(Messenger));
if (error)
*error = MESSENGER_ERROR_OTHER;
if ( ! m ) if ( ! m )
return NULL; return NULL;
unsigned int net_err = 0;
if (options->udp_disabled) { if (options->udp_disabled) {
/* this is the easiest way to completely disable UDP without changing too much code. */ /* this is the easiest way to completely disable UDP without changing too much code. */
m->net = calloc(1, sizeof(Networking_Core)); m->net = calloc(1, sizeof(Networking_Core));
} else { } else {
IP ip; IP ip;
ip_init(&ip, options->ipv6enabled); ip_init(&ip, options->ipv6enabled);
m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1]); m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1], &net_err);
} }
if (m->net == NULL) { if (m->net == NULL) {
free(m); free(m);
if (error && net_err == 1) {
*error = MESSENGER_ERROR_PORT;
}
return NULL; return NULL;
} }
@ -1654,6 +1664,9 @@ Messenger *new_messenger(Messenger_Options *options)
set_nospam(&(m->fr), random_int()); set_nospam(&(m->fr), random_int());
set_filter_function(&(m->fr), &friend_already_added, m); set_filter_function(&(m->fr), &friend_already_added, m);
if (error)
*error = MESSENGER_ERROR_NONE;
return m; return m;
} }

View File

@ -711,11 +711,20 @@ void custom_lossless_packet_registerhandler(Messenger *m, void (*packet_handler_
int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length); int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length);
/**********************************************/ /**********************************************/
enum {
MESSENGER_ERROR_NONE,
MESSENGER_ERROR_PORT,
MESSENGER_ERROR_OTHER
};
/* Run this at startup. /* Run this at startup.
* return allocated instance of Messenger on success. * return allocated instance of Messenger on success.
* return 0 if there are problems. * return 0 if there are problems.
*
* if error is not NULL it will be set to one of the values in the enum above.
*/ */
Messenger *new_messenger(Messenger_Options *options); Messenger *new_messenger(Messenger_Options *options, unsigned int *error);
/* Run this before closing shop /* Run this before closing shop
* Free all datastructures. * Free all datastructures.

View File

@ -471,7 +471,7 @@ static void at_shutdown(void)
*/ */
Networking_Core *new_networking(IP ip, uint16_t port) Networking_Core *new_networking(IP ip, uint16_t port)
{ {
return new_networking_ex(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM)); return new_networking_ex(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), 0);
} }
/* Initialize networking. /* Initialize networking.
@ -481,8 +481,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
* *
* return Networking_Core object if no problems * return Networking_Core object if no problems
* return NULL if there are problems. * return NULL if there are problems.
*
* If error is non NULL it is set to 0 if no issues, 1 if bind failed, 2 if other.
*/ */
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to) Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error)
{ {
/* If both from and to are 0, use default port range /* If both from and to are 0, use default port range
* If one is 0 and the other is non-0, use the non-0 value as only port * If one is 0 and the other is non-0, use the non-0 value as only port
@ -501,6 +503,9 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to)
port_to = temp; port_to = temp;
} }
if (error)
*error = 2;
/* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
if (ip.family != AF_INET && ip.family != AF_INET6) { if (ip.family != AF_INET && ip.family != AF_INET6) {
#ifdef DEBUG #ifdef DEBUG
@ -643,6 +648,9 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to)
if (tries > 0) if (tries > 0)
errno = 0; errno = 0;
if (error)
*error = 0;
return temp; return temp;
} }
@ -658,6 +666,10 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to)
ip_ntoa(&ip), port_from, port_to); ip_ntoa(&ip), port_from, port_to);
kill_networking(temp); kill_networking(temp);
if (error)
*error = 1;
return NULL; return NULL;
} }

View File

@ -362,9 +362,11 @@ void networking_poll(Networking_Core *net);
* *
* return Networking_Core object if no problems * return Networking_Core object if no problems
* return NULL if there are problems. * return NULL if there are problems.
*
* If error is non NULL it is set to 0 if no issues, 1 if bind failed, 2 if other.
*/ */
Networking_Core *new_networking(IP ip, uint16_t port); Networking_Core *new_networking(IP ip, uint16_t port);
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to); Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error);
/* Function to cleanup networking stuff (doesn't do much right now). */ /* Function to cleanup networking stuff (doesn't do much right now). */
void kill_networking(Networking_Core *net); void kill_networking(Networking_Core *net);

View File

@ -154,13 +154,18 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng
} }
} }
Messenger *m = new_messenger(&m_options); unsigned int m_error;
//TODO: TOX_ERR_NEW_MALLOC Messenger *m = new_messenger(&m_options, &m_error);
//TODO: TOX_ERR_NEW_PORT_ALLOC
if (!new_groupchats(m)) { if (!new_groupchats(m)) {
kill_messenger(m); kill_messenger(m);
if (m_error == MESSENGER_ERROR_PORT) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC);
} else {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
}
return NULL; return NULL;
} }