Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse550
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Winston Jodjana
cse550
Commits
73515ce3
Commit
73515ce3
authored
2 years ago
by
Mengqi Chen
Browse files
Options
Downloads
Patches
Plain Diff
update find newline
parent
422034ae
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
assignment1/partb/550server.cpp
+6
-1
6 additions, 1 deletion
assignment1/partb/550server.cpp
assignment1/partb/server_util.cpp
+2
-87
2 additions, 87 deletions
assignment1/partb/server_util.cpp
assignment1/partb/server_util.h
+0
-4
0 additions, 4 deletions
assignment1/partb/server_util.h
with
8 additions
and
92 deletions
assignment1/partb/550server.cpp
+
6
−
1
View file @
73515ce3
...
...
@@ -125,6 +125,11 @@ int main(int argc, char **argv) {
// read file path from client and do path validation
string
base_dir
(
"."
);
if
(
!
IsPathSafe
(
base_dir
,
req_file_path
))
{
perror
(
" Path is not safe"
);
close
(
new_client_fd
);
continue
;
}
// all good, dispatch thread and setup pollfd
cout
<<
" New incoming connection - "
<<
new_client_fd
<<
"
\n
"
;
...
...
@@ -160,7 +165,7 @@ int main(int argc, char **argv) {
Work
*
thread_work
=
worker_to_client
[
events
[
i
].
fd
];
if
(
thread_work
==
nullptr
)
{
cerr
<<
" worker_to_client mapping corrupted for fd: "
<<
events
[
i
].
fd
<<
"
\n
"
;
// TODO: need some action
continue
;
}
// return data to client
...
...
This diff is collapsed.
Click to expand it.
assignment1/partb/server_util.cpp
+
2
−
87
View file @
73515ce3
...
...
@@ -13,86 +13,6 @@
using
namespace
std
;
#define BUFFER_SIZE 4096
int
Listen
(
char
*
portnum
)
{
// Populate the "hints" addrinfo structure for getaddrinfo().
struct
addrinfo
hints
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
// IPv6 (also handles IPv4 clients)
hints
.
ai_socktype
=
SOCK_STREAM
;
// stream
hints
.
ai_flags
=
AI_PASSIVE
;
// use wildcard "INADDR_ANY"
hints
.
ai_flags
|=
AI_V4MAPPED
;
// use v4-mapped v6 if no v6 found
hints
.
ai_protocol
=
IPPROTO_TCP
;
// tcp protocol
hints
.
ai_canonname
=
nullptr
;
hints
.
ai_addr
=
nullptr
;
hints
.
ai_next
=
nullptr
;
// use the portnum string representation to
// pass in to getaddrinfo(). getaddrinfo() returns a list of
// address structures via the output parameter "result".
struct
addrinfo
*
result
;
int
res
=
getaddrinfo
(
nullptr
,
portnum
,
&
hints
,
&
result
);
// check if getaddrinfo failed
if
(
res
!=
0
)
{
std
::
cerr
<<
"getaddrinfo() failed: "
;
std
::
cerr
<<
gai_strerror
(
res
)
<<
std
::
endl
;
return
-
1
;
}
// Loop through the returned address structures until we are able
// to create a socket and bind to one. The address structures are
// linked in a list through the "ai_next" field of result.
int
listen_fd
=
-
1
;
for
(
struct
addrinfo
*
rp
=
result
;
rp
!=
nullptr
;
rp
=
rp
->
ai_next
)
{
listen_fd
=
socket
(
rp
->
ai_family
,
rp
->
ai_socktype
,
rp
->
ai_protocol
);
if
(
listen_fd
==
-
1
)
{
// Creating this socket failed. So, loop to the next returned
// result and try again.
std
::
cerr
<<
"socket() failed: "
<<
strerror
(
errno
)
<<
std
::
endl
;
listen_fd
=
-
1
;
continue
;
}
// Configure the socket; we're setting a socket "option." In
// particular, we set "SO_REUSEADDR", which tells the TCP stack
// so make the port we bind to available again as soon as we
// exit, rather than waiting for a few tens of seconds to recycle it.
int
optval
=
1
;
assert
(
setsockopt
(
listen_fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
optval
,
sizeof
(
optval
))
==
0
);
// Try binding the socket to the address and port number returned
// by getaddrinfo().
if
(
bind
(
listen_fd
,
rp
->
ai_addr
,
rp
->
ai_addrlen
)
==
0
)
{
break
;
}
// The bind failed. Close the socket, then loop back around and
// try the next address/port returned by getaddrinfo().
close
(
listen_fd
);
listen_fd
=
-
1
;
}
// Free the structure returned by getaddrinfo().
freeaddrinfo
(
result
);
// If we failed to bind, return failure.
if
(
listen_fd
<=
0
)
return
listen_fd
;
// Success. Tell the OS that we want this to be a listening socket.
if
(
listen
(
listen_fd
,
SOMAXCONN
)
!=
0
)
{
std
::
cerr
<<
"Failed to mark socket as listening: "
;
std
::
cerr
<<
strerror
(
errno
)
<<
std
::
endl
;
close
(
listen_fd
);
return
-
1
;
}
return
listen_fd
;
}
int
WrappedRead
(
int
fd
,
string
*
file_path
)
{
int
read_res
;
int
bytes_read
=
0
;
...
...
@@ -118,13 +38,8 @@ int WrappedRead(int fd, string *file_path) {
file_path
->
append
((
char
*
)
buffer
,
read_res
);
int
endline_idx
=
file_path
->
find
(
'\n'
);
while
(
endline_idx
!=
-
1
)
{
file_path
->
erase
(
endline_idx
,
1
);
endline_idx
=
file_path
->
find
(
'\n'
);
found_endline
=
true
;
}
if
(
found_endline
)
{
if
(
endline_idx
!=
-
1
)
{
*
file_path
=
file_path
->
substr
(
0
,
endline_idx
);
break
;
}
}
...
...
This diff is collapsed.
Click to expand it.
assignment1/partb/server_util.h
+
0
−
4
View file @
73515ce3
...
...
@@ -8,10 +8,6 @@ using namespace std;
static
const
int
PATH_MAX
=
254
;
// accepts a char*/string representation of a portnum
// it returns a listen file descriptor to use
int
Listen
(
char
*
portnum
);
// Wrapper function around read that deals with reading errors.
// Read at most readlen bytes from fd and stores it into buf.
// on success it returns the number of bytes that
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment