-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.c
87 lines (76 loc) · 2.9 KB
/
comm.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
#include "comm.h"
/*
Creates socket (sockfd)
Returns the socket file descriptor or -1 on error.
*/
int createSocket(char * ip, uint16_t port){
int sockfd, bindRetCode;
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
//-------------------------------------------------------------------
//Create the Socket
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd == -1){
perror("socket");
return -1;
}
printf("Socket Created. File Descriptor #:%d\n", sockfd);
//-------------------------------------------------------------------
//Populate the srvaddr struct with Family,Port and IP addr
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; //IPv4
addr.sin_port = htons(port);
//srvaddr.sin_addr.s_addr = INADDR_ANY;
inet_pton(AF_INET, ip, &addr.sin_addr.s_addr);
//-------------------------------------------------------------------
//Bind to the Socket
bindRetCode = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
if(bindRetCode !=0){
perror("bind");
return -1;
}
if (getsockname(sockfd, (struct sockaddr *)&addr, &len) == -1)
perror("getsockname");
else
printf("Bound to IP: %s\n", ip);
printf("Bound to Port: %d\n", ntohs(addr.sin_port));
printf("-----------------------------------------------------------------\n");
//-------------------------------------------------------------------
//sockfd is the file descriptor for the newly created socket
return sockfd;
}
/*
Send to address in clientaddr given a socket file descriptor, clientaddr sockaddr_in struct, and a pointer to the message to be sent
*/
int rps_send(int sockfd,char * ip, uint16_t port, char * message){
int bytes_sent;
struct sockaddr_in sendaddr;
memset(&sendaddr, 0, sizeof(sendaddr));
sendaddr.sin_family = AF_INET;
sendaddr.sin_port = htons(port);
inet_pton(AF_INET, ip, &sendaddr.sin_addr.s_addr);
bytes_sent = sendto(sockfd,message,strlen(message),0,(struct sockaddr *)&sendaddr,sizeof(sendaddr));
if(bytes_sent == -1){
perror("sendto");
return -1;
}
return bytes_sent;
}
/*
Recieves from the address in clientaddr given a socket file descriptor, clientaddr sockaddr_in struct, and the amount to recieve
returns a pointer to the buffer where the information will be stored
*/
char *rps_recv(int sockfd,char * ip, uint16_t * port, int recv_amount){
struct sockaddr_in recvaddr;
char * dst_buffer;
memset(&recvaddr, 0, sizeof(recvaddr));
socklen_t len = sizeof(recvaddr);
// recvaddr.sin_family = AF_INET;
//recvaddr.sin_port = htons(port);
// recvaddr.sin_addr.s_addr = inet_addr(ip);
dst_buffer = (char *)malloc(recv_amount);
recvfrom(sockfd,dst_buffer,recv_amount,0,(struct sockaddr *)&recvaddr,&len);
(*port) = ntohs(recvaddr.sin_port);
inet_ntop(AF_INET,&(recvaddr.sin_addr),ip, 16);
return dst_buffer;
}