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