forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
221 lines (182 loc) · 5.47 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package ldapserver
import (
"bufio"
"log"
"net"
"reflect"
"sync"
"time"
)
type client struct {
Numero int
srv *Server
rwc net.Conn
br *bufio.Reader
bw *bufio.Writer
chanOut chan response
wg sync.WaitGroup
closing bool
requestList map[int]Message
}
func (c *client) GetConn() net.Conn {
return c.rwc
}
func (c *client) SetConn(conn net.Conn) {
c.rwc = conn
c.br = bufio.NewReader(c.rwc)
c.bw = bufio.NewWriter(c.rwc)
}
func (c *client) GetMessageByID(messageID int) (*Message, bool) {
if requestToAbandon, ok := c.requestList[messageID]; ok {
return &requestToAbandon, true
}
return nil, false
}
func (c *client) Addr() net.Addr {
return c.rwc.RemoteAddr()
}
func (c *client) serve() {
defer c.close()
if onc := c.srv.OnNewConnection; onc != nil {
if err := onc(c.rwc); err != nil {
log.Printf("Erreur OnNewConnection: %s", err)
return
}
}
// Create the ldap response queue to be writted to client (buffered to 20)
// buffered to 20 means that If client is slow to handler responses, Server
// Handlers will stop to send more respones
c.chanOut = make(chan response)
// for each message in c.chanOut send it to client
go func() {
for msg := range c.chanOut {
c.writeLdapResult(msg)
}
}()
// Listen for server signal to shutdown
go func() {
for {
select {
case <-c.srv.chDone: // server signals shutdown process
r := NewExtendedResponse(LDAPResultUnwillingToPerform)
r.DiagnosticMessage = "server is about to stop"
r.ResponseName = NoticeOfDisconnection
c.chanOut <- r
c.rwc.SetReadDeadline(time.Now().Add(time.Second))
return
default:
//FIX: This cause a Race condition
if c.closing == true {
return
}
}
}
}()
c.requestList = make(map[int]Message)
for {
if c.srv.ReadTimeout != 0 {
c.rwc.SetReadDeadline(time.Now().Add(c.srv.ReadTimeout))
}
//Read client input as a ASN1/BER binary message
messagePacket, err := readMessagePacket(c.br)
if err != nil {
log.Printf("Error readMessagePacket: %s", err)
return
}
// if client is in closing mode, drop message and exit
// FIX: this cause a race condition
if c.closing == true {
log.Print("one client message dropped !")
return
}
//Convert ASN1 binaryMessage to a ldap Request Message
var message Message
message, err = messagePacket.readMessage()
if err != nil {
log.Printf("Error reading Message : %s", err.Error())
continue
}
log.Printf("<<< %d - %s - hex=%x", c.Numero, reflect.TypeOf(message.protocolOp).Name(), messagePacket.Packet.Bytes())
// TODO: Use a implementation to limit runnuning request by client
// solution 1 : when the buffered output channel is full, send a busy
// solution 2 : when 10 client requests (goroutines) are running, send a busy message
// And when the limit is reached THEN send a BusyLdapMessage
// When message is an UnbindRequest, stop serving
if _, ok := message.protocolOp.(UnbindRequest); ok {
return
}
// If client requests a startTls, do not handle it in a
// goroutine, connection has to remain free until TLS is OK
// @see RFC https://tools.ietf.org/html/rfc4511#section-4.14.1
if req, ok := message.protocolOp.(ExtendedRequest); ok {
if req.GetResponseName() == NoticeOfStartTLS {
c.wg.Add(1)
c.ProcessRequestMessage(message)
continue
}
}
// TODO: go/non go routine choice should be done in the ProcessRequestMessage
// not in the client.serve func
c.wg.Add(1)
go c.ProcessRequestMessage(message)
}
}
// close closes client,
// * stop reading from client
// * signals to all currently running request processor to stop
// * wait for all request processor to end
// * close client connection
// * signal to server that client shutdown is ok
func (c *client) close() {
log.Printf("client %d close()", c.Numero)
c.closing = true //FIXME: subject to data race condition ?
// stop reading from client
c.rwc.SetReadDeadline(time.Now().Add(time.Second))
// TODO: Send a Disconnection notification
// signals to all currently running request processor to stop
for messageID, request := range c.requestList {
log.Printf("Client [%d] sent abandon signal to request[messageID = %d]", c.Numero, messageID)
go request.Abandon()
}
// wait for all request processor to end
//log.Printf("waiting the end of current (%d) client's requests", c.Numero)
c.wg.Wait()
close(c.chanOut)
log.Printf("client [%d] request processors ended", c.Numero)
// close client connection
c.rwc.Close()
log.Printf("client [%d] connection closed", c.Numero)
// signal to server that client shutdown is ok
c.srv.wg.Done()
}
func (c *client) writeLdapResult(lr response) {
data := newMessagePacket(lr).Bytes()
log.Printf(">>> %d - %s - hex=%x", c.Numero, reflect.TypeOf(lr).Elem().Name(), data)
c.bw.Write(data)
c.bw.Flush()
}
// ResponseWriter interface is used by an LDAP handler to
// construct an LDAP response.
type ResponseWriter interface {
// Write writes the LDAPResponse to the connection as part of an LDAP reply.
Write(lr response)
}
type responseWriterImpl struct {
chanOut chan response
messageID int
}
func (w responseWriterImpl) Write(lr response) {
lr.SetMessageID(w.messageID)
w.chanOut <- lr
}
func (c *client) ProcessRequestMessage(m Message) {
defer c.wg.Done()
c.requestList[m.MessageID] = m
defer delete(c.requestList, m.MessageID)
m.Done = make(chan bool)
var w responseWriterImpl
w.chanOut = c.chanOut
w.messageID = m.MessageID
m.Client = c
c.srv.Handler.ServeLDAP(w, &m)
}