-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.h
More file actions
129 lines (107 loc) · 4.91 KB
/
udp.h
File metadata and controls
129 lines (107 loc) · 4.91 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
119
120
121
122
123
124
125
126
127
128
129
// libraries needed for various functions
// use man page for details
#ifndef UDP_HFILE
#define UDP_HFILE
#include <sys/types.h> // data types like size_t, socklen_t
#include <sys/socket.h> // socket(), bind(), connect(), listen(), accept()
#include <netinet/in.h> // sockaddr_in, htons(), htonl(), INADDR_ANY
#include <arpa/inet.h> // inet_pton(), inet_ntop()
#include <unistd.h> // close()
#include <string.h> // memset(), memcpy()
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
#define BUFFER_SIZE 1024
#define SERVER_PORT 12000
#define NAME_SIZE 20
#define MAX_CMD_SIZE 50 // Support multi-word messages
#define MAX_MSGS 100
#define MAX_LEN 1024
//Takes in a socket address and puts the ip and the port in the socket address in "The correct format", Returns 0 if success else -1
int set_socket_addr(struct sockaddr_in *addr, const char *ip, int port)
{
// This is a helper function that fills
// data into an address variable of the
// type struct sockaddr_in
// Basic initialization (sin stands for 'socket internet')
memset(addr, 0, sizeof(*addr)); // zero the whole memory
addr->sin_family = AF_INET; // use IPv4
addr->sin_port = htons(port); // host-to-network short: required to store port in network byte order
if (ip == NULL)
{ // special behaviour for using IP 0.0.0.0 (INADDR_ANY)
addr->sin_addr.s_addr = INADDR_ANY;
}
else
{
// Convert dotted-decimal string (e.g., "127.0.0.1") to binary.
// In other words: inet_pton will parse the human-readable IP string
// and write the corresponding 32-bit binary value into sin_addr
if (inet_pton(AF_INET, ip, &addr->sin_addr) <= 0)
{
return -1; // invalid IP string
}
}
return 0;
}
//makes a new socket and binds to NULL IP (0.0.0.0), Returns socket descriptor.
int udp_socket_open(int port)
{
// 1. Create a UDP socket and obtain a socket descriptor
// (sd) to it
int sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0) {
perror("socket creation failed");
return -1;
}
// 2. Create an address variable to associate
// (or bind) with the socket
struct sockaddr_in this_addr;
// 3. Fill in the address variable with IP and Port.
// Having ip = NULL (second parameter below), sets IP to 0.0.0.0
// which represents all network interfaces (IP addresses)
// for this machine.
set_socket_addr(&this_addr, NULL, port);
// 4. Bind (associate) this address with the socket
// created earlier.
/// Note: binding with 0.0.0.0 means that the socket will accept
// packets coming in to any interface (ip address) of this machine
if (bind(sd, (struct sockaddr *)&this_addr, sizeof(this_addr)) < 0) {
perror("bind failed");
close(sd);
return -1;
}
printf("Socket bound successfully to port %d\n", port);
return sd; // return the socket descriptor
}
//sleep thread and await response of max n bytes and put into buffer. Returns number of bytes recieved in the buffer else -1 for error/ 0 for closed connection
int udp_socket_read(int sd, struct sockaddr_in *addr, char *buffer, int n)
{
// Receive up to n bytes into buffer from the socket with descriptor sd.
// The source address (addr) of the sender is stored in addr for later use.
// Note: recvfrom is a blocking call, meaning that the function will not return
// until a packet arrives (or an error/timeout occurs). During this time, the
// calling thread is put to sleep by the kernel and placed on a wait queue, and
// it resumes only when recvfrom completes.
// The fourth parameter 'flags' of recvfrom is normally set to 0
socklen_t len = sizeof(struct sockaddr_in);
return recvfrom(sd, buffer, n, 0, (struct sockaddr *)addr, &len);
}
//sleep thread and await sending of a message inside buffer of size n bytes to an address over a socket (sd), Returns n bytes sent else -1 for error/ 0 for closed connection.
int udp_socket_write(int sd, struct sockaddr_in *addr, char *buffer, int n)
{
// Send the contents of buffer (n bytes) to the given destination
// address (addr) over UDP (through socket with descriptor sd)
// addr_len: tells the kernel how many bytes of addr are valid
// (needed to distinguish IPv4 vs IPv6 etc.)
// Note: sendto may also block if the kernel’s send buffer is full, which usually
// happens only under high load or network pressure. In that case, the function will
// not return until buffer space becomes available (or an error/timeout occurs).
// During this time, the calling thread is put to sleep by the kernel and placed on
// a wait queue, and it resumes only when sendto completes.
// The fourth parameter 'flags' of sendto is normally set to 0
int addr_len = sizeof(struct sockaddr_in);
return sendto(sd, buffer, n, 0, (struct sockaddr *)addr, addr_len);
}
#endif