]> git.proxmox.com Git - ovs.git/blame - lib/stream.c
tests: Define a variable "IS_WIN32" for tests.
[ovs.git] / lib / stream.c
CommitLineData
c34b65c7 1/*
798e1352 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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"
c34b65c7
BP
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"
b302749b 27#include "fatal-signal.h"
c34b65c7 28#include "flow.h"
36359150 29#include "jsonrpc.h"
c34b65c7
BP
30#include "ofp-print.h"
31#include "ofpbuf.h"
32#include "openflow/nicira-ext.h"
33#include "openflow/openflow.h"
26e1fdc4 34#include "ovs-thread.h"
c34b65c7
BP
35#include "packets.h"
36#include "poll-loop.h"
37#include "random.h"
26e1fdc4 38#include "socket-util.h"
c34b65c7 39#include "util.h"
36359150
JP
40#include "vlog.h"
41
42VLOG_DEFINE_THIS_MODULE(stream);
5136ce49 43
d76f09ea
BP
44COVERAGE_DEFINE(pstream_open);
45COVERAGE_DEFINE(stream_open);
46
c34b65c7
BP
47/* State of an active stream.*/
48enum 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
da327b18 54static const struct stream_class *stream_classes[] = {
c34b65c7 55 &tcp_stream_class,
6500157f 56#ifndef _WIN32
c34b65c7 57 &unix_stream_class,
7ff04d92 58#endif
55709289
BP
59#ifdef HAVE_OPENSSL
60 &ssl_stream_class,
61#endif
c34b65c7
BP
62};
63
da327b18 64static const struct pstream_class *pstream_classes[] = {
c34b65c7 65 &ptcp_pstream_class,
6500157f 66#ifndef _WIN32
c34b65c7 67 &punix_pstream_class,
7ff04d92 68#endif
55709289
BP
69#ifdef HAVE_OPENSSL
70 &pssl_pstream_class,
71#endif
c34b65c7
BP
72};
73
26e1fdc4
GS
74#ifdef _WIN32
75static void
76do_winsock_start(void)
77{
78 WSADATA wsaData;
79 int error;
80
81 error = WSAStartup(MAKEWORD(2, 2), &wsaData);
82 if (error != 0) {
83 VLOG_FATAL("WSAStartup failed: %s", sock_strerror(sock_errno()));
84 }
85}
86
87static void
88winsock_start(void)
89{
90 static pthread_once_t once = PTHREAD_ONCE_INIT;
91 pthread_once(&once, do_winsock_start);
92}
93#endif
94
c34b65c7
BP
95/* Check the validity of the stream class structures. */
96static void
97check_stream_classes(void)
98{
99#ifndef NDEBUG
100 size_t i;
101
102 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
da327b18 103 const struct stream_class *class = stream_classes[i];
cb22974d
BP
104 ovs_assert(class->name != NULL);
105 ovs_assert(class->open != NULL);
539e96f6
BP
106 if (class->close || class->recv || class->send || class->run
107 || class->run_wait || class->wait) {
cb22974d
BP
108 ovs_assert(class->close != NULL);
109 ovs_assert(class->recv != NULL);
110 ovs_assert(class->send != NULL);
111 ovs_assert(class->wait != NULL);
c34b65c7
BP
112 } else {
113 /* This class delegates to another one. */
114 }
115 }
116
117 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
da327b18 118 const struct pstream_class *class = pstream_classes[i];
cb22974d
BP
119 ovs_assert(class->name != NULL);
120 ovs_assert(class->listen != NULL);
c34b65c7 121 if (class->close || class->accept || class->wait) {
cb22974d
BP
122 ovs_assert(class->close != NULL);
123 ovs_assert(class->accept != NULL);
124 ovs_assert(class->wait != NULL);
c34b65c7
BP
125 } else {
126 /* This class delegates to another one. */
127 }
128 }
129#endif
130}
131
132/* Prints information on active (if 'active') and passive (if 'passive')
133 * connection methods supported by the stream. */
134void
9467fe62 135stream_usage(const char *name, bool active, bool passive,
c69ee87c 136 bool bootstrap OVS_UNUSED)
c34b65c7
BP
137{
138 /* Really this should be implemented via callbacks into the stream
139 * providers, but that seems too heavy-weight to bother with at the
140 * moment. */
141
142 printf("\n");
143 if (active) {
144 printf("Active %s connection methods:\n", name);
145 printf(" tcp:IP:PORT "
146 "PORT at remote IP\n");
9467fe62
BP
147#ifdef HAVE_OPENSSL
148 printf(" ssl:IP:PORT "
149 "SSL PORT at remote IP\n");
150#endif
c34b65c7
BP
151 printf(" unix:FILE "
152 "Unix domain socket named FILE\n");
153 }
154
155 if (passive) {
156 printf("Passive %s connection methods:\n", name);
157 printf(" ptcp:PORT[:IP] "
158 "listen to TCP PORT on IP\n");
9467fe62
BP
159#ifdef HAVE_OPENSSL
160 printf(" pssl:PORT[:IP] "
161 "listen for SSL on PORT on IP\n");
162#endif
c34b65c7
BP
163 printf(" punix:FILE "
164 "listen on Unix domain socket FILE\n");
165 }
9467fe62
BP
166
167#ifdef HAVE_OPENSSL
168 printf("PKI configuration (required to use SSL):\n"
169 " -p, --private-key=FILE file with private key\n"
170 " -c, --certificate=FILE file with certificate for private key\n"
171 " -C, --ca-cert=FILE file with peer CA certificate\n");
172 if (bootstrap) {
173 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
174 "to read or create\n");
175 }
176#endif
c34b65c7
BP
177}
178
26ad129e
BP
179/* Given 'name', a stream name in the form "TYPE:ARGS", stores the class
180 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
181 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
182 * class exists. */
183static int
da327b18 184stream_lookup_class(const char *name, const struct stream_class **classp)
c34b65c7
BP
185{
186 size_t prefix_len;
187 size_t i;
188
c34b65c7
BP
189 check_stream_classes();
190
26ad129e 191 *classp = NULL;
c34b65c7 192 prefix_len = strcspn(name, ":");
26ad129e 193 if (name[prefix_len] == '\0') {
c34b65c7
BP
194 return EAFNOSUPPORT;
195 }
196 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
da327b18 197 const struct stream_class *class = stream_classes[i];
c34b65c7
BP
198 if (strlen(class->name) == prefix_len
199 && !memcmp(class->name, name, prefix_len)) {
26ad129e
BP
200 *classp = class;
201 return 0;
c34b65c7
BP
202 }
203 }
204 return EAFNOSUPPORT;
205}
206
26ad129e
BP
207/* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is
208 * a supported stream type, otherwise EAFNOSUPPORT. */
209int
210stream_verify_name(const char *name)
211{
da327b18 212 const struct stream_class *class;
26ad129e
BP
213 return stream_lookup_class(name, &class);
214}
215
216/* Attempts to connect a stream to a remote peer. 'name' is a connection name
217 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
218 * ARGS are stream class-specific.
219 *
220 * Returns 0 if successful, otherwise a positive errno value. If successful,
221 * stores a pointer to the new connection in '*streamp', otherwise a null
222 * pointer. */
223int
f125905c 224stream_open(const char *name, struct stream **streamp, uint8_t dscp)
26ad129e 225{
da327b18 226 const struct stream_class *class;
26ad129e
BP
227 struct stream *stream;
228 char *suffix_copy;
229 int error;
230
231 COVERAGE_INC(stream_open);
232
26e1fdc4
GS
233#ifdef _WIN32
234 winsock_start();
235#endif
236
26ad129e
BP
237 /* Look up the class. */
238 error = stream_lookup_class(name, &class);
239 if (!class) {
240 goto error;
241 }
242
243 /* Call class's "open" function. */
244 suffix_copy = xstrdup(strchr(name, ':') + 1);
f125905c 245 error = class->open(name, suffix_copy, &stream, dscp);
26ad129e
BP
246 free(suffix_copy);
247 if (error) {
248 goto error;
249 }
250
251 /* Success. */
252 *streamp = stream;
253 return 0;
254
255error:
256 *streamp = NULL;
257 return error;
258}
259
766407ea
BP
260/* Blocks until a previously started stream connection attempt succeeds or
261 * fails. 'error' should be the value returned by stream_open() and 'streamp'
262 * should point to the stream pointer set by stream_open(). Returns 0 if
263 * successful, otherwise a positive errno value other than EAGAIN or
264 * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes
265 * '*streamp' and sets '*streamp' to null.
266 *
267 * Typical usage:
268 * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
269 */
c34b65c7 270int
766407ea 271stream_open_block(int error, struct stream **streamp)
c34b65c7 272{
766407ea 273 struct stream *stream = *streamp;
c34b65c7 274
b302749b
BP
275 fatal_signal_run();
276
b0bfeb3e
BP
277 if (!error) {
278 while ((error = stream_connect(stream)) == EAGAIN) {
279 stream_run(stream);
280 stream_run_wait(stream);
281 stream_connect_wait(stream);
282 poll_block();
283 }
cb22974d 284 ovs_assert(error != EINPROGRESS);
c34b65c7 285 }
b0bfeb3e 286
c34b65c7
BP
287 if (error) {
288 stream_close(stream);
289 *streamp = NULL;
290 } else {
291 *streamp = stream;
292 }
293 return error;
294}
295
296/* Closes 'stream'. */
297void
298stream_close(struct stream *stream)
299{
300 if (stream != NULL) {
301 char *name = stream->name;
302 (stream->class->close)(stream);
303 free(name);
304 }
305}
306
307/* Returns the name of 'stream', that is, the string passed to
308 * stream_open(). */
309const char *
310stream_get_name(const struct stream *stream)
311{
312 return stream ? stream->name : "(null)";
313}
314
c34b65c7
BP
315static void
316scs_connecting(struct stream *stream)
317{
318 int retval = (stream->class->connect)(stream);
cb22974d 319 ovs_assert(retval != EINPROGRESS);
c34b65c7
BP
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
294e9fc8
BP
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. */
c34b65c7
BP
332int
333stream_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:
428b2edd 351 OVS_NOT_REACHED();
c34b65c7
BP
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. */
369int
370stream_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. */
387int
388stream_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
539e96f6
BP
396/* Allows 'stream' to perform maintenance activities, such as flushing
397 * output buffers. */
398void
399stream_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. */
408void
409stream_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'. */
c34b65c7
BP
418void
419stream_wait(struct stream *stream, enum stream_wait_type wait)
420{
cb22974d
BP
421 ovs_assert(wait == STREAM_CONNECT || wait == STREAM_RECV
422 || wait == STREAM_SEND);
c34b65c7
BP
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
436void
437stream_connect_wait(struct stream *stream)
438{
439 stream_wait(stream, STREAM_CONNECT);
440}
441
442void
443stream_recv_wait(struct stream *stream)
444{
445 stream_wait(stream, STREAM_RECV);
446}
447
448void
449stream_send_wait(struct stream *stream)
450{
451 stream_wait(stream, STREAM_SEND);
452}
453
26ad129e
BP
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. */
458static int
da327b18 459pstream_lookup_class(const char *name, const struct pstream_class **classp)
c34b65c7
BP
460{
461 size_t prefix_len;
462 size_t i;
463
464 check_stream_classes();
465
26ad129e 466 *classp = NULL;
c34b65c7 467 prefix_len = strcspn(name, ":");
26ad129e 468 if (name[prefix_len] == '\0') {
c34b65c7
BP
469 return EAFNOSUPPORT;
470 }
471 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
da327b18 472 const struct pstream_class *class = pstream_classes[i];
c34b65c7
BP
473 if (strlen(class->name) == prefix_len
474 && !memcmp(class->name, name, prefix_len)) {
26ad129e
BP
475 *classp = class;
476 return 0;
c34b65c7
BP
477 }
478 }
479 return EAFNOSUPPORT;
480}
481
26ad129e
BP
482/* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
483 * a supported pstream type, otherwise EAFNOSUPPORT. */
484int
485pstream_verify_name(const char *name)
486{
da327b18 487 const struct pstream_class *class;
26ad129e
BP
488 return pstream_lookup_class(name, &class);
489}
490
f1936eb6
EJ
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. */
495int
496stream_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
26ad129e
BP
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. */
517int
f125905c 518pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp)
26ad129e 519{
da327b18 520 const struct pstream_class *class;
26ad129e
BP
521 struct pstream *pstream;
522 char *suffix_copy;
523 int error;
524
525 COVERAGE_INC(pstream_open);
526
26e1fdc4
GS
527#ifdef _WIN32
528 winsock_start();
529#endif
530
26ad129e
BP
531 /* Look up the class. */
532 error = pstream_lookup_class(name, &class);
533 if (!class) {
534 goto error;
535 }
536
537 /* Call class's "open" function. */
538 suffix_copy = xstrdup(strchr(name, ':') + 1);
f125905c 539 error = class->listen(name, suffix_copy, &pstream, dscp);
26ad129e
BP
540 free(suffix_copy);
541 if (error) {
542 goto error;
543 }
544
545 /* Success. */
546 *pstreamp = pstream;
547 return 0;
548
549error:
550 *pstreamp = NULL;
551 return error;
552}
553
c34b65c7
BP
554/* Returns the name that was used to open 'pstream'. The caller must not
555 * modify or free the name. */
556const char *
557pstream_get_name(const struct pstream *pstream)
558{
559 return pstream->name;
560}
561
562/* Closes 'pstream'. */
563void
564pstream_close(struct pstream *pstream)
565{
566 if (pstream != NULL) {
567 char *name = pstream->name;
568 (pstream->class->close)(pstream);
569 free(name);
570 }
571}
572
573/* Tries to accept a new connection on 'pstream'. If successful, stores the
574 * new connection in '*new_stream' and returns 0. Otherwise, returns a
575 * positive errno value.
576 *
577 * pstream_accept() will not block waiting for a connection. If no connection
578 * is ready to be accepted, it returns EAGAIN immediately. */
579int
580pstream_accept(struct pstream *pstream, struct stream **new_stream)
581{
582 int retval = (pstream->class->accept)(pstream, new_stream);
583 if (retval) {
584 *new_stream = NULL;
585 } else {
cb22974d
BP
586 ovs_assert((*new_stream)->state != SCS_CONNECTING
587 || (*new_stream)->class->connect);
c34b65c7
BP
588 }
589 return retval;
590}
591
8d76bcca
BP
592/* Tries to accept a new connection on 'pstream'. If successful, stores the
593 * new connection in '*new_stream' and returns 0. Otherwise, returns a
594 * positive errno value.
595 *
596 * pstream_accept_block() blocks until a connection is ready or until an error
597 * occurs. It will not return EAGAIN. */
598int
599pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
600{
601 int error;
602
b302749b 603 fatal_signal_run();
8d76bcca
BP
604 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
605 pstream_wait(pstream);
606 poll_block();
607 }
608 if (error) {
609 *new_stream = NULL;
610 }
611 return error;
612}
613
c34b65c7
BP
614void
615pstream_wait(struct pstream *pstream)
616{
617 (pstream->class->wait)(pstream);
618}
f89b7ce5
IY
619
620int
621pstream_set_dscp(struct pstream *pstream, uint8_t dscp)
622{
623 if (pstream->class->set_dscp) {
624 return pstream->class->set_dscp(pstream, dscp);
625 }
626 return 0;
627}
798e1352
BP
628
629/* Returns the transport port on which 'pstream' is listening, or 0 if the
630 * concept doesn't apply. */
631ovs_be16
632pstream_get_bound_port(const struct pstream *pstream)
633{
634 return pstream->bound_port;
635}
c34b65c7
BP
636\f
637/* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
638 * The initial connection status, supplied as 'connect_status', is interpreted
639 * as follows:
640 *
641 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
642 * called in the normal fashion.
643 *
644 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
645 * function should be called to complete the connection.
646 *
647 * - Other positive errno values indicate that the connection failed with
648 * the specified error.
649 *
650 * After calling this function, stream_close() must be used to destroy
651 * 'stream', otherwise resources will be leaked.
652 *
653 * The caller retains ownership of 'name'. */
654void
da327b18 655stream_init(struct stream *stream, const struct stream_class *class,
c34b65c7
BP
656 int connect_status, const char *name)
657{
c4bed75b 658 memset(stream, 0, sizeof *stream);
c34b65c7
BP
659 stream->class = class;
660 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
661 : !connect_status ? SCS_CONNECTED
662 : SCS_DISCONNECTED);
663 stream->error = connect_status;
664 stream->name = xstrdup(name);
cb22974d 665 ovs_assert(stream->state != SCS_CONNECTING || class->connect);
c34b65c7
BP
666}
667
c34b65c7 668void
da327b18 669pstream_init(struct pstream *pstream, const struct pstream_class *class,
c34b65c7
BP
670 const char *name)
671{
798e1352 672 memset(pstream, 0, sizeof *pstream);
c34b65c7
BP
673 pstream->class = class;
674 pstream->name = xstrdup(name);
675}
798e1352
BP
676
677void
678pstream_set_bound_port(struct pstream *pstream, ovs_be16 port)
679{
680 pstream->bound_port = port;
681}
f39dc942
BP
682\f
683static int
684count_fields(const char *s_)
685{
686 char *s, *field, *save_ptr;
687 int n = 0;
688
689 save_ptr = NULL;
690 s = xstrdup(s_);
691 for (field = strtok_r(s, ":", &save_ptr); field != NULL;
692 field = strtok_r(NULL, ":", &save_ptr)) {
693 n++;
694 }
695 free(s);
696
697 return n;
698}
699
ca843648
JP
700/* Like stream_open(), but the port defaults to 'default_port' if no port
701 * number is given. */
f39dc942 702int
ca843648
JP
703stream_open_with_default_port(const char *name_,
704 uint16_t default_port,
705 struct stream **streamp,
706 uint8_t dscp)
f39dc942
BP
707{
708 char *name;
709 int error;
710
ca843648
JP
711 if ((!strncmp(name_, "tcp:", 4) || !strncmp(name_, "ssl:", 4))
712 && count_fields(name_) < 3) {
36359150
JP
713 if (default_port == OFP_OLD_PORT) {
714 VLOG_WARN_ONCE("The default OpenFlow port number will change "
715 "from %d to %d in a future release",
716 OFP_OLD_PORT, OFP_PORT);
717 } else if (default_port == OVSDB_OLD_PORT) {
718 VLOG_WARN_ONCE("The default OVSDB port number will change "
719 "from %d to %d in a future release",
720 OVSDB_OLD_PORT, OVSDB_PORT);
721 }
ca843648 722 name = xasprintf("%s:%d", name_, default_port);
f39dc942
BP
723 } else {
724 name = xstrdup(name_);
725 }
f125905c 726 error = stream_open(name, streamp, dscp);
f39dc942
BP
727 free(name);
728
729 return error;
730}
731
ca843648
JP
732/* Like pstream_open(), but port defaults to 'default_port' if no port
733 * number is given. */
f39dc942 734int
ca843648
JP
735pstream_open_with_default_port(const char *name_,
736 uint16_t default_port,
737 struct pstream **pstreamp,
738 uint8_t dscp)
f39dc942
BP
739{
740 char *name;
741 int error;
742
ca843648
JP
743 if ((!strncmp(name_, "ptcp:", 5) || !strncmp(name_, "pssl:", 5))
744 && count_fields(name_) < 2) {
745 name = xasprintf("%s%d", name_, default_port);
f39dc942
BP
746 } else {
747 name = xstrdup(name_);
748 }
f125905c 749 error = pstream_open(name, pstreamp, dscp);
f39dc942
BP
750 free(name);
751
752 return error;
753}
ac4c900d
AA
754
755/*
756 * This function extracts IP address and port from the target string.
757 *
e731d71b 758 * - On success, function returns true and fills *ss structure with port
ac4c900d
AA
759 * and IP address. If port was absent in target string then it will use
760 * corresponding default port value.
e731d71b 761 * - On error, function returns false and *ss contains garbage.
ac4c900d
AA
762 */
763bool
ca843648
JP
764stream_parse_target_with_default_port(const char *target,
765 uint16_t default_port,
e731d71b 766 struct sockaddr_storage *ss)
ca843648
JP
767{
768 return ((!strncmp(target, "tcp:", 4) || !strncmp(target, "ssl:", 4))
e731d71b 769 && inet_parse_active(target + 4, default_port, ss));
ac4c900d
AA
770}
771
1e3c0047
BP
772/* Attempts to guess the content type of a stream whose first few bytes were
773 * the 'size' bytes of 'data'. */
774static enum stream_content_type
c55acc2e 775stream_guess_content(const uint8_t *data, ssize_t size)
1e3c0047
BP
776{
777 if (size >= 2) {
778#define PAIR(A, B) (((A) << 8) | (B))
779 switch (PAIR(data[0], data[1])) {
780 case PAIR(0x16, 0x03): /* Handshake, version 3. */
781 return STREAM_SSL;
782 case PAIR('{', '"'):
783 return STREAM_JSONRPC;
982697a4 784 case PAIR(OFP10_VERSION, 0 /* OFPT_HELLO */):
1e3c0047
BP
785 return STREAM_OPENFLOW;
786 }
787 }
788
789 return STREAM_UNKNOWN;
790}
791
792/* Returns a string represenation of 'type'. */
793static const char *
794stream_content_type_to_string(enum stream_content_type type)
795{
796 switch (type) {
797 case STREAM_UNKNOWN:
798 default:
799 return "unknown";
f39dc942 800
1e3c0047
BP
801 case STREAM_JSONRPC:
802 return "JSON-RPC";
f39dc942 803
1e3c0047
BP
804 case STREAM_OPENFLOW:
805 return "OpenFlow";
806
807 case STREAM_SSL:
808 return "SSL";
809 }
810}
811
812/* Attempts to guess the content type of a stream whose first few bytes were
813 * the 'size' bytes of 'data'. If this is done successfully, and the guessed
814 * content type is other than 'expected_type', then log a message in vlog
815 * module 'module', naming 'stream_name' as the source, explaining what
816 * content was expected and what was actually received. */
817void
c55acc2e 818stream_report_content(const void *data, ssize_t size,
1e3c0047 819 enum stream_content_type expected_type,
480ce8ab 820 struct vlog_module *module, const char *stream_name)
1e3c0047
BP
821{
822 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
823 enum stream_content_type actual_type;
824
825 actual_type = stream_guess_content(data, size);
826 if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) {
827 vlog_rate_limit(module, VLL_WARN, &rl,
828 "%s: received %s data on %s channel",
829 stream_name,
c662c789
BP
830 stream_content_type_to_string(actual_type),
831 stream_content_type_to_string(expected_type));
1e3c0047
BP
832 }
833}