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
c8c39205
Commit
c8c39205
authored
Mar 14, 2009
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
socket_util: added socket_bind_listen()
Moved code from listen_add_address() (listen.c) to socket_util.c.
parent
dccb973c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
41 deletions
+85
-41
listen.c
src/listen.c
+4
-41
socket_util.c
src/socket_util.c
+61
-0
socket_util.h
src/socket_util.h
+20
-0
No files found.
src/listen.c
View file @
c8c39205
...
...
@@ -18,6 +18,7 @@
*/
#include "listen.h"
#include "socket_util.h"
#include "client.h"
#include "conf.h"
#include "utils.h"
...
...
@@ -45,8 +46,6 @@
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "listen"
#define ALLOW_REUSE 1
#define DEFAULT_PORT 6600
struct
listen_socket
{
...
...
@@ -73,49 +72,13 @@ static bool
listen_add_address
(
int
pf
,
const
struct
sockaddr
*
addrp
,
socklen_t
addrlen
,
GError
**
error
)
{
int
fd
,
ret
;
const
int
reuse
=
ALLOW_REUSE
;
#ifdef HAVE_STRUCT_UCRED
int
passcred
=
1
;
#endif
int
fd
;
struct
listen_socket
*
ls
;
GIOChannel
*
channel
;
fd
=
socket
(
pf
,
SOCK_STREAM
,
0
);
if
(
fd
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"Failed to create socket: %s"
,
strerror
(
errno
));
fd
=
socket_bind_listen
(
pf
,
SOCK_STREAM
,
0
,
addrp
,
addrlen
,
5
,
error
);
if
(
fd
<
0
)
return
false
;
}
ret
=
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
reuse
,
sizeof
(
reuse
));
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"setsockopt() failed: %s"
,
strerror
(
errno
));
close
(
fd
);
return
false
;
}
ret
=
bind
(
fd
,
addrp
,
addrlen
);
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"%s"
,
strerror
(
errno
));
close
(
fd
);
return
false
;
}
ret
=
listen
(
fd
,
5
);
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"listen() failed: %s"
,
strerror
(
errno
));
close
(
fd
);
return
false
;
}
#ifdef HAVE_STRUCT_UCRED
setsockopt
(
fd
,
SOL_SOCKET
,
SO_PASSCRED
,
&
passcred
,
sizeof
(
passcred
));
#endif
ls
=
g_new
(
struct
listen_socket
,
1
);
ls
->
fd
=
fd
;
...
...
src/socket_util.c
View file @
c8c39205
...
...
@@ -20,17 +20,27 @@
#include "socket_util.h"
#include "config.h"
#include <errno.h>
#include <unistd.h>
#ifndef G_OS_WIN32
#include <sys/socket.h>
#include <netdb.h>
#else
/* G_OS_WIN32 */
#include <ws2tcpip.h>
#include <winsock.h>
#endif
/* G_OS_WIN32 */
#ifdef HAVE_IPV6
#include <string.h>
#endif
static
GQuark
listen_quark
(
void
)
{
return
g_quark_from_static_string
(
"listen"
);
}
char
*
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
GError
**
error
)
{
...
...
@@ -79,3 +89,54 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
return
g_strconcat
(
host
,
":"
,
serv
,
NULL
);
}
int
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
const
struct
sockaddr
*
address
,
size_t
address_length
,
int
backlog
,
GError
**
error
)
{
int
fd
,
ret
;
const
int
reuse
=
1
;
#ifdef HAVE_STRUCT_UCRED
int
passcred
=
1
;
#endif
fd
=
socket
(
domain
,
type
,
protocol
);
if
(
fd
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"Failed to create socket: %s"
,
g_strerror
(
errno
));
return
-
1
;
}
ret
=
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
reuse
,
sizeof
(
reuse
));
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"setsockopt() failed: %s"
,
g_strerror
(
errno
));
close
(
fd
);
return
-
1
;
}
ret
=
bind
(
fd
,
address
,
address_length
);
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"%s"
,
strerror
(
errno
));
close
(
fd
);
return
-
1
;
}
ret
=
listen
(
fd
,
backlog
);
if
(
ret
<
0
)
{
g_set_error
(
error
,
listen_quark
(),
errno
,
"listen() failed: %s"
,
g_strerror
(
errno
));
close
(
fd
);
return
-
1
;
}
#ifdef HAVE_STRUCT_UCRED
setsockopt
(
fd
,
SOL_SOCKET
,
SO_PASSCRED
,
&
passcred
,
sizeof
(
passcred
));
#endif
return
fd
;
}
src/socket_util.h
View file @
c8c39205
...
...
@@ -43,4 +43,24 @@ struct sockaddr;
char
*
sockaddr_to_string
(
const
struct
sockaddr
*
sa
,
size_t
length
,
GError
**
error
);
/**
* Creates a socket listening on the specified address. This is a
* shortcut for socket(), bind() and listen().
*
* @param domain the socket domain, e.g. PF_INET6
* @param type the socket type, e.g. SOCK_STREAM
* @param protocol the protocol, usually 0 to let the kernel choose
* @param address the address to listen on
* @param address_length the size of #address
* @param backlog the backlog parameter for the listen() system call
* @param error location to store the error occuring, or NULL to
* ignore errors
* @return the socket file descriptor or -1 on error
*/
int
socket_bind_listen
(
int
domain
,
int
type
,
int
protocol
,
const
struct
sockaddr
*
address
,
size_t
address_length
,
int
backlog
,
GError
**
error
);
#endif
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