1 /* $NetBSD: session.c,v 1.2 2007/04/07 21:08:46 plunky Exp $ */
2 /* $DragonFly: src/lib/libsdp/session.c,v 1.1 2008/01/03 11:47:53 hasso Exp $ */
3
4 /*-
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
7 *
8 * Written by Iain Hibbert for Itronix Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * session.c
36 *
37 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * $Id: session.c,v 1.2 2007/04/07 21:08:46 plunky Exp $
62 * $FreeBSD: src/lib/libsdp/session.c,v 1.3 2004/01/09 22:44:28 emax Exp $
63 */
64
65 #include <sys/types.h>
66 #include <sys/un.h>
67 #include <bluetooth.h>
68 #include <errno.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72
73 #include <sdp-int.h>
74 #include <sdp.h>
75
76 void *
sdp_open(bdaddr_t const * l,bdaddr_t const * r)77 sdp_open(bdaddr_t const *l, bdaddr_t const *r)
78 {
79 sdp_session_p ss = NULL;
80 struct sockaddr_bt sa;
81 struct linger li;
82 socklen_t size;
83
84 if ((ss = calloc(1, sizeof(*ss))) == NULL)
85 goto fail;
86
87 if (l == NULL || r == NULL) {
88 ss->error = EINVAL;
89 goto fail;
90 }
91
92 ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
93 if (ss->s < 0) {
94 ss->error = errno;
95 goto fail;
96 }
97
98 memset(&li, 0, sizeof(li));
99 li.l_onoff = 1;
100 li.l_linger = 5;
101 if (setsockopt(ss->s, SOL_SOCKET, SO_LINGER, &li, sizeof(li)) < 0) {
102 ss->error = errno;
103 goto fail;
104 }
105
106 memset(&sa, 0, sizeof(sa));
107 sa.bt_len = sizeof(sa);
108 sa.bt_family = AF_BLUETOOTH;
109 bdaddr_copy(&sa.bt_bdaddr, l);
110 if (bind(ss->s, (void *) &sa, sizeof(sa)) < 0) {
111 ss->error = errno;
112 goto fail;
113 }
114
115 sa.bt_psm = L2CAP_PSM_SDP;
116 bdaddr_copy(&sa.bt_bdaddr, r);
117 if (connect(ss->s, (void *) &sa, sizeof(sa)) < 0) {
118 ss->error = errno;
119 goto fail;
120 }
121
122 size = sizeof(ss->omtu);
123 if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) {
124 ss->error = errno;
125 goto fail;
126 }
127 if ((ss->req = malloc((size_t)ss->omtu)) == NULL) {
128 ss->error = ENOMEM;
129 goto fail;
130 }
131 ss->req_e = ss->req + ss->omtu;
132
133 size = sizeof(ss->imtu);
134 if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) {
135 ss->error = errno;
136 goto fail;
137 }
138 if ((ss->rsp = malloc((size_t)ss->imtu)) == NULL) {
139 ss->error = ENOMEM;
140 goto fail;
141 }
142 ss->rsp_e = ss->rsp + ss->imtu;
143 ss->error = 0;
144 fail:
145 return ((void *) ss);
146 }
147
148 void *
sdp_open_local(char const * control)149 sdp_open_local(char const *control)
150 {
151 sdp_session_p ss = NULL;
152 struct sockaddr_un sa;
153
154 if ((ss = calloc(1, sizeof(*ss))) == NULL)
155 goto fail;
156
157 ss->s = socket(PF_LOCAL, SOCK_STREAM, 0);
158 if (ss->s < 0) {
159 ss->error = errno;
160 goto fail;
161 }
162
163 if (control == NULL)
164 control = SDP_LOCAL_PATH;
165
166 sa.sun_len = sizeof(sa);
167 sa.sun_family = AF_LOCAL;
168 strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
169
170 if (connect(ss->s, (void *) &sa, sizeof(sa)) < 0) {
171 ss->error = errno;
172 goto fail;
173 }
174
175 ss->flags |= SDP_SESSION_LOCAL;
176 ss->imtu = ss->omtu = SDP_LOCAL_MTU;
177
178 if ((ss->req = malloc((size_t)ss->omtu)) == NULL) {
179 ss->error = ENOMEM;
180 goto fail;
181 }
182 ss->req_e = ss->req + ss->omtu;
183
184 if ((ss->rsp = malloc((size_t)ss->imtu)) == NULL) {
185 ss->error = ENOMEM;
186 goto fail;
187 }
188 ss->rsp_e = ss->rsp + ss->imtu;
189 ss->error = 0;
190 fail:
191 return ((void *) ss);
192 }
193
194 int32_t
sdp_close(void * xss)195 sdp_close(void *xss)
196 {
197 sdp_session_p ss = (sdp_session_p) xss;
198
199 if (ss != NULL) {
200 if (ss->s >= 0)
201 close(ss->s);
202
203 if (ss->req != NULL)
204 free(ss->req);
205 if (ss->rsp != NULL)
206 free(ss->rsp);
207
208 memset(ss, 0, sizeof(*ss));
209 free(ss);
210 }
211
212 return (0);
213 }
214
215 int32_t
sdp_error(void * xss)216 sdp_error(void *xss)
217 {
218 sdp_session_p ss = (sdp_session_p) xss;
219
220 return ((ss != NULL)? ss->error : EINVAL);
221 }
222