xref: /freebsd/lib/libsdp/session.c (revision 5b9c547c)
1 /*
2  * session.c
3  *
4  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: session.c,v 1.2 2003/09/04 22:12:13 max Exp $
29  * $FreeBSD$
30  */
31 #define L2CAP_SOCKET_CHECKED
32 #include <bluetooth.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <sdp-int.h>
39 #include <sdp.h>
40 
41 void *
42 sdp_open(bdaddr_t const *l, bdaddr_t const *r)
43 {
44 	sdp_session_p		ss = NULL;
45 	struct sockaddr_l2cap	sa;
46 	socklen_t		size;
47 
48 	if ((ss = calloc(1, sizeof(*ss))) == NULL)
49 		goto fail;
50 
51 	if (l == NULL || r == NULL) {
52 		ss->error = EINVAL;
53 		goto fail;
54 	}
55 
56 	ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
57 	if (ss->s < 0) {
58 		ss->error = errno;
59 		goto fail;
60 	}
61 
62 	sa.l2cap_len = sizeof(sa);
63 	sa.l2cap_family = AF_BLUETOOTH;
64 	sa.l2cap_psm = 0;
65 	sa.l2cap_cid = 0;
66 	sa.l2cap_bdaddr_type = BDADDR_BREDR;
67 
68 	memcpy(&sa.l2cap_bdaddr, l, sizeof(sa.l2cap_bdaddr));
69 	if (bind(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
70 		ss->error = errno;
71 		goto fail;
72 	}
73 
74 	sa.l2cap_psm = htole16(NG_L2CAP_PSM_SDP);
75 	memcpy(&sa.l2cap_bdaddr, r, sizeof(sa.l2cap_bdaddr));
76 	if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
77 		ss->error = errno;
78 		goto fail;
79 	}
80 
81 	size = sizeof(ss->omtu);
82 	if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) {
83 		ss->error = errno;
84 		goto fail;
85 	}
86 	if ((ss->req = malloc(ss->omtu)) == NULL) {
87 		ss->error = ENOMEM;
88 		goto fail;
89 	}
90 	ss->req_e = ss->req + ss->omtu;
91 
92 	size = sizeof(ss->imtu);
93 	if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) {
94 		ss->error = errno;
95 		goto fail;
96 	}
97 	if ((ss->rsp = malloc(ss->imtu)) == NULL) {
98 		ss->error = ENOMEM;
99 		goto fail;
100 	}
101 	ss->rsp_e = ss->rsp + ss->imtu;
102 	ss->error = 0;
103 fail:
104 	return ((void *) ss);
105 }
106 
107 void *
108 sdp_open_local(char const *control)
109 {
110 	sdp_session_p		ss = NULL;
111 	struct sockaddr_un	sa;
112 
113 	if ((ss = calloc(1, sizeof(*ss))) == NULL)
114 		goto fail;
115 
116 	ss->s = socket(PF_UNIX, SOCK_STREAM, 0);
117 	if (ss->s < 0) {
118 		ss->error = errno;
119 		goto fail;
120 	}
121 
122 	if (control == NULL)
123 		control = SDP_LOCAL_PATH;
124 
125 	sa.sun_len = sizeof(sa);
126 	sa.sun_family = AF_UNIX;
127 	strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
128 
129 	if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
130 		ss->error = errno;
131 		goto fail;
132 	}
133 
134 	ss->flags |= SDP_SESSION_LOCAL;
135 	ss->imtu = ss->omtu = SDP_LOCAL_MTU;
136 
137 	if ((ss->req = malloc(ss->omtu)) == NULL) {
138 		ss->error = ENOMEM;
139 		goto fail;
140 	}
141 	ss->req_e = ss->req + ss->omtu;
142 
143 	if ((ss->rsp = malloc(ss->imtu)) == NULL) {
144 		ss->error = ENOMEM;
145 		goto fail;
146 	}
147 	ss->rsp_e = ss->rsp + ss->imtu;
148 	ss->error = 0;
149 fail:
150 	return ((void *) ss);
151 }
152 
153 int32_t
154 sdp_close(void *xss)
155 {
156 	sdp_session_p	ss = (sdp_session_p) xss;
157 
158 	if (ss != NULL) {
159 		if (ss->s >= 0)
160 			close(ss->s);
161 
162 		if (ss->req != NULL)
163 			free(ss->req);
164 		if (ss->rsp != NULL)
165 			free(ss->rsp);
166 
167 		memset(ss, 0, sizeof(*ss));
168 		free(ss);
169 	}
170 
171 	return (0);
172 }
173 
174 int32_t
175 sdp_error(void *xss)
176 {
177 	sdp_session_p	ss = (sdp_session_p) xss;
178 
179 	return ((ss != NULL)? ss->error : EINVAL);
180 }
181