-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
75 lines (60 loc) · 1.68 KB
/
utils.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
//server - utils.c
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netdb.h>
#include"server.h"
#include"utils.h"
extern config_t config;
int getpacket(int sfd, char buf[], ssize_t buf_size) {
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len;
ssize_t nread;
peer_addr_len = sizeof(struct sockaddr_storage);
nread = recvfrom(sfd, buf, buf_size, 0, (struct sockaddr*)&peer_addr, &peer_addr_len);
if( nread == -1 ) {
int errsv = errno;
fprintf(stderr, "ERROR getpacket(): %s\n", strerror(errsv));
return(1);
}
else {
status = getnameinfo((struct sockaddr*)&peer_addr, peer_addr_len, peer, NI_MAXHOST, service)
if(status) {
fprintf(stderr, "WARNING getnameinfo(): %s\n", gai_strerror(s));
memset(peer, 0, NI_MAXHOST);
memset(service, 0, NI_MAXSERV);
}
else {
fprintf(stderr, "INFO recieved packet of %zd bytes from %s:%s\n", nread, service, peer);
}
}
return(0);
}
int putpacket(int sfd, char buf[], size_t len) {
if( len != sendto(sfd, buf, len, 0, (struct sockaddr*)&peer_addr, peer_addr_len) ) {
int errsv = errno;
fprintf(stderr, "ERROR putpacket(): %s\n", strerror(errsv));
return(1);
}
}
int putpacketl(int sfd, char buf[]) {
return putpacket(sfd, buf, strlen(buf));
}
int setsocketin(int sfd) {
if( dup2(sfd, stdin) == -1 ) {
int errsv = errno;
fprintf(stderr, "ERROR setsocketin(): %s\n", strerror(errno));
return(1);
}
return(0);
}
int setsocketout(int sfd) {
if( dup2(sfd, stdout) == -1 ) {
int errsv = errno;
fprintf(stderr, "ERROR setsocketout(): %s\n", strerror(errno));
return(1);
}
return(0);
}