lib: Add "starting_id" to idr_get_new_random()
authorVolker Lendecke <vl@samba.org>
Wed, 4 Jan 2023 10:43:59 +0000 (11:43 +0100)
committerJeremy Allison <jra@samba.org>
Tue, 10 Jan 2023 00:28:37 +0000 (00:28 +0000)
To be used in smbXsrv_open.c, for this we need a lower bound.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/idtree_random.c
lib/util/idtree_random.h
libcli/cldap/cldap.c
libcli/nbt/nbtsocket.c
source3/rpc_server/rpc_server.c
source3/rpc_server/rpc_worker.c
source4/rpc_server/dcerpc_server.c
source4/smb_server/session.c
source4/smb_server/tcon.c

index 80758e74d6dfbb8f3f3dbc09463efb9450a29694..d22245a77340b8722032ab66da1ec62bd1215424 100644 (file)
 /**
   allocate a new id randomly in the given range
 */
-_PUBLIC_ int idr_get_new_random(struct idr_context *idp, void *ptr, int limit)
+_PUBLIC_ int idr_get_new_random(struct idr_context *idp,
+                               void *ptr,
+                               int starting_id,
+                               int limit)
 {
        int id;
 
@@ -48,12 +51,17 @@ _PUBLIC_ int idr_get_new_random(struct idr_context *idp, void *ptr, int limit)
           then start randomly in the bottom half of the range. This can only
           fail if the range is over half full, and finally fallback to any
           free id */
-       id = idr_get_new_above(idp, ptr, 1+(generate_random() % limit), limit);
+       id = idr_get_new_above(
+               idp, ptr, starting_id+(generate_random() % limit), limit);
        if (id == -1) {
-               id = idr_get_new_above(idp, ptr, 1+(generate_random()%(limit/2)), limit);
+               id = idr_get_new_above(
+                       idp,
+                       ptr,
+                       starting_id+(generate_random()%(limit/2)),
+                       limit);
        }
        if (id == -1) {
-               id = idr_get_new_above(idp, ptr, 1, limit);
+               id = idr_get_new_above(idp, ptr, starting_id, limit);
        }
 
        return id;
index 4d3b61c33d594ebab0de161e932c0d6005224f2f..623147c48a7c3981dcc340d42b7b4f27fe4eef35 100644 (file)
@@ -33,6 +33,9 @@
 /**
   allocate a new id randomly in the given range
 */
-int idr_get_new_random(struct idr_context *idp, void *ptr, int limit);
+int idr_get_new_random(struct idr_context *idp,
+                      void *ptr,
+                      int starting_id,
+                      int limit);
 
 #endif /* _SAMBA_IDTREE_RANDOM_H_ */
index 3cca85b992182278c6eeb9058ad80119c149a5d8..22a47705d65479e9eb6a329fabc1d423eaa42296 100644 (file)
@@ -623,8 +623,8 @@ struct tevent_req *cldap_search_send(TALLOC_CTX *mem_ctx,
                state->request.dest = NULL;
        }
 
-       state->message_id = idr_get_new_random(cldap->searches.idr,
-                                              state, UINT16_MAX);
+       state->message_id = idr_get_new_random(
+               cldap->searches.idr, state, 1, UINT16_MAX);
        if (state->message_id == -1) {
                tevent_req_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
                goto post;
index 97b0ca34337fa14af43102f34d4d343b7231e4fa..3e0dd493a911238396bbf886fe24ecc47626f9b2 100644 (file)
@@ -407,7 +407,8 @@ struct nbt_name_request *nbt_name_request_send(TALLOC_CTX *mem_ctx,
 
        /* we select a random transaction id unless the user supplied one */
        if (request->name_trn_id == 0) {
-               id = idr_get_new_random(req->nbtsock->idr, req, UINT16_MAX);
+               id = idr_get_new_random(
+                       req->nbtsock->idr, req, 1, UINT16_MAX);
        } else {
                if (idr_find(req->nbtsock->idr, request->name_trn_id)) goto failed;
                id = idr_get_new_above(req->nbtsock->idr, req, request->name_trn_id,
index 32fbb78f5ffb398965b6887bee04876ea6529ddc..71be70bf79d733305a9ee646b9f21b9b03608a8a 100644 (file)
@@ -161,6 +161,7 @@ static NTSTATUS dcesrv_assoc_group_new(struct dcesrv_call_state *call)
 
        id = idr_get_new_random(dce_ctx->assoc_groups_idr,
                                assoc_group,
+                               1,
                                UINT16_MAX);
        if (id == -1) {
                TALLOC_FREE(assoc_group);
index 2d2bb35af0f189d56b2a1a73397ef8e840e3d09e..1bc84531e5560c96fa58278185b6faa6ff950b91 100644 (file)
@@ -654,7 +654,7 @@ static struct dcesrv_assoc_group *rpc_worker_assoc_group_new(
        }
 
        id = idr_get_new_random(
-               dce_ctx->assoc_groups_idr, assoc_group, UINT16_MAX);
+               dce_ctx->assoc_groups_idr, assoc_group, 1, UINT16_MAX);
        if (id == -1) {
                talloc_free(assoc_group);
                DBG_WARNING("Out of association groups!\n");
index c896b3574a2ee410ff48772d7efc266de1dfd3a3..6ec6f832da2c770a319ffd725d203444b27739a5 100644 (file)
@@ -99,7 +99,8 @@ static struct dcesrv_assoc_group *dcesrv_assoc_group_new(struct dcesrv_connectio
                return NULL;
        }
 
-       id = idr_get_new_random(dce_ctx->assoc_groups_idr, assoc_group, UINT16_MAX);
+       id = idr_get_new_random(
+               dce_ctx->assoc_groups_idr, assoc_group, 1, UINT16_MAX);
        if (id == -1) {
                talloc_free(assoc_group);
                DEBUG(0,(__location__ ": Out of association groups!\n"));
index 321d7dd63888b01be32cbcfa942b3732d0db86cc..a561b2e7fc41832670f8a855a4cca58cf3392a9c 100644 (file)
@@ -141,7 +141,11 @@ struct smbsrv_session *smbsrv_session_new(struct smbsrv_connection *smb_conn,
        if (!sess) return NULL;
        sess->smb_conn = smb_conn;
 
-       i = idr_get_new_random(smb_conn->sessions.idtree_vuid, sess, smb_conn->sessions.idtree_limit);
+       i = idr_get_new_random(
+               smb_conn->sessions.idtree_vuid,
+               sess,
+               1,
+               smb_conn->sessions.idtree_limit);
        if (i == -1) {
                DEBUG(1,("ERROR! Out of connection structures\n"));
                talloc_free(sess);
index 39bc0a5ff5f596452ab89a03cfda2d729d6bc0de..8186bba23ef6e96194979605603595ce6ae8eed5 100644 (file)
@@ -163,7 +163,8 @@ static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn,
                goto failed;
        }
 
-       i = idr_get_new_random(tcons_ctx->idtree_tid, tcon, tcons_ctx->idtree_limit);
+       i = idr_get_new_random(
+               tcons_ctx->idtree_tid, tcon, 1, tcons_ctx->idtree_limit);
        if (i == -1) {
                DEBUG(1,("ERROR! Out of connection structures\n"));
                goto failed;