Commit 9a928eeb authored by Roger Dingledine's avatar Roger Dingledine
Browse files

Initial revision


svn:r2
parent 766a465a
Obvious things I'd like to do that won't break anything:
* Abstract out crypto calls, with the eventual goal of moving
from openssl to something with a more flexible license.
* Test suite. We need one.
* Switch the "return -1" cases that really mean "you've got a bug"
into calls to assert().
* Since my OR can handle multiple circuits through a given OP,
I think it's clear that the OP should pass new create cells through the
same channel. Thus we can take advantage of the padding we're already
getting. Does that mean the choose_onion functions should be changed
to always pick a favorite OR first, so the OP can minimize the number
of outgoing connections it must sustain?
* Rewrite the OP to be non-blocking single-process.
* Add autoconf support.
Figure out what .h files we're actually using, and how portable
those are.
* Since we're using a stream cipher, an adversary's cell arriving with the
same aci will forever trash our circuit. Since each side picks half
the aci, for each cell the adversary has a 1/256 chance of trashing a
circuit. This is really nasty. We want to make ACIs something reasonably
hard to collide with, such as 20 bytes.
While we're at it, I'd like more than 4 bits for Version. :)
* Exit policies. Since we don't really know what protocol is being spoken,
it really comes down to an IP range and port range that we
allow/disallow. The 'application' connection can evaluate it and make
a decision.
* We currently block on gethostbyname in OR. This is poor. The complex
solution is to have a separate process that we talk to. There are some
free software versions we can use, but they'll still be tricky. The
better answer is to realize that the OP can do the resolution and
simply hand the OR an IP directly.
A) This prevents us from doing sneaky things like having the name resolve
differently at the OR than at the OP. I'm ok with that.
B) It actually just shunts the "dns lookups block" problem back onto the
OP. But that's ok too, because the OP doesn't have to be as robust.
(Heck, can we have the application proxy resolve it, even?)
* I'd like a cleaner interface for the configuration files, keys, etc.
Perhaps the next step is a central repository where we download router
lists? Something that takes the human more out of the loop.
We should look into a 'topology communication protocol'; there's one
mentioned in the spec that Paul has, but I haven't looked at it to
know how complete it is or how well it would work. This would also
allow us to add new ORs on the fly. Directory servers, a la the ones
we're developing for Mixminion (see http://mixminion.net/), are also
a very nice approach to consider.
* Should ORs rotate their link keys periodically?
* We probably want OAEP padding for RSA.
* The parts of the code that say 'FIXME'
Non-obvious things I'd like to do:
(Many of these topics are inter-related. It's clear that we need more
analysis before we can guess which approaches are good.)
* Padding between ORs, and correct padding between OPs. Currently
the OP seems to send padding at a steady rate, but data cells can
come more quickly than that. This doesn't provide much protection
at all. I'd like to investigate a synchronous mixing approach, where
cells are sent at fixed intervals. We need to investigate the effects
of this on DoS resistance -- what do we do when we have too many
packets? One approach is to do traffic shaping rather than traffic
padding -- we gain a bit more resistance to DoS at the expense of some
anonymity. Can we compare this analysis to that of the Cottrell Mix,
and learn something new? We'll need to decide on exactly how the
traffic shaping algorithm works.
* Make the connection buf's grow dynamically as needed. This won't
really solve the fundamental problem above, though, that a buffer
can be given an adversary-controlled number of cells.
* I'd like to add a scheduler of some sort. Currently we only need one
for sending out padding cells, and if these events are periodic and
synchronized, we don't yet need a scheduler per se, but rather we just
need to have poll return every so often and avoid sending cells onto
the sockets except at the appointed time. We're nearly ready to do
that as it is, with the separation of write_to_buf() and flush_buf().
Edge case: what do we do with circuits that receive a destroy
cell before all data has been sent out? Currently there's only one
(outgoing) buffer per connection, so since it's crypted, a circuit
can't recognize its own packet once it's been queued. We could mark
the circuits for destruction, and go through and cull them once the
buffer is entirely flushed; but with the synchronous approach above,
the buffer may never become empty. Perhaps I should implement a callback
system, so a function can get called when a particular cell gets sent
out. That sounds very flexible, but might also be overkill.
* Currently when a connection goes down, it generates a destroy cell
(either in both directions or just the appropriate one). When a
destroy cell arrives to an OR (and it gets read after all previous
cells have arrived), it delivers a destroy cell for the "other side"
of the circuit: if the other side is an OP or APP, it closes the entire
connection as well.
But by "a connection going down", I mean "I read eof from it". Yet
reading an eof simply means that it promises not to send any more
data. It may still be perfectly fine receiving data (read "man 2
shutdown"). In fact, some webservers work that way -- the client sends
his entire request, and when the webserver reads an eof it begins
its response. We currently don't support that sort of protocol; we
may want to switch to some sort of a two-way-destry-ripple technique
(where a destroy makes its way all the way to the end of the circuit
before being echoed back, and data stops flowing only when a destroy
has been received from both sides of the circuit); this extends the
one-hop-ack approach that Matej used.
* Reply onions. Hrm.
SRC=log.c config.c utils.c key.c onion.c cell.c routent.c scheduler.c
OBJ=${SRC:.c=.o}
INCLUDE=-I/usr/local/ssl/include
CFLAGS=${INCLUDE} -Wall -Wpointer-arith -O2
all: ${OBJ}
clean:
rm -f $(OBJ)
depend:
makedepend -- $(CFLAGS) -- $(SRC)
# DO NOT DELETE
cell.o: cell.h log.h /usr/include/alloca.h /usr/include/asm/socket.h
cell.o: /usr/include/asm/sockios.h /usr/include/bits/byteswap.h
cell.o: /usr/include/bits/confname.h /usr/include/bits/endian.h
cell.o: /usr/include/bits/environments.h /usr/include/bits/in.h
cell.o: /usr/include/bits/local_lim.h /usr/include/bits/posix1_lim.h
cell.o: /usr/include/bits/posix2_lim.h /usr/include/bits/posix_opt.h
cell.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
cell.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
cell.o: /usr/include/bits/sockaddr.h /usr/include/bits/socket.h
cell.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
cell.o: /usr/include/bits/types.h /usr/include/bits/waitflags.h
cell.o: /usr/include/bits/waitstatus.h /usr/include/bits/wchar.h
cell.o: /usr/include/bits/wordsize.h /usr/include/bits/xopen_lim.h
cell.o: /usr/include/endian.h /usr/include/features.h /usr/include/getopt.h
cell.o: /usr/include/gnu/stubs.h /usr/include/limits.h
cell.o: /usr/include/linux/limits.h /usr/include/netinet/in.h
cell.o: /usr/include/openssl/rand.h /usr/include/stdint.h
cell.o: /usr/include/stdlib.h /usr/include/string.h /usr/include/sys/cdefs.h
cell.o: /usr/include/syslog.h /usr/include/sys/select.h
cell.o: /usr/include/sys/syslog.h /usr/include/sys/sysmacros.h
cell.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/unistd.h
cell.o: /usr/include/xlocale.h
cell.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/limits.h
cell.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
cell.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
cell.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/syslimits.h
config.o: config.h log.h /usr/include/alloca.h /usr/include/asm/errno.h
config.o: /usr/include/bits/endian.h /usr/include/bits/errno.h
config.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
config.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
config.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
config.o: /usr/include/bits/types.h /usr/include/bits/waitflags.h
config.o: /usr/include/bits/waitstatus.h /usr/include/bits/wchar.h
config.o: /usr/include/ctype.h /usr/include/endian.h /usr/include/errno.h
config.o: /usr/include/features.h /usr/include/_G_config.h
config.o: /usr/include/gconv.h /usr/include/gnu/stubs.h /usr/include/libio.h
config.o: /usr/include/linux/errno.h /usr/include/stdio.h
config.o: /usr/include/stdlib.h /usr/include/string.h /usr/include/sys/cdefs.h
config.o: /usr/include/syslog.h /usr/include/sys/select.h
config.o: /usr/include/sys/syslog.h /usr/include/sys/sysmacros.h
config.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/wchar.h
config.o: /usr/include/xlocale.h
config.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
config.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
key.o: config.h key.h log.h /usr/include/alloca.h /usr/include/asm/errno.h
key.o: /usr/include/bits/endian.h /usr/include/bits/errno.h
key.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
key.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
key.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
key.o: /usr/include/bits/types.h /usr/include/bits/waitflags.h
key.o: /usr/include/bits/waitstatus.h /usr/include/bits/wchar.h
key.o: /usr/include/endian.h /usr/include/errno.h /usr/include/features.h
key.o: /usr/include/_G_config.h /usr/include/gconv.h /usr/include/gnu/stubs.h
key.o: /usr/include/libio.h /usr/include/linux/errno.h
key.o: /usr/include/openssl/asn1.h /usr/include/openssl/bio.h
key.o: /usr/include/openssl/blowfish.h /usr/include/openssl/bn.h
key.o: /usr/include/openssl/buffer.h /usr/include/openssl/cast.h
key.o: /usr/include/openssl/crypto.h /usr/include/openssl/des.h
key.o: /usr/include/openssl/dh.h /usr/include/openssl/dsa.h
key.o: /usr/include/openssl/e_os2.h /usr/include/openssl/err.h
key.o: /usr/include/openssl/evp.h /usr/include/openssl/lhash.h
key.o: /usr/include/openssl/md2.h /usr/include/openssl/md4.h
key.o: /usr/include/openssl/md5.h /usr/include/openssl/objects.h
key.o: /usr/include/openssl/obj_mac.h /usr/include/openssl/opensslconf.h
key.o: /usr/include/openssl/opensslv.h /usr/include/openssl/pem2.h
key.o: /usr/include/openssl/pem.h /usr/include/openssl/pkcs7.h
key.o: /usr/include/openssl/rc2.h /usr/include/openssl/rc4.h
key.o: /usr/include/openssl/ripemd.h /usr/include/openssl/rsa.h
key.o: /usr/include/openssl/safestack.h /usr/include/openssl/sha.h
key.o: /usr/include/openssl/stack.h /usr/include/openssl/symhacks.h
key.o: /usr/include/openssl/x509.h /usr/include/openssl/x509_vfy.h
key.o: /usr/include/stdio.h /usr/include/stdlib.h /usr/include/string.h
key.o: /usr/include/sys/cdefs.h /usr/include/syslog.h
key.o: /usr/include/sys/select.h /usr/include/sys/syslog.h
key.o: /usr/include/sys/sysmacros.h /usr/include/sys/types.h
key.o: /usr/include/time.h /usr/include/wchar.h /usr/include/xlocale.h
key.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
key.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
log.o: log.h /usr/include/asm/errno.h /usr/include/bits/errno.h
log.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
log.o: /usr/include/bits/stdio_lim.h /usr/include/bits/types.h
log.o: /usr/include/bits/wchar.h /usr/include/errno.h /usr/include/features.h
log.o: /usr/include/_G_config.h /usr/include/gconv.h /usr/include/gnu/stubs.h
log.o: /usr/include/libio.h /usr/include/linux/errno.h /usr/include/stdio.h
log.o: /usr/include/string.h /usr/include/sys/cdefs.h /usr/include/syslog.h
log.o: /usr/include/sys/syslog.h /usr/include/wchar.h /usr/include/xlocale.h
log.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
log.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
onion.o: cell.h log.h onion.h routent.h /usr/include/alloca.h
onion.o: /usr/include/arpa/inet.h /usr/include/asm/errno.h
onion.o: /usr/include/asm/socket.h /usr/include/asm/sockios.h
onion.o: /usr/include/bits/byteswap.h /usr/include/bits/confname.h
onion.o: /usr/include/bits/endian.h /usr/include/bits/environments.h
onion.o: /usr/include/bits/errno.h /usr/include/bits/in.h
onion.o: /usr/include/bits/local_lim.h /usr/include/bits/posix1_lim.h
onion.o: /usr/include/bits/posix2_lim.h /usr/include/bits/posix_opt.h
onion.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
onion.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
onion.o: /usr/include/bits/sockaddr.h /usr/include/bits/socket.h
onion.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
onion.o: /usr/include/bits/types.h /usr/include/bits/uio.h
onion.o: /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h
onion.o: /usr/include/bits/wchar.h /usr/include/bits/wordsize.h
onion.o: /usr/include/bits/xopen_lim.h /usr/include/endian.h
onion.o: /usr/include/errno.h /usr/include/features.h /usr/include/_G_config.h
onion.o: /usr/include/gconv.h /usr/include/getopt.h /usr/include/gnu/stubs.h
onion.o: /usr/include/libio.h /usr/include/limits.h /usr/include/linux/errno.h
onion.o: /usr/include/linux/limits.h /usr/include/netinet/in.h
onion.o: /usr/include/openssl/asn1.h /usr/include/openssl/bio.h
onion.o: /usr/include/openssl/blowfish.h /usr/include/openssl/bn.h
onion.o: /usr/include/openssl/cast.h /usr/include/openssl/crypto.h
onion.o: /usr/include/openssl/des.h /usr/include/openssl/dh.h
onion.o: /usr/include/openssl/dsa.h /usr/include/openssl/e_os2.h
onion.o: /usr/include/openssl/err.h /usr/include/openssl/evp.h
onion.o: /usr/include/openssl/lhash.h /usr/include/openssl/md2.h
onion.o: /usr/include/openssl/md4.h /usr/include/openssl/md5.h
onion.o: /usr/include/openssl/objects.h /usr/include/openssl/obj_mac.h
onion.o: /usr/include/openssl/opensslconf.h /usr/include/openssl/opensslv.h
onion.o: /usr/include/openssl/rand.h /usr/include/openssl/rc2.h
onion.o: /usr/include/openssl/rc4.h /usr/include/openssl/ripemd.h
onion.o: /usr/include/openssl/rsa.h /usr/include/openssl/safestack.h
onion.o: /usr/include/openssl/sha.h /usr/include/openssl/stack.h
onion.o: /usr/include/openssl/symhacks.h /usr/include/stdint.h
onion.o: /usr/include/stdio.h /usr/include/stdlib.h /usr/include/string.h
onion.o: /usr/include/sys/cdefs.h /usr/include/syslog.h
onion.o: /usr/include/sys/select.h /usr/include/sys/socket.h
onion.o: /usr/include/sys/syslog.h /usr/include/sys/sysmacros.h
onion.o: /usr/include/sys/timeb.h /usr/include/sys/time.h
onion.o: /usr/include/sys/types.h /usr/include/sys/uio.h /usr/include/time.h
onion.o: /usr/include/unistd.h /usr/include/wchar.h /usr/include/xlocale.h
onion.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/limits.h
onion.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
onion.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
onion.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/syslimits.h version.h
routent.o: cell.h routent.h /usr/include/alloca.h /usr/include/bits/confname.h
routent.o: /usr/include/bits/endian.h /usr/include/bits/environments.h
routent.o: /usr/include/bits/posix_opt.h /usr/include/bits/pthreadtypes.h
routent.o: /usr/include/bits/sched.h /usr/include/bits/select.h
routent.o: /usr/include/bits/sigset.h /usr/include/bits/stdio_lim.h
routent.o: /usr/include/bits/time.h /usr/include/bits/types.h
routent.o: /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h
routent.o: /usr/include/bits/wchar.h /usr/include/bits/wordsize.h
routent.o: /usr/include/endian.h /usr/include/features.h
routent.o: /usr/include/_G_config.h /usr/include/gconv.h /usr/include/getopt.h
routent.o: /usr/include/gnu/stubs.h /usr/include/libio.h
routent.o: /usr/include/openssl/asn1.h /usr/include/openssl/bio.h
routent.o: /usr/include/openssl/blowfish.h /usr/include/openssl/bn.h
routent.o: /usr/include/openssl/cast.h /usr/include/openssl/crypto.h
routent.o: /usr/include/openssl/des.h /usr/include/openssl/dh.h
routent.o: /usr/include/openssl/dsa.h /usr/include/openssl/e_os2.h
routent.o: /usr/include/openssl/evp.h /usr/include/openssl/md2.h
routent.o: /usr/include/openssl/md4.h /usr/include/openssl/md5.h
routent.o: /usr/include/openssl/objects.h /usr/include/openssl/obj_mac.h
routent.o: /usr/include/openssl/opensslconf.h /usr/include/openssl/opensslv.h
routent.o: /usr/include/openssl/rc2.h /usr/include/openssl/rc4.h
routent.o: /usr/include/openssl/ripemd.h /usr/include/openssl/rsa.h
routent.o: /usr/include/openssl/safestack.h /usr/include/openssl/sha.h
routent.o: /usr/include/openssl/stack.h /usr/include/openssl/symhacks.h
routent.o: /usr/include/stdint.h /usr/include/stdio.h /usr/include/stdlib.h
routent.o: /usr/include/sys/cdefs.h /usr/include/sys/select.h
routent.o: /usr/include/sys/sysmacros.h /usr/include/sys/timeb.h
routent.o: /usr/include/sys/time.h /usr/include/sys/types.h
routent.o: /usr/include/time.h /usr/include/unistd.h /usr/include/wchar.h
routent.o: /usr/include/xlocale.h
routent.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
routent.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
scheduler.o: log.h scheduler.h /usr/include/bits/confname.h
scheduler.o: /usr/include/bits/environments.h /usr/include/bits/posix_opt.h
scheduler.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
scheduler.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
scheduler.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
scheduler.o: /usr/include/bits/types.h /usr/include/bits/wchar.h
scheduler.o: /usr/include/bits/wordsize.h /usr/include/features.h
scheduler.o: /usr/include/_G_config.h /usr/include/gconv.h
scheduler.o: /usr/include/getopt.h /usr/include/gnu/stubs.h
scheduler.o: /usr/include/libio.h /usr/include/malloc.h /usr/include/stdio.h
scheduler.o: /usr/include/sys/cdefs.h /usr/include/syslog.h
scheduler.o: /usr/include/sys/select.h /usr/include/sys/syslog.h
scheduler.o: /usr/include/sys/time.h /usr/include/time.h /usr/include/unistd.h
scheduler.o: /usr/include/wchar.h
scheduler.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
scheduler.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
utils.o: log.h /usr/include/alloca.h /usr/include/asm/errno.h
utils.o: /usr/include/asm/socket.h /usr/include/asm/sockios.h
utils.o: /usr/include/bits/confname.h /usr/include/bits/endian.h
utils.o: /usr/include/bits/environments.h /usr/include/bits/errno.h
utils.o: /usr/include/bits/local_lim.h /usr/include/bits/posix1_lim.h
utils.o: /usr/include/bits/posix2_lim.h /usr/include/bits/posix_opt.h
utils.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
utils.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
utils.o: /usr/include/bits/sockaddr.h /usr/include/bits/socket.h
utils.o: /usr/include/bits/stdio_lim.h /usr/include/bits/time.h
utils.o: /usr/include/bits/types.h /usr/include/bits/uio.h
utils.o: /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h
utils.o: /usr/include/bits/wchar.h /usr/include/bits/wordsize.h
utils.o: /usr/include/bits/xopen_lim.h /usr/include/ctype.h
utils.o: /usr/include/endian.h /usr/include/errno.h /usr/include/features.h
utils.o: /usr/include/_G_config.h /usr/include/gconv.h /usr/include/getopt.h
utils.o: /usr/include/gnu/stubs.h /usr/include/libio.h /usr/include/limits.h
utils.o: /usr/include/linux/errno.h /usr/include/linux/limits.h
utils.o: /usr/include/stdio.h /usr/include/stdlib.h /usr/include/string.h
utils.o: /usr/include/sys/cdefs.h /usr/include/syslog.h
utils.o: /usr/include/sys/select.h /usr/include/sys/socket.h
utils.o: /usr/include/sys/syslog.h /usr/include/sys/sysmacros.h
utils.o: /usr/include/sys/time.h /usr/include/sys/types.h
utils.o: /usr/include/sys/uio.h /usr/include/sys/un.h /usr/include/time.h
utils.o: /usr/include/unistd.h /usr/include/wchar.h /usr/include/xlocale.h
utils.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/limits.h
utils.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stdarg.h
utils.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/stddef.h
utils.o: /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/syslimits.h utils.h
/**
* cell.c
* Cell manipulation.
*
* Matej Pfajfar <mp292@cam.ac.uk>
*/
/*
* Changes :
* $Log$
* Revision 1.1 2002/06/26 22:45:50 arma
* Initial revision
*
* Revision 1.16 2002/06/14 20:41:19 mp292
* Parameter checking error - thanks Roger.
*
* Revision 1.15 2002/04/02 14:27:11 badbytes
* Final finishes.
*
* Revision 1.14 2002/04/02 10:19:37 badbytes
* Stricter parameter checking.
*
* Revision 1.13 2002/03/12 23:30:19 mp292
* Removed some memory overruns.
*
* Revision 1.12 2002/03/03 00:06:45 mp292
* Modifications to support re-transmission.
*
* Revision 1.11 2002/02/03 22:41:45 mp292
* Changes to cell size.
*
* Revision 1.10 2002/01/21 20:57:19 mp292
* Reviewed according to Secure-Programs-HOWTO.
*
* Revision 1.9 2002/01/17 15:00:43 mp292
* Fixed a bug which caused malloc() generate a seg fault.
*
* Revision 1.8 2002/01/16 23:01:54 mp292
* First phase of system testing completed (main functionality).
*
* Revision 1.7 2002/01/14 13:05:37 badbytes
* System testing in progress.
*
* Revision 1.6 2002/01/10 13:15:54 badbytes
* Fixed ACI size from 32bits to 16bits.
*
* Revision 1.5 2002/01/07 13:06:06 badbytes
* cell.ACI is now cell.aci
*
* Revision 1.4 2002/01/07 09:26:00 badbytes
* Added pack_create() and pack_data().
*
* Revision 1.3 2002/01/07 07:48:34 badbytes
* fixed new_create_cell()
*
* Revision 1.2 2002/01/04 12:11:54 badbytes
* Syntax errors fixed.
*
* Revision 1.1 2002/01/04 12:08:34 badbytes
* Added functions for cell creation.
*
*/
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <openssl/rand.h>
#include "cell.h"
#include "log.h"
cell_t *new_padding_cell()
{
cell_t *c = NULL;
int retval;
c = malloc(sizeof(cell_t));
if (!c) /* malloc() error */
return NULL;
retval = RAND_pseudo_bytes((unsigned char *)c,sizeof(cell_t));
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
c->command = CELL_PADDING;
return c;
}
cell_t *new_destroy_cell(uint16_t aci)
{
cell_t *c = NULL;
int retval;
if (aci) /* valid ACI */
{
c = (cell_t *)malloc(sizeof(cell_t));
if (!c) /* malloc error */
return NULL;
retval = RAND_pseudo_bytes((unsigned char *)c+3,sizeof(cell_t)-3);
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
c->aci = aci;
c->command = CELL_DESTROY;
return c;
} /* valid ACI */
else /* invalid ACI */
return NULL;
}
cell_t *new_ack_cell(uint16_t aci)
{
cell_t *c = NULL;
int retval;
if (aci) /* valid ACI */
{
c = (cell_t *)malloc(sizeof(cell_t));
if (!c) /* malloc error */
return NULL;
retval = RAND_pseudo_bytes((unsigned char *)c+3,sizeof(cell_t)-3);
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
c->aci = aci;
c->command = CELL_ACK;
return c;
} /* valid ACI */
else /* invalid ACI */
return NULL;
}
cell_t *new_nack_cell(uint16_t aci)
{
cell_t *c = NULL;
int retval;
if (aci) /* valid ACI */
{
c = (cell_t *)malloc(sizeof(cell_t));
if (!c) /* malloc error */
return NULL;
retval = RAND_pseudo_bytes((unsigned char *)c+3,sizeof(cell_t)-3);
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
c->aci = aci;
c->command = CELL_NACK;
return c;
} /* valid ACI */
else /* invalid ACI */
return NULL;
}
cell_t *new_create_cell(uint16_t aci, unsigned char length, unsigned char *buf)
{
cell_t *c = NULL;
int retval;
if ((aci) && (buf) && (length <= CELL_PAYLOAD_SIZE)) /* valid parameters */
{
c = (cell_t *)malloc(sizeof(cell_t));
if (!c) /* malloc() error */
return NULL;
c->command = CELL_CREATE;
c->aci = aci;
c->length = length;
c->seq = 0;
memcpy((void *)c->payload, (void *)buf, length);
retval = RAND_pseudo_bytes((unsigned char *)(c->payload+length),CELL_PAYLOAD_SIZE-length);
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
return c;
} /* valid parameters */
else /* invalid parameters */
return NULL;
}
cell_t *new_data_cell(uint16_t aci, unsigned char length, unsigned char *buf)
{
cell_t *c = NULL;
int retval;
if ((aci) && (buf) && (length <= CELL_PAYLOAD_SIZE)) /* valid parameters */
{
c = malloc(sizeof(cell_t));
if (!c) /* malloc() error */
return NULL;
c->command = CELL_DATA;
c->aci = aci;
c->length = length;
c->seq = 0;
memcpy((void *)c->payload, (void *)buf, length);
retval = RAND_pseudo_bytes((unsigned char *)(c->payload+length),CELL_PAYLOAD_SIZE-length);
if (retval == -1) /* RAND_pseudo_bytes() error */
{
free((void *)c);
return NULL;
} /* RAND_pseudo_bytes() error */
return c;
} /* valid parameters */
else /* invalid parameters */
return NULL;
}
int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen)
{
cell_t *c;
unsigned char *buf;
unsigned int buflen;
unsigned int cells;
unsigned int dataleft;
unsigned int i;
if ((aci) && (onion) && (cellbuf) && (cellbuflen) && (onionlen)) /* valid parameters */
{
/* copy the onion into a buffer, prepend with onion length */
buflen = onionlen+4;
buf = (unsigned char *)malloc(buflen);
if (!buf) /* malloc() error */
return -1;
log(LOG_DEBUG,"pack_create() : Setting onion length to %u.",onionlen);
onionlen=htonl(onionlen);
memcpy((void *)buf,(void *)&onionlen,4);
onionlen=ntohl(onionlen);
memcpy((void *)(buf+4),(void *)onion,onionlen);
/* calculate number of cells required */
if (buflen%CELL_PAYLOAD_SIZE == 0)
cells = buflen/CELL_PAYLOAD_SIZE;
else
cells = buflen/CELL_PAYLOAD_SIZE+1;
/* allocate memory for the cells */
*cellbuflen = cells * sizeof(cell_t);
*cellbuf = malloc(*cellbuflen);
if (!*cellbuf) /* malloc() error */
return -1;
log(LOG_DEBUG,"pack_create() : Allocated memory for %u cells.",cells);
/* create cells one by one */
dataleft = buflen;
for(i=0; i<cells; i++)
{
log(LOG_DEBUG,"pack_create() : Packing %u bytes of data.",dataleft);
if (dataleft >= CELL_PAYLOAD_SIZE)