blob: 6f9639f6ff586b11679a6aeed5048cf8537df2be [file] [log] [blame]
Pablo Neira Ayuso96e81282011-06-09 15:06:11 +02001#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>
18#include <talloc.h>
19#include <osmocom/abis/e1_input.h>
20#include <osmocom/abis/ipaccess.h>
21#include <osmocom/core/socket.h>
22#include <osmocom/abis/logging.h>
23
24#include <osmocom/abis/ipa.h>
25
26#define IPA_ALLOC_SIZE 1200
27
28static struct msgb *ipa_msg_alloc(void)
29{
30 return msgb_alloc(IPA_ALLOC_SIZE, "Abis/IP");
31}
32
33int ipa_msg_recv(int fd, struct msgb **rmsg)
34{
35 struct msgb *msg;
36 struct ipaccess_head *hh;
37 int len, ret;
38
39 msg = ipa_msg_alloc();
40 if (msg == NULL)
41 return -ENOMEM;
42
43 /* first read our 3-byte header */
44 hh = (struct ipaccess_head *) msg->data;
45 ret = recv(fd, msg->data, sizeof(*hh), 0);
46 if (ret <= 0) {
47 msgb_free(msg);
48 return ret;
49 } else if (ret != sizeof(*hh)) {
50 msgb_free(msg);
51 return -EIO;
52 }
53 msgb_put(msg, ret);
54
55 /* then read the length as specified in header */
56 msg->l2h = msg->data + sizeof(*hh);
57 len = ntohs(hh->len);
58
59 if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) {
60 msgb_free(msg);
61 return -EIO;
62 }
63
64 ret = recv(fd, msg->l2h, len, 0);
65 if (ret <= 0) {
66 msgb_free(msg);
67 return ret;
68 } else if (ret < len) {
69 msgb_free(msg);
70 return -EIO;
71 }
72 msgb_put(msg, ret);
73 *rmsg = msg;
74 return ret;
75}
76
77void ipa_client_link_close(struct ipa_link *link);
78
79static void ipa_client_retry(struct ipa_link *link)
80{
81 LOGP(DINP, LOGL_NOTICE, "connection closed\n");
82 ipa_client_link_close(link);
83 LOGP(DINP, LOGL_NOTICE, "retrying in 5 seconds...\n");
84 osmo_timer_schedule(&link->timer, 5, 0);
85 link->state = IPA_LINK_STATE_CONNECTING;
86}
87
88void ipa_client_link_close(struct ipa_link *link)
89{
90 osmo_fd_unregister(&link->ofd);
91 close(link->ofd.fd);
92}
93
94static void ipa_client_read(struct ipa_link *link)
95{
96 struct osmo_fd *ofd = &link->ofd;
97 struct msgb *msg;
98 int ret;
99
100 LOGP(DINP, LOGL_NOTICE, "message received\n");
101
102 ret = ipa_msg_recv(ofd->fd, &msg);
103 if (ret < 0) {
104 if (errno == EPIPE || errno == ECONNRESET) {
105 LOGP(DINP, LOGL_ERROR, "lost connection with server\n");
106 } else {
107 LOGP(DINP, LOGL_ERROR, "unknown error\n");
108 }
109 ipa_client_retry(link);
110 return;
111 } else if (ret == 0) {
112 LOGP(DINP, LOGL_ERROR, "connection closed with server\n");
113 ipa_client_retry(link);
114 return;
115 }
116 if (link->process)
117 link->process(link, msg);
118}
119
120static void ipa_client_write(struct ipa_link *link)
121{
122 struct osmo_fd *ofd = &link->ofd;
123 struct msgb *msg;
124 struct llist_head *lh;
125 int ret;
126
127 LOGP(DINP, LOGL_NOTICE, "sending data\n");
128
129 if (llist_empty(&link->tx_queue)) {
130 ofd->when &= ~BSC_FD_WRITE;
131 return;
132 }
133 lh = link->tx_queue.next;
134 llist_del(lh);
135 msg = llist_entry(lh, struct msgb, list);
136
137 ret = send(link->ofd.fd, msg->data, msg->len, 0);
138 if (ret < 0) {
139 if (errno == EPIPE || errno == ENOTCONN) {
140 ipa_client_retry(link);
141 }
142 LOGP(DINP, LOGL_ERROR, "error to send\n");
143 }
144 msgb_free(msg);
145}
146
147int ipa_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
148{
149 struct ipa_link *link = ofd->data;
150 int error, ret;
151 size_t len = sizeof(error);
152
153 switch(link->state) {
154 case IPA_LINK_STATE_CONNECTING:
155 ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
156 if (ret >= 0 && error > 0) {
157 ipa_client_retry(link);
158 return 0;
159 }
160 ofd->when &= ~BSC_FD_WRITE;
161 LOGP(DINP, LOGL_NOTICE, "connection done.\n");
162 link->state = IPA_LINK_STATE_CONNECTED;
163 break;
164 case IPA_LINK_STATE_CONNECTED:
165 LOGP(DINP, LOGL_NOTICE, "connected read/write\n");
166 if (what & BSC_FD_READ)
167 ipa_client_read(link);
168 if (what & BSC_FD_WRITE)
169 ipa_client_write(link);
170 break;
171 default:
172 break;
173 }
174 return 0;
175}
176
177static void ipa_link_timer_cb(void *data);
178
179struct ipa_link *ipa_client_link_create(void *ctx)
180{
181 struct ipa_link *ipa_link;
182
183 ipa_link = talloc_zero(ctx, struct ipa_link);
184 if (!ipa_link)
185 return NULL;
186
187 ipa_link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
188 ipa_link->ofd.cb = ipa_client_fd_cb;
189 ipa_link->ofd.data = ipa_link;
190 ipa_link->state = IPA_LINK_STATE_CONNECTING;
191 ipa_link->timer.cb = ipa_link_timer_cb;
192 ipa_link->timer.data = ipa_link;
193
194 return ipa_link;
195}
196
197void ipa_client_link_destroy(struct ipa_link *link)
198{
199 talloc_free(link);
200}
201
202int ipa_client_link_open(struct ipa_link *link)
203{
204 int ret;
205
206 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
207 "127.0.0.1", IPA_TCP_PORT_OML,
208 OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
209 if (ret < 0) {
210 if (errno != EINPROGRESS)
211 return ret;
212 }
213 link->ofd.fd = ret;
214 if (osmo_fd_register(&link->ofd) < 0) {
215 close(ret);
216 return -EIO;
217 }
218 return 0;
219}
220
221static void ipa_link_timer_cb(void *data)
222{
223 struct ipa_link *link = data;
224
225 LOGP(DINP, LOGL_NOTICE, "reconnecting.\n");
226
227 switch(link->state) {
228 case IPA_LINK_STATE_CONNECTING:
229 ipa_client_link_open(link);
230 break;
231 default:
232 break;
233 }
234}