-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.c
169 lines (151 loc) · 4.23 KB
/
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
167
168
169
#include "socket.h"
#define _POSIX_C_SOURCE 200112L
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
//forward declarations
void socket_addr_iterate(socket_t* skt, struct addrinfo* result,
bool* connection_established);
int socket_getaddrinfo(struct addrinfo **result, const char* host,
const char* service, bool pasive);
int socket_bind(int sockfd, struct addrinfo* ptr);
int socket_listen(int sockfd, int listen_amount);
void socket_init(socket_t* socket) {
socket->fd = -1; //initialize to invalid fd
}
void socket_release(socket_t* skt) {
shutdown(skt->fd, SHUT_RDWR);
close(skt->fd);
}
int socket_connect(socket_t* skt, const char* host, const char* service) {
struct addrinfo *result = NULL;
int s = socket_getaddrinfo(&result, host, service, false);
if (s != 0) {
fprintf(stderr, "Error in getaddrinfo: %s\n", gai_strerror(s));
freeaddrinfo(result);
return 1;
}
bool connection_established = false;
socket_addr_iterate(skt, result, &connection_established);
if (!connection_established) {
fprintf(stderr, "Error: connection couldn't been established\n");
freeaddrinfo(result);
return 1;
}
freeaddrinfo(result);
return 0;
}
int socket_bind_and_listen(socket_t* skt, const char* service,
int listen_amount) {
struct addrinfo *ptr = NULL;
int s = socket_getaddrinfo(&ptr, NULL, service, true);
if (s != 0) {
fprintf(stderr, "Error in getaddrinfo: %s\n", gai_strerror(s));
return 1;
}
skt->fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (skt->fd == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
freeaddrinfo(ptr);
return 1;
}
if (socket_bind(skt->fd, ptr) == 1) {
freeaddrinfo(ptr);
return 1;
}
freeaddrinfo(ptr);
if (socket_listen(skt->fd, listen_amount) == 1) {
return 1;
}
return 0;
}
int socket_accept_client(socket_t* sv_skt, socket_t* peer_skt) {
peer_skt->fd = accept(sv_skt->fd, NULL, NULL);
if (peer_skt->fd == -1){
fprintf(stderr, "Error: %s\n", strerror(errno));
return 1;
}
return 0;
}
int socket_recv_message(socket_t* skt, char *buf, int size){
int received = 0;
int s = 0;
while (received < size) {
s = recv(skt->fd, &buf[received], size-received, MSG_NOSIGNAL);
if (s == 0 || s == -1) { //socket was closed or error occurred
return -1;
} else {
received += s;
}
}
return received;
}
int socket_send_message(socket_t* skt, char *buf, int size){
int sent = 0;
int s = 0;
while (sent < size) {
s = send(skt->fd, &buf[sent], size-sent, MSG_NOSIGNAL);
if (s == 0 || s == -1) { //socket was closed or error occurred
return -1;
} else {
sent += s;
}
}
return sent;
}
void socket_addr_iterate(socket_t* skt, struct addrinfo* result,
bool* connection_established) {
struct addrinfo* ptr;
int s;
for (ptr = result; ptr != NULL && *connection_established == false;
ptr = ptr->ai_next) {
skt->fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (skt->fd == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
} else {
s = connect(skt->fd, ptr->ai_addr, ptr->ai_addrlen);
if (s == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
close(skt->fd);
}
*connection_established = (s != -1); //are we connected now?
}
}
}
int socket_getaddrinfo(struct addrinfo **addrinfo_ptr, const char* host,
const char* service, bool passive) {
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (passive){
hints.ai_flags = AI_PASSIVE;
return getaddrinfo(NULL, service, &hints, addrinfo_ptr);
} else {
hints.ai_flags = 0;
return getaddrinfo(host, service, &hints, addrinfo_ptr);
}
}
int socket_bind(int sockfd, struct addrinfo* ptr) {
int s = bind(sockfd, ptr->ai_addr, ptr->ai_addrlen);
if (s == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
close(sockfd);
return 1;
}
return 0;
}
int socket_listen(int sockfd, int listen_amount) {
int s = listen(sockfd, listen_amount);
if (s == -1) {
fprintf(stderr, "Error: %s\n", strerror(errno));
close(sockfd);
return 1;
}
return 0;
}