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