summaryrefslogtreecommitdiffstats
path: root/common/sys
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-04-18 13:30:44 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-04-18 13:30:44 +0800
commit18f83795218bf3ea80255e9e84781ffa306921c5 (patch)
treecceef0864166b6265b53e9b04cd5aa52b046d62d /common/sys
parentac1de1f0a63c5d49442fa0a7d6b336e4b01c5dc7 (diff)
downloadpttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar.gz
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar.bz2
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar.lz
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar.xz
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.tar.zst
pttbbs-18f83795218bf3ea80255e9e84781ffa306921c5.zip
- (internal) tobind/toconnect: allow specifying INADDR_ANY (*:port) or loopback (:port).
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4186 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'common/sys')
-rw-r--r--common/sys/net.c43
1 files changed, 29 insertions, 14 deletions
diff --git a/common/sys/net.c b/common/sys/net.c
index 27f7975c..80566487 100644
--- a/common/sys/net.c
+++ b/common/sys/net.c
@@ -33,16 +33,22 @@ ipstr2int(const char *ip)
return val;
}
+// addr format:
+// xxx.xxx.xxx.xxx:port
+// :port (bind to loopback)
+// *:port (bind to addr_any, allow remote connect)
+// all others formats are UNIX domain socket path.
+
int tobind(const char * addr)
{
int sockfd, val = 1;
- assert(addr != NULL);
+ assert(addr && *addr);
- if (!isdigit(addr[0] && addr[0] != ':')) {
+ if (!isdigit(addr[0]) && addr[0] != ':' && addr[0] != '*') {
struct sockaddr_un servaddr;
- if ( (sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
+ if ( (sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
perror("socket()");
exit(1);
}
@@ -62,7 +68,7 @@ int tobind(const char * addr)
char buf[64], *port;
struct sockaddr_in servaddr;
- if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
+ if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
perror("socket()");
exit(1);
}
@@ -75,14 +81,18 @@ int tobind(const char * addr)
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(char *)&val, sizeof(val));
- servaddr.sin_family = AF_INET;
- if (buf[0] == '\0')
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- if (inet_aton(buf, &servaddr.sin_addr) == 0) {
+
+ if (!buf[0])
+ servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ else if (buf[0] == '*')
+ servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // XXX use INADDR_LOOPBACK?
+ else if (inet_aton(buf, &servaddr.sin_addr) == 0) {
perror("inet_aton()");
exit(1);
}
+
servaddr.sin_port = htons(atoi(port));
+ servaddr.sin_family = AF_INET;
if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
perror("bind()");
@@ -102,12 +112,12 @@ int toconnect(const char *addr)
{
int sock;
- assert(addr != NULL);
+ assert(addr && *addr);
- if (!isdigit(addr[0])) {
+ if (!isdigit(addr[0]) && addr[0] != ':' && addr[0] != '*') {
struct sockaddr_un serv_name;
- if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
+ if ( (sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
perror("socket");
return -1;
}
@@ -124,7 +134,7 @@ int toconnect(const char *addr)
char buf[64], *port;
struct sockaddr_in serv_name;
- if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
+ if( (sock = socket(PF_INET, SOCK_STREAM, 0)) < 0 ){
perror("socket");
return -1;
}
@@ -135,9 +145,14 @@ int toconnect(const char *addr)
assert(port && atoi(port) != 0);
- serv_name.sin_family = AF_INET;
- serv_name.sin_addr.s_addr = inet_addr(buf);
+ if (!buf[0] || buf[0] == '*')
+ serv_name.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ else
+ serv_name.sin_addr.s_addr = inet_addr(buf);
+
serv_name.sin_port = htons(atoi(port));
+ serv_name.sin_family = AF_INET;
+
if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){
close(sock);
return -1;