Vadim Yanitskiy has uploaded this change for review. ( 
https://gerrit.osmocom.org/13720


Change subject: Assign random MSISDN to subscribers created on demand
......................................................................

Assign random MSISDN to subscribers created on demand

Change-Id: I475c71f9902950fa7498855a616e1ec231fad6ac
---
M src/hlr.c
1 file changed, 41 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/20/13720/1

diff --git a/src/hlr.c b/src/hlr.c
index 7facef7..4fb039e 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -34,6 +34,7 @@
 #include <osmocom/ctrl/control_vty.h>
 #include <osmocom/gsm/apn.h>
 #include <osmocom/gsm/gsm48_ie.h>
+#include <osmocom/gsm/gsm_utils.h>

 #include "db.h"
 #include "hlr.h"
@@ -148,19 +149,54 @@
        }
 }

-static void subscr_create_on_demand(const char *imsi)
+static int subscr_create_on_demand(const char *imsi)
 {
-       int rc;
+       uint8_t rand_buf[15];
+       char msisdn[15 + 1];
+       int i, rc;

        if (!g_hlr->create_subscr_on_demand)
-               return;
+               return -1;
        if (db_subscr_exists_by_imsi(g_hlr->dbc, imsi) == 0)
-               return;
+               return -1;

        LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Creating subscriber on demand\n", 
imsi);
        rc = db_subscr_create(g_hlr->dbc, imsi, 1, 1); /* Allow both CS and PS 
domains by default */
-       if (rc)
+       if (rc) {
                LOGP(DMAIN, LOGL_ERROR, "Failed to create subscriber on demand 
(rc=%d): IMSI='%s'\n", rc, imsi);
+               return rc;
+       }
+
+       /* FIXME: this should be optional and configurable! */
+       #define RAND_MSISDN_LEN 6
+
+       /* Generate a random unique MSISDN */
+       while (1) {
+               rc = osmo_get_rand_id(rand_buf, RAND_MSISDN_LEN);
+               if (rc) /* Keep trying until we get some result */
+                       continue;
+
+               /* Shift 0x00 ... 0xff range to 30 ... 39 (ASCII numbers) */
+               for (i = 0; i < RAND_MSISDN_LEN; i++)
+                       msisdn[i] = 48 + (rand_buf[i] % 10);
+               msisdn[i] = '\0';
+
+               /* Ensure there is subscriber with such MSISDN */
+               if (db_subscr_exists_by_msisdn(g_hlr->dbc, msisdn))
+                       break;
+       }
+
+       /* Update MSISDN of the new (just allocated) subscriber */
+       rc = db_subscr_update_msisdn_by_imsi(g_hlr->dbc, imsi, msisdn);
+       if (rc) {
+               LOGP(DMAIN, LOGL_ERROR, "IMSI='%s': Failed to assign 
MSISDN='%s' "
+                                       "(rc=%d)\n", imsi, msisdn, rc);
+               return rc;
+       }
+
+       LOGP(DMAIN, LOGL_INFO, "IMSI='%s': Successfully assigned 
MSISDN='%s'\n", imsi, msisdn);
+
+       return 0;
 }

 /***********************************************************************

--
To view, visit https://gerrit.osmocom.org/13720
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I475c71f9902950fa7498855a616e1ec231fad6ac
Gerrit-Change-Number: 13720
Gerrit-PatchSet: 1
Gerrit-Owner: Vadim Yanitskiy <[email protected]>

Reply via email to