]> git.sur5r.net Git - openldap/blob - servers/ldapd/message.c
Blindly added Autoconf support to ldapd.
[openldap] / servers / ldapd / message.c
1 /*
2  * Copyright (c) 1990 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19
20 #include <quipu/commonarg.h>
21 #include <quipu/ds_error.h>
22
23 #include "lber.h"
24 #include "ldap.h"
25 #include "common.h"
26
27 static struct msg       *messages;
28
29 struct msg *add_msg(
30     int                 msgid,
31     int                 msgtype,
32     BerElement          *ber,
33     struct conn         *dsaconn,
34     int                 udp,
35     struct sockaddr     *clientaddr
36 )
37 {
38         struct msg              *new;
39         static int              uniqid = 0;
40
41         /* make a new message */
42         if ( (new = (struct msg *) malloc( sizeof(struct msg) )) == NULL ) {
43                 Debug( LDAP_DEBUG_ANY, "addmsg: malloc failed\n", 0, 0, 0 );
44                 return( NULL );
45         }
46         new->m_msgid = msgid;
47         new->m_uniqid = ++uniqid;
48         new->m_msgtype = msgtype;
49         new->m_ber = ber;
50         new->m_mods = NULL;
51         new->m_conn = dsaconn;
52         new->m_conn->c_refcnt++;
53         new->m_next = NULL;
54
55 #ifdef LDAP_CONNECTIONLESS
56         new->m_cldap = udp;
57         new->m_searchbase = NULLDN;
58
59         if ( udp ) {
60                 new->m_clientaddr = *clientaddr;
61                 Debug( LDAP_DEBUG_TRACE, "udp message from %s port %d\n", 
62                     inet_ntoa( ((struct sockaddr_in *)clientaddr)->sin_addr ),
63                     ((struct sockaddr_in *)clientaddr)->sin_port, 0 );
64         }
65 #endif
66
67         /* add it to the front of the queue */
68         new->m_next = messages;
69         messages = new;
70
71         return( new );
72 }
73
74 struct msg *get_msg( int uniqid )
75 {
76         struct msg      *tmp;
77
78         for ( tmp = messages; tmp != NULL; tmp = tmp->m_next ) {
79                 if ( tmp->m_uniqid == uniqid )
80                         return( tmp );
81         }
82
83         return( NULL );
84 }
85
86 int
87 del_msg( struct msg *m )
88 {
89         struct msg      *cur, *prev;
90
91         prev = NULL;
92         for ( cur = messages; cur != NULL; cur = cur->m_next ) {
93                 if ( cur == m )
94                         break;
95                 prev = cur;
96         }
97
98         if ( cur == NULL ) {
99                 Debug( LDAP_DEBUG_ANY, "delmsg: cannot find msg %x\n", m,
100                     0, 0 );
101                 return( -1 );
102         }
103
104         if ( prev == NULL ) {
105                 messages = cur->m_next;
106         } else {
107                 prev->m_next = cur->m_next;
108         }
109         conn_free( cur->m_conn );
110         modlist_free( cur->m_mods );
111         ber_free( cur->m_ber, 1 );
112 #ifdef LDAP_CONNECTIONLESS
113         if ( cur->m_searchbase != NULLDN ) {
114             dn_free( cur->m_searchbase );
115         }
116 #endif /* LDAP_CONNECTIONLESS */
117         free( (char *) cur );
118
119         return( 0 );
120 }
121
122 /*
123  * send_msg - Send a messge in response to every outstanding request on
124  * a given connection.  This is used, for example, when an association to
125  * a dsa fails.  It deletes messages to which it responds.
126  */
127
128 void
129 send_msg(
130     struct conn *conn,
131     Sockbuf     *clientsb,
132     int         err,
133     char        *str
134 )
135 {
136         struct msg      *tmp, *next;
137
138         next = NULL;
139         for ( tmp = messages; tmp != NULL; tmp = next ) {
140                 next = tmp->m_next;
141
142                 if ( tmp->m_conn == conn ) {
143                         send_ldap_msgresult( clientsb, tmp->m_msgtype, tmp,
144                             err, NULL, str );
145                 }
146
147                 del_msg( tmp );
148         }
149 }
150
151
152 #ifdef LDAP_CONNECTIONLESS
153 struct msg *
154 get_cldap_msg(
155     int                 msgid,
156     int                 msgtype,
157     struct sockaddr     *fromaddr
158 )
159 {
160         struct msg      *tmp;
161
162         for ( tmp = messages; tmp != NULL; tmp = tmp->m_next ) {
163                 if ( tmp->m_cldap && tmp->m_msgid == msgid &&
164                     tmp->m_msgtype == msgtype &&
165                     ((struct sockaddr_in *)&tmp->m_clientaddr)->sin_port ==
166                     ((struct sockaddr_in *)fromaddr)->sin_port &&
167                     ((struct sockaddr_in *)&tmp->m_clientaddr)->sin_addr.s_addr
168                     == ((struct sockaddr_in *)fromaddr)->sin_addr.s_addr ) {
169                         break;
170                 }
171         }
172
173         return( tmp );
174 }
175 #endif /* LDAP_CONNECTIONLESS */