-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcast.c
176 lines (150 loc) · 6.12 KB
/
mcast.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
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
170
171
172
173
174
175
176
////////////////////////////////////////////////////////////////////////////////
//
// James D. Browne
// Eric Carlson
// Distributed Assignment 2
//
// mutils.c
// Functions required by mcast.c
// Date: 2016/10/10
//
////////////////////////////////////////////////////////////////////////////////
# include "mutils.h"
int main(int argc, char *argv[])
{
struct sockaddr_in name;
struct sockaddr_in send_addr;
int mcast_addr;
struct ip_mreq mreq;
unsigned char ttl_val;
int ss,sr;
fd_set mask;
fd_set dummy_mask,temp_mask;
int bytes;
int num;
char mess_buf[MAX_MESS_LEN];
char input_buf[80];
long num_packets; //the number of packets to send.
int machine_index;
int num_machines;
int loss_rate;
int state = 0;
////////////////////////////////////////////////////////////////////////
//get input from user.
////////////////////////////////////////////////////////////////////////
get_cli(&num_packets, &machine_index, &num_machines, &loss_rate, argv, argc);
///////////////////////////////////////////////////////////////////////
//setup networking
///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Place everything in this large block into functions.
// int makeSock();
// void setSock(int socket, ip_mreq *mreq);
// void setSendAddress(sockaddr_in *send_addr, int *mcast_addr);
//
// Make sure to get rid of all unused variables after transition.
//
//set multicast address
mcast_addr = 225 << 24 | 0 << 16 | 1 << 8 | 1; /* (225.0.1.1) */
sr = socket(AF_INET, SOCK_DGRAM, 0); /* socket for receiving */
if (sr<0) {
perror("Mcast: socket");
exit(1);
}
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = htons(PORT);
if ( bind( sr, (struct sockaddr *)&name, sizeof(name) ) < 0 ) {
perror("Mcast: bind");
exit(1);
}
mreq.imr_multiaddr.s_addr = htonl( mcast_addr );
/* the interface could be changed to a specific interface if needed */
mreq.imr_interface.s_addr = htonl( INADDR_ANY );
if (setsockopt(sr, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq,
sizeof(mreq)) < 0)
{
perror("Mcast: problem in setsockopt to join multicast address" );
}
ss = socket(AF_INET, SOCK_DGRAM, 0); /* Socket for sending */
if (ss<0) {
perror("Mcast: socket");
exit(1);
}
ttl_val = 1;
if (setsockopt(ss, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl_val,
sizeof(ttl_val)) < 0)
{
printf("Mcast: problem in setsockopt of multicast ttl %d - ignore in WinNT or Win95\n", ttl_val );
}
send_addr.sin_family = AF_INET;
send_addr.sin_addr.s_addr = htonl(mcast_addr); /* mcast address */
send_addr.sin_port = htons(PORT);
//////////////////////////////////////////////////////////////////////////////////
//The block above should become three lines.
//////////////////////////////////////////////////////////////////////////////////
for(;;){
/////////////////////////////////////////////////////////////////////////////////
//if not id #1 wait and listen this will be state 0;
//make sure not lossy listen
////////////////////////////////////////////////////////////////////////////////
if (state == 0){
//////////////////////////////////////////////////////////////////////////////////
//state 1 will be when everyone learns all the unicast IPs.
//machine id 1 will move to state 2 all others will move to state 4
/////////////////////////////////////////////////////////////////////////////////
}else if( state == 1){
/////////////////////////////////////////////////////////////////////////////////
//State 2 means you have the token.
//At end of state you should send token and move to state 3.
////////////////////////////////////////////////////////////////////////////////
}else if(state == 2){
//////////////////////////////////////////////////////////////////////////////
//State 3 means you sent the token but you want to make sure the token was
//recieved. When confirmed, moved to state 4
//While searching for the token ensure you continue to process all packets.
//////////////////////////////////////////////////////////////////////////////
}else if( state == 3){
/////////////////////////////////////////////////////////////////////////////
//This is the recieving state. All packets should be processed as received.
/////////////////////////////////////////////////////////////////////////////
}else if( state == 4){
/////////////////////////////////////////////////////////////////////////////
//when everyone is done move to state 5 and say goodbye.
////////////////////////////////////////////////////////////////////////////
}else if ( state == 5){
}
///////////////////////////////////////////////////////////////////////////////
//the below is just useful code, but of course it doesn't belong here.
////////////////////////////////////////////////////////////////////////////////
FD_ZERO( &mask );
FD_ZERO( &dummy_mask );
FD_SET( sr, &mask );
FD_SET( (long)0, &mask ); /* stdin */
for(;;)
{
temp_mask = mask;
num = select( FD_SETSIZE, &temp_mask, &dummy_mask, &dummy_mask, NULL);
if (num > 0) {
if ( FD_ISSET( sr, &temp_mask) ) {
bytes = recv( sr, mess_buf, sizeof(mess_buf), 0 );
mess_buf[bytes] = 0;
printf( "received : %s\n", mess_buf );
}else if( FD_ISSET(0, &temp_mask) ) {
bytes = read( 0, input_buf, sizeof(input_buf) );
input_buf[bytes] = 0;
printf( "there is an input: %s\n", input_buf );
sendto( ss, input_buf, strlen(input_buf), 0,
(struct sockaddr *)&send_addr, sizeof(send_addr) );
}
}
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// Revsion History
//
//
////////////////////////////////////////////////////////////////////////////////