]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
LDAPworldP15: Internal LDAP function rename
[openldap] / libraries / libldap / abandon.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  abandon.c
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #if !defined( MACOS ) && !defined( DOS )
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #endif
19
20 #if defined( DOS ) || defined( _WIN32 )
21 #include <malloc.h>
22 #include "msdos.h"
23 #endif /* DOS */
24
25 #ifdef MACOS
26 #include <stdlib.h>
27 #include "macos.h"
28 #endif /* MACOS */
29
30 #include "lber.h"
31 #include "ldap.h"
32 #include "ldap-int.h"
33
34 #ifdef NEEDPROTOS
35 static int do_abandon( LDAP *ld, int origid, int msgid );
36 #else /* NEEDPROTOS */
37 static int do_abandon();
38 #endif /* NEEDPROTOS */
39 /*
40  * ldap_abandon - perform an ldap (and X.500) abandon operation. Parameters:
41  *
42  *      ld              LDAP descriptor
43  *      msgid           The message id of the operation to abandon
44  *
45  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
46  *
47  * Example:
48  *      ldap_abandon( ld, msgid );
49  */
50 int
51 ldap_abandon( LDAP *ld, int msgid )
52 {
53         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
54         return( do_abandon( ld, msgid, msgid ));
55 }
56
57
58 static int
59 do_abandon( LDAP *ld, int origid, int msgid )
60 {
61         BerElement      *ber;
62         int             i, err, sendabandon;
63         Sockbuf         *sb;
64 #ifdef LDAP_REFERRALS
65         LDAPRequest     *lr;
66 #endif /* LDAP_REFERRALS */
67
68         /*
69          * An abandon request looks like this:
70          *      AbandonRequest ::= MessageID
71          */
72
73         Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
74                 origid, msgid, 0 );
75
76         sendabandon = 1;
77
78 #ifdef LDAP_REFERRALS
79         /* find the request that we are abandoning */
80         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
81                 if ( lr->lr_msgid == msgid ) {  /* this message */
82                         break;
83                 }
84                 if ( lr->lr_origid == msgid ) { /* child:  abandon it */
85                         do_abandon( ld, msgid, lr->lr_msgid );
86                 }
87         }
88
89         if ( lr != NULL ) {
90                 if ( origid == msgid && lr->lr_parent != NULL ) {
91                         /* don't let caller abandon child requests! */
92                         ld->ld_errno = LDAP_PARAM_ERROR;
93                         return( -1 );
94                 }
95                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
96                         /* no need to send abandon message */
97                         sendabandon = 0;
98                 }
99         }
100 #endif /* LDAP_REFERRALS */
101
102         if ( ldap_msgdelete( ld, msgid ) == 0 ) {
103                 ld->ld_errno = LDAP_SUCCESS;
104                 return( 0 );
105         }
106
107         err = 0;
108         if ( sendabandon ) {
109                 /* create a message to send */
110                 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
111                         err = -1;
112                         ld->ld_errno = LDAP_NO_MEMORY;
113                 } else {
114 #ifdef CLDAP
115                         if ( ld->ld_sb.sb_naddr > 0 ) {
116                                 err = ber_printf( ber, "{isti}",
117                                     ++ld->ld_msgid, ld->ld_cldapdn,
118                                     LDAP_REQ_ABANDON, msgid );
119                         } else {
120 #endif /* CLDAP */
121                                 err = ber_printf( ber, "{iti}", ++ld->ld_msgid,
122                                     LDAP_REQ_ABANDON, msgid );
123 #ifdef CLDAP
124                         }
125 #endif /* CLDAP */
126
127                         if ( err == -1 ) {
128                                 ld->ld_errno = LDAP_ENCODING_ERROR;
129                                 ber_free( ber, 1 );
130                         } else {
131                                 /* send the message */
132 #ifdef LDAP_REFERRALS
133                                 if ( lr != NULL ) {
134                                         sb = lr->lr_conn->lconn_sb;
135                                 } else {
136                                         sb = &ld->ld_sb;
137                                 }
138 #else /* LDAP_REFERRALS */
139                                 sb = &ld->ld_sb;
140 #endif /* LDAP_REFERRALS */
141                                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
142                                         ld->ld_errno = LDAP_SERVER_DOWN;
143                                         err = -1;
144                                 } else {
145                                         err = 0;
146                                 }
147                         }
148                 }
149         }
150
151 #ifdef LDAP_REFERRALS
152         if ( lr != NULL ) {
153                 if ( sendabandon ) {
154                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
155                 }
156                 if ( origid == msgid ) {
157                         ldap_free_request( ld, lr );
158                 }
159         }
160 #endif /* LDAP_REFERRALS */
161
162
163         if ( ld->ld_abandoned == NULL ) {
164                 if ( (ld->ld_abandoned = (int *) malloc( 2 * sizeof(int) ))
165                     == NULL ) {
166                         ld->ld_errno = LDAP_NO_MEMORY;
167                         return( -1 );
168                 }
169                 i = 0;
170         } else {
171                 for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
172                         ;       /* NULL */
173                 if ( (ld->ld_abandoned = (int *) realloc( (char *)
174                     ld->ld_abandoned, (i + 2) * sizeof(int) )) == NULL ) {
175                         ld->ld_errno = LDAP_NO_MEMORY;
176                         return( -1 );
177                 }
178         }
179         ld->ld_abandoned[i] = msgid;
180         ld->ld_abandoned[i + 1] = -1;
181
182         if ( err != -1 ) {
183                 ld->ld_errno = LDAP_SUCCESS;
184         }
185         return( err );
186 }