Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
LSH
lsh
Commits
a5cc244e
Commit
a5cc244e
authored
Sep 02, 1998
by
Niels Möller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Almost compiles.
Rev: src/client.c:1.3 Rev: src/io.c:1.4
parent
9935e799
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
74 deletions
+119
-74
src/client.c
src/client.c
+51
-16
src/io.c
src/io.c
+68
-58
No files found.
src/client.c
View file @
a5cc244e
...
...
@@ -2,29 +2,44 @@
*
*/
#include <stdio.h>
#include "client.h"
#include "version.h"
#include "session.h"
#include "abstract_io.h"
#include "read_line.h"
#include "read_packet.h"
#include "debug.h"
#include "format.h"
#include "werror.h"
#include "void.c"
#include "xalloc.h"
struct
read_handler
*
make_client_read_line
();
struct
callback
*
make_client_close_handler
();
static
int
client_initiate
(
struct
client_callback
*
closure
,
int
fd
)
int
fd
)
{
struct
abstract_write
*
write
=
io_write
(
closure
->
backend
,
fd
,
closure
->
block_size
,
...
close
);
struct
session
=
session_alloc
(...);
session
->
sent_version
=
ssh_format
(
"SSH-"
PROTOCOL_VERSION
struct
ssh_session
*
session
=
ssh_session_alloc
();
struct
abstract_write
*
write
=
io_read_write
(
closure
->
backend
,
fd
,
make_client_read_line
(),
closure
->
block_size
,
make_client_close_handler
());
session
->
client_version
=
ssh_format
(
"SSH-"
PROTOCOL_VERSION
"-"
SOFTWARE_VERSION
" %lS
\r\n
"
,
closure
->
id_comment
);
/* FIXME: Retain the version string (by copying or increfing) */
#error foo
A_WRITE
(
write
,
session
->
sent_version
);
io_read
(
closure
->
backend
,
fd
,
make_client_read_line
());
/* Copies the version string, so that it is isn't freed */
return
A_WRITE
(
write
,
ssh_format
(
"%lS"
,
session
->
client_version
));
}
struct
client_line_handler
{
struct
line_handler
super
;
struct
session
*
session
;
struct
ssh_
session
*
session
;
};
static
struct
read_handler
*
do_line
(
struct
client_line_handler
*
closure
,
...
...
@@ -37,17 +52,26 @@ static struct read_handler *do_line(struct client_line_handler *closure,
if
(
((
length
>=
8
)
&&
!
memcmp
(
line
+
4
,
"2.0-"
,
4
))
||
((
length
>=
9
)
&&
!
memcmp
(
line
+
4
,
"1.99-"
,
5
)))
{
closure
->
session
->
recieved_version
struct
read_handler
*
new
=
make_read_packet
(
make_debug_processor
(
make_packet_void
(),
stderr
),
closure
->
session
->
max_packet
);
closure
->
session
->
server_version
=
ssh_format
(
"%s"
,
length
,
line
);
/* return a new read-handler */
return
...
/* FIXME: Cleanup properly. */
free
(
closure
);
return
new
;
}
else
{
werror
(
"Unsupported protocol version: "
);
werror_safe
(
length
,
line
);
werror
(
"
\n
"
);
/* FIXME: What could be returned here? */
fatal
(
"client.c: do_line: Unsupported version.
\n
"
);
return
0
;
}
}
...
...
@@ -57,14 +81,25 @@ static struct read_handler *do_line(struct client_line_handler *closure,
werror_safe
(
length
,
line
);
/* Read next line */
return
closure
;
return
0
;
}
}
struct
read_handler
*
make_client_read_line
(
struct
ssh_session
*
s
)
{
struct
client_line_handler
*
closure
=
xalloc
(
sizeof
(
struct
client_line_handler
));
closure
->
super
.
handler
=
(
line_handler_f
)
do_line
;
closure
->
session
=
s
;
return
make_read_line
(
(
struct
line_handler
*
)
closure
);
}
struct
fd_callback
*
make_client_callback
(
struct
io_backend
*
b
,
UINT32
block_size
)
{
struct
client_callback
connected
=
xalloc
(
sizeof
(
struct
client_callback
));
struct
client_callback
*
connected
=
xalloc
(
sizeof
(
struct
client_callback
));
connected
->
c
.
f
=
(
fd_callback_f
)
client_initiate
;
connected
->
backend
=
b
;
...
...
src/io.c
View file @
a5cc244e
...
...
@@ -2,15 +2,20 @@
*
*/
#include "io.h"
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "io.h"
#include "werror.h"
#include "write_buffer.h"
#include "xalloc.h"
/* A little more than an hour */
#define MAX_TIMEOUT 4000
...
...
@@ -37,6 +42,7 @@ static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length)
case
EWOULDBLOCK
:
/* aka EAGAIN */
return
0
;
default:
werror
(
"io.c: do_read: read() failed, %s
\n
"
,
strerror
(
errno
));
return
A_FAIL
;
}
}
...
...
@@ -45,7 +51,8 @@ static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length)
#define FOR_FDS(type, fd, list, extra) \
{ \
type **(_fd); \
for(_fd = &(list); (fd) = *_fd; _fd = &(*_fd)->next, (extra))
type *(fd); \
for(_fd = &(list); ((fd) = *_fd); _fd = &(*_fd)->next, (extra))
#define END_FOR_FDS }
...
...
@@ -62,19 +69,19 @@ void io_run(struct io_backend *b)
int
timeout
;
int
res
;
nfds
=
b
->
nio
+
b
->
nlisten
+
n
->
nconnect
;
nfds
=
b
->
nio
+
b
->
nlisten
+
b
->
nconnect
;
if
(
b
->
callouts
)
{
time_t
now
=
time
();
if
(
now
>=
b
->
callout
->
when
)
time_t
now
=
time
(
NULL
);
if
(
now
>=
b
->
callout
s
->
when
)
timeout
=
0
;
else
{
if
(
b
->
callout
->
when
>
now
+
MAX_TIMEOUT
)
if
(
b
->
callout
s
->
when
>
now
+
MAX_TIMEOUT
)
timeout
=
MAX_TIMEOUT
*
1000
;
else
timeout
=
(
b
->
callout
->
when
-
now
)
*
1000
;
timeout
=
(
b
->
callout
s
->
when
-
now
)
*
1000
;
}
}
else
...
...
@@ -92,26 +99,28 @@ void io_run(struct io_backend *b)
FOR_FDS
(
struct
io_fd
,
fd
,
b
->
io
,
i
++
)
{
fds
[
i
]
->
fd
=
fd
->
hold_on
?
-
1
:
fd
->
fd
;
fds
[
i
]
->
events
=
(
fd
->
hold_on
?
0
:
POLLIN
)
/* pre_write returns 0 if the buffer is empty */
|
(
write_buffer_pre_write
(
fd
->
buffer
)
?
POLLOUT
:
0
);
fds
[
i
].
fd
=
fd
->
on_hold
?
-
1
:
fd
->
fd
;
fds
[
i
].
events
=
0
;
if
(
!
fd
->
on_hold
)
fds
[
i
].
events
|=
POLLIN
;
/* pre_write returns 0 if the buffer is empty */
if
(
write_buffer_pre_write
(
fd
->
buffer
))
fds
[
i
].
events
|=
POLLOUT
;
}
END_FOR_FDS
;
FOR_FDS
(
struct
accept
_fd
,
fd
,
b
->
accept
,
i
++
)
FOR_FDS
(
struct
listen
_fd
,
fd
,
b
->
listen
,
i
++
)
{
fds
[
i
]
->
fd
=
fd
->
fd
;
fds
[
i
]
->
events
=
POLLIN
;
fds
[
i
]
.
fd
=
fd
->
fd
;
fds
[
i
]
.
events
=
POLLIN
;
}
END_FOR_FDS
;
FOR_FDS
(
struct
connect_fd
,
fd
,
b
->
connect
,
i
++
)
{
fds
[
i
]
->
fd
=
fd
->
fd
;
fds
[
i
]
->
events
=
POLLOUT
;
fds
[
i
]
.
fd
=
fd
->
fd
;
fds
[
i
]
.
events
=
POLLOUT
;
}
END_FOR_FDS
;
...
...
@@ -122,7 +131,7 @@ void io_run(struct io_backend *b)
/* Timeout. Run the callout */
struct
callout
*
f
=
b
->
callouts
;
if
(
!
CALLBACK
(
f
->
callout
)
;
)
if
(
!
CALLBACK
(
f
->
callout
))
fatal
(
"What now?"
);
b
->
callouts
=
f
->
next
;
free
(
f
);
...
...
@@ -145,11 +154,12 @@ void io_run(struct io_backend *b)
/* Handle writing first */
FOR_FDS
(
struct
io_fd
,
fd
,
b
->
io
,
i
++
)
{
if
(
fds
[
i
]
->
revents
&
POLLOUT
)
if
(
fds
[
i
]
.
revents
&
POLLOUT
)
{
UINT32
size
=
MIN
(
fd
->
buffer
->
end
-
fd
->
buffer
->
start
,
fd
->
buffer
->
block_size
);
int
res
=
write
(
fd
->
fd
,
fd
->
buffer
->
data
+
fd
->
buffer
->
start
,
int
res
=
write
(
fd
->
fd
,
fd
->
buffer
->
buffer
+
fd
->
buffer
->
start
,
size
);
if
(
!
res
)
fatal
(
"Closed?"
);
...
...
@@ -163,11 +173,8 @@ void io_run(struct io_backend *b)
CALLBACK
(
fd
->
close_callback
);
/* FIXME: Must do this later. Perhaps add a
* closed flag to th io_fd struct? */
#if 0
UNLINK_FD;
free(fd->write_buffer);
free(fd);
#endif
fd
->
please_close
=
1
;
break
;
}
else
...
...
@@ -180,28 +187,42 @@ void io_run(struct io_backend *b)
i
=
0
;
/* Start over */
FOR_FDS
(
struct
io_fd
,
fd
,
b
->
io
,
i
++
)
{
if
(
fds
[
i
]
->
revents
&
POLLIN
)
if
(
!
fd
->
please_close
&&
(
fds
[
i
].
revents
&
POLLIN
))
{
struct
fd_read
r
=
{
{
(
abstract_read_f
)
do_read
},
fd
->
fd
,
0
};
{
{
(
abstract_read_f
)
do_read
},
fd
->
fd
};
/* The handler function returns a new handler for the
* file, or NULL. */
if
(
!
(
fd
->
handler
=
READ_HANDLER
(
fd
->
handler
,
&
r
)))
if
(
!
(
fd
->
handler
=
READ_HANDLER
(
fd
->
handler
,
(
struct
abstract_read
*
)
&
r
)))
{
/* FIXME: Should fd be closed here? */
UNLINK_FD
;
free
(
fd
);
fd
->
please_close
=
1
;
}
}
if
(
fd
->
please_close
)
{
/* FIXME: Cleanup properly...
*
* After a write error, read state must be freed,
* and vice versa. */
UNLINK_FD
;
free
(
fd
->
buffer
);
free
(
fd
);
}
}
END_FOR_FDS
;
FOR_FDS
(
struct
accept
_fd
,
fd
,
b
->
accept
,
i
++
)
FOR_FDS
(
struct
listen
_fd
,
fd
,
b
->
listen
,
i
++
)
{
if
(
fds
[
i
]
->
revents
&
POLLIN
)
if
(
fds
[
i
]
.
revents
&
POLLIN
)
{
int
conn
=
accept
(
fd
->
fd
);
/* FIXME: Do something with the peer address? */
struct
sockaddr_in
peer
;
int
conn
=
accept
(
fd
->
fd
,
&
peer
,
sizeof
(
peer
));
if
(
conn
<
0
)
{
werror
(
"io.c: accept() failed, %s"
,
strerror
(
errno
));
...
...
@@ -213,7 +234,6 @@ void io_run(struct io_backend *b)
UNLINK_FD
;
free
(
fd
);
}
}
}
END_FOR_FDS
;
...
...
@@ -394,35 +414,25 @@ struct listen_fd *io_listen(struct io_backend *b,
return
fd
;
}
void
io_read
(
struct
io_backend
*
b
,
int
fd
,
struct
read_callback
*
callback
)
{
struct
input_fd
*
fd
=
xalloc
(
sizeof
(
struct
input_fd
));
fd
->
fd
==
fd
;
fd
->
callback
=
callback
;
fd
->
next
=
b
->
input
;
b
->
input
=
fd
;
b
->
ninput
++
;
}
struct
abstract_write
*
io_write
(
struct
io_backend
*
b
,
int
fd
,
UINT32
block_size
,
struct
callback
*
close_callback
)
struct
abstract_write
*
io_read_write
(
struct
io_backend
*
b
,
int
fd
,
struct
read_callback
*
read_callback
,
UINT32
block_size
,
struct
callback
*
close_callback
)
{
struct
o
utput
_fd
=
xalloc
(
sizeof
(
struct
o
utput
_fd
));
struct
i
o_fd
=
xalloc
(
sizeof
(
struct
i
o_fd
));
struct
write_buffer
*
buffer
=
write_buffer_alloc
(
block_size
);
fd
->
fd
=
fd
;
fd
->
please_close
=
0
;
fd
->
read_callback
=
read_callback
;
fd
->
close_callback
=
close_callback
;
fd
->
buffer
=
buffer
;
fd
->
next
=
b
->
o
utput
;
b
->
o
utput
=
fd
;
b
->
no
utput
++
;
fd
->
next
=
b
->
i
o
;
b
->
i
o
=
fd
;
b
->
n
i
o
++
;
return
(
struct
abstract_write
*
)
buffer
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment