-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetCode.c
More file actions
118 lines (91 loc) · 2.98 KB
/
Copy pathnetCode.c
File metadata and controls
118 lines (91 loc) · 2.98 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
112
113
114
115
116
117
118
/*
#####################################################################
CMPT 361 - Assignment 3
Group 4: Nick, John, Alex, Kevin
November 9th, 2015
Filename: netCode.c
Description: Contains various functions for sending and receiving
data across a socket
#####################################################################
*/
#include "netCode.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "netCode.h"
#include "log.h"
int recvAll (int sock, int amount, uint8_t * dest){
uint8_t * pos = dest;
while (pos - dest < amount){
int received = recv(sock, pos, amount - (pos - dest), 0);
if (received == -1){
fprintf(getLog(), "ERROR: Error when receiving data: %s\n", strerror(errno));
return -1;
}
pos += received;
}
return amount;
}
int send_string(int sd, uint8_t * buf){
size_t queued = MAXLEN;
ssize_t sent;
while (queued > 0){
sent = send(sd, buf, queued, 0);
if (sent == -1)
return FALSE;
queued -= sent;
buf += sent;
}
return TRUE;
}
int connectTo (int sock, struct addrinfo * info){
int returnCode = connect(sock, (struct sockaddr *) info->ai_addr, info->ai_addrlen);
if (returnCode != 0){
fprintf(getLog(), "ERROR: Failed to connect: %s/n", strerror(errno));
return sock = -1;
}
return sock;
}
//Inspired by code seen in Beej's Guide to Network Programming
//http://beej.us/guide/bgnet/
int sendAll(int sock, uint8_t * buffer, int len){
int sent = 0;
int left = len;
int ret;
while (sent < len){
ret = send(sock, buffer + sent, left, 0);
if (ret == -1){ return -1; }
sent += ret;
left -= ret;
}
return sent;
}
int acceptCon(int socket, struct sockaddr_storage *clientAddr) {
/* Handles client requests
will be updated to support Port Knocking */
socklen_t clientAddrLen = sizeof(*clientAddr);
int cd;
cd = accept(socket, (struct sockaddr *) clientAddr, &clientAddrLen);
if (cd == -1) {
fprintf(getLog(), "ERROR: Error accepting connection(s): %s\n", strerror(errno));
}
return cd;
}
struct addrinfo * buildAddrInfo (char * address, char * port){
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int returnCode = getaddrinfo(address, port, &hints, &res);
if (returnCode != 0){
fprintf(getLog(), "ERROR: Getaddrinfo failure: %s\n", gai_strerror(returnCode));
closeProgram(true, false);
}
else{
return res;
}
return NULL;
}