-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
145 lines (134 loc) · 3.02 KB
/
net.c
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
/*
* net.c
*
* Created on: Mar 8, 2013
* Author: volpol
* See LICENSE file for more details.
* Modifed:
* 2013-05-31 support bind address, statics no more [uw]
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include "log.h"
int net_open_client( const char *host, unsigned short port, const char *bind_addr )
{
int sock = -1;
struct sockaddr_in server;
struct hostent *he = NULL;
//WHOAMI;
DPRINT( "%s:%d [%s]", host, port, bind_addr ? bind_addr : "*" );
if ( 0 > ( sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) )
return perror( "socket()" ), -1;
server.sin_port = htons( port );
server.sin_family = AF_INET;
if ( NULL == ( he = gethostbyname( host ) ) )
{
herror( "gethostbyname()" );
goto ERROUT;
}
if ( NULL != bind_addr )
{
struct sockaddr_in lob;
lob.sin_family = AF_INET;
if ( 0 != strcmp( bind_addr, "*" ) )
{
if ( 0 == strcmp( bind_addr, "localhost" ) )
bind_addr = "127.0.0.1";
lob.sin_family = AF_INET;
if ( 0 == inet_aton( bind_addr, (struct in_addr *)&lob.sin_addr.s_addr ) )
{
fprintf( stderr, "inet_aton() failed\n" );
goto ERROUT;
}
}
else
lob.sin_addr.s_addr = INADDR_ANY;
if ( 0 > bind( sock, (const struct sockaddr*)&lob, sizeof lob ) )
{
perror( "bind()" );
goto ERROUT;
}
}
server.sin_addr.s_addr = *((in_addr_t*)he->h_addr_list[0]);
if ( 0 != connect( sock, (struct sockaddr *)&server, sizeof server ) )
{
perror( "connect()" );
goto ERROUT;
}
return sock;
ERROUT:
close( sock );
return -1;
}
int net_accept( int sock )
{
WHOAMI;
int conn = -1;
if ( 0 > ( conn = accept( sock, NULL, NULL ) ) )
perror( "accept()" );
else
{
DPRINT( "accepted conn: %d\n", conn );
}
return conn;
}
void net_close( int conn )
{
WHOAMI;
if ( 0 <= conn )
{
DPRINT( "shutting down conn: %d\n", conn );
shutdown( conn, SHUT_RDWR );
close( conn );
}
}
int net_open_server( unsigned short port, const char *bind_addr )
{
int sock;
int reuse = 1;
struct sockaddr_in lob;
//WHOAMI;
DPRINT( "%d [%s]", port, bind_addr ? bind_addr : "*" );
if ( 0 > ( sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) )
return perror( "socket()" ), -1;
if ( 0 != setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse ) )
{
perror( "setsockopt()" );
goto ERROUT;
}
lob.sin_family = AF_INET;
lob.sin_port = htons( port );
if ( NULL != bind_addr && 0 != strcmp( bind_addr, "*" ) )
{
if ( 0 == strcmp( bind_addr, "localhost" ) )
bind_addr = "127.0.0.1";
lob.sin_family = AF_INET;
if ( 0 == inet_aton( bind_addr, (struct in_addr *)&lob.sin_addr.s_addr ) )
{
fprintf( stderr, "inet_aton() failed\n" );
goto ERROUT;
}
}
else
lob.sin_addr.s_addr = INADDR_ANY;
if ( 0 > bind( sock, (const struct sockaddr*)&lob, sizeof lob ) )
{
perror( "bind()" );
goto ERROUT;
}
if ( 0 > listen( sock, 0 ) )
{
perror( "listen()" );
goto ERROUT;
}
return sock;
ERROUT:
close( sock );
return -1;
}