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 */
Messenger_Options options = {0};
options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
m = new_messenger(&options);
m = new_messenger(&options, 0);
/* setup a default friend and friendnum */
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};
options.ipv6enabled = ipv6enabled;
m = new_messenger(&options);
m = new_messenger(&options, 0);
if ( !m ) {
fputs("Failed to allocate messenger datastructure\n", stderr);

View File

@ -1593,24 +1593,34 @@ static void LANdiscovery(Messenger *m)
}
/* 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));
if (error)
*error = MESSENGER_ERROR_OTHER;
if ( ! m )
return NULL;
unsigned int net_err = 0;
if (options->udp_disabled) {
/* this is the easiest way to completely disable UDP without changing too much code. */
m->net = calloc(1, sizeof(Networking_Core));
} else {
IP ip;
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) {
free(m);
if (error && net_err == 1) {
*error = MESSENGER_ERROR_PORT;
}
return NULL;
}
@ -1654,6 +1664,9 @@ Messenger *new_messenger(Messenger_Options *options)
set_nospam(&(m->fr), random_int());
set_filter_function(&(m->fr), &friend_already_added, m);
if (error)
*error = MESSENGER_ERROR_NONE;
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);
/**********************************************/
enum {
MESSENGER_ERROR_NONE,
MESSENGER_ERROR_PORT,
MESSENGER_ERROR_OTHER
};
/* Run this at startup.
* return allocated instance of Messenger on success.
* 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
* Free all datastructures.

View File

@ -471,7 +471,7 @@ static void at_shutdown(void)
*/
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.
@ -481,8 +481,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
*
* return Networking_Core object if no 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 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;
}
if (error)
*error = 2;
/* 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) {
#ifdef DEBUG
@ -643,6 +648,9 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to)
if (tries > 0)
errno = 0;
if (error)
*error = 0;
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);
kill_networking(temp);
if (error)
*error = 1;
return NULL;
}

View File

@ -362,9 +362,11 @@ void networking_poll(Networking_Core *net);
*
* return Networking_Core object if no 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_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). */
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);
//TODO: TOX_ERR_NEW_MALLOC
//TODO: TOX_ERR_NEW_PORT_ALLOC
unsigned int m_error;
Messenger *m = new_messenger(&m_options, &m_error);
if (!new_groupchats(m)) {
kill_messenger(m);
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
if (m_error == MESSENGER_ERROR_PORT) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC);
} else {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
}
return NULL;
}