Skip to content

Commit 76b1c4b

Browse files
author
oopo
committed
Lots of error checking. Network changes to get large ps2netfs transfers working. (Cygwin is broken!)
1 parent 235c3c8 commit 76b1c4b

File tree

9 files changed

+630
-638
lines changed

9 files changed

+630
-638
lines changed

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
INC = -I/usr/include -I/usr/local/include
44
LIB =
55

6-
ifeq "$(QUIET)" "YES"
7-
GCC += -D__QUIET__
8-
endif
9-
106
ifeq "$(DEBUG)" "YES"
117
GCC += -D__DEBUG__
128
endif

doc/readme.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929

3030
PREFIX=/dir - Install directory, defaults to: $PS2DEV/bin
3131

32-
QUIET=YES - Disable non-essential text output.
33-
3432
DEBUG=YES - Enable all debug text output.
3533

3634
A building example:
3735

38-
make clean; make DEBUG=NO QUIET=YES install
36+
make clean; make DEBUG=NO install
3937

4038
---------------
4139
PS2CLIENT USAGE

src/network.c

Lines changed: 90 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <stdlib.h>
44
#include <string.h>
55
#include <netdb.h>
6-
#include <fcntl.h>
76
#include <unistd.h>
87
#include <sys/time.h>
98
#include <sys/socket.h>
@@ -18,62 +17,77 @@
1817
// NETWORK FUNCTIONS //
1918
///////////////////////
2019

21-
int network_connect(char *hostname, int port, int type) { int loop0, nd = -1;
22-
struct sockaddr_in sockaddr; struct hostent *hostent;
20+
int network_connect(char *hostname, int port, int type) { int result = 0;
21+
struct sockaddr_in sockaddr; int loop0, nd = -1;
2322

24-
// Find an available network descriptor, aborting it none is found.
23+
// Allocate the network descriptor.
2524
for(loop0=0;loop0<10;loop0++) { if (sock[loop0] <= 0) { nd = loop0; } }
26-
if (nd == -1) { return -1; }
25+
if (nd < 0) { printf("[ERROR] Unable to allocate network descriptor. (%d)\n", nd); return -1; }
2726

28-
// Set the host information.
27+
// Set the sockaddr information.
2928
sockaddr.sin_family = AF_INET;
30-
sockaddr.sin_port = htons(port);
31-
hostent = gethostbyname(hostname);
32-
sockaddr.sin_addr = *(struct in_addr *)hostent->h_addr;
29+
sockaddr.sin_port = htons(port);
30+
sockaddr.sin_addr = *(struct in_addr *)gethostbyname(hostname)->h_addr;
3331

34-
// Create and connect the socket.
32+
// Create the socket.
3533
if (type == SOCKET_TCP) { sock[nd] = socket(AF_INET, SOCK_STREAM, 0); }
3634
if (type == SOCKET_UDP) { sock[nd] = socket(AF_INET, SOCK_DGRAM, 0); }
35+
if (sock[nd] < 0) { printf("[ERROR] Create socket failed. (%d)\n", result); return -2; }
3736

38-
// Connect the socket to the remote host.
39-
connect(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));
37+
// Connect the socket.
38+
result = connect(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));
39+
if (result < 0) { printf("[ERROR] Connect socket failed. (%d)\n", result); return -3; }
4040

4141
// End function.
4242
return nd;
4343

4444
}
4545

46-
int network_listen(int port, int type) { int loop0, nd = -1;
47-
struct sockaddr_in sockaddr;
46+
int network_listen(int port, int type) { int result = 0;
47+
struct sockaddr_in sockaddr; int loop0, nd = -1;
4848

49-
// Find an available network descriptor, aborting if none is found.
49+
// Find an available network descriptor.
5050
for(loop0=0;loop0<10;loop0++) { if (sock[loop0] <= 0) { nd = loop0; } }
51-
if (nd == -1) { return -1; }
51+
if (nd < 0) { printf("[ERROR] Unable to allocate network descriptor. (%d)\n", nd); return -1; }
5252

5353
// Set the sockaddr information.
5454
sockaddr.sin_family = AF_INET;
55-
sockaddr.sin_port = htons(port);
55+
sockaddr.sin_port = htons(port);
5656
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
5757

5858
// Create the socket.
5959
if (type == SOCKET_TCP) { sock[nd] = socket(AF_INET, SOCK_STREAM, 0); }
6060
if (type == SOCKET_UDP) { sock[nd] = socket(AF_INET, SOCK_DGRAM, 0); }
61+
if (sock[nd] < 0) { printf("[ERROR] Create socket failed. (%d)\n", result); return -2; }
6162

62-
// Bind to the socket.
63-
bind(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));
64-
65-
// Listen for data on the socket.
66-
listen(sock[nd], 10);
63+
// Bind the socket.
64+
result = bind(sock[nd], (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));
65+
if (result < 0) { printf("[ERROR] Bind socket failed. (%d)\n", result); return -3; }
6766

6867
// End function.
6968
return nd;
7069

7170
}
7271

73-
int network_send(int nd, void *buffer, int size) {
72+
int network_send(int nd, void *buffer, int size) { int result = 0;
73+
74+
// Send the data.
75+
result = send(sock[nd], buffer, size, 0);
76+
if (result < 0) { printf("[ERROR] Send data failed. (%d)\n", result); return -1; }
77+
78+
// End function.
79+
return result;
80+
81+
}
82+
83+
int network_sendall(int nd, void *buffer, int size) { int result = 0;
7484

75-
// Send data to the network.
76-
return send(sock[nd], buffer, size, 0);
85+
// Send all the data.
86+
result = send(sock[nd], buffer, size, MSG_WAITALL);
87+
if (result < 0) { printf("[ERROR] Send all data failed. (%d)\n", result); return -1; }
88+
89+
// End function.
90+
return result;
7791

7892
}
7993

@@ -100,36 +114,79 @@
100114

101115
}
102116

103-
int network_recv(int nd, void *buffer, int size) {
117+
int network_recv(int nd, void *buffer, int size) { int result = 0;
104118

105119
// Clear the buffer.
106120
memset(buffer, 0, size);
107121

108-
// Receive some data from the network.
109-
if (FD_ISSET(sock[nd], &rfds)) { return recv(sock[nd], buffer, size, 0); }
122+
// Check to see if data is available.
123+
if (!FD_ISSET(sock[nd], &rfds)) { return 0; }
124+
125+
// Read the data.
126+
result = recv(sock[nd], buffer, size, 0);
127+
if (result < 0) { printf("[ERROR] Read data failure. (%d)\n", result); return -1; }
110128

111129
// End function.
112-
return 0;
130+
return result;
113131

114132
}
115133

116-
int network_recvfrom(int nd, void *buffer, int size) {
134+
int network_recvall(int nd, void *buffer, int size) { int result = 0;
117135

118136
// Clear the buffer.
119137
memset(buffer, 0, size);
120138

121-
// Receive data from the network.
122-
if (FD_ISSET(sock[nd], &rfds)) { return recvfrom(sock[nd], buffer, size, 0, NULL, NULL); }
139+
// Check to see if data is available.
140+
if (!FD_ISSET(sock[nd], &rfds)) { return 0; }
141+
142+
// Read all the data.
143+
result = recv(sock[nd], buffer, size, MSG_WAITALL);
144+
if (result < 0) { printf("[ERROR] Read all data failure. (%d)\n", result); return -1; }
123145

124146
// End function.
125-
return 0;
147+
return result;
148+
149+
}
150+
151+
int network_recvfrom(int nd, void *buffer, int size) { int result = 0;
152+
153+
// Clear the buffer.
154+
memset(buffer, 0, size);
155+
156+
// Check to see if data is available.
157+
if (!FD_ISSET(sock[nd], &rfds)) { return 0; }
158+
159+
// Read the data.
160+
result = recvfrom(sock[nd], buffer, size, 0, NULL, NULL);
161+
if (result < 0) { printf("[ERROR] Read data failure. (%d)\n", result); return -1; }
162+
163+
// End function.
164+
return result;
165+
166+
}
167+
168+
int network_recvallfrom(int nd, void *buffer, int size) { int result = 0;
169+
170+
// Clear the buffer.
171+
memset(buffer, 0, size);
172+
173+
// Check to see if data is available.
174+
if (!FD_ISSET(sock[nd], &rfds)) { return 0; }
175+
176+
// Read all the data.
177+
result = recvfrom(sock[nd], buffer, size, MSG_WAITALL, NULL, NULL);
178+
if (result < 0) { printf("[ERROR] Read all data failure. (%d)\n", result); return -1; }
179+
180+
// End function.
181+
return result;
126182

127183
}
128184

129185
int network_disconnect(int nd) { int result = 0;
130186

131187
// Close the socket.
132188
result = close(sock[nd]);
189+
if (result < 0) { printf("[ERROR] Close socket failed. (%d)\n", result); return -1; }
133190

134191
// Clear the descriptor.
135192
sock[nd] = -1;

src/network.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818

1919
int network_send(int nd, void *buffer, int size);
2020

21+
int network_sendall(int nd, void *buffer, int size);
22+
2123
int network_wait(int timeout);
2224

2325
int network_recv(int nd, void *buffer, int size);
2426

27+
int network_recvall(int nd, void *buffer, int size);
28+
2529
int network_recvfrom(int nd, void *buffer, int size);
2630

31+
int network_recvallfrom(int nd, void *buffer, int size);
32+
2733
int network_disconnect(int nd);
2834

2935
#endif

src/ps2client.c

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,39 +67,31 @@
6767
if (argc > count) { strncpy(arg2, argv[count++], sizeof(arg2)); } else { sprintf(arg2, "NULL"); }
6868
if (argc > count) { strncpy(arg3, argv[count++], sizeof(arg3)); } else { sprintf(arg3, "NULL"); }
6969

70-
#ifndef __QUIET__
71-
72-
// Output the startup message.
73-
printf("[***] %s - Your friendly, neighbourhood ps2 client.\n", argv[0]);
74-
printf("[***] Using ps2 located at: %s\n", hostname);
75-
76-
#endif
77-
7870
// Peform any ps2link commands.
79-
if (strcmp(command, "reset") == 0) { return ps2link_command_reset (hostname); }
80-
if (strcmp(command, "execiop") == 0) { return ps2link_command_execiop (hostname, timeout, argcount, argvalues); }
81-
if (strcmp(command, "execee") == 0) { return ps2link_command_execee (hostname, timeout, argcount, argvalues); }
82-
if (strcmp(command, "poweroff") == 0) { return ps2link_command_poweroff (hostname); }
83-
if (strcmp(command, "dumpmem") == 0) { return ps2link_command_dumpmem (hostname, timeout, arg0, atoi(arg1), atoi(arg2)); }
84-
if (strcmp(command, "startvu") == 0) { return ps2link_command_startvu (hostname, atoi(arg0)); }
85-
if (strcmp(command, "stopvu") == 0) { return ps2link_command_stopvu (hostname, atoi(arg0)); }
86-
if (strcmp(command, "dumpreg") == 0) { return ps2link_command_dumpreg (hostname, timeout, arg0, atoi(arg1)); }
87-
if (strcmp(command, "gsexec") == 0) { return ps2link_command_gsexec (hostname, timeout, arg0); }
88-
if (strcmp(command, "listen") == 0) { return ps2link_command_listen (hostname, timeout); }
71+
if (strcmp(command, "reset") == 0) { return ps2link_command_reset(hostname); }
72+
if (strcmp(command, "execiop") == 0) { return ps2link_command_execiop(hostname, timeout, argcount, argvalues); }
73+
if (strcmp(command, "execee") == 0) { return ps2link_command_execee(hostname, timeout, argcount, argvalues); }
74+
if (strcmp(command, "poweroff") == 0) { return ps2link_command_poweroff(hostname); }
75+
if (strcmp(command, "dumpmem") == 0) { return ps2link_command_dumpmem(hostname, timeout, arg0, atoi(arg1), atoi(arg2)); }
76+
if (strcmp(command, "startvu") == 0) { return ps2link_command_startvu(hostname, atoi(arg0)); }
77+
if (strcmp(command, "stopvu") == 0) { return ps2link_command_stopvu(hostname, atoi(arg0)); }
78+
if (strcmp(command, "dumpreg") == 0) { return ps2link_command_dumpreg(hostname, timeout, arg0, atoi(arg1)); }
79+
if (strcmp(command, "gsexec") == 0) { return ps2link_command_gsexec(hostname, timeout, arg0); }
80+
if (strcmp(command, "listen") == 0) { return ps2link_command_listen(hostname, timeout); }
8981

9082
// Perform any ps2netfs commands.
91-
if (strcmp(command, "copyfrom") == 0) { return ps2netfs_command_copyfrom (hostname, arg0, arg1); }
92-
if (strcmp(command, "copyto") == 0) { return ps2netfs_command_copyto (hostname, arg0, arg1); }
93-
if (strcmp(command, "delete") == 0) { return ps2netfs_command_delete (hostname, arg0); }
94-
if (strcmp(command, "devlist") == 0) { return ps2netfs_command_devlist (hostname); }
95-
if (strcmp(command, "dir") == 0) { return ps2netfs_command_dir (hostname, arg0); }
96-
if (strcmp(command, "format") == 0) { return ps2netfs_command_format (hostname, arg0); }
97-
if (strcmp(command, "mkdir") == 0) { return ps2netfs_command_mkdir (hostname, arg0); }
98-
if (strcmp(command, "mount") == 0) { return ps2netfs_command_mount (hostname, arg0, arg1); }
99-
if (strcmp(command, "rename") == 0) { return ps2netfs_command_rename (hostname, arg0, arg1); }
100-
if (strcmp(command, "rmdir") == 0) { return ps2netfs_command_rmdir (hostname, arg0); }
101-
if (strcmp(command, "sync") == 0) { return ps2netfs_command_sync (hostname, arg0); }
102-
if (strcmp(command, "umount") == 0) { return ps2netfs_command_umount (hostname, arg0); }
83+
if (strcmp(command, "copyfrom") == 0) { return ps2netfs_command_copyfrom(hostname, arg0, arg1); }
84+
if (strcmp(command, "copyto") == 0) { return ps2netfs_command_copyto(hostname, arg0, arg1); }
85+
if (strcmp(command, "delete") == 0) { return ps2netfs_command_delete(hostname, arg0); }
86+
if (strcmp(command, "devlist") == 0) { return ps2netfs_command_devlist(hostname); }
87+
if (strcmp(command, "dir") == 0) { return ps2netfs_command_dir(hostname, arg0); }
88+
if (strcmp(command, "format") == 0) { return ps2netfs_command_format(hostname, arg0); }
89+
if (strcmp(command, "mkdir") == 0) { return ps2netfs_command_mkdir(hostname, arg0); }
90+
if (strcmp(command, "mount") == 0) { return ps2netfs_command_mount(hostname, arg0, arg1); }
91+
if (strcmp(command, "rename") == 0) { return ps2netfs_command_rename(hostname, arg0, arg1); }
92+
if (strcmp(command, "rmdir") == 0) { return ps2netfs_command_rmdir(hostname, arg0); }
93+
if (strcmp(command, "sync") == 0) { return ps2netfs_command_sync(hostname, arg0); }
94+
if (strcmp(command, "umount") == 0) { return ps2netfs_command_umount(hostname, arg0); }
10395

10496
// Command not found.
10597
ps2client_printusage(argv[0]);
@@ -117,6 +109,7 @@
117109

118110
// Output the startup message.
119111
printf("[***] %s - Your friendly, neighbourhood ps2 client.\n", program);
112+
printf("[***] Using ps2 hostname: %s\n", hostname);
120113

121114
// Output the usage instructions.
122115
printf("\n Basic usage:\n\n");

0 commit comments

Comments
 (0)