-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs3_network.c
181 lines (148 loc) · 5.08 KB
/
fs3_network.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
177
178
////////////////////////////////////////////////////////////////////////////////
//
// File : fs3_netowork.c
// Description : This is the network implementation for the FS3 system.
//
// Author : Patrick McDaniel
// Last Modified : Thu 16 Sep 2021 03:04:04 PM EDT
//
// Includes
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cmpsc311_log.h>
// Project Includes
#include <fs3_network.h>
#include <fs3_driver.h>
#include <cmpsc311_util.h>
//
// Global data
unsigned char *fs3_network_address = NULL; // Address of FS3 server
unsigned short fs3_network_port = 0; // Port of FS3 serve
int sock;
typedef struct{
uint8_t opcode;
uint16_t sectorNumber;
uint32_t trackNumber;
uint8_t returnVal;
} deconstVals;
//
// Network functions
int mountoperations(FS3CmdBlk *cmdBlk, FS3CmdBlk *ret){
if (fs3_network_address == NULL) fs3_network_address = (char *)FS3_DEFAULT_IP;
if (fs3_network_port == 0) fs3_network_port = FS3_DEFAULT_PORT;
struct sockaddr_in v4;
v4.sin_family = AF_INET;
v4.sin_port = htons(fs3_network_port);
int returnvaleualsd = inet_aton(fs3_network_address, &(v4.sin_addr));
if (returnvaleualsd == 0){
logMessage(LOG_ERROR_LEVEL, "Invalid address specified");
kill(getpid(), SIGUSR1);
//return -1;
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1){
logMessage(LOG_ERROR_LEVEL, "Could not create socket");
kill(getpid(), SIGUSR1);
//return -1;
}
if (connect(sock, (const struct sockaddr *)&v4, sizeof(v4)) == -1){
logMessage(LOG_ERROR_LEVEL, "Could not connect to server... might not be running");
kill(getpid(), SIGUSR1);
//return -1;
}
printCmdBlock(*cmdBlk, 1);
write(sock, cmdBlk, sizeof(cmdBlk));
read(sock, ret, sizeof(FS3CmdBlk));
*ret = ntohll64(*ret);
return 0;
}
int seekoperations(FS3CmdBlk *cmdBlk, FS3CmdBlk *ret){
//printCmdBlock(*cmdBlk, 1);
*cmdBlk = htonll64(*cmdBlk);
write(sock, cmdBlk, sizeof(FS3CmdBlk));
//write(sock, cmdBlk, sizeof(FS3CmdBlk));
read(sock, ret, sizeof(FS3CmdBlk));
*ret = ntohll64(*ret);
return 0;
}
int readoperations(FS3CmdBlk *cmdBlk, FS3CmdBlk *ret, char *readbuffer){
printCmdBlock(*cmdBlk, 1);
*cmdBlk = htonll64(*cmdBlk);
write(sock, cmdBlk, sizeof(FS3CmdBlk));
read(sock, ret, sizeof(FS3CmdBlk));
*ret = ntohll64(*ret);
read(sock, readbuffer, sizeof(char)*1024); //NOTE: maybe try padding
//*readbuffer = ntohll64(*readbuffer); //MAYBE: not needed
return 0;
}
int writeoperations(FS3CmdBlk *cmdBlk, FS3CmdBlk *ret, char *writebuffer){
printCmdBlock(*cmdBlk, 1);
*cmdBlk = htonll64(*cmdBlk);
write(sock, cmdBlk, sizeof(FS3CmdBlk));
//*writebuffer = htonll64(*writebuffer); //MAYBE: not needed
write(sock, writebuffer, sizeof(char)*1024); //NOTE: maybe try padding
read(sock, ret, sizeof(FS3CmdBlk));
*ret = ntohll64(*ret);
return 0;
}
int unmountoperations(FS3CmdBlk *cmdBlk){
*cmdBlk = htonll64(*cmdBlk);
write(sock, cmdBlk, sizeof(cmdBlk));
close(sock);
sock = -1;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// Function : network_fs3_syscall
// Description : Perform a system call over the network
//
// Inputs : cmd - the command block to send
// ret - the returned command block
// buf - the buffer to place received data in
// Outputs : 0 if successful, -1 if failure
int network_fs3_syscall(FS3CmdBlk cmd, FS3CmdBlk *ret, void *buf)
{
int opret;
FS3CmdBlk blk = cmd;
FS3CmdBlk returned;
deconstVals vals;
if (deconstCmdBlock(cmd, &vals) != 0) return -1;
logMessage(LOG_INFO_LEVEL, "OPCODE RECIEVED: %d", vals.opcode);
switch(vals.opcode){
case 0:
//mounting op
opret = mountoperations(&blk, &ret);
logMessage(LOG_INFO_LEVEL, "Mounted Disk, Returned: %d ", opret);
break;
case 1:
//seeking op
opret = seekoperations(&blk, &ret); //NEED TO IMPLEMENT
logMessage(LOG_INFO_LEVEL, "Seeking to track: %d ", vals.trackNumber);
break;
case 2:
//reading op
opret = readoperations(&blk, &ret, buf); //NEED TO IMPLEMENT
logMessage(LOG_INFO_LEVEL, "Reading from {sector: %d, track: %d}", vals.sectorNumber, vals.trackNumber);
break;
case 3:
//writing op
opret = writeoperations(&blk, &ret, buf); //NEED TO IMPLEMENT
logMessage(LOG_INFO_LEVEL, "Writing from {sector: %d, track: %d}", vals.sectorNumber, vals.trackNumber);
break;
case 4:
//unmounting op
opret = unmountoperations(&blk);
logMessage(LOG_INFO_LEVEL, "Unmounted Disk, Returned: %d ", opret);
break;
default:
//invalid code
return -1;
}
//memcpy(ret, &returned, sizeof(FS3CmdBlk));
return opret;
}