]>
Commit | Line | Data |
---|---|---|
c34b65c7 | 1 | /* |
c662c789 | 2 | * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. |
c34b65c7 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "stream-provider.h" | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
22 | #include <netinet/in.h> | |
23 | #include <poll.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include "coverage.h" | |
27 | #include "dynamic-string.h" | |
b302749b | 28 | #include "fatal-signal.h" |
c34b65c7 BP |
29 | #include "flow.h" |
30 | #include "ofp-print.h" | |
31 | #include "ofpbuf.h" | |
32 | #include "openflow/nicira-ext.h" | |
33 | #include "openflow/openflow.h" | |
34 | #include "packets.h" | |
35 | #include "poll-loop.h" | |
36 | #include "random.h" | |
37 | #include "util.h" | |
c34b65c7 BP |
38 | #include "vlog.h" |
39 | ||
d98e6007 | 40 | VLOG_DEFINE_THIS_MODULE(stream); |
5136ce49 | 41 | |
d76f09ea BP |
42 | COVERAGE_DEFINE(pstream_open); |
43 | COVERAGE_DEFINE(stream_open); | |
44 | ||
c34b65c7 BP |
45 | /* State of an active stream.*/ |
46 | enum stream_state { | |
47 | SCS_CONNECTING, /* Underlying stream is not connected. */ | |
48 | SCS_CONNECTED, /* Connection established. */ | |
49 | SCS_DISCONNECTED /* Connection failed or connection closed. */ | |
50 | }; | |
51 | ||
da327b18 | 52 | static const struct stream_class *stream_classes[] = { |
c34b65c7 BP |
53 | &tcp_stream_class, |
54 | &unix_stream_class, | |
55709289 BP |
55 | #ifdef HAVE_OPENSSL |
56 | &ssl_stream_class, | |
57 | #endif | |
c34b65c7 BP |
58 | }; |
59 | ||
da327b18 | 60 | static const struct pstream_class *pstream_classes[] = { |
c34b65c7 BP |
61 | &ptcp_pstream_class, |
62 | &punix_pstream_class, | |
55709289 BP |
63 | #ifdef HAVE_OPENSSL |
64 | &pssl_pstream_class, | |
65 | #endif | |
c34b65c7 BP |
66 | }; |
67 | ||
68 | /* Check the validity of the stream class structures. */ | |
69 | static void | |
70 | check_stream_classes(void) | |
71 | { | |
72 | #ifndef NDEBUG | |
73 | size_t i; | |
74 | ||
75 | for (i = 0; i < ARRAY_SIZE(stream_classes); i++) { | |
da327b18 | 76 | const struct stream_class *class = stream_classes[i]; |
c34b65c7 BP |
77 | assert(class->name != NULL); |
78 | assert(class->open != NULL); | |
539e96f6 BP |
79 | if (class->close || class->recv || class->send || class->run |
80 | || class->run_wait || class->wait) { | |
c34b65c7 BP |
81 | assert(class->close != NULL); |
82 | assert(class->recv != NULL); | |
83 | assert(class->send != NULL); | |
84 | assert(class->wait != NULL); | |
85 | } else { | |
86 | /* This class delegates to another one. */ | |
87 | } | |
88 | } | |
89 | ||
90 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 91 | const struct pstream_class *class = pstream_classes[i]; |
c34b65c7 BP |
92 | assert(class->name != NULL); |
93 | assert(class->listen != NULL); | |
94 | if (class->close || class->accept || class->wait) { | |
95 | assert(class->close != NULL); | |
96 | assert(class->accept != NULL); | |
97 | assert(class->wait != NULL); | |
98 | } else { | |
99 | /* This class delegates to another one. */ | |
100 | } | |
101 | } | |
102 | #endif | |
103 | } | |
104 | ||
105 | /* Prints information on active (if 'active') and passive (if 'passive') | |
106 | * connection methods supported by the stream. */ | |
107 | void | |
9467fe62 | 108 | stream_usage(const char *name, bool active, bool passive, |
c69ee87c | 109 | bool bootstrap OVS_UNUSED) |
c34b65c7 BP |
110 | { |
111 | /* Really this should be implemented via callbacks into the stream | |
112 | * providers, but that seems too heavy-weight to bother with at the | |
113 | * moment. */ | |
114 | ||
115 | printf("\n"); | |
116 | if (active) { | |
117 | printf("Active %s connection methods:\n", name); | |
118 | printf(" tcp:IP:PORT " | |
119 | "PORT at remote IP\n"); | |
9467fe62 BP |
120 | #ifdef HAVE_OPENSSL |
121 | printf(" ssl:IP:PORT " | |
122 | "SSL PORT at remote IP\n"); | |
123 | #endif | |
c34b65c7 BP |
124 | printf(" unix:FILE " |
125 | "Unix domain socket named FILE\n"); | |
126 | } | |
127 | ||
128 | if (passive) { | |
129 | printf("Passive %s connection methods:\n", name); | |
130 | printf(" ptcp:PORT[:IP] " | |
131 | "listen to TCP PORT on IP\n"); | |
9467fe62 BP |
132 | #ifdef HAVE_OPENSSL |
133 | printf(" pssl:PORT[:IP] " | |
134 | "listen for SSL on PORT on IP\n"); | |
135 | #endif | |
c34b65c7 BP |
136 | printf(" punix:FILE " |
137 | "listen on Unix domain socket FILE\n"); | |
138 | } | |
9467fe62 BP |
139 | |
140 | #ifdef HAVE_OPENSSL | |
141 | printf("PKI configuration (required to use SSL):\n" | |
142 | " -p, --private-key=FILE file with private key\n" | |
143 | " -c, --certificate=FILE file with certificate for private key\n" | |
144 | " -C, --ca-cert=FILE file with peer CA certificate\n"); | |
145 | if (bootstrap) { | |
146 | printf(" --bootstrap-ca-cert=FILE file with peer CA certificate " | |
147 | "to read or create\n"); | |
148 | } | |
149 | #endif | |
c34b65c7 BP |
150 | } |
151 | ||
26ad129e BP |
152 | /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class |
153 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
154 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
155 | * class exists. */ | |
156 | static int | |
da327b18 | 157 | stream_lookup_class(const char *name, const struct stream_class **classp) |
c34b65c7 BP |
158 | { |
159 | size_t prefix_len; | |
160 | size_t i; | |
161 | ||
c34b65c7 BP |
162 | check_stream_classes(); |
163 | ||
26ad129e | 164 | *classp = NULL; |
c34b65c7 | 165 | prefix_len = strcspn(name, ":"); |
26ad129e | 166 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
167 | return EAFNOSUPPORT; |
168 | } | |
169 | for (i = 0; i < ARRAY_SIZE(stream_classes); i++) { | |
da327b18 | 170 | const struct stream_class *class = stream_classes[i]; |
c34b65c7 BP |
171 | if (strlen(class->name) == prefix_len |
172 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
173 | *classp = class; |
174 | return 0; | |
c34b65c7 BP |
175 | } |
176 | } | |
177 | return EAFNOSUPPORT; | |
178 | } | |
179 | ||
26ad129e BP |
180 | /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is |
181 | * a supported stream type, otherwise EAFNOSUPPORT. */ | |
182 | int | |
183 | stream_verify_name(const char *name) | |
184 | { | |
da327b18 | 185 | const struct stream_class *class; |
26ad129e BP |
186 | return stream_lookup_class(name, &class); |
187 | } | |
188 | ||
189 | /* Attempts to connect a stream to a remote peer. 'name' is a connection name | |
190 | * in the form "TYPE:ARGS", where TYPE is an active stream class's name and | |
191 | * ARGS are stream class-specific. | |
192 | * | |
193 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
194 | * stores a pointer to the new connection in '*streamp', otherwise a null | |
195 | * pointer. */ | |
196 | int | |
f125905c | 197 | stream_open(const char *name, struct stream **streamp, uint8_t dscp) |
26ad129e | 198 | { |
da327b18 | 199 | const struct stream_class *class; |
26ad129e BP |
200 | struct stream *stream; |
201 | char *suffix_copy; | |
202 | int error; | |
203 | ||
204 | COVERAGE_INC(stream_open); | |
205 | ||
206 | /* Look up the class. */ | |
207 | error = stream_lookup_class(name, &class); | |
208 | if (!class) { | |
209 | goto error; | |
210 | } | |
211 | ||
212 | /* Call class's "open" function. */ | |
213 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 214 | error = class->open(name, suffix_copy, &stream, dscp); |
26ad129e BP |
215 | free(suffix_copy); |
216 | if (error) { | |
217 | goto error; | |
218 | } | |
219 | ||
220 | /* Success. */ | |
221 | *streamp = stream; | |
222 | return 0; | |
223 | ||
224 | error: | |
225 | *streamp = NULL; | |
226 | return error; | |
227 | } | |
228 | ||
766407ea BP |
229 | /* Blocks until a previously started stream connection attempt succeeds or |
230 | * fails. 'error' should be the value returned by stream_open() and 'streamp' | |
231 | * should point to the stream pointer set by stream_open(). Returns 0 if | |
232 | * successful, otherwise a positive errno value other than EAGAIN or | |
233 | * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes | |
234 | * '*streamp' and sets '*streamp' to null. | |
235 | * | |
236 | * Typical usage: | |
237 | * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream); | |
238 | */ | |
c34b65c7 | 239 | int |
766407ea | 240 | stream_open_block(int error, struct stream **streamp) |
c34b65c7 | 241 | { |
766407ea | 242 | struct stream *stream = *streamp; |
c34b65c7 | 243 | |
b302749b BP |
244 | fatal_signal_run(); |
245 | ||
b0bfeb3e BP |
246 | if (!error) { |
247 | while ((error = stream_connect(stream)) == EAGAIN) { | |
248 | stream_run(stream); | |
249 | stream_run_wait(stream); | |
250 | stream_connect_wait(stream); | |
251 | poll_block(); | |
252 | } | |
c34b65c7 BP |
253 | assert(error != EINPROGRESS); |
254 | } | |
b0bfeb3e | 255 | |
c34b65c7 BP |
256 | if (error) { |
257 | stream_close(stream); | |
258 | *streamp = NULL; | |
259 | } else { | |
260 | *streamp = stream; | |
261 | } | |
262 | return error; | |
263 | } | |
264 | ||
265 | /* Closes 'stream'. */ | |
266 | void | |
267 | stream_close(struct stream *stream) | |
268 | { | |
269 | if (stream != NULL) { | |
270 | char *name = stream->name; | |
271 | (stream->class->close)(stream); | |
272 | free(name); | |
273 | } | |
274 | } | |
275 | ||
276 | /* Returns the name of 'stream', that is, the string passed to | |
277 | * stream_open(). */ | |
278 | const char * | |
279 | stream_get_name(const struct stream *stream) | |
280 | { | |
281 | return stream ? stream->name : "(null)"; | |
282 | } | |
283 | ||
284 | /* Returns the IP address of the peer, or 0 if the peer is not connected over | |
285 | * an IP-based protocol or if its IP address is not yet known. */ | |
4408d18a | 286 | ovs_be32 |
c34b65c7 BP |
287 | stream_get_remote_ip(const struct stream *stream) |
288 | { | |
289 | return stream->remote_ip; | |
290 | } | |
291 | ||
292 | /* Returns the transport port of the peer, or 0 if the connection does not | |
293 | * contain a port or if the port is not yet known. */ | |
4408d18a | 294 | ovs_be16 |
c34b65c7 BP |
295 | stream_get_remote_port(const struct stream *stream) |
296 | { | |
297 | return stream->remote_port; | |
298 | } | |
299 | ||
300 | /* Returns the IP address used to connect to the peer, or 0 if the connection | |
301 | * is not an IP-based protocol or if its IP address is not yet known. */ | |
4408d18a | 302 | ovs_be32 |
c34b65c7 BP |
303 | stream_get_local_ip(const struct stream *stream) |
304 | { | |
305 | return stream->local_ip; | |
306 | } | |
307 | ||
308 | /* Returns the transport port used to connect to the peer, or 0 if the | |
309 | * connection does not contain a port or if the port is not yet known. */ | |
4408d18a | 310 | ovs_be16 |
c34b65c7 BP |
311 | stream_get_local_port(const struct stream *stream) |
312 | { | |
313 | return stream->local_port; | |
314 | } | |
315 | ||
316 | static void | |
317 | scs_connecting(struct stream *stream) | |
318 | { | |
319 | int retval = (stream->class->connect)(stream); | |
320 | assert(retval != EINPROGRESS); | |
321 | if (!retval) { | |
322 | stream->state = SCS_CONNECTED; | |
323 | } else if (retval != EAGAIN) { | |
324 | stream->state = SCS_DISCONNECTED; | |
325 | stream->error = retval; | |
326 | } | |
327 | } | |
328 | ||
294e9fc8 BP |
329 | /* Tries to complete the connection on 'stream'. If 'stream''s connection is |
330 | * complete, returns 0 if the connection was successful or a positive errno | |
331 | * value if it failed. If the connection is still in progress, returns | |
332 | * EAGAIN. */ | |
c34b65c7 BP |
333 | int |
334 | stream_connect(struct stream *stream) | |
335 | { | |
336 | enum stream_state last_state; | |
337 | ||
338 | do { | |
339 | last_state = stream->state; | |
340 | switch (stream->state) { | |
341 | case SCS_CONNECTING: | |
342 | scs_connecting(stream); | |
343 | break; | |
344 | ||
345 | case SCS_CONNECTED: | |
346 | return 0; | |
347 | ||
348 | case SCS_DISCONNECTED: | |
349 | return stream->error; | |
350 | ||
351 | default: | |
352 | NOT_REACHED(); | |
353 | } | |
354 | } while (stream->state != last_state); | |
355 | ||
356 | return EAGAIN; | |
357 | } | |
358 | ||
359 | /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns: | |
360 | * | |
361 | * - If successful, the number of bytes received (between 1 and 'n'). | |
362 | * | |
363 | * - On error, a negative errno value. | |
364 | * | |
365 | * - 0, if the connection has been closed in the normal fashion, or if 'n' | |
366 | * is zero. | |
367 | * | |
368 | * The recv function will not block waiting for a packet to arrive. If no | |
369 | * data have been received, it returns -EAGAIN immediately. */ | |
370 | int | |
371 | stream_recv(struct stream *stream, void *buffer, size_t n) | |
372 | { | |
373 | int retval = stream_connect(stream); | |
374 | return (retval ? -retval | |
375 | : n == 0 ? 0 | |
376 | : (stream->class->recv)(stream, buffer, n)); | |
377 | } | |
378 | ||
379 | /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns: | |
380 | * | |
381 | * - If successful, the number of bytes sent (between 1 and 'n'). 0 is | |
382 | * only a valid return value if 'n' is 0. | |
383 | * | |
384 | * - On error, a negative errno value. | |
385 | * | |
386 | * The send function will not block. If no bytes can be immediately accepted | |
387 | * for transmission, it returns -EAGAIN immediately. */ | |
388 | int | |
389 | stream_send(struct stream *stream, const void *buffer, size_t n) | |
390 | { | |
391 | int retval = stream_connect(stream); | |
392 | return (retval ? -retval | |
393 | : n == 0 ? 0 | |
394 | : (stream->class->send)(stream, buffer, n)); | |
395 | } | |
396 | ||
539e96f6 BP |
397 | /* Allows 'stream' to perform maintenance activities, such as flushing |
398 | * output buffers. */ | |
399 | void | |
400 | stream_run(struct stream *stream) | |
401 | { | |
402 | if (stream->class->run) { | |
403 | (stream->class->run)(stream); | |
404 | } | |
405 | } | |
406 | ||
407 | /* Arranges for the poll loop to wake up when 'stream' needs to perform | |
408 | * maintenance activities. */ | |
409 | void | |
410 | stream_run_wait(struct stream *stream) | |
411 | { | |
412 | if (stream->class->run_wait) { | |
413 | (stream->class->run_wait)(stream); | |
414 | } | |
415 | } | |
416 | ||
417 | /* Arranges for the poll loop to wake up when 'stream' is ready to take an | |
418 | * action of the given 'type'. */ | |
c34b65c7 BP |
419 | void |
420 | stream_wait(struct stream *stream, enum stream_wait_type wait) | |
421 | { | |
422 | assert(wait == STREAM_CONNECT || wait == STREAM_RECV | |
423 | || wait == STREAM_SEND); | |
424 | ||
425 | switch (stream->state) { | |
426 | case SCS_CONNECTING: | |
427 | wait = STREAM_CONNECT; | |
428 | break; | |
429 | ||
430 | case SCS_DISCONNECTED: | |
431 | poll_immediate_wake(); | |
432 | return; | |
433 | } | |
434 | (stream->class->wait)(stream, wait); | |
435 | } | |
436 | ||
437 | void | |
438 | stream_connect_wait(struct stream *stream) | |
439 | { | |
440 | stream_wait(stream, STREAM_CONNECT); | |
441 | } | |
442 | ||
443 | void | |
444 | stream_recv_wait(struct stream *stream) | |
445 | { | |
446 | stream_wait(stream, STREAM_RECV); | |
447 | } | |
448 | ||
449 | void | |
450 | stream_send_wait(struct stream *stream) | |
451 | { | |
452 | stream_wait(stream, STREAM_SEND); | |
453 | } | |
454 | ||
26ad129e BP |
455 | /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class |
456 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
457 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
458 | * class exists. */ | |
459 | static int | |
da327b18 | 460 | pstream_lookup_class(const char *name, const struct pstream_class **classp) |
c34b65c7 BP |
461 | { |
462 | size_t prefix_len; | |
463 | size_t i; | |
464 | ||
465 | check_stream_classes(); | |
466 | ||
26ad129e | 467 | *classp = NULL; |
c34b65c7 | 468 | prefix_len = strcspn(name, ":"); |
26ad129e | 469 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
470 | return EAFNOSUPPORT; |
471 | } | |
472 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 473 | const struct pstream_class *class = pstream_classes[i]; |
c34b65c7 BP |
474 | if (strlen(class->name) == prefix_len |
475 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
476 | *classp = class; |
477 | return 0; | |
c34b65c7 BP |
478 | } |
479 | } | |
480 | return EAFNOSUPPORT; | |
481 | } | |
482 | ||
26ad129e BP |
483 | /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is |
484 | * a supported pstream type, otherwise EAFNOSUPPORT. */ | |
485 | int | |
486 | pstream_verify_name(const char *name) | |
487 | { | |
da327b18 | 488 | const struct pstream_class *class; |
26ad129e BP |
489 | return pstream_lookup_class(name, &class); |
490 | } | |
491 | ||
492 | /* Attempts to start listening for remote stream connections. 'name' is a | |
493 | * connection name in the form "TYPE:ARGS", where TYPE is an passive stream | |
494 | * class's name and ARGS are stream class-specific. | |
495 | * | |
496 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
497 | * stores a pointer to the new connection in '*pstreamp', otherwise a null | |
498 | * pointer. */ | |
499 | int | |
f125905c | 500 | pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp) |
26ad129e | 501 | { |
da327b18 | 502 | const struct pstream_class *class; |
26ad129e BP |
503 | struct pstream *pstream; |
504 | char *suffix_copy; | |
505 | int error; | |
506 | ||
507 | COVERAGE_INC(pstream_open); | |
508 | ||
509 | /* Look up the class. */ | |
510 | error = pstream_lookup_class(name, &class); | |
511 | if (!class) { | |
512 | goto error; | |
513 | } | |
514 | ||
515 | /* Call class's "open" function. */ | |
516 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 517 | error = class->listen(name, suffix_copy, &pstream, dscp); |
26ad129e BP |
518 | free(suffix_copy); |
519 | if (error) { | |
520 | goto error; | |
521 | } | |
522 | ||
523 | /* Success. */ | |
524 | *pstreamp = pstream; | |
525 | return 0; | |
526 | ||
527 | error: | |
528 | *pstreamp = NULL; | |
529 | return error; | |
530 | } | |
531 | ||
c34b65c7 BP |
532 | /* Returns the name that was used to open 'pstream'. The caller must not |
533 | * modify or free the name. */ | |
534 | const char * | |
535 | pstream_get_name(const struct pstream *pstream) | |
536 | { | |
537 | return pstream->name; | |
538 | } | |
539 | ||
540 | /* Closes 'pstream'. */ | |
541 | void | |
542 | pstream_close(struct pstream *pstream) | |
543 | { | |
544 | if (pstream != NULL) { | |
545 | char *name = pstream->name; | |
546 | (pstream->class->close)(pstream); | |
547 | free(name); | |
548 | } | |
549 | } | |
550 | ||
551 | /* Tries to accept a new connection on 'pstream'. If successful, stores the | |
552 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
553 | * positive errno value. | |
554 | * | |
555 | * pstream_accept() will not block waiting for a connection. If no connection | |
556 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
557 | int | |
558 | pstream_accept(struct pstream *pstream, struct stream **new_stream) | |
559 | { | |
560 | int retval = (pstream->class->accept)(pstream, new_stream); | |
561 | if (retval) { | |
562 | *new_stream = NULL; | |
563 | } else { | |
564 | assert((*new_stream)->state != SCS_CONNECTING | |
565 | || (*new_stream)->class->connect); | |
566 | } | |
567 | return retval; | |
568 | } | |
569 | ||
8d76bcca BP |
570 | /* Tries to accept a new connection on 'pstream'. If successful, stores the |
571 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
572 | * positive errno value. | |
573 | * | |
574 | * pstream_accept_block() blocks until a connection is ready or until an error | |
575 | * occurs. It will not return EAGAIN. */ | |
576 | int | |
577 | pstream_accept_block(struct pstream *pstream, struct stream **new_stream) | |
578 | { | |
579 | int error; | |
580 | ||
b302749b | 581 | fatal_signal_run(); |
8d76bcca BP |
582 | while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) { |
583 | pstream_wait(pstream); | |
584 | poll_block(); | |
585 | } | |
586 | if (error) { | |
587 | *new_stream = NULL; | |
588 | } | |
589 | return error; | |
590 | } | |
591 | ||
c34b65c7 BP |
592 | void |
593 | pstream_wait(struct pstream *pstream) | |
594 | { | |
595 | (pstream->class->wait)(pstream); | |
596 | } | |
597 | \f | |
598 | /* Initializes 'stream' as a new stream named 'name', implemented via 'class'. | |
599 | * The initial connection status, supplied as 'connect_status', is interpreted | |
600 | * as follows: | |
601 | * | |
602 | * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be | |
603 | * called in the normal fashion. | |
604 | * | |
605 | * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect' | |
606 | * function should be called to complete the connection. | |
607 | * | |
608 | * - Other positive errno values indicate that the connection failed with | |
609 | * the specified error. | |
610 | * | |
611 | * After calling this function, stream_close() must be used to destroy | |
612 | * 'stream', otherwise resources will be leaked. | |
613 | * | |
614 | * The caller retains ownership of 'name'. */ | |
615 | void | |
da327b18 | 616 | stream_init(struct stream *stream, const struct stream_class *class, |
c34b65c7 BP |
617 | int connect_status, const char *name) |
618 | { | |
c4bed75b | 619 | memset(stream, 0, sizeof *stream); |
c34b65c7 BP |
620 | stream->class = class; |
621 | stream->state = (connect_status == EAGAIN ? SCS_CONNECTING | |
622 | : !connect_status ? SCS_CONNECTED | |
623 | : SCS_DISCONNECTED); | |
624 | stream->error = connect_status; | |
625 | stream->name = xstrdup(name); | |
e0668bd1 | 626 | assert(stream->state != SCS_CONNECTING || class->connect); |
c34b65c7 BP |
627 | } |
628 | ||
629 | void | |
4408d18a | 630 | stream_set_remote_ip(struct stream *stream, ovs_be32 ip) |
c34b65c7 BP |
631 | { |
632 | stream->remote_ip = ip; | |
633 | } | |
634 | ||
635 | void | |
4408d18a | 636 | stream_set_remote_port(struct stream *stream, ovs_be16 port) |
c34b65c7 BP |
637 | { |
638 | stream->remote_port = port; | |
639 | } | |
640 | ||
641 | void | |
4408d18a | 642 | stream_set_local_ip(struct stream *stream, ovs_be32 ip) |
c34b65c7 BP |
643 | { |
644 | stream->local_ip = ip; | |
645 | } | |
646 | ||
647 | void | |
4408d18a | 648 | stream_set_local_port(struct stream *stream, ovs_be16 port) |
c34b65c7 BP |
649 | { |
650 | stream->local_port = port; | |
651 | } | |
652 | ||
653 | void | |
da327b18 | 654 | pstream_init(struct pstream *pstream, const struct pstream_class *class, |
c34b65c7 BP |
655 | const char *name) |
656 | { | |
657 | pstream->class = class; | |
658 | pstream->name = xstrdup(name); | |
659 | } | |
f39dc942 BP |
660 | \f |
661 | static int | |
662 | count_fields(const char *s_) | |
663 | { | |
664 | char *s, *field, *save_ptr; | |
665 | int n = 0; | |
666 | ||
667 | save_ptr = NULL; | |
668 | s = xstrdup(s_); | |
669 | for (field = strtok_r(s, ":", &save_ptr); field != NULL; | |
670 | field = strtok_r(NULL, ":", &save_ptr)) { | |
671 | n++; | |
672 | } | |
673 | free(s); | |
674 | ||
675 | return n; | |
676 | } | |
677 | ||
678 | /* Like stream_open(), but for tcp streams the port defaults to | |
679 | * 'default_tcp_port' if no port number is given and for SSL streams the port | |
680 | * defaults to 'default_ssl_port' if no port number is given. */ | |
681 | int | |
682 | stream_open_with_default_ports(const char *name_, | |
683 | uint16_t default_tcp_port, | |
684 | uint16_t default_ssl_port, | |
f125905c MM |
685 | struct stream **streamp, |
686 | uint8_t dscp) | |
f39dc942 BP |
687 | { |
688 | char *name; | |
689 | int error; | |
690 | ||
691 | if (!strncmp(name_, "tcp:", 4) && count_fields(name_) < 3) { | |
692 | name = xasprintf("%s:%d", name_, default_tcp_port); | |
693 | } else if (!strncmp(name_, "ssl:", 4) && count_fields(name_) < 3) { | |
694 | name = xasprintf("%s:%d", name_, default_ssl_port); | |
695 | } else { | |
696 | name = xstrdup(name_); | |
697 | } | |
f125905c | 698 | error = stream_open(name, streamp, dscp); |
f39dc942 BP |
699 | free(name); |
700 | ||
701 | return error; | |
702 | } | |
703 | ||
704 | /* Like pstream_open(), but for ptcp streams the port defaults to | |
705 | * 'default_ptcp_port' if no port number is given and for passive SSL streams | |
706 | * the port defaults to 'default_pssl_port' if no port number is given. */ | |
707 | int | |
708 | pstream_open_with_default_ports(const char *name_, | |
709 | uint16_t default_ptcp_port, | |
710 | uint16_t default_pssl_port, | |
f125905c MM |
711 | struct pstream **pstreamp, |
712 | uint8_t dscp) | |
f39dc942 BP |
713 | { |
714 | char *name; | |
715 | int error; | |
716 | ||
717 | if (!strncmp(name_, "ptcp:", 5) && count_fields(name_) < 2) { | |
718 | name = xasprintf("%s%d", name_, default_ptcp_port); | |
719 | } else if (!strncmp(name_, "pssl:", 5) && count_fields(name_) < 2) { | |
720 | name = xasprintf("%s%d", name_, default_pssl_port); | |
721 | } else { | |
722 | name = xstrdup(name_); | |
723 | } | |
f125905c | 724 | error = pstream_open(name, pstreamp, dscp); |
f39dc942 BP |
725 | free(name); |
726 | ||
727 | return error; | |
728 | } | |
ac4c900d AA |
729 | |
730 | /* | |
731 | * This function extracts IP address and port from the target string. | |
732 | * | |
733 | * - On success, function returns true and fills *sin structure with port | |
734 | * and IP address. If port was absent in target string then it will use | |
735 | * corresponding default port value. | |
736 | * - On error, function returns false and *sin contains garbage. | |
737 | */ | |
738 | bool | |
739 | stream_parse_target_with_default_ports(const char *target, | |
740 | uint16_t default_tcp_port, | |
741 | uint16_t default_ssl_port, | |
742 | struct sockaddr_in *sin) | |
743 | { | |
744 | return (!strncmp(target, "tcp:", 4) | |
745 | && inet_parse_active(target + 4, default_tcp_port, sin)) || | |
746 | (!strncmp(target, "ssl:", 4) | |
747 | && inet_parse_active(target + 4, default_ssl_port, sin)); | |
748 | } | |
749 | ||
1e3c0047 BP |
750 | /* Attempts to guess the content type of a stream whose first few bytes were |
751 | * the 'size' bytes of 'data'. */ | |
752 | static enum stream_content_type | |
c55acc2e | 753 | stream_guess_content(const uint8_t *data, ssize_t size) |
1e3c0047 BP |
754 | { |
755 | if (size >= 2) { | |
756 | #define PAIR(A, B) (((A) << 8) | (B)) | |
757 | switch (PAIR(data[0], data[1])) { | |
758 | case PAIR(0x16, 0x03): /* Handshake, version 3. */ | |
759 | return STREAM_SSL; | |
760 | case PAIR('{', '"'): | |
761 | return STREAM_JSONRPC; | |
87ea5e5e | 762 | case PAIR(OFP10_VERSION, OFPT_HELLO): |
1e3c0047 BP |
763 | return STREAM_OPENFLOW; |
764 | } | |
765 | } | |
766 | ||
767 | return STREAM_UNKNOWN; | |
768 | } | |
769 | ||
770 | /* Returns a string represenation of 'type'. */ | |
771 | static const char * | |
772 | stream_content_type_to_string(enum stream_content_type type) | |
773 | { | |
774 | switch (type) { | |
775 | case STREAM_UNKNOWN: | |
776 | default: | |
777 | return "unknown"; | |
f39dc942 | 778 | |
1e3c0047 BP |
779 | case STREAM_JSONRPC: |
780 | return "JSON-RPC"; | |
f39dc942 | 781 | |
1e3c0047 BP |
782 | case STREAM_OPENFLOW: |
783 | return "OpenFlow"; | |
784 | ||
785 | case STREAM_SSL: | |
786 | return "SSL"; | |
787 | } | |
788 | } | |
789 | ||
790 | /* Attempts to guess the content type of a stream whose first few bytes were | |
791 | * the 'size' bytes of 'data'. If this is done successfully, and the guessed | |
792 | * content type is other than 'expected_type', then log a message in vlog | |
793 | * module 'module', naming 'stream_name' as the source, explaining what | |
794 | * content was expected and what was actually received. */ | |
795 | void | |
c55acc2e | 796 | stream_report_content(const void *data, ssize_t size, |
1e3c0047 | 797 | enum stream_content_type expected_type, |
480ce8ab | 798 | struct vlog_module *module, const char *stream_name) |
1e3c0047 BP |
799 | { |
800 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
801 | enum stream_content_type actual_type; | |
802 | ||
803 | actual_type = stream_guess_content(data, size); | |
804 | if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) { | |
805 | vlog_rate_limit(module, VLL_WARN, &rl, | |
806 | "%s: received %s data on %s channel", | |
807 | stream_name, | |
c662c789 BP |
808 | stream_content_type_to_string(actual_type), |
809 | stream_content_type_to_string(expected_type)); | |
1e3c0047 BP |
810 | } |
811 | } |