-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsNetwork.h
More file actions
185 lines (162 loc) · 4.12 KB
/
lsNetwork.h
File metadata and controls
185 lines (162 loc) · 4.12 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* This file describes the functions used for the router network
*
* @author Jeffrey Bromen
* @date 4/19/17
* @info Systems and Networks II
* @info Project 3
*/
#ifndef _LSNETWORK_H
#define _LSNETWORK_H
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "lsDijkstra.h"
#include "lsGraph.h"
#include "lsPacket.h"
#define DELIM ","
struct NeighborList
{
int size;
struct Neighbor *head;
} NeighborList;
struct Neighbor
{
char label;
char address[INET_ADDRSTRLEN];
int port;
int cost;
struct Neighbor *next;
} Neighbor;
struct FifoQueue
{
int size;
struct QueueNode *head;
struct QueueNode *tail;
} FifoQueue;
struct QueueNode
{
char packet[LS_PACKET_SIZE];
struct QueueNode *next;
} QueueNode;
/**
* Initializes a new neighbor list structure.
*
* @return - pointer to list structure
*/
struct NeighborList *newNeighborList();
/**
* Initializes a new neighbor list node structure.
*
* @param label - label of neighboring router
* @param address - network address of neighboring router
* @param port - port number of neighboring router
* @param cost - cost to reach neighboring router
*
* @return - pointer to list node
*/
struct Neighbor *newNeighbor(char label, const char *address, int port, int cost);
/**
* Adds a neighbor node to a neighbor list.
*
* @param list - neighbor list
* @param node - neighbor node being added
*/
void addToList(struct NeighborList *list, struct Neighbor *node);
/**
* Initializes a new FIFO queue.
*
* @return - pointer to queue
*/
struct FifoQueue *newFifoQueue();
/**
* Initializes a new queue node.
*
* @param packet - packet being stored in node
*
* @return - pointer to node
*/
struct QueueNode *newQueueNode(const char *packet);
/**
* Pushes a packet into a FIFO queue
*
* @param queue - FIFO queue
* @param packet - packet being pushed to queue
*/
void push(struct FifoQueue *queue, const char *packet);
/**
* Pops a packet from a FIFO queue
*
* @param queue - FIFO queue
* @param buffer - buffer where popped packet will be stored
*/
void pop(struct FifoQueue *queue, char *buffer);
/**
* Check if FIFO queue is empty
*
* @param queue - FIFO queue
*
* @return - 1 if empty, 0 if not empty
*/
int isEmptyQueue(struct FifoQueue *queue);
/**
* Initializes and binds a UDP socket
*
* @param localPort - port number of socket being opened
* @param sender - 1 if sender timeout option is needed, 0 otherwise
*
* @return file descriptor for socket if success, -1 if failure
*/
int initializeSocket(int localPort);
/**
* Sends a packet to a specified destination.
*
* @param fd - file descriptor of socket being used
* @param packet - packet being sent
* @param destHost - network address of destination
* @param destPort - port number of destination
*
* @return - 0 if successful, -1 if error occurred
*/
int sendPacket(int fd, const char *packet, const char *destHost, int destPort);
/**
* Sends a packet to all of the neighboring routers.
*
* @param fd - file descriptor of socket being used
* @param packet - packet being sent
* @param neighbors - list of neighboring routers
*
*/
int floodPacket(int fd, const char *packet, struct NeighborList *neighbors);
/**
* Get the IP address of a host in dot format
*
* @param buffer - buffer where the IP address string will be stored
* @param hostname - host name of machine whose IP address is needed
*
* @return -1 if failed, 0 if success
*/
int getAddress(char *buffer, const char *hostname);
/**
* Reads a text file to discover the neighboring routers.
*
* @param filename - filename of discovery text file
* @param neighbors - neighbor list structure
*
* @return - 0 if success, -1 if error
*/
int processTextFile(const char *filename, struct NeighborList *neighbors);
/**
* Queues all of the neighboring router edges.
*
* @param neighbors - neighbor list
* @param queue - FIFO queue
* @param label - label of router
*/
void queueNeighbors(struct NeighborList *neighbors, struct FifoQueue *queue, char label);
#endif // _LSNETWORK_H