-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.c
115 lines (104 loc) · 2.98 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
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "error.h"
#include "net.h"
const struct net_param net_params[] = {
#ifdef USEUNIXSOCK
{ STR_UNIX, sizeof(STR_UNIX) -1, STR_NAME_UNIX, AF_UNIX, SOCK_STREAM, IPPROTO_IP },
#endif
#ifdef USETCP4
{ STR_TCP4, sizeof(STR_TCP4) -1, STR_NAME_TCP, AF_INET, SOCK_STREAM, IPPROTO_TCP },
#endif
#ifdef USEUDP4
{ STR_UDP4, sizeof(STR_UDP4) -1, STR_NAME_UDP, AF_INET, SOCK_DGRAM, IPPROTO_UDP },
#endif
#ifdef USESCTP4
{ STR_SCTP4, sizeof(STR_SCTP4)-1, STR_NAME_SCTP, AF_INET, SOCK_STREAM, IPPROTO_SCTP },
#endif
#ifdef USETCP6
{ STR_TCP6, sizeof(STR_TCP6) -1, STR_NAME_TCP, AF_INET6, SOCK_STREAM, IPPROTO_TCP },
#endif
#ifdef USEUDP6
{ STR_UDP6, sizeof(STR_UDP6) -1, STR_NAME_UDP, AF_INET6, SOCK_DGRAM, IPPROTO_UDP },
#endif
#ifdef USESCTP6
{ STR_SCTP6, sizeof(STR_SCTP6)-1, STR_NAME_SCTP, AF_INET6, SOCK_STREAM, IPPROTO_SCTP },
#endif
{ NULL, 0, NULL, -1, -1, -1 }
};
int parsetarget(char* target, struct hostent** host, int* port)
{
int proto = -1;
int index = 0;
while ( net_params[index].m_str )
{
if ( !memcmp( target, net_params[index].m_str, net_params[index].m_strlength ) )
{
proto = index;
target += net_params[index].m_strlength;
break;
}
++index;
}
if ( proto == -1 )
{
fprintf( stderr, "protocol not specified\n" );
exit( EXIT_FAILURE );
}
char endchar = ( *target == '[' )? ']' : ':';
if ( *target == '[' )
++target;
char* hostend;
for (hostend = target; *hostend && *hostend != endchar; ++hostend)
;
if ( !*hostend )
{
#ifdef USEUNIXSOCK
if ( net_params[index].m_str == STR_UNIX )
{
*host = (struct hostent*)malloc( sizeof(struct hostent) );
if ( *host == NULL )
{
error_malloc( errno );
exit( EXIT_FAILURE );
}
memset( *host, 0, sizeof(struct hostent) );
(*host)->h_length = strlen( target );
(*host)->h_addr_list = (char**)malloc( sizeof(char*) );
if ( (*host)->h_addr_list == NULL )
{
error_malloc( errno );
exit( EXIT_FAILURE );
}
*((*host)->h_addr_list) = strdup( target );
return proto;
}
else
#endif
{
fprintf( stderr, "port not specified\n" );
exit( EXIT_FAILURE );
}
}
int lenght = hostend - target;
char* hostname = (char*)malloc( lenght + 1 );
if ( hostname == NULL )
{
error_malloc( errno );
exit( EXIT_FAILURE );
}
memcpy( hostname, target, lenght );
hostname[lenght] = 0;
*host = gethostbyname2( hostname, net_params[index].m_domain );
free( hostname );
if ( *hostend == ']' )
++hostend;
++hostend;
struct servent* serv = getservbyname( hostend, net_params[index].m_protoname );
if ( serv )
*port = serv->s_port;
else
*port = htons( atoi( hostend ) );
return proto;
}