Now checks socklist size and does realloc if needed.

FossilOrigin-Name: 70b13d7684bec9c6632d2f8343c724a74737ef7d57cbe2f0ccf3e4e259e34a84
This commit is contained in:
7u83@mail.ru 2016-02-18 08:25:45 +00:00
parent 969e1e9003
commit e45f87e9f7

View File

@ -19,10 +19,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h>
#include <pthread.h> #include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "capwap/log.h" #include "capwap/log.h"
@ -32,9 +32,6 @@
#include "socklist.h" #include "socklist.h"
#include "stdio.h"
struct socklistelem *socklist = 0; struct socklistelem *socklist = 0;
int socklist_len; int socklist_len;
static pthread_mutex_t socklist_mutex; static pthread_mutex_t socklist_mutex;
@ -111,7 +108,6 @@ int socklist_find_reply_socket(struct sockaddr *sa,int port)
continue; continue;
} }
/* we want first the same port */ /* we want first the same port */
if (sock_getport(&socklist[i].addr) != port) { if (sock_getport(&socklist[i].addr) != port) {
continue; continue;
@ -123,13 +119,10 @@ int socklist_find_reply_socket(struct sockaddr *sa,int port)
bestindex = i; bestindex = i;
} }
/* the next checks only aply to IPv4 */ /* the next checks only aply to IPv4 */
if (socklist[i].addr.sa_family != AF_INET) if (socklist[i].addr.sa_family != AF_INET)
continue; continue;
/* get our source address and netmask */ /* get our source address and netmask */
uint32_t addr = uint32_t addr =
((struct sockaddr_in *) &socklist[i].addr)->sin_addr.s_addr; ((struct sockaddr_in *) &socklist[i].addr)->sin_addr.s_addr;
@ -139,12 +132,12 @@ int socklist_find_reply_socket(struct sockaddr *sa,int port)
/* get source of requested address */ /* get source of requested address */
uint32_t saddr = ((struct sockaddr_in *) sa)->sin_addr.s_addr; uint32_t saddr = ((struct sockaddr_in *) sa)->sin_addr.s_addr;
/* if the request comes from the same subnet where our
* socket is cconnected, this is our new best socked.
* So we can serve AP's w*here no deault route is configured */
if ((addr & mask) == (saddr & mask)) { if ((addr & mask) == (saddr & mask)) {
bestindex = i; bestindex = i;
} }
} }
if (bestindex != -1) if (bestindex != -1)
@ -323,9 +316,29 @@ int socklist_add_multicast(const char *addr, const char *port, int ac_proto)
return 1; return 1;
} }
static int socklist_check_size()
{
if (socklist_len>0 && (socklist_len % SOCKLIST_SIZE)==0){
struct socklistelem *newsocklist;
newsocklist = realloc(socklist, sizeof(struct socklistelem)*(socklist_len+SOCKLIST_SIZE));
if (!newsocklist) {
cw_log(LOG_ERR,"Can't increase socklist size, realoc failed");
return 0;
}
socklist = newsocklist;
}
return 1;
}
int socklist_add_unicast(const char *addr, const char *port, int ac_proto) int socklist_add_unicast(const char *addr, const char *port, int ac_proto)
{ {
if (!socklist_check_size())
return 0;
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *res, *res0; struct addrinfo *res, *res0;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
@ -400,6 +413,9 @@ int socklist_add_unicast(const char *addr, const char *port, int ac_proto)
int socklist_add_broadcast(const char *addr, const char *port, int ac_proto) int socklist_add_broadcast(const char *addr, const char *port, int ac_proto)
{ {
if (!socklist_check_size())
return 0;
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *res, *res0; struct addrinfo *res, *res0;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));