-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.c
More file actions
169 lines (149 loc) · 4.56 KB
/
udp.c
File metadata and controls
169 lines (149 loc) · 4.56 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
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
/*
* udp.c: This class listens/creates a UDP socket to accept JOIN request
* This file is a part of Tutor
*
* Tutor is an application to create a tutor relationship tree using the client-server model.
* Consists of a tutor node (server) and many student nodes (clients).
* This was created as a part of CS 6390 Advanced Computer Networks course at UT Dallas.
*
* Contributors: Avinash Joshi <axj107420@utdallas.edu>, Sandeep Shenoy <sxs115220@utdallas.edu>
*/
#include "myheader.h"
int child_count=0;
int k_child=0;
int tcpport=0;
// Functin definition
static void* handle_udp(void*);
/*
* The node_details holds the structure for the list of
* nodes a.k.a 'children' to be sent if join is not accepted
*/
struct node_details {
int nid;
char node_ip[30];
int node_udpport;
int nchildren;
} node_det[10];
/*
* This function creates a new thread for the UDP socket
*
*/
void
create_udp (size_t uport,size_t tport,int k) {
k_child = k;
tcpport = tport;
pthread_t udp_pid;
pthread_create(&udp_pid,NULL,&handle_udp,(void *)uport);
return;
}
/*
* This function opens and binds the UDP socket at the requested port
*
*/
static void*
handle_udp (void* uport) {
pthread_detach(pthread_self());
int udp_sockfd, bytes_received, bytes_sent, client_addr_size;
struct sockaddr_in server_addr, client_addr;
char udp_buffer[STRLEN];
char udp_list_buffer[STRLEN];
char temp_k[20];
char server_host[50];
struct hostent *he;
int m = 0;
//Creates a UDP socket
if ((udp_sockfd = socket (AF_INET,SOCK_DGRAM,0)) < 0) {
perror ("socket() failed");
return NULL;
}
//Creates a local server structure
bzero (&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons ((size_t)uport);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero (&udp_list_buffer,sizeof(udp_list_buffer));
gethostname (server_host, 49);
if ((he = gethostbyname(server_host)) == NULL) {
herror(server_host);
fprintf (stdout, "Unable to resolve host %s", server_host);
exit (EXIT_FAILURE);
}
struct in_addr **tmp = (struct in_addr **) he->h_addr_list;
DBG (("Hostname:IP = %s:%s", server_host, inet_ntoa(*tmp[0])));
/*Binds the socket*/
if (bind (udp_sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror ("bind() failed");
exit (EXIT_FAILURE);
}
int next_node = (node_number * k_child) + 1;
DBG (("%s \n","UDP Socket created"));
/*Blocking Receive call that keeps waiting for join request*/
client_addr_size = sizeof(client_addr);
while (1)
{
bzero (&udp_buffer,sizeof(udp_buffer));
char temp[STRLEN];
if ((bytes_received = recvfrom(udp_sockfd, udp_buffer, STRLEN, 0, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_size)) < 0) {
perror ("recvfrom() failed");
exit (EXIT_FAILURE);
}
char temp_buff[50], *temp_port, *temp_command;
int child_port;
strcpy(temp_buff,udp_buffer);
temp_command = strtok (temp_buff, ":");
temp_port = strtok (NULL, ":");
child_port = atoi(temp_port);
if (strcmp( temp_command , "join") == 0) {
if (child_count < k_child ) {
strcpy(temp,"accept:");
sprintf(temp_k,"%s", inet_ntoa(*tmp[0]));
strcat(temp,temp_k);
node_det[child_count].nid = node_number;
strcpy(node_det[child_count].node_ip,inet_ntoa(client_addr.sin_addr));
strcat(temp,":");
sprintf(temp_k,"%d",tcpport);
node_det[child_count].node_udpport = child_port;
strcat(temp, temp_k);
strcat(temp,":");
sprintf(temp_k,"%d",k_child);
node_det[child_count].nchildren = k_child;
strcat(temp, temp_k);
strcat(temp,":");
sprintf(temp_k,"%d",next_node);
strcat(temp, temp_k);
strcat(temp,"\0");
next_node++;
DBG (("Sending %s", temp));
if ((bytes_sent = sendto(udp_sockfd,temp,strlen(temp),0,(struct sockaddr *)&client_addr,client_addr_size)) < 0) {
perror ("sendto() failed");
exit (EXIT_FAILURE);
}
child_count++;
}
else {
char temp_b[30];
bzero (temp, sizeof(temp));
for (m=0;m<k_child;m++) {
sprintf(temp_b,"%d",node_det[m].nid);
strcat(temp,temp_b);
strcat(temp,":");
strcat(temp,node_det[m].node_ip);
strcat(temp,":");
sprintf(temp_b,"%d",node_det[m].node_udpport);
strcat(temp,temp_b);
strcat(temp,":");
sprintf(temp_b,"%d",node_det[m].nchildren);
strcat(temp,temp_b);
if(m != k_child-1)
strcat(temp,",");
}
DBG (("Sending %s",temp));
if ((bytes_sent = sendto(udp_sockfd,temp,strlen(temp),0,(struct sockaddr *)&client_addr,client_addr_size)) < 0) {
perror ("sendto() failed");
exit (EXIT_FAILURE);
}
}
}
}
return NULL;
}