| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
/* Common platform-independent implementation for proactor libraries */ |
| 22 |
|
| 23 |
#include "proactor-internal.h" |
| 24 |
#include <proton/error.h> |
| 25 |
#include <proton/listener.h> |
| 26 |
#include <proton/netaddr.h> |
| 27 |
#include <proton/proactor.h> |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
|
| 34 |
static const char *AMQP_PORT = "5672"; |
| 35 |
static const char *AMQP_PORT_NAME = "amqp"; |
| 36 |
static const char *AMQPS_PORT = "5671"; |
| 37 |
static const char *AMQPS_PORT_NAME = "amqps"; |
| 38 |
|
| 39 |
const char *PNI_IO_CONDITION = "proton:io"; |
| 40 |
|
| 41 |
int pn_proactor_addr(char *buf, size_t len, const char *host, const char *port) { |
| 42 |
return snprintf(buf, len, "%s:%s", host ? host : "", port ? port : ""); |
| 43 |
} |
| 44 |
|
| 45 |
int pni_parse_addr(const char *addr, char *buf, size_t len, const char **host, const char **port) |
| 46 |
{ |
| 47 |
size_t hplen = strlen(addr); |
| 48 |
if (hplen >= len) { |
| 49 |
return PN_OVERFLOW; |
| 50 |
} |
| 51 |
memcpy(buf, addr, hplen+1); |
| 52 |
char *p = strrchr(buf, ':'); |
| 53 |
if (p) { |
| 54 |
*port = p + 1; |
| 55 |
*p = '\0'; |
| 56 |
if (**port == '\0' || !strcmp(*port, AMQP_PORT_NAME)) { |
| 57 |
*port = AMQP_PORT; |
| 58 |
} else if (!strcmp(*port, AMQPS_PORT_NAME)) { |
| 59 |
*port = AMQPS_PORT; |
| 60 |
} |
| 61 |
} else { |
| 62 |
*port = AMQP_PORT; |
| 63 |
} |
| 64 |
if (*buf) { |
| 65 |
*host = buf; |
| 66 |
} else { |
| 67 |
*host = NULL; |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static inline const char *nonull(const char *str) { return str ? str : ""; } |
| 73 |
|
| 74 |
void pni_proactor_set_cond( |
| 75 |
pn_condition_t *cond, const char *what, const char *host, const char *port, const char *msg) |
| 76 |
{ |
| 77 |
if (!pn_condition_is_set(cond)) { /* Preserve older error information */ |
| 78 |
pn_condition_format(cond, PNI_IO_CONDITION, "%s - %s %s:%s", |
| 79 |
msg, what, nonull(host), nonull(port)); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
pn_event_t *pn_event_batch_next(pn_event_batch_t *batch) { |
| 84 |
return batch->next_event(batch); |
| 85 |
} |
| 86 |
|
| 87 |
// Backwards compatibility signatures. |
| 88 |
|
| 89 |
void pn_proactor_connect(pn_proactor_t *p, pn_connection_t *c, const char *addr) { |
| 90 |
pn_proactor_connect2(p, c, NULL, addr); |
| 91 |
} |
| 92 |
|
| 93 |
void pn_listener_accept(pn_listener_t *l, pn_connection_t *c) { |
| 94 |
pn_listener_accept2(l, c, NULL); |
| 95 |
} |
| 96 |
|
| 97 |
/* Deprecated, keep backwards compatible library symbols */ |
| 98 |
const pn_netaddr_t *pn_netaddr_local(pn_transport_t *t) { return pn_transport_local_addr(t); } |
| 99 |
const pn_netaddr_t *pn_netaddr_remote(pn_transport_t *t) { return pn_transport_remote_addr(t); } |
| 100 |
const pn_netaddr_t *pn_netaddr_listening(pn_listener_t *l) { return pn_listener_addr(l); } |
| 101 |
|