Description: Add support for radcli library.
Author: Răzvan Crainea <razvan@opensips.org>
Last-Update: 2016-12-01
--- a/Makefile
+++ b/Makefile
@@ -31,14 +31,6 @@
 #  2007-09-28  added db_berkeley (wiquan)
 #
 
-#FREERADIUS=1
-# freeradius libs check (must be done in toplevel makefile)
-ifneq ("$(wildcard /usr/include/freeradius-client.h)","")
-FREERADIUS=1
-else
-#FREERADIUS=0
-endif
-
 #SQLITE_BIND=1
 NICER?=1
 auto_gen=lex.yy.c cfg.tab.c   #lexx, yacc etc
@@ -124,7 +116,6 @@
 
 ALLDEP=Makefile Makefile.sources Makefile.defs Makefile.rules Makefile.conf
 
-
 install_docs := README-MODULES AUTHORS NEWS README
 ifneq ($(skip-install-doc),yes)
 	install_docs += INSTALL
@@ -179,6 +170,7 @@
 .PHONY: all
 all: $(NAME) modules utils
 
+
 .PHONY: app
 app: $(NAME)
 
--- a/Makefile.defs
+++ b/Makefile.defs
@@ -601,13 +601,6 @@
 	DEFS+= -DVERSIONTYPE='"$(VERSIONTYPE)"' -DTHISREVISION='"$(THISREVISION)"'
 endif
 
-ifneq ($(FREERADIUS),)
-	DEFS+= -DUSE_FREERADIUS
-	RADIUS_LIB= -lfreeradius-client
-else
-	RADIUS_LIB= -lradiusclient-ng
-endif
-
 ifneq ($(SQLITE_BIND),)
 	DEFS+= -DSQLITE_BIND
 endif
--- a/modules/aaa_radius/Makefile
+++ b/modules/aaa_radius/Makefile
@@ -10,10 +10,32 @@
 
 ETC_DIR?=../../etc/
 
-# Comment the next two line and uncomment the following two if you want
-# to enable Radius support
-DEFS+= -I$(LOCALBASE)/include
-LIBS=-L$(LOCALBASE)/lib $(RADIUS_LIB)
+# Find the radius library we shall use
+#
+ifeq ($(RADIUSCLIENT),)
+RADIUSCLIENT=$(shell if [ -n "`ldconfig -p | grep radcli`" ]; then echo "RADCLI"; \
+	elif [ -n "`ldconfig -p | grep freeradius`" ]; then echo "FREERADIUS"; \
+	elif [ -n "`ldconfig -p | grep radiusclient-ng`" ];then echo "RADIUSCLIENT"; fi)
+endif
+
+
+ifneq ($(RADIUSCLIENT), RADCLI)
+ifneq ($(RADIUSCLIENT), FREERADIUS)
+ifeq ($(RADIUSCLIENT), RADIUSCLIENT)
+	DEFS+= -DRADIUSCLIENT
+	RADIUS_LIB= -lradiusclient-ng
+endif
+else
+	DEFS+= -DFREERADIUS
+	RADIUS_LIB= -lfreeradius-client
+endif
+else
+	RADIUS_LIB= -lradiusclient-ng
+	DEFS+= -DRADCLI
+	RADIUS_LIB= -lradcli
+endif
+
+LIBS=$(RADIUS_LIB)
 
 include ../../Makefile.modules
 
--- a/modules/aaa_radius/README
+++ b/modules/aaa_radius/README
@@ -102,9 +102,18 @@
        http://developer.berlios.de/projects/radiusclient-ng/.
      * freeradius-client See http://freeradius.org/.
 
-   By default, radiusclient-ng is used. To change at compile time
-   to freeradius, uncomment the USE_FREERADIUS=1 line in main
-   Makefile.
+   One can force used radius library by setting RADIUSCLIENT env,
+   before compiling the module, to one of the following values:
+     * RADCLI libradcli-dev library shall be used;
+     * FREERADIUS libfreeradius-client-dev library shall be used;
+     * RADIUSCLIENT libradiusclient-ng library shall be used;
+
+   IMPORTANT: If the selected library is not installed the module
+   won't compile. NOTE: If RADIUSCLIENT env not set, the module
+   will try to find one of the three radius libraries in the
+   following order: radcli, freeradius, radiusclient-ng. That is
+   if radcli library is installed it shall be used, else
+   freeradius shall be looked for and so on.
 
 1.3. Exported Parameters
 
--- a/modules/aaa_radius/aaa_radius.c
+++ b/modules/aaa_radius/aaa_radius.c
@@ -31,10 +31,16 @@
  * to get information from the RADIUS reply.
  */
 
-#ifndef USE_FREERADIUS
-	#include <radiusclient-ng.h>
-#else
+#ifdef FREERADIUS
 	#include <freeradius-client.h>
+#else
+	#ifdef RADCLI
+		#include <radcli/radcli.h>
+	#else
+		#ifdef RADIUSCLIENT
+			#include <radiusclient-ng.h>
+		#endif
+	#endif
 #endif
 
 #include "../../sr_module.h"
--- a/modules/aaa_radius/doc/aaa_radius_admin.xml
+++ b/modules/aaa_radius/doc/aaa_radius_admin.xml
@@ -70,8 +70,25 @@
 			</listitem>
 			</itemizedlist>
 			<para>
-				By default, radiusclient-ng is used. To change at compile time
-				to freeradius, uncomment the USE_FREERADIUS=1 line in main Makefile.
+				One can force the radius library that is usedby setting RADIUSCLIENT env, before compiling the module, to one of the following values:
+				<itemizedlist>
+					<listitem>
+						<para><emphasis>RADCLI  ***</emphasis> libradcli-dev library shall be used;</para>
+					</listitem>
+
+					<listitem>
+						<para><emphasis>FREERADIUS  ***</emphasis> libfreeradius-client-dev library shall be used;</para>
+					</listitem>
+
+					<listitem>
+						<para><emphasis>RADIUSCLIENT  ***</emphasis> libradiusclient-ng library shall be used;</para>
+					</listitem>
+				</itemizedlist>
+
+				<para>IMPORTANT: If the selected library is not installed the module won't compile.</para>
+				<para>NOTE: If RADIUSCLIENT env not set, the module will try to find one of the three radius libraries in
+				the following order: radcli, freeradius, radiusclient-ng. That is if radcli library is installed
+				it shall be used, else freeradius shall be looked for and so on.</para>
 			</para>
 	</section>
 	</section>
--- a/modules/aaa_radius/rad.c
+++ b/modules/aaa_radius/rad.c
@@ -28,10 +28,16 @@
  * This is the Radius implementation for the generic AAA Interface.
  */
 
-#ifndef USE_FREERADIUS
-	#include <radiusclient-ng.h>
-#else
+#ifdef FREERADIUS
 	#include <freeradius-client.h>
+#else
+	#ifdef RADCLI
+		#include <radcli/radcli.h>
+	#else
+		#ifdef RADIUSCLIENT
+			#include <radiusclient-ng.h>
+		#endif
+	#endif
 #endif
 
 #ifndef REJECT_RC
@@ -45,6 +51,33 @@
 #include "rad.h"
 #include "../../ut.h"
 #include "../../usr_avp.h"
+#include "../../resolve.h"
+
+/**
+ * this function is removed from current versions
+ * of FREERADIUS-CLIENT and RADCLI because it only offers
+ * support for IPv4
+ * but since the whole code is built around IPv4 we will
+ * implement it only for IPv4 usage
+ */
+#ifdef RADCLI
+uint32_t rc_get_ipaddr (char *host)
+{
+	const struct hostent* he;
+	struct in_addr** addr_list;
+
+	he=resolvehost(host, 0/*do test if is ip*/);
+
+	/* FIXME the function is not for IPV6 */
+	addr_list = (struct in_addr **)he->h_addr_list;
+	if (addr_list[0])
+		return addr_list[0]->s_addr;
+
+	return 0;
+
+}
+#endif
+
 
 
 /*
--- a/modules/aaa_radius/rad.h
+++ b/modules/aaa_radius/rad.h
@@ -47,4 +47,7 @@
 
 int extract_avp(VALUE_PAIR* vp);
 
+#ifdef RADCLI
+uint32_t rc_get_ipaddr (char *host);
+#endif
 #endif
--- a/version.h
+++ b/version.h
@@ -171,7 +171,6 @@
 #define NOSMP_STR ""
 #endif
 
-
 #define OPENSIPS_COMPILE_FLAGS \
 	STATS_STR EXTRA_DEBUG_STR \
 	DISABLE_NAGLE_STR USE_MCAST_STR NO_DEBUG_STR NO_LOG_STR \
