[go: up one dir, main page]

Menu

[7510ab]: / src / address.c  Maximize  Restore  History

Download this file

295 lines (246 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (C) 2004 Steve Harris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#endif
#include "lo_types_internal.h"
#include "lo/lo.h"
#include "config.h"
lo_address lo_address_new(const char *host, const char *port)
{
lo_address a = calloc(1, sizeof(struct _lo_address));
a->ai = NULL;
a->socket = -1;
a->protocol = LO_UDP;
if (host) {
a->host = strdup(host);
} else {
a->host = strdup("localhost");
}
if (port) {
a->port = strdup(port);
} else {
a->port = NULL;
}
return a;
}
lo_address lo_address_new_from_url(const char *url)
{
lo_address a;
char *protocol;
if (!url || !*url) {
return NULL;
}
a = calloc(1, sizeof(struct _lo_address));
protocol = lo_url_get_protocol(url);
if (!protocol) {
return NULL;
} else if (!strcmp(protocol, "udp")) {
a->protocol = LO_UDP;
a->port = lo_url_get_port(url);
} else if (!strcmp(protocol, "tcp")) {
a->protocol = LO_TCP;
a->port = lo_url_get_port(url);
#ifndef WIN32
} else if (!strcmp(protocol, "unix")) {
a->protocol = LO_UNIX;
a->port = lo_url_get_path(url);
#endif
} else {
fprintf(stderr, PACKAGE_NAME ": protocol '%s' not supported by this "
"version\n", protocol);
free(a);
free(protocol);
return NULL;
}
free(protocol);
a->ai = NULL;
a->socket = -1;
a->host = lo_url_get_hostname(url);
if (!a->host) {
a->host = strdup("localhost");
}
return a;
}
const char *lo_address_get_hostname(lo_address a)
{
if (!a) {
return NULL;
}
return a->host;
}
int lo_address_get_protocol(lo_address a)
{
if (!a) {
return -1;
}
return a->protocol;
}
const char *lo_address_get_port(lo_address a)
{
if (!a) {
return NULL;
}
return a->port;
}
static const char* get_protocol_name(int proto)
{
switch(proto) {
case LO_UDP:
return "udp";
case LO_TCP:
return "tcp";
#ifndef WIN32
case LO_UNIX:
return "unix";
#endif
}
return NULL;
}
char *lo_address_get_url(lo_address a)
{
char *buf;
int ret;
int needquote = strchr(a->host, ':') ? 1 : 0;
char *fmt;
if (needquote) {
fmt = "osc.%s://[%s]:%s/";
} else {
fmt = "osc.%s://%s:%s/";
}
ret = snprintf(NULL, 0, fmt,
get_protocol_name(a->protocol), a->host, a->port);
if (ret <= 0) {
/* this libc is not C99 compliant, guess a size */
ret = 1023;
}
buf = malloc((ret + 2) * sizeof(char));
snprintf(buf, ret+1, fmt,
get_protocol_name(a->protocol), a->host, a->port);
return buf;
}
void lo_address_free(lo_address a)
{
if (a) {
if (a->socket != -1) {
close(a->socket);
}
if (a->host) free(a->host);
if (a->port) free(a->port);
if (a->ai) freeaddrinfo(a->ai);
free(a);
}
}
int lo_address_errno(lo_address a)
{
return a->errnum;
}
const char *lo_address_errstr(lo_address a)
{
char *msg;
if (a->errstr) {
return a->errstr;
}
msg = strerror(a->errnum);
if (msg) {
return msg;
} else {
return "unknown error";
}
return "unknown error";
}
char *lo_url_get_protocol(const char *url)
{
char *protocol,*ret;
if (!url) {
return NULL;
}
protocol = malloc(strlen(url));
if (sscanf(url, "osc://%s", protocol)) {
fprintf(stderr, PACKAGE_NAME " warning: no protocol specified in URL, "
"assuming UDP.\n");
ret = strdup("udp");
} else if (sscanf(url, "osc.%[^:/[]", protocol)) {
ret = strdup(protocol);
} else {
ret = NULL;
}
free(protocol);
return ret;
}
char *lo_url_get_hostname(const char *url)
{
char *hostname = malloc(strlen(url));
if (sscanf(url, "osc://%[^[:/]", hostname)) {
return hostname;
}
if (sscanf(url, "osc.%*[^:/]://[%[^]/]]", hostname)) {
return hostname;
}
if (sscanf(url, "osc.%*[^:/]://%[^[:/]", hostname)) {
return hostname;
}
/* doesnt look like an OSC URL */
free(hostname);
return NULL;
}
char *lo_url_get_port(const char *url)
{
char *port = malloc(strlen(url));
if (sscanf(url, "osc://%*[^:]:%[0-9]", port)) {
return port;
}
if (sscanf(url, "osc.%*[^:]://%*[^:]:%[0-9]", port)) {
return port;
}
if (sscanf(url, "osc://[%*[^]]]:%[0-9]", port)) {
return port;
}
if (sscanf(url, "osc.%*[^:]://[%*[^]]]:%[0-9]", port)) {
return port;
}
/* doesnt look like an OSC URL with port number */
free(port);
return NULL;
}
char *lo_url_get_path(const char *url)
{
char *path = malloc(strlen(url));
if (sscanf(url, "osc://%*[^:]:%*[0-9]%s", path)) {
return path;
}
if (sscanf(url, "osc.%*[^:]://%*[^:]:%*[0-9]%s", path) == 1) {
return path;
}
if (sscanf(url, "osc.unix://%*[^/]%s", path) == 1) {
return path;
}
if (sscanf(url, "osc.%*[^:]://%s", path)) {
return path;
}
/* doesnt look like an OSC URL with port number and path*/
return NULL;
}
/* vi:set ts=8 sts=4 sw=4: */