Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
7d9d459a
Commit
7d9d459a
authored
Sep 20, 2011
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
resolver: add function resolve_host_port()
parent
3ea10738
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
157 additions
and
2 deletions
+157
-2
Makefile.am
Makefile.am
+6
-0
resolver.c
src/resolver.c
+61
-2
resolver.h
src/resolver.h
+23
-0
run_resolver.c
test/run_resolver.c
+67
-0
No files found.
Makefile.am
View file @
7d9d459a
...
...
@@ -903,6 +903,7 @@ TESTS = $(C_TESTS)
noinst_PROGRAMS
=
\
$(C_TESTS)
\
test
/read_conf
\
test
/run_resolver
\
test
/run_input
\
test
/dump_playlist
\
test
/run_decoder
\
...
...
@@ -926,6 +927,11 @@ test_read_conf_LDADD = $(MPD_LIBS) \
test_read_conf_SOURCES
=
test
/read_conf.c
\
src/conf.c src/tokenizer.c src/utils.c src/string_util.c
test_run_resolver_LDADD
=
$(MPD_LIBS)
\
$(GLIB_LIBS)
test_run_resolver_SOURCES
=
test
/run_resolver.c
\
src/resolver.c
test_run_input_CPPFLAGS
=
$(AM_CPPFLAGS)
\
$(ARCHIVE_CFLAGS)
\
$(INPUT_CFLAGS)
...
...
src/resolver.c
View file @
7d9d459a
...
...
@@ -19,6 +19,7 @@
#include "config.h"
#include "resolver.h"
#include "glib_compat.h"
#ifndef G_OS_WIN32
#include <sys/socket.h>
...
...
@@ -29,9 +30,7 @@
#include <winsock.h>
#endif
/* G_OS_WIN32 */
#ifdef HAVE_IPV6
#include <string.h>
#endif
char
*
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
GError
**
error
)
...
...
@@ -81,3 +80,63 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
return
g_strconcat
(
host
,
":"
,
serv
,
NULL
);
}
struct
addrinfo
*
resolve_host_port
(
const
char
*
host_port
,
unsigned
default_port
,
int
flags
,
int
socktype
,
GError
**
error_r
)
{
char
*
p
=
g_strdup
(
host_port
);
const
char
*
host
=
p
,
*
port
=
NULL
;
if
(
host_port
[
0
]
==
'['
)
{
/* IPv6 needs enclosing square braces, to
differentiate between IP colons and the port
separator */
char
*
q
=
strchr
(
p
+
1
,
']'
);
if
(
q
!=
NULL
&&
q
[
1
]
==
':'
&&
q
[
2
]
!=
0
)
{
*
q
=
0
;
++
host
;
port
=
q
+
2
;
}
}
if
(
port
==
NULL
)
{
/* port is after the colon, but only if it's the only
colon (don't split IPv6 addresses) */
char
*
q
=
strchr
(
p
,
':'
);
if
(
q
!=
NULL
&&
q
[
1
]
!=
0
&&
strchr
(
q
+
1
,
':'
)
==
NULL
)
{
*
q
=
0
;
port
=
q
+
1
;
}
}
char
buffer
[
32
];
if
(
port
==
NULL
&&
default_port
!=
0
)
{
g_snprintf
(
buffer
,
sizeof
(
buffer
),
"%u"
,
default_port
);
port
=
buffer
;
}
if
((
flags
&
AI_PASSIVE
)
!=
0
&&
strcmp
(
host
,
"*"
)
==
0
)
host
=
NULL
;
const
struct
addrinfo
hints
=
{
.
ai_flags
=
flags
,
.
ai_family
=
AF_UNSPEC
,
.
ai_socktype
=
socktype
,
};
struct
addrinfo
*
ai
;
int
ret
=
getaddrinfo
(
host
,
port
,
&
hints
,
&
ai
);
g_free
(
p
);
if
(
ret
!=
0
)
{
g_set_error
(
error_r
,
resolver_quark
(),
ret
,
"Failed to look up '%s': %s"
,
host_port
,
gai_strerror
(
ret
));
return
NULL
;
}
return
ai
;
}
src/resolver.h
View file @
7d9d459a
...
...
@@ -23,6 +23,14 @@
#include <glib.h>
struct
sockaddr
;
struct
addrinfo
;
G_GNUC_CONST
static
inline
GQuark
resolver_quark
(
void
)
{
return
g_quark_from_static_string
(
"resolver"
);
}
/**
* Converts the specified socket address into a string in the form
...
...
@@ -37,4 +45,19 @@ struct sockaddr;
char
*
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
GError
**
error
);
/**
* Resolve a specification in the form "host", "host:port",
* "[host]:port". This is a convenience wrapper for getaddrinfo().
*
* @param default_port a default port number that will be used if none
* is given in the string (if applicable); pass 0 to go without a
* default
* @return an #addrinfo linked list that must be freed with
* freeaddrinfo(), or NULL on error
*/
struct
addrinfo
*
resolve_host_port
(
const
char
*
host_port
,
unsigned
default_port
,
int
flags
,
int
socktype
,
GError
**
error_r
);
#endif
test/run_resolver.c
0 → 100644
View file @
7d9d459a
/*
* Copyright (C) 2003-2011 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "resolver.h"
#ifdef WIN32
#define WINVER 0x0501
#include <ws2tcpip.h>
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif
#include <stdlib.h>
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
g_printerr
(
"Usage: run_resolver HOST
\n
"
);
return
EXIT_FAILURE
;
}
GError
*
error
=
NULL
;
struct
addrinfo
*
ai
=
resolve_host_port
(
argv
[
1
],
80
,
AI_PASSIVE
,
SOCK_STREAM
,
&
error
);
if
(
ai
==
NULL
)
{
g_printerr
(
"%s
\n
"
,
error
->
message
);
g_error_free
(
error
);
return
EXIT_FAILURE
;
}
for
(
const
struct
addrinfo
*
i
=
ai
;
i
!=
NULL
;
i
=
i
->
ai_next
)
{
char
*
p
=
sockaddr_to_string
(
i
->
ai_addr
,
i
->
ai_addrlen
,
&
error
);
if
(
p
==
NULL
)
{
freeaddrinfo
(
ai
);
g_printerr
(
"%s
\n
"
,
error
->
message
);
g_error_free
(
error
);
return
EXIT_FAILURE
;
}
g_print
(
"%s
\n
"
,
p
);
g_free
(
p
);
}
freeaddrinfo
(
ai
);
return
EXIT_SUCCESS
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment