-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_socket.c
167 lines (158 loc) · 4.23 KB
/
common_socket.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#define _POSIX_C_SOURCE 201112L
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include "common_socket.h"
#define SOCKTS_EN_COLA 1
//-------------------------------------------------------------------------
int socket_init(socket_t *self) {
if (!self) {
return -1;
}
self->fd = -1;
return 0;
}
int socket_uninit(socket_t *self) {
if (shutdown(self->fd, SHUT_RDWR)) {
return -1;
}
if(close(self->fd)) {
return -1;
}
return 0;
}
int socket_get_addresses(socket_t *self, const char *host,
const char *service, bool is_server) {
int status = 0;
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if(is_server){
hints.ai_flags = AI_PASSIVE;
} else {
hints.ai_flags = 0;
}
status = getaddrinfo(host, service, &hints, &(self->results_getaddr));
if (status != 0) {
fprintf(stderr, "Error in getaddrinfo:%s\n", gai_strerror(status));
freeaddrinfo(self->results_getaddr);
return -1;
}
if (is_server) {
status = socket_bind(self);
} else {
status = socket_connect(self);
}
freeaddrinfo(self->results_getaddr);
if (status != 0) {
close(self->fd);
return -1;
}
return 0;
}
int socket_bind(socket_t *self) {
bool connected = false;
struct addrinfo *ptr;
for (ptr = self->results_getaddr; ptr != NULL && connected == false;
ptr = ptr->ai_next) {
self->fd = socket((self->results_getaddr)->ai_family,
(self->results_getaddr)->ai_socktype,
(self->results_getaddr)->ai_protocol);
if (self->fd == -1) {
fprintf(stderr,"Error in socket_bind: %s\n", strerror(errno));
return -1;
}
int val = 1;
if (setsockopt(self->fd, SOL_SOCKET, SO_REUSEADDR,
&val, sizeof(val))) {
fprintf(stderr,"Error in socket_time_wait: %s\n", strerror(errno));
return -1;
}
if (bind(self->fd, (self->results_getaddr)->ai_addr,
(self->results_getaddr)->ai_addrlen)) {
fprintf(stderr,"Error in socket_bind: %s\n", strerror(errno));
return -1;
} else {
connected = true;
}
}
return (!connected);
}
int socket_listen(socket_t *self) {
int status = listen(self->fd, SOCKTS_EN_COLA);
if(status) {
fprintf(stderr, "Error in socket_listen:%s\n", strerror(status));
close(self->fd);
return -1;
}
return 0;
}
int socket_accept(socket_t *listener, socket_t *peer) {
int accepted = accept(listener->fd, NULL, NULL);
if (accepted == -1) {
fprintf(stderr, "Error in socket_accept:%s\n", strerror(accepted));
close(listener->fd);
return -1;
}
peer->fd = accepted;
return 0;
}
int socket_connect(socket_t *self) {
bool connected = false;
struct addrinfo *ptr;
for (ptr = self->results_getaddr; ptr != NULL && connected == false;
ptr = ptr->ai_next) {
self->fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (self->fd == -1) {
fprintf(stderr,"Error in socket_connect: %s\n", strerror(errno));
return -1;
}
if (connect(self->fd, ptr->ai_addr, ptr->ai_addrlen)) {
fprintf(stderr,"Error in socket_connect: %s\n", strerror(errno));
close(self->fd);
return -1;
} else {
connected = true;
}
}
return (!connected);
}
int socket_send(socket_t *self, char* buffer, size_t buffer_size) {
int n = 0;
while (n < buffer_size) {
int status = 0;
status = send(self->fd, &buffer[n], buffer_size-n, MSG_NOSIGNAL);
if (status == -1) {
fprintf(stderr,"Error in socket_send: %s\n", strerror(errno));
close(self->fd);
return -1;
} else if (status == 0){
return 0;
} else {
n += status;
}
}
return 0;
}
ssize_t socket_receive(socket_t *self, char* buffer, int buffer_size){
ssize_t n = 0;
while (n < buffer_size) {
ssize_t status = recv(self->fd, &buffer[n], buffer_size-n, 0);
if (status == -1) {
fprintf(stderr, "Error in socket_receive:%s\n", strerror(status));
close(self->fd);
return -1;
} else if (status == 0) {
return n;
} else {
n += status;
}
}
return n;
}