layer23: use correct sizeof(sockaddr) for connecting to L1CTL socket
[osmocom-bb.git] / src / host / layer23 / src / common / l1l2_interface.c
1 /* Layer 1 socket interface of layer2/3 stack */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <osmocom/bb/common/osmocom_data.h>
25 #include <osmocom/bb/common/l1ctl.h>
26 #include <osmocom/bb/common/logging.h>
27 #include <osmocom/bb/common/l1l2_interface.h>
28
29 #include <osmocore/utils.h>
30
31 #include <sys/socket.h>
32 #include <sys/un.h>
33
34 #include <arpa/inet.h>
35
36 #define _GNU_SOURCE
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 #define GSM_L2_LENGTH 256
43 #define GSM_L2_HEADROOM 32
44
45 static int layer2_read(struct bsc_fd *fd)
46 {
47         struct msgb *msg;
48         u_int16_t len;
49         int rc;
50
51         msg = msgb_alloc_headroom(GSM_L2_LENGTH+GSM_L2_HEADROOM, GSM_L2_HEADROOM, "Layer2");
52         if (!msg) {
53                 LOGP(DL1C, LOGL_ERROR, "Failed to allocate msg.\n");
54                 return -ENOMEM;
55         }
56
57         rc = read(fd->fd, &len, sizeof(len));
58         if (rc < sizeof(len)) {
59                 fprintf(stderr, "Layer2 socket failed\n");
60                 msgb_free(msg);
61                 if (rc >= 0)
62                         rc = -EIO;
63                 layer2_close((struct osmocom_ms *) fd->data);
64                 return rc;
65         }
66
67         len = ntohs(len);
68         if (len > GSM_L2_LENGTH) {
69                 LOGP(DL1C, LOGL_ERROR, "Length is too big: %u\n", len);
70                 msgb_free(msg);
71                 return -EINVAL;
72         }
73
74
75         msg->l1h = msgb_put(msg, len);
76         rc = read(fd->fd, msg->l1h, msgb_l1len(msg));
77         if (rc != msgb_l1len(msg)) {
78                 LOGP(DL1C, LOGL_ERROR, "Can not read data: len=%d rc=%d "
79                      "errno=%d\n", len, rc, errno);
80                 msgb_free(msg);
81                 return rc;
82         }
83
84         l1ctl_recv((struct osmocom_ms *) fd->data, msg);
85
86         return 0;
87 }
88
89 static int layer2_write(struct bsc_fd *fd, struct msgb *msg)
90 {
91         int rc;
92
93         if (fd->fd <= 0)
94                 return -EINVAL;
95
96         rc = write(fd->fd, msg->data, msg->len);
97         if (rc != msg->len) {
98                 LOGP(DL1C, LOGL_ERROR, "Failed to write data: rc: %d\n", rc);
99                 return rc;
100         }
101
102         return 0;
103 }
104
105 int layer2_open(struct osmocom_ms *ms, const char *socket_path)
106 {
107         int rc;
108         struct sockaddr_un local;
109
110         ms->l2_wq.bfd.fd = socket(AF_UNIX, SOCK_STREAM, 0);
111         if (ms->l2_wq.bfd.fd < 0) {
112                 fprintf(stderr, "Failed to create unix domain socket.\n");
113                 return ms->l2_wq.bfd.fd;
114         }
115
116         local.sun_family = AF_UNIX;
117         strncpy(local.sun_path, socket_path, sizeof(local.sun_path));
118         local.sun_path[sizeof(local.sun_path) - 1] = '\0';
119
120         rc = connect(ms->l2_wq.bfd.fd, (struct sockaddr *) &local,
121                      sizeof(local));
122         if (rc < 0) {
123                 fprintf(stderr, "Failed to connect to '%s': %s\n", local.sun_path,
124                         strerror(errno));
125                 close(ms->l2_wq.bfd.fd);
126                 return rc;
127         }
128
129         write_queue_init(&ms->l2_wq, 100);
130         ms->l2_wq.bfd.data = ms;
131         ms->l2_wq.bfd.when = BSC_FD_READ;
132         ms->l2_wq.read_cb = layer2_read;
133         ms->l2_wq.write_cb = layer2_write;
134
135         rc = bsc_register_fd(&ms->l2_wq.bfd);
136         if (rc != 0) {
137                 fprintf(stderr, "Failed to register fd.\n");
138                 close(ms->l2_wq.bfd.fd);
139                 return rc;
140         }
141
142         return 0;
143 }
144
145 int layer2_close(struct osmocom_ms *ms)
146 {
147         if (ms->l2_wq.bfd.fd <= 0)
148                 return -EINVAL;
149
150         close(ms->l2_wq.bfd.fd);
151         ms->l2_wq.bfd.fd = -1;
152         bsc_unregister_fd(&ms->l2_wq.bfd);
153
154         return 0;
155 }
156
157 int osmo_send_l1(struct osmocom_ms *ms, struct msgb *msg)
158 {
159         uint16_t *len;
160
161         DEBUGP(DL1C, "Sending: '%s'\n", hexdump(msg->data, msg->len));
162
163         if (msg->l1h != msg->data)
164                 LOGP(DL1C, LOGL_ERROR, "Message L1 header != Message Data\n");
165         
166         /* prepend 16bit length before sending */
167         len = (uint16_t *) msgb_push(msg, sizeof(*len));
168         *len = htons(msg->len - sizeof(*len));
169
170         if (write_queue_enqueue(&ms->l2_wq, msg) != 0) {
171                 LOGP(DL1C, LOGL_ERROR, "Failed to enqueue msg.\n");
172                 msgb_free(msg);
173                 return -1;
174         }
175
176         return 0;
177 }
178
179