Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 1 | #include "internal.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <unistd.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
| 8 | #include <time.h> |
| 9 | #include <sys/fcntl.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <arpa/inet.h> |
| 13 | |
| 14 | #include <osmocom/core/select.h> |
| 15 | #include <osmocom/gsm/tlv.h> |
| 16 | #include <osmocom/core/msgb.h> |
| 17 | #include <osmocom/core/logging.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 18 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 19 | #include <osmocom/abis/e1_input.h> |
| 20 | #include <osmocom/abis/ipaccess.h> |
| 21 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 22 | |
| 23 | #include <osmocom/abis/ipa.h> |
| 24 | |
| 25 | #define IPA_ALLOC_SIZE 1200 |
| 26 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 27 | struct msgb *ipa_msg_alloc(int headroom) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 28 | { |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 29 | struct msgb *nmsg; |
| 30 | |
| 31 | headroom += sizeof(struct ipaccess_head); |
| 32 | |
| 33 | nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "Abis/IP"); |
| 34 | if (!nmsg) |
| 35 | return NULL; |
| 36 | return nmsg; |
| 37 | } |
| 38 | |
| 39 | void ipa_msg_push_header(struct msgb *msg, uint8_t proto) |
| 40 | { |
| 41 | struct ipaccess_head *hh; |
| 42 | |
| 43 | msg->l2h = msg->data; |
| 44 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 45 | hh->proto = proto; |
| 46 | hh->len = htons(msgb_l2len(msg)); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int ipa_msg_recv(int fd, struct msgb **rmsg) |
| 50 | { |
| 51 | struct msgb *msg; |
| 52 | struct ipaccess_head *hh; |
| 53 | int len, ret; |
| 54 | |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 55 | msg = ipa_msg_alloc(0); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 56 | if (msg == NULL) |
| 57 | return -ENOMEM; |
| 58 | |
| 59 | /* first read our 3-byte header */ |
| 60 | hh = (struct ipaccess_head *) msg->data; |
| 61 | ret = recv(fd, msg->data, sizeof(*hh), 0); |
| 62 | if (ret <= 0) { |
| 63 | msgb_free(msg); |
| 64 | return ret; |
| 65 | } else if (ret != sizeof(*hh)) { |
| 66 | msgb_free(msg); |
| 67 | return -EIO; |
| 68 | } |
| 69 | msgb_put(msg, ret); |
| 70 | |
| 71 | /* then read the length as specified in header */ |
| 72 | msg->l2h = msg->data + sizeof(*hh); |
| 73 | len = ntohs(hh->len); |
| 74 | |
| 75 | if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) { |
| 76 | msgb_free(msg); |
| 77 | return -EIO; |
| 78 | } |
| 79 | |
| 80 | ret = recv(fd, msg->l2h, len, 0); |
| 81 | if (ret <= 0) { |
| 82 | msgb_free(msg); |
| 83 | return ret; |
| 84 | } else if (ret < len) { |
| 85 | msgb_free(msg); |
| 86 | return -EIO; |
| 87 | } |
| 88 | msgb_put(msg, ret); |
| 89 | *rmsg = msg; |
| 90 | return ret; |
| 91 | } |
| 92 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 93 | void ipa_client_link_close(struct ipa_client_link *link); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 94 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 95 | static void ipa_client_retry(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 96 | { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 97 | LOGP(DLINP, LOGL_NOTICE, "connection closed\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 98 | ipa_client_link_close(link); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 99 | LOGP(DLINP, LOGL_NOTICE, "retrying in 5 seconds...\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 100 | osmo_timer_schedule(&link->timer, 5, 0); |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 101 | link->state = IPA_CLIENT_LINK_STATE_CONNECTING; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 104 | void ipa_client_link_close(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 105 | { |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 106 | osmo_fd_unregister(link->ofd); |
| 107 | close(link->ofd->fd); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 110 | static void ipa_client_read(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 111 | { |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 112 | struct osmo_fd *ofd = link->ofd; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 113 | struct msgb *msg; |
| 114 | int ret; |
| 115 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 116 | LOGP(DLINP, LOGL_NOTICE, "message received\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 117 | |
| 118 | ret = ipa_msg_recv(ofd->fd, &msg); |
| 119 | if (ret < 0) { |
| 120 | if (errno == EPIPE || errno == ECONNRESET) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 121 | LOGP(DLINP, LOGL_ERROR, "lost connection with server\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 122 | } else { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 123 | LOGP(DLINP, LOGL_ERROR, "unknown error\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 124 | } |
| 125 | ipa_client_retry(link); |
| 126 | return; |
| 127 | } else if (ret == 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 128 | LOGP(DLINP, LOGL_ERROR, "connection closed with server\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 129 | ipa_client_retry(link); |
| 130 | return; |
| 131 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 132 | if (link->read_cb) |
| 133 | link->read_cb(link, msg); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 136 | static void ipa_client_write(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 137 | { |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 138 | if (link->write_cb) |
| 139 | link->write_cb(link); |
| 140 | } |
| 141 | |
| 142 | int ipa_client_write_default_cb(struct ipa_client_link *link) |
| 143 | { |
| 144 | struct osmo_fd *ofd = link->ofd; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 145 | struct msgb *msg; |
| 146 | struct llist_head *lh; |
| 147 | int ret; |
| 148 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 149 | LOGP(DLINP, LOGL_NOTICE, "sending data\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 150 | |
| 151 | if (llist_empty(&link->tx_queue)) { |
| 152 | ofd->when &= ~BSC_FD_WRITE; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 153 | return 0; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 154 | } |
| 155 | lh = link->tx_queue.next; |
| 156 | llist_del(lh); |
| 157 | msg = llist_entry(lh, struct msgb, list); |
| 158 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 159 | ret = send(link->ofd->fd, msg->data, msg->len, 0); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 160 | if (ret < 0) { |
| 161 | if (errno == EPIPE || errno == ENOTCONN) { |
| 162 | ipa_client_retry(link); |
| 163 | } |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 164 | LOGP(DLINP, LOGL_ERROR, "error to send\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 165 | } |
| 166 | msgb_free(msg); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 167 | return 0; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int ipa_client_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 171 | { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 172 | struct ipa_client_link *link = ofd->data; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 173 | int error, ret; |
| 174 | size_t len = sizeof(error); |
| 175 | |
| 176 | switch(link->state) { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 177 | case IPA_CLIENT_LINK_STATE_CONNECTING: |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 178 | ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len); |
| 179 | if (ret >= 0 && error > 0) { |
| 180 | ipa_client_retry(link); |
| 181 | return 0; |
| 182 | } |
| 183 | ofd->when &= ~BSC_FD_WRITE; |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 184 | LOGP(DLINP, LOGL_NOTICE, "connection done.\n"); |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 185 | link->state = IPA_CLIENT_LINK_STATE_CONNECTED; |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 186 | if (link->connect_cb) |
| 187 | link->connect_cb(link); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 188 | break; |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 189 | case IPA_CLIENT_LINK_STATE_CONNECTED: |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 190 | if (what & BSC_FD_READ) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 191 | LOGP(DLINP, LOGL_NOTICE, "connected read\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 192 | ipa_client_read(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 193 | } |
| 194 | if (what & BSC_FD_WRITE) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 195 | LOGP(DLINP, LOGL_NOTICE, "connected write\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 196 | ipa_client_write(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 197 | } |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static void ipa_link_timer_cb(void *data); |
| 206 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 207 | struct ipa_client_link * |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 208 | ipa_client_link_create(void *ctx, struct e1inp_ts *ts, const char *driver_name, |
| 209 | int priv_nr, const char *addr, uint16_t port, |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 210 | int (*connect_cb)(struct ipa_client_link *link), |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 211 | int (*read_cb)(struct ipa_client_link *link, |
| 212 | struct msgb *msgb), |
| 213 | int (*write_cb)(struct ipa_client_link *link), |
| 214 | void *data) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 215 | { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 216 | struct ipa_client_link *ipa_link; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 217 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 218 | ipa_link = talloc_zero(ctx, struct ipa_client_link); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 219 | if (!ipa_link) |
| 220 | return NULL; |
| 221 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 222 | if (ts) { |
| 223 | struct e1inp_driver *driver; |
| 224 | |
| 225 | driver = e1inp_driver_find(driver_name); |
| 226 | if (driver == NULL) { |
| 227 | talloc_free(ipa_link); |
| 228 | return NULL; |
| 229 | } |
| 230 | ts->line->driver = driver; |
| 231 | ipa_link->ofd = &ts->driver.ipaccess.fd; |
| 232 | } else { |
| 233 | ipa_link->ofd = talloc_zero(ctx, struct osmo_fd); |
| 234 | if (ipa_link->ofd == NULL) { |
| 235 | talloc_free(ipa_link); |
| 236 | return NULL; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | ipa_link->ofd->when |= BSC_FD_READ | BSC_FD_WRITE; |
| 241 | ipa_link->ofd->priv_nr = priv_nr; |
| 242 | ipa_link->ofd->cb = ipa_client_fd_cb; |
| 243 | ipa_link->ofd->data = ipa_link; |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 244 | ipa_link->state = IPA_CLIENT_LINK_STATE_CONNECTING; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 245 | ipa_link->timer.cb = ipa_link_timer_cb; |
| 246 | ipa_link->timer.data = ipa_link; |
Pablo Neira Ayuso | c00ee73 | 2011-06-21 12:22:49 +0200 | [diff] [blame] | 247 | ipa_link->addr = talloc_strdup(ipa_link, addr); |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 248 | ipa_link->port = port; |
Pablo Neira Ayuso | 88136fc | 2011-07-08 16:21:55 +0200 | [diff] [blame] | 249 | ipa_link->connect_cb = connect_cb; |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 250 | ipa_link->read_cb = read_cb; |
| 251 | ipa_link->write_cb = write_cb; |
| 252 | ipa_link->line = ts->line; |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 253 | ipa_link->data = data; |
Pablo Neira Ayuso | af3fed9 | 2011-06-23 20:42:19 +0200 | [diff] [blame] | 254 | INIT_LLIST_HEAD(&ipa_link->tx_queue); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 255 | |
| 256 | return ipa_link; |
| 257 | } |
| 258 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 259 | void ipa_client_link_destroy(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 260 | { |
| 261 | talloc_free(link); |
| 262 | } |
| 263 | |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 264 | int ipa_client_link_open(struct ipa_client_link *link) |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 265 | { |
| 266 | int ret; |
| 267 | |
| 268 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
Pablo Neira Ayuso | 9b3a33c | 2011-06-21 13:52:41 +0200 | [diff] [blame] | 269 | link->addr, link->port, |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 270 | OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK); |
| 271 | if (ret < 0) { |
| 272 | if (errno != EINPROGRESS) |
| 273 | return ret; |
| 274 | } |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 275 | link->ofd->fd = ret; |
| 276 | if (osmo_fd_register(link->ofd) < 0) { |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 277 | close(ret); |
| 278 | return -EIO; |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static void ipa_link_timer_cb(void *data) |
| 284 | { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 285 | struct ipa_client_link *link = data; |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 286 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 287 | LOGP(DLINP, LOGL_NOTICE, "reconnecting.\n"); |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 288 | |
| 289 | switch(link->state) { |
Pablo Neira Ayuso | c07a8e7 | 2011-06-21 19:50:04 +0200 | [diff] [blame] | 290 | case IPA_CLIENT_LINK_STATE_CONNECTING: |
Pablo Neira Ayuso | 96e8128 | 2011-06-09 15:06:11 +0200 | [diff] [blame] | 291 | ipa_client_link_open(link); |
| 292 | break; |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | } |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 297 | |
Pablo Neira Ayuso | af3fed9 | 2011-06-23 20:42:19 +0200 | [diff] [blame] | 298 | void ipa_client_link_send(struct ipa_client_link *link, struct msgb *msg) |
| 299 | { |
| 300 | msgb_enqueue(&link->tx_queue, msg); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 301 | link->ofd->when |= BSC_FD_WRITE; |
Pablo Neira Ayuso | af3fed9 | 2011-06-23 20:42:19 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 304 | int ipa_server_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 305 | { |
| 306 | int ret; |
| 307 | struct sockaddr_in sa; |
| 308 | socklen_t sa_len = sizeof(sa); |
| 309 | struct ipa_server_link *link = ofd->data; |
| 310 | |
| 311 | ret = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len); |
| 312 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 313 | LOGP(DLINP, LOGL_ERROR, "failed to accept from origin " |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 314 | "peer, reason=`%s'\n", strerror(errno)); |
| 315 | return ret; |
| 316 | } |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 317 | LOGP(DLINP, LOGL_NOTICE, "accept()ed new link from %s to port %u\n", |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 318 | inet_ntoa(sa.sin_addr), link->port); |
| 319 | |
| 320 | if (link->accept_cb) |
| 321 | link->accept_cb(link, ret); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | struct ipa_server_link * |
| 327 | ipa_server_link_create(void *ctx, struct e1inp_line *line, |
| 328 | const char *addr, uint16_t port, |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 329 | int (*accept_cb)(struct ipa_server_link *link, int fd), |
| 330 | void *data) |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 331 | { |
| 332 | struct ipa_server_link *ipa_link; |
| 333 | |
| 334 | ipa_link = talloc_zero(ctx, struct ipa_server_link); |
| 335 | if (!ipa_link) |
| 336 | return NULL; |
| 337 | |
| 338 | ipa_link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE; |
| 339 | ipa_link->ofd.cb = ipa_server_fd_cb; |
| 340 | ipa_link->ofd.data = ipa_link; |
| 341 | ipa_link->addr = talloc_strdup(ipa_link, addr); |
| 342 | ipa_link->port = port; |
| 343 | ipa_link->accept_cb = accept_cb; |
| 344 | ipa_link->line = line; |
Pablo Neira Ayuso | e009f4a | 2011-06-23 13:36:34 +0200 | [diff] [blame] | 345 | ipa_link->data = data; |
Pablo Neira Ayuso | 986191f | 2011-06-21 19:56:26 +0200 | [diff] [blame] | 346 | |
| 347 | return ipa_link; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | void ipa_server_link_destroy(struct ipa_server_link *link) |
| 352 | { |
| 353 | talloc_free(link); |
| 354 | } |
| 355 | |
| 356 | int ipa_server_link_open(struct ipa_server_link *link) |
| 357 | { |
| 358 | int ret; |
| 359 | |
| 360 | ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, |
| 361 | link->addr, link->port, OSMO_SOCK_F_BIND); |
| 362 | if (ret < 0) |
| 363 | return ret; |
| 364 | |
| 365 | link->ofd.fd = ret; |
| 366 | if (osmo_fd_register(&link->ofd) < 0) { |
| 367 | close(ret); |
| 368 | return -EIO; |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | void ipa_server_link_close(struct ipa_server_link *link) |
| 374 | { |
| 375 | osmo_fd_unregister(&link->ofd); |
| 376 | close(link->ofd.fd); |
| 377 | } |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 378 | |
| 379 | static void ipa_server_peer_read(struct ipa_server_peer *peer) |
| 380 | { |
| 381 | struct osmo_fd *ofd = &peer->ofd; |
| 382 | struct msgb *msg; |
| 383 | int ret; |
| 384 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 385 | LOGP(DLINP, LOGL_NOTICE, "message received\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 386 | |
| 387 | ret = ipa_msg_recv(ofd->fd, &msg); |
| 388 | if (ret < 0) { |
| 389 | if (errno == EPIPE || errno == ECONNRESET) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 390 | LOGP(DLINP, LOGL_ERROR, "lost connection with server\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 391 | } else { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 392 | LOGP(DLINP, LOGL_ERROR, "unknown error\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 393 | } |
| 394 | return; |
| 395 | } else if (ret == 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 396 | LOGP(DLINP, LOGL_ERROR, "connection closed with server\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 397 | ipa_server_peer_destroy(peer); |
| 398 | return; |
| 399 | } |
| 400 | if (peer->cb) |
| 401 | peer->cb(peer, msg); |
| 402 | |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | static void ipa_server_peer_write(struct ipa_server_peer *peer) |
| 407 | { |
| 408 | struct osmo_fd *ofd = &peer->ofd; |
| 409 | struct msgb *msg; |
| 410 | struct llist_head *lh; |
| 411 | int ret; |
| 412 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 413 | LOGP(DLINP, LOGL_NOTICE, "sending data\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 414 | |
| 415 | if (llist_empty(&peer->tx_queue)) { |
| 416 | ofd->when &= ~BSC_FD_WRITE; |
| 417 | return; |
| 418 | } |
| 419 | lh = peer->tx_queue.next; |
| 420 | llist_del(lh); |
| 421 | msg = llist_entry(lh, struct msgb, list); |
| 422 | |
| 423 | ret = send(peer->ofd.fd, msg->data, msg->len, 0); |
| 424 | if (ret < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 425 | LOGP(DLINP, LOGL_ERROR, "error to send\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 426 | } |
| 427 | msgb_free(msg); |
| 428 | } |
| 429 | |
| 430 | static int ipa_server_peer_cb(struct osmo_fd *ofd, unsigned int what) |
| 431 | { |
| 432 | struct ipa_server_peer *peer = ofd->data; |
| 433 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 434 | LOGP(DLINP, LOGL_NOTICE, "connected read/write\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 435 | if (what & BSC_FD_READ) |
| 436 | ipa_server_peer_read(peer); |
| 437 | if (what & BSC_FD_WRITE) |
| 438 | ipa_server_peer_write(peer); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | struct ipa_server_peer * |
| 444 | ipa_server_peer_create(void *ctx, struct ipa_server_link *link, int fd, |
| 445 | int (*cb)(struct ipa_server_peer *peer, struct msgb *msg), |
| 446 | void *data) |
| 447 | { |
| 448 | struct ipa_server_peer *peer; |
| 449 | |
| 450 | peer = talloc_zero(ctx, struct ipa_server_peer); |
| 451 | if (peer == NULL) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 452 | LOGP(DLINP, LOGL_ERROR, "cannot allocate new peer in server, " |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 453 | "reason=`%s'\n", strerror(errno)); |
| 454 | return NULL; |
| 455 | } |
| 456 | peer->server = link; |
| 457 | peer->ofd.fd = fd; |
| 458 | peer->ofd.data = peer; |
| 459 | peer->ofd.cb = ipa_server_peer_cb; |
| 460 | peer->ofd.when = BSC_FD_READ; |
| 461 | peer->cb = cb; |
| 462 | peer->data = data; |
Pablo Neira Ayuso | af3fed9 | 2011-06-23 20:42:19 +0200 | [diff] [blame] | 463 | INIT_LLIST_HEAD(&peer->tx_queue); |
| 464 | |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 465 | if (osmo_fd_register(&peer->ofd) < 0) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 466 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Pablo Neira Ayuso | 6af9b61 | 2011-06-23 14:07:47 +0200 | [diff] [blame] | 467 | talloc_free(peer); |
| 468 | return NULL; |
| 469 | } |
| 470 | return peer; |
| 471 | } |
| 472 | |
| 473 | void ipa_server_peer_destroy(struct ipa_server_peer *peer) |
| 474 | { |
| 475 | close(peer->ofd.fd); |
| 476 | osmo_fd_unregister(&peer->ofd); |
| 477 | talloc_free(peer); |
| 478 | } |
Pablo Neira Ayuso | af3fed9 | 2011-06-23 20:42:19 +0200 | [diff] [blame] | 479 | |
| 480 | void ipa_server_peer_send(struct ipa_server_peer *peer, struct msgb *msg) |
| 481 | { |
| 482 | msgb_enqueue(&peer->tx_queue, msg); |
| 483 | peer->ofd.when |= BSC_FD_WRITE; |
| 484 | } |