summaryrefslogtreecommitdiff
path: root/main/netsock2.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/netsock2.c')
-rw-r--r--main/netsock2.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/main/netsock2.c b/main/netsock2.c
index bd682b17d..0e83f27cf 100644
--- a/main/netsock2.c
+++ b/main/netsock2.c
@@ -129,6 +129,40 @@ char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *sa, int format)
return ast_str_buffer(str);
}
+int ast_sockaddr_cidr_bits(const struct ast_sockaddr *sa)
+{
+ struct ast_sockaddr sa_ipv4;
+ const struct ast_sockaddr *sa_tmp;
+ int bits = 0;
+ int bytes;
+ int i;
+ int j;
+ char *addr;
+
+ if (ast_sockaddr_isnull(sa)) {
+ return 0;
+ }
+
+ if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
+ sa_tmp = &sa_ipv4;
+ } else {
+ sa_tmp = sa;
+ }
+
+ bytes = sa_tmp->len;
+ addr = ((struct sockaddr *)&sa_tmp->ss)->sa_data;
+
+ for (i = 0; i < bytes ; ++i) {
+ for (j = 0; j < 8; ++j) {
+ if ((addr[i] >> j) & 1) {
+ bits++;
+ }
+ }
+ }
+
+ return bits;
+}
+
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
{
char *s = str;