socket_init.c
2.81 KB
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
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h> // for atoi() and exit()
#include <errno.h> // for errno
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "tr/socket.h"
#include "tr/logger.h"
int
TR_socketInit(TR_Socket this, TR_socketAction_fptr action)
{
struct addrinfo hint;
struct addrinfo * info, * current_info;
char port_str[6];
int code;
hint.ai_socktype = this->type;
hint.ai_flags = this->flags;
hint.ai_family = AF_UNSPEC;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
sprintf(port_str, "%u", this->port);
code = getaddrinfo(this->host, port_str, &hint, &info);
if (0 != code) {
// TODO error handling...
puts(gai_strerror(code));
return FALSE;
}
current_info = info;
for (
current_info = info;
current_info;
current_info = current_info->ai_next)
{
this->handle = socket(
current_info->ai_family,
current_info->ai_socktype,
current_info->ai_protocol);
if (-1 == this->handle) {
continue;
}
this->addrlen = current_info->ai_addrlen;
memcpy(&(this->addr), current_info->ai_addr, this->addrlen);
if (! action || 0 == action(this)) {
break; // success / open and bind or open and connect...
}
close(this->handle);
this->handle = -1;
}
if (NULL != current_info) {
int reUse = 1; //! \todo make this configurable
//int flags = fcntl(this->handle, F_GETFL, 0);
//fcntl(this->handle, F_SETFL, flags | O_NONBLOCK);
if (current_info->ai_canonname) {
this->cname[TR_MAX_CNAME-1] = 0;
strncpy(this->cname, current_info->ai_canonname, TR_MAX_CNAME-1);
}
this->fin_state = TR_FIN_NO;
//! Make the socket REUSE a TIME_WAIT socket
setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse));
}
freeaddrinfo(info);
return TRUE;
}
// vim: set ts=4 sw=4: