3 @@ -43,6 +43,18 @@ U_BOOT_CMD(
4 "[loadAddress] [[hostIPaddr:]bootfilename]"
7 +#if defined(CONFIG_CMD_HTTPD)
8 +int do_httpd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
10 + return NetLoopHttpd();
14 + httpd, 1, 1, do_httpd,
15 + "httpd\t- start webserver\n", "\n"
19 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
21 return netboot_common (TFTP, cmdtp, argc, argv);
24 @@ -383,7 +383,8 @@ extern int NetTimeOffset; /* offset ti
26 /* Initialize the network adapter */
27 extern int NetLoop(proto_t);
29 +extern int NetLoopHttpd(void);
30 +extern void NetSendHttpd(void);
31 /* Shutdown adapters and cleanup */
32 extern void NetStop(void);
38 + * Copyright 1994, 1995, 2000 Neil Russell.
40 + * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
46 +#include "uip-0.9/uipopt.h"
47 +#include "uip-0.9/uip.h"
48 +#include "uip-0.9/uip_arp.h"
51 +#if defined(CONFIG_CMD_HTTPD)
55 +static int arptimer = 0;
61 + for(i = 0; i < UIP_CONNS; i++) {
68 + if(++arptimer == 20) {
78 + NetSetTimeout (TIMEOUT * 1000, HttpdTimeout);
92 +#ifndef _UIP_HTTPD_H__
93 +#define _UIP_HTTPD_H__
95 +void HttpdStart (void);
96 +void HttpdHandler (void);
98 +/* board specific implementation */
99 +extern int do_http_upgrade(const unsigned char *data, const ulong size);
101 +#define HTTP_PROGRESS_START 0
102 +#define HTTP_PROGRESS_TIMEOUT 1
103 +#define HTTP_PROGRESS_UPLOAD_READY 2
104 +#define HTTP_PROGRESS_UGRADE_READY 3
105 +#define HTTP_PROGRESS_UGRADE_FAILED 4
106 +extern int do_http_progress(const int state);
111 @@ -26,6 +26,10 @@ include $(TOPDIR)/config.mk
117 +$(shell mkdir -p $(obj)$(UIPDIR))
118 +$(shell mkdir -p $(obj)$(RSADIR))
121 COBJS-$(CONFIG_CMD_DNS) += dns.o
122 @@ -36,6 +40,9 @@ COBJS-y += rarp.o
123 COBJS-$(CONFIG_CMD_SNTP) += sntp.o
126 +COBJS-$(CONFIG_CMD_HTTPD) += httpd.o $(UIPDIR)/fs.o $(UIPDIR)/httpd.o $(UIPDIR)/uip_arp.o $(UIPDIR)/uip_arch.o $(UIPDIR)/uip.o
127 +COBJS-$(CONFIG_CMD_RSA) += $(RSADIR)/bigint.o $(RSADIR)/base64.o $(RSADIR)/rmd160.o $(RSADIR)/rsa.o
130 SRCS := $(COBJS:.o=.c)
131 OBJS := $(addprefix $(obj),$(COBJS))
135 #if defined(CONFIG_CMD_DNS)
138 +#if defined(CONFIG_CMD_HTTPD)
140 +#include "uip-0.9/uipopt.h"
141 +#include "uip-0.9/uip.h"
142 +#include "uip-0.9/uip_arp.h"
143 +static int https_running = 0;
144 +int httpd_upload_complete = 0;
145 +unsigned char *httpd_upload_data = 0;
146 +extern int upload_running;
147 +void NetReceiveHttpd(volatile uchar * inpkt, int len);
148 +void NetSendHttpd(void);
149 +extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
152 #if defined(CONFIG_CMD_NET)
154 @@ -1310,6 +1323,13 @@ NetReceive(volatile uchar * inpkt, int l
156 debug("packet received\n");
158 +#if defined(CONFIG_CMD_HTTPD)
159 + if(https_running) {
160 + NetReceiveHttpd(inpkt, len);
166 NetRxPacketLen = len;
167 et = (Ethernet_t *)inpkt;
168 @@ -1952,3 +1972,162 @@ ushort getenv_VLAN(char *var)
170 return (string_to_VLAN(getenv(var)));
173 +#if defined(CONFIG_CMD_HTTPD)
178 + volatile uchar *tmpbuf = NetTxPacket;
181 + for(i = 0; i < 40 + UIP_LLH_LEN; i++) {
182 + tmpbuf[i] = uip_buf[i];
185 + for(; i < uip_len; i++) {
186 + tmpbuf[i] = uip_appdata[i - 40 - UIP_LLH_LEN];
188 + eth_send(NetTxPacket, uip_len);
191 +#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
194 +NetReceiveHttpd(volatile uchar * inpkt, int len)
196 + memcpy(uip_buf, inpkt, len);
198 + if(BUF->type == htons(UIP_ETHTYPE_IP)) {
205 + } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
216 + unsigned long long tout = 0;
218 + unsigned short int ip[2];
220 +#ifdef CONFIG_NET_MULTI
225 + /* XXX problem with bss workaround */
226 + NetArpWaitPacketMAC = NULL;
227 + NetArpWaitTxPacket = NULL;
228 + NetArpWaitPacketIP = 0;
229 + NetArpWaitReplyIP = 0;
230 + NetArpWaitTxPacket = NULL;
231 + NetTxPacket = NULL;
234 + if (!NetTxPacket) {
237 + * Setup packet buffers, aligned correctly.
239 + NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
240 + NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
241 + for (i = 0; i < PKTBUFSRX; i++) {
242 + NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
246 + if (!NetArpWaitTxPacket) {
247 + NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
248 + NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
249 + NetArpWaitTxPacketSize = 0;
255 +#ifdef CONFIG_NET_MULTI
258 + if (eth_init(bd) < 0) {
263 +#ifdef CONFIG_NET_MULTI
264 + memcpy (NetOurEther, eth_get_dev()->enetaddr, 6);
266 + eth_getenv_enetaddr("ethaddr", NetOurEther);
269 + NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
270 + NetOurGatewayIP = getenv_IPaddr ("gatewayip");
271 + NetOurSubnetMask= getenv_IPaddr ("netmask");
272 + NetOurVLAN = getenv_VLAN("vlan");
273 + NetOurNativeVLAN = getenv_VLAN("nvlan");
275 + printf("starting httpd server from server %ld.%ld.%ld.%ld\n",
276 + (bd->bi_ip_addr & 0xff000000) >> 24,
277 + (bd->bi_ip_addr & 0x00ff0000) >> 16,
278 + (bd->bi_ip_addr & 0x0000ff00) >> 8,
279 + (bd->bi_ip_addr & 0x000000ff));
283 + ip[0] = ((bd->bi_ip_addr & 0xffff0000) >> 16);
284 + ip[1] = (bd->bi_ip_addr & 0x0000ffff);
285 + uip_sethostaddr(ip);
287 + do_http_progress(HTTP_PROGRESS_START);
291 + unsigned long long t1;
297 + if(t1 - tout > 1000) {
298 + do_http_progress(HTTP_PROGRESS_TIMEOUT);
302 + if(!httpd_upload_complete)
304 + printf("Bytes transferred = %ld (%lx hex)\n",
305 + NetBootFileXferSize,
306 + NetBootFileXferSize);
308 + do_http_progress(HTTP_PROGRESS_UPLOAD_READY);
309 + if(do_http_upgrade(&httpd_upload_data[0], NetBootFileXferSize) == 0) {
310 + do_http_progress(HTTP_PROGRESS_UGRADE_READY);
312 + do_reset (0,0,0,0);
318 + NetBootFileXferSize = 0;
319 + httpd_upload_complete = 0;
320 + upload_running = 0;
321 +// free(httpd_upload_data);
323 + do_http_progress(HTTP_PROGRESS_UGRADE_FAILED);
332 +++ b/net/rsa/base64.c
336 +static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
337 +/* Note that '=' (padding) is 0 */
338 +static const unsigned char fb64[256] = {
339 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
340 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
341 + 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
342 + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
343 + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
344 + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
345 + 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
346 + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
347 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
348 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
349 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
350 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
351 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
352 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
353 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
354 + 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
357 +static int encodeblock( unsigned char *in, char *out, int len )
362 + for (i = 0; i < len; i++)
364 + for (i = len; i < 3; i++)
366 + out[0] = (unsigned char)(cb64[(s[0] & 0xfc) >> 2 ]);
367 + out[1] = (unsigned char)(cb64[((s[0] & 0x03) << 4) | ((s[1] & 0xf0) >> 4) ]);
368 + out[2] = (unsigned char)(cb64[((s[1] & 0x0f) << 2) | ((s[2] & 0xc0) >> 6) ]);
369 + out[3] = (unsigned char)(cb64[s[2] & 0x3f ]);
383 +static int decodeblock(char *ins, unsigned char *out, int len)
386 + unsigned char in[4];
391 + for (i = 0; i < len; i++) {
392 + if (ins[i] == '=') {
396 + in[i] = fb64[(int)(ins[i])];
397 + if (in[i] == 255) {
401 + out[0] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
405 + out[1] = (unsigned char )((in[1] & 0x0f) << 4 | in[2] >> 2);
409 + out[2] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
414 +int B64_encode(char *source, char *destination, int size_source, int size_destination)
416 + int chunks, reminder, size, d, i, size_expected;
420 + chunks = size_source / 3;
421 + reminder = size_source % 3;
423 + size_expected = (chunks * 4) + (reminder?(reminder + 1):0);
424 + if (size_destination < ((chunks * 4) + (reminder?4:0))) {
427 + for (i = 0; i < chunks; i++) {
428 + s = source + (i * 3);
429 + t = destination + (i * 4);
430 + d = encodeblock(s, t, 3);
437 + d = encodeblock(source + (chunks * 3), destination + (chunks * 4), reminder);
446 +int B64_decode(char *source, char *destination, int size_source, int size_destination)
448 + int chunks, reminder, size, d, i;
450 + chunks = size_source / 4;
451 + reminder = size_source % 4;
456 + if (size_destination < ((chunks * 3))) {
458 + size_destination, ((chunks * 3) + reminder));
461 + for (i = 0; i < chunks; i++) {
462 + d = decodeblock(source + (i * 4), destination + (i * 3), 4);
472 +++ b/net/rsa/base64.h
479 +int B64_encode(char *source, char *destination, int size_source, int size_destination);
480 +int B64_decode(char *source, char *destination, int size_source, int size_destination);
486 +++ b/net/rsa/bigint.c
489 + * Copyright(C) 2006
491 + * This library is free software; you can redistribute it and/or modify
492 + * it under the terms of the GNU Lesser General Public License as published by
493 + * the Free Software Foundation; either version 2.1 of the License, or
494 + * (at your option) any later version.
496 + * This library is distributed in the hope that it will be useful,
497 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
498 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
499 + * GNU Lesser General Public License for more details.
501 + * You should have received a copy of the GNU Lesser General Public License
502 + * along with this library; if not, write to the Free Software
503 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
507 + * @defgroup bigint_api Big Integer API
508 + * @brief The bigint implementation as used by the axTLS project.
510 + * The bigint library is for RSA encryption/decryption as well as signing.
511 + * This code tries to minimise use of malloc/free by maintaining a small
512 + * cache. A bigint context may maintain state by being made "permanent".
513 + * It be be later released with a bi_depermanent() and bi_free() call.
515 + * It supports the following reduction techniques:
520 + * It also implements the following:
521 + * - Karatsuba multiplication
523 + * - Sliding window exponentiation
524 + * - Chinese Remainder Theorem (implemented in rsa.c).
526 + * All the algorithms used are pretty standard, and designed for different
527 + * data bus sizes. Negative numbers are not dealt with at all, so a subtraction
528 + * may need to be tested for negativity.
530 + * This library steals some ideas from Jef Poskanzer
531 + * <http://cs.marlboro.edu/term/cs-fall02/algorithms/crypto/RSA/bigint>
532 + * and GMP <http://www.swox.com/gmp>. It gets most of its implementation
533 + * detail from "The Handbook of Applied Cryptography"
534 + * <http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf>
542 +static bigint *bi_int_multiply(BI_CTX *ctx, bigint *bi, comp i);
543 +static bigint *bi_int_divide(BI_CTX *ctx, bigint *biR, comp denom);
544 +static bigint *alloc(BI_CTX *ctx, int size);
545 +static bigint *trim(bigint *bi);
546 +static void more_comps(bigint *bi, int n);
549 + * @brief Start a new bigint context.
550 + * @return A bigint context.
552 +BI_CTX *bi_initialize(void)
554 + BI_CTX *ctx = (BI_CTX *)calloc(1, sizeof(BI_CTX));
556 + ctx->active_list = NULL;
557 + ctx->active_count = 0;
558 + ctx->free_list = NULL;
559 + ctx->free_count = 0;
560 + ctx->mod_offset = 0;
563 + ctx->bi_radix = alloc(ctx, 2);
564 + ctx->bi_radix->comps[0] = 0;
565 + ctx->bi_radix->comps[1] = 1;
566 + bi_permanent(ctx->bi_radix);
572 + * @brief Close the bigint context and free any resources.
574 + * Free up any used memory - a check is done if all objects were not
576 + * @param ctx [in] The bigint session context.
578 +void bi_terminate(BI_CTX *ctx)
582 + bi_depermanent(ctx->bi_radix);
583 + bi_free(ctx, ctx->bi_radix);
585 + if (ctx->active_count != 0)
587 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
588 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
589 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
590 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
591 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
592 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
596 + for (p = ctx->free_list; p != NULL; p = pn)
607 + * @brief Increment the number of references to this object.
608 + * It does not do a full copy.
609 + * @param bi [in] The bigint to copy.
610 + * @return A referent to the same bigint.
612 +bigint *bi_copy(bigint *bi)
615 + if (bi->refs != PERMANENT)
621 + * @brief Simply make a bigint object "unfreeable" if bi_free() is called on it.
623 + * For this object to be freed, bi_depermanent() must be called.
624 + * @param bi [in] The bigint to be made permanent.
626 +void bi_permanent(bigint *bi)
631 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
632 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
633 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
637 + bi->refs = PERMANENT;
641 + * @brief Take a permanent object and make it elligible for freedom.
642 + * @param bi [in] The bigint to be made back to temporary.
644 +void bi_depermanent(bigint *bi)
647 + if (bi->refs != PERMANENT)
649 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
650 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
651 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
652 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
660 + * @brief Free a bigint object so it can be used again.
662 + * The memory itself it not actually freed, just tagged as being available
663 + * @param ctx [in] The bigint session context.
664 + * @param bi [in] The bigint to be freed.
666 +void bi_free(BI_CTX *ctx, bigint *bi)
669 + if (bi->refs == PERMANENT)
674 + if (--bi->refs > 0)
679 + bi->next = ctx->free_list;
680 + ctx->free_list = bi;
683 + if (--ctx->active_count < 0)
685 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
686 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
687 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
688 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
694 + * @brief Convert an (unsigned) integer into a bigint.
695 + * @param ctx [in] The bigint session context.
696 + * @param i [in] The (unsigned) integer to be converted.
699 +bigint *int_to_bi(BI_CTX *ctx, comp i)
701 + bigint *biR = alloc(ctx, 1);
707 + * @brief Do a full copy of the bigint object.
708 + * @param ctx [in] The bigint session context.
709 + * @param bi [in] The bigint object to be copied.
711 +bigint *bi_clone(BI_CTX *ctx, const bigint *bi)
713 + bigint *biR = alloc(ctx, bi->size);
715 + memcpy(biR->comps, bi->comps, bi->size*COMP_BYTE_SIZE);
720 + * @brief Perform an additon operation between two bigints.
721 + * @param ctx [in] The bigint session context.
722 + * @param bia [in] A bigint.
723 + * @param bib [in] Another bigint.
724 + * @return The result of the addition.
726 +bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib)
735 + if (bia->size > bib->size)
739 + more_comps(bia, n+1);
740 + more_comps(bib, n);
750 + carry = cy1 | (rl < sl);
752 + } while (--n != 0);
754 + *pa = carry; /* do overflow */
760 + * @brief Perform a subtraction operation between two bigints.
761 + * @param ctx [in] The bigint session context.
762 + * @param bia [in] A bigint.
763 + * @param bib [in] Another bigint.
764 + * @param is_negative [out] If defined, indicates that the result was negative.
765 + * is_negative may be NULL.
766 + * @return The result of the subtraction. The result is always positive.
768 +bigint *bi_subtract(BI_CTX *ctx,
769 + bigint *bia, bigint *bib, int *is_negative)
772 + comp *pa, *pb, carry = 0;
777 + more_comps(bib, n);
787 + carry = cy1 | (rl > sl);
789 + } while (--n != 0);
791 + if (is_negative) /* indicate a negative result */
793 + *is_negative = carry;
796 + bi_free(ctx, trim(bib)); /* put bib back to the way it was */
801 + * Perform a multiply between a bigint an an (unsigned) integer
803 +static bigint *bi_int_multiply(BI_CTX *ctx, bigint *bia, comp b)
805 + int j = 0, n = bia->size;
806 + bigint *biR = alloc(ctx, n + 1);
808 + comp *r = biR->comps;
809 + comp *a = bia->comps;
813 + /* clear things to start with */
814 + memset(r, 0, ((n+1)*COMP_BYTE_SIZE));
818 + long_comp tmp = *r + (long_comp)a[j]*b + carry;
819 + *r++ = (comp)tmp; /* downsize */
820 + carry = (comp)(tmp >> COMP_BIT_SIZE);
829 + * @brief Does both division and modulo calculations.
831 + * Used extensively when doing classical reduction.
832 + * @param ctx [in] The bigint session context.
833 + * @param u [in] A bigint which is the numerator.
834 + * @param v [in] Either the denominator or the modulus depending on the mode.
835 + * @param is_mod [n] Determines if this is a normal division (0) or a reduction
837 + * @return The result of the division/reduction.
839 +bigint *bi_divide(BI_CTX *ctx, bigint *u, bigint *v, int is_mod)
841 + int n = v->size, m = u->size-n;
842 + int j = 0, orig_u_size = u->size;
843 + uint8_t mod_offset = ctx->mod_offset;
845 + bigint *quotient, *tmp_u;
851 + /* if doing reduction and we are < mod, then return mod */
852 + if (is_mod && bi_compare(v, u) > 0)
858 + quotient = alloc(ctx, m+1);
859 + tmp_u = alloc(ctx, n+1);
860 + v = trim(v); /* make sure we have no leading 0's */
861 + // d = (comp)((long_comp)COMP_RADIX/(V1+1));
862 + long_comp x = COMP_RADIX; do_div(x, V1+1); d = x;
864 + /* clear things to start with */
865 + memset(quotient->comps, 0, ((quotient->size)*COMP_BYTE_SIZE));
870 + u = bi_int_multiply(ctx, u, d);
874 + v = ctx->bi_normalised_mod[mod_offset];
878 + v = bi_int_multiply(ctx, v, d);
882 + if (orig_u_size == u->size) /* new digit position u0 */
884 + more_comps(u, orig_u_size + 1);
889 + /* get a temporary short version of u */
890 + memcpy(tmp_u->comps, &u->comps[u->size-n-1-j], (n+1)*COMP_BYTE_SIZE);
895 + q_dash = COMP_RADIX-1;
899 + //q_dash = (comp)(((long_comp)U(0)*COMP_RADIX + U(1))/V1);
900 + long_comp x = U(0)*COMP_RADIX + U(1); do_div(x, V1); q_dash = x;
904 + if (v->size > 1 && V2)
906 + /* we are implementing the following
907 + if (V2*q_dash > (((U(0)*COMP_RADIX + U(1) -
908 + q_dash*V1)*COMP_RADIX) + U(2))) ... */
909 + comp inner = (comp)((long_comp)COMP_RADIX*U(0) + U(1) -
910 + (long_comp)q_dash*V1);
911 + if ((long_comp)V2*q_dash > (long_comp)inner*COMP_RADIX + U(2))
917 + /* multiply and subtract */
921 + tmp_u = bi_subtract(ctx, tmp_u,
922 + bi_int_multiply(ctx, bi_copy(v), q_dash), &is_negative);
923 + more_comps(tmp_u, n+1);
931 + tmp_u = bi_add(ctx, tmp_u, bi_copy(v));
932 + /* lop off the carry */
942 + /* copy back to u */
943 + memcpy(&u->comps[u->size-n-1-j], tmp_u->comps, (n+1)*COMP_BYTE_SIZE);
944 + } while (++j <= m);
946 + bi_free(ctx, tmp_u);
949 + if (is_mod) /* get the remainder */
951 + bi_free(ctx, quotient);
952 + return bi_int_divide(ctx, trim(u), d);
954 + else /* get the quotient */
957 + return trim(quotient);
962 + * Perform an integer divide on a bigint.
964 +static bigint *bi_int_divide(BI_CTX *ctx, bigint *biR, comp denom)
966 + int i = biR->size - 1;
973 + r = (r<<COMP_BIT_SIZE) + biR->comps[i];
974 + //biR->comps[i] = (comp)(r / denom);
975 + long_comp x = r; do_div(x, denom); biR->comps[i] = x;
981 + } while (--i != 0);
987 + * @brief Allow a binary sequence to be imported as a bigint.
988 + * @param ctx [in] The bigint session context.
989 + * @param data [in] The data to be converted.
990 + * @param size [in] The number of bytes of data.
991 + * @return A bigint representing this data.
993 +bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int size)
995 + bigint *biR = alloc(ctx, (size+COMP_BYTE_SIZE-1)/COMP_BYTE_SIZE);
996 + int i, j = 0, offset = 0;
998 + memset(biR->comps, 0, biR->size*COMP_BYTE_SIZE);
1000 + for (i = size-1; i >= 0; i--)
1002 + biR->comps[offset] += data[i] << (j*8);
1004 + if (++j == COMP_BYTE_SIZE)
1015 + * @brief Take a bigint and convert it into a byte sequence.
1017 + * This is useful after a decrypt operation.
1018 + * @param ctx [in] The bigint session context.
1019 + * @param x [in] The bigint to be converted.
1020 + * @param data [out] The converted data as a byte stream.
1021 + * @param size [in] The maximum size of the byte stream. Unused bytes will be
1024 +void bi_export(BI_CTX *ctx, bigint *x, uint8_t *data, int size)
1026 + int i, j, k = size-1;
1029 + memset(data, 0, size); /* ensure all leading 0's are cleared */
1031 + for (i = 0; i < x->size; i++)
1033 + for (j = 0; j < COMP_BYTE_SIZE; j++)
1035 + comp mask = 0xff << (j*8);
1036 + int num = (x->comps[i] & mask) >> (j*8);
1050 + * @brief Pre-calculate some of the expensive steps in reduction.
1052 + * This function should only be called once (normally when a session starts).
1053 + * When the session is over, bi_free_mod() should be called. bi_mod_power()
1054 + * relies on this function being called.
1055 + * @param ctx [in] The bigint session context.
1056 + * @param bim [in] The bigint modulus that will be used.
1057 + * @param mod_offset [in] There are three moduluii that can be stored - the
1058 + * standard modulus, and it's two primes p and q. This offset refers to which
1059 + * modulus we are referring to.
1060 + * @see bi_free_mod(), bi_mod_power().
1062 +void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset)
1064 + int k = bim->size;
1066 +// comp d = (comp)((long_comp)COMP_RADIX/(bim->comps[k-1]+1));
1067 + long_comp x = COMP_RADIX; do_div(x, bim->comps[k-1]+1); d = x;
1069 + ctx->bi_mod[mod_offset] = bim;
1070 + bi_permanent(ctx->bi_mod[mod_offset]);
1071 + ctx->bi_normalised_mod[mod_offset] = bi_int_multiply(ctx, bim, d);
1072 + bi_permanent(ctx->bi_normalised_mod[mod_offset]);
1076 + * @brief Used when cleaning various bigints at the end of a session.
1077 + * @param ctx [in] The bigint session context.
1078 + * @param mod_offset [in] The offset to use.
1079 + * @see bi_set_mod().
1081 +void bi_free_mod(BI_CTX *ctx, int mod_offset)
1083 + bi_depermanent(ctx->bi_mod[mod_offset]);
1084 + bi_free(ctx, ctx->bi_mod[mod_offset]);
1085 + bi_depermanent(ctx->bi_normalised_mod[mod_offset]);
1086 + bi_free(ctx, ctx->bi_normalised_mod[mod_offset]);
1090 + * Perform a standard multiplication between two bigints.
1092 +static bigint *regular_multiply(BI_CTX *ctx, bigint *bia, bigint *bib)
1094 + int i, j, i_plus_j, n = bia->size, t = bib->size;
1095 + bigint *biR = alloc(ctx, n + t);
1096 + comp *sr = biR->comps;
1097 + comp *sa = bia->comps;
1098 + comp *sb = bib->comps;
1103 + /* clear things to start with */
1104 + memset(biR->comps, 0, ((n+t)*COMP_BYTE_SIZE));
1116 + long_comp tmp = sr[i_plus_j] + (long_comp)sa[j]*b + carry;
1117 + sr[i_plus_j++] = (comp)tmp; /* downsize */
1118 + carry = (comp)(tmp >> COMP_BIT_SIZE);
1119 + } while (++j < n);
1121 + sr[i_plus_j] = carry;
1122 + } while (++i < t);
1124 + bi_free(ctx, bia);
1125 + bi_free(ctx, bib);
1130 + * @brief Perform a multiplication operation between two bigints.
1131 + * @param ctx [in] The bigint session context.
1132 + * @param bia [in] A bigint.
1133 + * @param bib [in] Another bigint.
1134 + * @return The result of the multiplication.
1136 +bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib)
1141 + return regular_multiply(ctx, bia, bib);
1146 + * @brief Compare two bigints.
1147 + * @param bia [in] A bigint.
1148 + * @param bib [in] Another bigint.
1149 + * @return -1 if smaller, 1 if larger and 0 if equal.
1151 +int bi_compare(bigint *bia, bigint *bib)
1158 + if (bia->size > bib->size)
1160 + else if (bia->size < bib->size)
1164 + comp *a = bia->comps;
1165 + comp *b = bib->comps;
1167 + /* Same number of components. Compare starting from the high end
1168 + * and working down. */
1170 + i = bia->size - 1;
1179 + else if (a[i] < b[i])
1184 + } while (--i >= 0);
1191 + * Allocate and zero more components. Does not consume bi.
1193 +static void more_comps(bigint *bi, int n)
1195 + if (n > bi->max_comps)
1197 + if ((bi->max_comps * 2) > n) {
1198 + bi->max_comps = bi->max_comps * 2;
1200 + bi->max_comps = n;
1202 + bi->comps = (comp*)realloc(bi->comps, bi->max_comps * COMP_BYTE_SIZE);
1207 + memset(&bi->comps[bi->size], 0, (n-bi->size)*COMP_BYTE_SIZE);
1214 + * Make a new empty bigint. It may just use an old one if one is available.
1215 + * Otherwise get one of the heap.
1217 +static bigint *alloc(BI_CTX *ctx, int size)
1221 + /* Can we recycle an old bigint? */
1222 + if (ctx->free_list != NULL)
1224 + biR = ctx->free_list;
1225 + ctx->free_list = biR->next;
1226 + ctx->free_count--;
1227 + if (biR->refs != 0)
1229 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1230 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1231 + printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1235 + more_comps(biR, size);
1239 + /* No free bigints available - create a new one. */
1240 + biR = (bigint *)malloc(sizeof(bigint));
1241 + biR->comps = (comp*) malloc(size * COMP_BYTE_SIZE);
1242 + biR->max_comps = size; /* give some space to spare */
1248 + ctx->active_count++;
1253 + * Work out the highest '1' bit in an exponent. Used when doing sliding-window
1256 +static int find_max_exp_index(bigint *biexp)
1258 + int i = COMP_BIT_SIZE-1;
1259 + comp shift = COMP_RADIX/2;
1260 + comp test = biexp->comps[biexp->size-1]; /* assume no leading zeroes */
1268 + return i+(biexp->size-1)*COMP_BIT_SIZE;
1272 + } while (--i != 0);
1274 + return -1; /* error - must have been a leading 0 */
1278 + * Is a particular bit is an exponent 1 or 0? Used when doing sliding-window
1281 +static int exp_bit_is_one(bigint *biexp, int offset)
1283 + comp test = biexp->comps[offset / COMP_BIT_SIZE];
1284 + int num_shifts = offset % COMP_BIT_SIZE;
1290 + for (i = 0; i < num_shifts; i++)
1295 + return test & shift;
1299 + * Delete any leading 0's (and allow for 0).
1301 +static bigint *trim(bigint *bi)
1305 + while (bi->comps[bi->size-1] == 0 && bi->size > 1)
1314 + * @brief Perform a modular exponentiation.
1316 + * This function requires bi_set_mod() to have been called previously. This is
1317 + * one of the optimisations used for performance.
1318 + * @param ctx [in] The bigint session context.
1319 + * @param bi [in] The bigint on which to perform the mod power operation.
1320 + * @param biexp [in] The bigint exponent.
1321 + * @see bi_set_mod().
1323 +bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp)
1325 + int i = find_max_exp_index(biexp), j, window_size = 1;
1326 + bigint *biR = int_to_bi(ctx, 1);
1331 + ctx->g = (bigint **)malloc(sizeof(bigint *));
1332 + ctx->g[0] = bi_clone(ctx, bi);
1334 + bi_permanent(ctx->g[0]);
1336 + /* if sliding-window is off, then only one bit will be done at a time and
1337 + * will reduce to standard left-to-right exponentiation */
1340 + if (exp_bit_is_one(biexp, i))
1342 + int l = i-window_size+1;
1345 + if (l < 0) /* LSB of exponent will always be 1 */
1351 + while (exp_bit_is_one(biexp, l) == 0)
1353 + l++; /* go back up */
1357 + /* build up the section of the exponent */
1358 + for (j = i; j >= l; j--)
1360 + biR = bi_residue(ctx, bi_square(ctx, biR));
1361 + if (exp_bit_is_one(biexp, j))
1368 + part_exp = (part_exp-1)/2; /* adjust for array */
1369 + biR = bi_residue(ctx,
1370 + bi_multiply(ctx, biR, ctx->g[part_exp]));
1373 + else /* square it */
1375 + biR = bi_residue(ctx, bi_square(ctx, biR));
1381 + for (i = 0; i < ctx->window; i++)
1383 + bi_depermanent(ctx->g[i]);
1384 + bi_free(ctx, ctx->g[i]);
1389 + bi_free(ctx, biexp);
1395 +++ b/net/rsa/bigint.h
1398 + * Copyright(C) 2006
1400 + * This library is free software; you can redistribute it and/or modify
1401 + * it under the terms of the GNU Lesser General Public License as published by
1402 + * the Free Software Foundation; either version 2 of the License, or
1403 + * (at your option) any later version.
1405 + * This library is distributed in the hope that it will be useful,
1406 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1407 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1408 + * GNU Lesser General Public License for more details.
1410 + * You should have received a copy of the GNU Lesser General Public License
1411 + * along with this library; if not, write to the Free Software
1412 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1414 + * Trimmed down from axTLS
1416 + * $Id: bigint.h 392 2007-06-25 16:24:51Z pablo.martin $
1420 +#ifndef BIGINT_HEADER
1421 +#define BIGINT_HEADER
1423 +#define CONFIG_BIGINT_CLASSICAL 1
1425 +#define SOCKET_READ(A,B,C) read(A,B,C)
1426 +#define SOCKET_WRITE(A,B,C) write(A,B,C)
1427 +#define SOCKET_CLOSE(A) close(A)
1428 +#define TTY_FLUSH()
1430 +#include "bigint_impl.h"
1432 +#ifndef CONFIG_BIGINT_CHECK_ON
1433 +#define check(A) /**< disappears in normal production mode */
1435 +BI_CTX *bi_initialize(void);
1436 +void bi_terminate(BI_CTX *ctx);
1437 +void bi_permanent(bigint *bi);
1438 +void bi_depermanent(bigint *bi);
1439 +void bi_free(BI_CTX *ctx, bigint *bi);
1440 +bigint *bi_copy(bigint *bi);
1441 +bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
1442 +void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size);
1443 +bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len);
1444 +bigint *int_to_bi(BI_CTX *ctx, comp i);
1446 +/* the functions that actually do something interesting */
1447 +bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
1448 +bigint *bi_subtract(BI_CTX *ctx, bigint *bia,
1449 + bigint *bib, int *is_negative);
1450 +bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod);
1451 +bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
1452 +bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
1453 +bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp);
1454 +int bi_compare(bigint *bia, bigint *bib);
1455 +void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
1456 +void bi_free_mod(BI_CTX *ctx, int mod_offset);
1460 + * Find the residue of B. bi_set_mod() must be called before hand.
1462 +#define bi_mod(A, B) bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)
1464 +#define bi_residue(A, B) bi_mod(A, B)
1466 +#define bi_square(A, B) bi_multiply(A, bi_copy(B), B)
1471 +++ b/net/rsa/bigint_impl.h
1474 + * Copyright(C) 2006
1476 + * This library is free software; you can redistribute it and/or modify
1477 + * it under the terms of the GNU Lesser General Public License as published by
1478 + * the Free Software Foundation; either version 2.1 of the License, or
1479 + * (at your option) any later version.
1481 + * This library is distributed in the hope that it will be useful,
1482 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1483 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1484 + * GNU Lesser General Public License for more details.
1486 + * You should have received a copy of the GNU Lesser General Public License
1487 + * along with this library; if not, write to the Free Software
1488 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1491 +#ifndef BIGINT_IMPL_HEADER
1492 +#define BIGINT_IMPL_HEADER
1494 +#include <linux/types.h>
1495 +#include <common.h>
1497 +/* Maintain a number of precomputed variables when doing reduction */
1498 +#define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */
1499 +#ifdef CONFIG_BIGINT_CRT
1500 +#define BIGINT_P_OFFSET 1 /**< p modulo offset. */
1501 +#define BIGINT_Q_OFFSET 2 /**< q module offset. */
1502 +#define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */
1504 +#define BIGINT_NUM_MODS 1
1507 +/* Architecture specific functions for big ints */
1509 +// #define COMP_RADIX 4294967296i64
1510 +// #define COMP_BIG_MSB 0x8000000000000000i64
1512 +#define COMP_RADIX 4294967296ULL /**< Max component + 1 */
1513 +#define COMP_BIG_MSB 0x8000000000000000ULL /**< (Max dbl comp + 1)/ 2 */
1515 +#define COMP_BIT_SIZE 32 /**< Number of bits in a component. */
1516 +#define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */
1517 +#define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */
1519 +typedef uint32_t comp; /**< A single precision component. */
1520 +typedef uint64_t long_comp; /**< A double precision component. */
1521 +typedef int64_t slong_comp; /**< A signed double precision component. */
1525 + * @brief A big integer basic object
1529 + struct _bigint* next; /**< The next bigint in the cache. */
1530 + short size; /**< The number of components in this bigint. */
1531 + short max_comps; /**< The heapsize allocated for this bigint */
1532 + int refs; /**< An internal reference count. */
1533 + comp* comps; /**< A ptr to the actual component data */
1536 +typedef struct _bigint bigint; /**< An alias for _bigint */
1539 + * Maintains the state of the cache, and a number of variables used in
1542 +typedef struct /**< A big integer "session" context. */
1544 + bigint *active_list; /**< Bigints currently used. */
1545 + bigint *free_list; /**< Bigints not used. */
1546 + bigint *bi_radix; /**< The radix used. */
1547 + bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */
1549 +#if defined(CONFIG_BIGINT_MONTGOMERY)
1550 + bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */
1551 + bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */
1552 + comp N0_dash[BIGINT_NUM_MODS];
1553 +#elif defined(CONFIG_BIGINT_BARRETT)
1554 + bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */
1556 + bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */
1557 + bigint **g; /**< Used by sliding-window. */
1558 + int window; /**< The size of the sliding window */
1560 + int active_count; /**< Number of active bigints. */
1561 + int free_count; /**< Number of free bigints. */
1563 +#ifdef CONFIG_BIGINT_MONTGOMERY
1564 + uint8_t use_classical; /**< Use classical reduction. */
1566 + uint8_t mod_offset; /**< The mod offset we are using */
1570 +#define max(a,b) ((a)>(b)?(a):(b)) /**< Find the maximum of 2 numbers. */
1571 +#define min(a,b) ((a)<(b)?(a):(b)) /**< Find the minimum of 2 numbers. */
1574 +#define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */
1576 +#define V1 v->comps[v->size-1] /**< v1 for division */
1577 +#define V2 v->comps[v->size-2] /**< v2 for division */
1578 +#define U(j) tmp_u->comps[tmp_u->size-j-1] /**< uj for division */
1579 +#define Q(j) quotient->comps[quotient->size-j-1] /**< qj for division */
1583 +++ b/net/rsa/div64.h
1586 + * Copyright (C) 2000, 2004 Maciej W. Rozycki
1587 + * Copyright (C) 2003 Ralf Baechle
1589 + * This file is subject to the terms and conditions of the GNU General Public
1590 + * License. See the file "COPYING" in the main directory of this archive
1591 + * for more details.
1593 +#ifndef _ASM_DIV64_H
1594 +#define _ASM_DIV64_H
1596 +#if (_MIPS_SZLONG == 32)
1598 +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
1599 +#define GCC_REG_ACCUM "$0"
1601 +#define GCC_REG_ACCUM "accum"
1605 +//#include <asm/compiler.h>
1608 + * No traps on overflows for any of these...
1611 +#define do_div64_32(res, high, low, base) ({ \
1612 + unsigned long __quot, __mod; \
1613 + unsigned long __cf, __tmp, __tmp2, __i; \
1615 + __asm__(".set push\n\t" \
1617 + ".set noreorder\n\t" \
1618 + "move %2, $0\n\t" \
1619 + "move %3, $0\n\t" \
1621 + " li %4, 0x21\n" \
1623 + "sll $1, %0, 0x1\n\t" \
1624 + "srl %3, %0, 0x1f\n\t" \
1625 + "or %0, $1, %5\n\t" \
1626 + "sll %1, %1, 0x1\n\t" \
1627 + "sll %2, %2, 0x1\n" \
1629 + "bnez %3, 2f\n\t" \
1630 + " sltu %5, %0, %z6\n\t" \
1633 + " addiu %4, %4, -1\n\t" \
1634 + "subu %0, %0, %z6\n\t" \
1635 + "addiu %2, %2, 1\n" \
1637 + "bnez %4, 0b\n\t" \
1638 + " srl %5, %1, 0x1f\n\t" \
1640 + : "=&r" (__mod), "=&r" (__tmp), "=&r" (__quot), "=&r" (__cf), \
1641 + "=&r" (__i), "=&r" (__tmp2) \
1642 + : "Jr" (base), "0" (high), "1" (low)); \
1647 +#define do_div(n, base) ({ \
1648 + unsigned long long __quot; \
1649 + unsigned long __mod; \
1650 + unsigned long long __div; \
1651 + unsigned long __upper, __low, __high, __base; \
1654 + __base = (base); \
1656 + __high = __div >> 32; \
1658 + __upper = __high; \
1661 + __asm__("divu $0, %z2, %z3" \
1662 + : "=h" (__upper), "=l" (__high) \
1663 + : "Jr" (__high), "Jr" (__base) \
1664 + : GCC_REG_ACCUM); \
1666 + __mod = do_div64_32(__low, __upper, __low, __base); \
1668 + __quot = __high; \
1669 + __quot = __quot << 32 | __low; \
1672 +#endif /* (_MIPS_SZLONG == 32) */
1674 +#if (_MIPS_SZLONG == 64)
1677 + * Hey, we're already 64-bit, no
1678 + * need to play games..
1680 +#define do_div(n, base) ({ \
1681 + unsigned long __quot; \
1682 + unsigned int __mod; \
1683 + unsigned long __div; \
1684 + unsigned int __base; \
1687 + __base = (base); \
1689 + __mod = __div % __base; \
1690 + __quot = __div / __base; \
1695 +#endif /* (_MIPS_SZLONG == 64) */
1697 +#endif /* _ASM_DIV64_H */
1699 +++ b/net/rsa/dump_key.c
1702 +#include <stdlib.h>
1703 +#include <sys/types.h>
1704 +#include <sys/stat.h>
1705 +#include <unistd.h>
1707 +int main(int argc, char **argv)
1709 + FILE *fp = fopen("public_fon_rsa_key_6.pem", "r");
1715 + stat("public_fon_rsa_key_6.pem", &s);
1716 + b = malloc(s.st_size);
1717 + fread(b, s.st_size, 1, fp);
1719 + printf("unsigned char public_key[] = {\n\t");
1720 + for(i = 0;i < s.st_size; i++)
1722 + printf("0x%02X,", b[i]);
1727 +// printf("\n%d %d\n", i, s.st_size);
1731 +++ b/net/rsa/foncheckrsa.c
1734 + * RSA + RIPEMD160 signature verification command
1736 + * Copyright (C) 2007 FON Wireless Ltd.
1738 + * This program is free software; you can redistribute it and/or
1739 + * modify it under the terms of the GNU General Public License
1740 + * as published by the Free Software Foundation; either version 2
1741 + * of the License, or (at your option) any later version.
1743 + * This program is distributed in the hope that it will be useful,
1744 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1745 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1746 + * GNU General Public License for more details.
1748 + * You should have received a copy of the GNU General Public License
1749 + * along with this program; if not, write to the Free Software
1750 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1752 + * Created: 20060728 Pablo MartÃn Medrano <pablo@fon.com>
1754 + * $Id: foncheckrsa.c 332 2007-05-02 09:45:54Z pablo.martin $
1757 +#include <stdlib.h>
1758 +#include <sys/types.h>
1759 +#include <sys/stat.h>
1761 +#include <string.h>
1763 +#include <unistd.h>
1764 +#include "fonrsa.h"
1766 +int main(int argc, char **argv)
1769 + FONRSA_ERROR fonrsa_error;
1773 + fprintf(stderr, " Usage: foncheckrsa [public_key.der] [signature] [file]\n");
1774 + fprintf(stderr, " Pablo MartÃn Medrano <pablo@fon.com>\n");
1775 + fprintf(stderr, " RIPEMD-160 software written by Antoon Bosselaers,\n");
1776 + fprintf(stderr, " available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.\n");
1777 + fprintf(stderr, " Uses the axTLS library bigint implementation (libfonrsa)\n");
1778 + fprintf(stderr, " http://www.leroc.com.au/axTLS/\n");
1781 + /* Check the existence of input files */
1782 + for (i = 1; i < 4; i++) {
1783 + if ((fd = open(argv[i], O_RDONLY)) == -1) {
1784 + fprintf(stderr, "Error: opening \"%s\": %s\n", argv[i],
1786 + fprintf(stderr, "Bailing out...");
1791 + handle = FR_init(argv[1]);
1792 + if (handle == NULL) {
1793 + printf("Error loading keys in %s\n", argv[1]);
1796 + fonrsa_error = FR_verify_file(handle, argv[3], argv[2]);
1798 + switch (fonrsa_error) {
1800 + printf("Verified OK\n");
1802 + case FONRSA_VERIFICATION_FAILURE:
1803 + printf("Verification failure\n");
1806 + printf("Verification error\n");
1813 +++ b/net/rsa/fonrsa.c
1816 + * FONSM RSA handling library
1818 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
1820 + * This library is free software; you can redistribute it and/or modify
1821 + * it under the terms of the GNU Lesser General Public License as published by
1822 + * the Free Software Foundation; either version 2 of the License, or
1823 + * (at your option) any later version.
1825 + * This library is distributed in the hope that it will be useful,
1826 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1827 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1828 + * GNU Lesser General Public License for more details.
1830 + * You should have received a copy of the GNU Lesser General Public License
1831 + * along with this library; if not, write to the Free Software
1832 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1834 + * Created: 20070306 Pablo Martin Medrano <pablo@fon.com>
1838 + * $Id: fonrsa.c 405 2007-09-19 15:26:17Z jesus.pico $
1840 +#include <sys/types.h>
1841 +#include <stdlib.h>
1843 +#include <string.h>
1844 +#include <sys/types.h>
1845 +#include <sys/stat.h>
1847 +#include <unistd.h>
1849 +#include "rmd160.h"
1850 +#include "bigint.h"
1851 +#include "fonrsa.h"
1852 +#include "base64.h"
1860 + bigint *m; /* modulus */
1861 + bigint *e; /* public exponent */
1863 + BI_CTX *bi_ctx; /* big integer handle */
1868 + RSA_parameters *rsaparms;
1871 +static uint8_t *CH_load_raw_file(char *filename, size_t *size);
1872 +static DER_key *CH_load_der_key(char *filename);
1873 +static void CH_free_der_key(DER_key *key);
1874 +static int asn1_get_public_key(const uint8_t *buf, int len, RSA_parameters **rsa_parameters);
1875 +void CH_pub_key_new(RSA_parameters **rsa_parameters, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len);
1876 +int CH_decrypt(RSA_parameters *rsa, uint8_t *buffer_in, uint8_t *buffer_out);
1877 +byte *RMDbinary(char *fname);
1878 +int CH_get_rmd160_hash_from_signature(byte *hash, char *signature_file, char *public_key_file);
1879 +static unsigned char *load_file_in_buffer(char *path, int *size);
1880 +static int save_file_from_buffer(char *path, unsigned char *buffer, int size);
1881 +int ExtractPadding(uint8_t* OutputBuffer, uint8_t* InputBuffer, int LengthOfInputBuffer);
1883 +#define RMDsize 160 /* A RIPEMD-160 hash has 160 bits */
1886 + * returns RMD(message in file fname) fname is read as binary data.
1889 +byte *RMDbinary(char *fname)
1891 + FILE *mf; /* pointer to file <fname> */
1892 + byte data[1024]; /* contains current mess. block */
1893 + dword nbytes; /* length of this block */
1894 + dword MDbuf[RMDsize / 32]; /* contains (A, B, C, D(, E)) */
1895 + static byte hashcode[RMDsize / 8]; /* for final hash-value */
1896 + dword X[16]; /* current 16-word chunk */
1897 + unsigned int i, j; /* counters */
1898 + dword length[2]; /* length in bytes of message */
1899 + dword offset; /* # of unprocessed bytes at */
1900 + /* call of MDfinish */
1903 + if ((mf = fopen(fname, "rb")) == NULL) {
1904 + fprintf(stderr, "\nRMDbinary: cannot open file \"%s\".\n",
1911 + while ((nbytes = fread(data, 1, 1024, mf)) != 0) {
1912 + /* process all complete blocks */
1913 + for (i = 0; i < (nbytes >> 6); i++) {
1914 + for (j = 0; j < 16; j++)
1915 + X[j] = BYTES_TO_DWORD(data + 64 * i + 4 * j);
1916 + compress(MDbuf, X);
1918 + /* update length[] */
1919 + if (length[0] + nbytes < length[0])
1920 + length[1]++; /* overflow to msb of length */
1921 + length[0] += nbytes;
1924 + offset = length[0] & 0x3C0; /* extract bytes 6 to 10 inclusive */
1925 + MDfinish(MDbuf, data + offset, length[0], length[1]);
1927 + for (i = 0; i < RMDsize / 8; i += 4) {
1928 + hashcode[i] = MDbuf[i >> 2];
1929 + hashcode[i + 1] = (MDbuf[i >> 2] >> 8);
1930 + hashcode[i + 2] = (MDbuf[i >> 2] >> 16);
1931 + hashcode[i + 3] = (MDbuf[i >> 2] >> 24);
1935 + return (byte *) hashcode;
1937 +byte *RMDbinary_buffer(char *buffer, int size_buffer)
1943 + * Extracts the RMD 160 hash from the signature file
1945 +int CH_get_rmd160_hash_from_signature(byte *hash, char *signature_file, char *public_key_file)
1947 + RSA_parameters *rsa_parameters;
1949 + uint8_t *signature;
1950 + size_t signature_size;
1951 + uint8_t *decrypted;
1953 + signature = CH_load_raw_file(signature_file, &signature_size);
1954 + if ((signature == NULL)||(signature_size != 512)) {
1955 + fprintf(stderr, "Error: Loading signature key '%s'\n", signature_file);
1958 + derkey = CH_load_der_key(public_key_file);
1959 + if (derkey == NULL) {
1960 + fprintf(stderr, "Error: opening DER key file '%s'\n", public_key_file);
1963 + if ((asn1_get_public_key(derkey->buffer, derkey->size, &rsa_parameters)) != 0) {
1964 + fprintf(stderr, "Error: Extracting public key from DER file\n");
1967 + CH_free_der_key(derkey);
1968 + if (rsa_parameters->num_octets != 512)
1969 + fprintf(stderr, "Error: The RSA public key size is not 4096 bits %d\n", rsa_parameters->num_octets);
1970 + decrypted = (uint8_t *)malloc(rsa_parameters->num_octets);
1971 + if (CH_decrypt(rsa_parameters, signature, decrypted)) {
1972 + fprintf(stderr, "Error: Decrypting signature\n");
1975 + memcpy(hash, decrypted + 492, 20);
1982 + * Decrypts the signature buffer using the rsa public key loaded
1984 +int CH_decrypt(RSA_parameters *rsa, uint8_t *buffer_in, uint8_t *buffer_out)
1987 + bigint *decrypted_bi;
1990 + byte_size = rsa->num_octets;
1991 + dat_bi = bi_import(rsa->bi_ctx, buffer_in, byte_size);
1992 + rsa->bi_ctx->mod_offset = BIGINT_M_OFFSET;
1994 + decrypted_bi = bi_mod_power(rsa->bi_ctx, dat_bi, rsa->e);
1995 + bi_export(rsa->bi_ctx, decrypted_bi, buffer_out, byte_size);
1999 + * Loads a file in a uint8_t buffer
2001 +static uint8_t *CH_load_raw_file(char *filename, size_t *size)
2008 + if ((stat(filename, &st)) == -1)
2010 + if ((ret = (uint8_t *)malloc(st.st_size)) == NULL)
2012 + fd = open(filename, O_RDONLY);
2017 + br = read(fd, ret, st.st_size);
2019 + if (br != st.st_size) {
2023 + *size = st.st_size;
2027 + * Loads a .der file in a buffer
2029 +static DER_key *CH_load_der_key(char *filename)
2033 + if ((ret = (DER_key *)malloc(sizeof(DER_key))) == NULL)
2035 + if ((ret->buffer = CH_load_raw_file(filename, &(ret->size))) == NULL) {
2044 +static DER_key *CH_load_pem_key(char *filename)
2053 + if ((ret = (DER_key *)malloc(sizeof(DER_key))) == NULL)
2055 + if ((buffer = CH_load_raw_file(filename, &filesize)) == NULL) {
2059 + p = (char *)buffer;
2060 + while ((*p != '\n') && (*p != '\0'))
2068 + while((p - b64) <= filesize) {
2069 + if ((*p == '-')) {
2071 + } else if ((*p != '\n') && (*p != ' ') && (*p != '\t')) {
2078 + size = B64_decode(b64, key, strlen(b64), 1024);
2085 + ret->buffer = (char *)malloc(size);
2087 + memcpy((void *)ret->buffer, (void *)key, size);
2094 +static void CH_free_der_key(DER_key *key)
2096 + free(key->buffer);
2101 + * Get the public key specifics from an ASN.1 encoded file
2102 + * A function lacking in the exported axTLS API
2104 + * This is a really weird hack that only works with RSA public key
2107 +static int asn1_get_public_key(const uint8_t *buf, int len, RSA_parameters **rsa_parameters)
2109 + uint8_t *modulus, *pub_exp;
2110 + int mod_len, pub_len;
2113 + mod_len = len - 37;
2114 + if (buf[0] != 0x30) {
2118 + pub_exp = (uint8_t *)malloc(3);
2119 + modulus = (uint8_t *)malloc(mod_len);
2120 + memcpy(modulus, buf + 32 , mod_len);
2121 + memcpy(pub_exp, buf + 34 + mod_len, 3);
2122 + if (mod_len <= 0 || pub_len <= 0 )
2124 + CH_pub_key_new(rsa_parameters, modulus, mod_len, pub_exp, pub_len);
2132 + * Similar to RSA_pub_key_new, rewritten to make this program depend only on bi module
2134 +void CH_pub_key_new(RSA_parameters **rsa, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len)
2136 + RSA_parameters *rsa_parameters;
2138 + BI_CTX *bi_ctx = bi_initialize();
2139 + *rsa = (RSA_parameters *)calloc(1, sizeof(RSA_parameters));
2140 + rsa_parameters = *rsa;
2141 + rsa_parameters->bi_ctx = bi_ctx;
2142 + rsa_parameters->num_octets = (mod_len & 0xFFF0);
2143 + rsa_parameters->m = bi_import(bi_ctx, modulus, mod_len);
2144 + bi_set_mod(bi_ctx, rsa_parameters->m, BIGINT_M_OFFSET);
2145 + rsa_parameters->e = bi_import(bi_ctx, pub_exp, pub_len);
2146 + bi_permanent(rsa_parameters->e);
2149 +static unsigned char *load_file_in_buffer(char *path, int *size)
2151 + unsigned char *buffer;
2155 + if (stat(path, &st))
2157 + buffer = (unsigned char *)malloc(st.st_size);
2158 + if (buffer == NULL)
2160 + if ((fd = open(path, O_RDONLY)) == -1) {
2164 + if (read(fd, (void *)buffer,st.st_size) != (ssize_t)st.st_size) {
2169 + *size = (int)st.st_size;
2174 +static int save_file_from_buffer(char *path, unsigned char *buffer, int size)
2178 + if ((fd = open(path, O_WRONLY | O_CREAT, 0644)) == -1)
2180 + if (write(fd, buffer, (size_t)size) != ((ssize_t)size)) {
2189 +void *FR_init(char *public_key_path)
2192 + RSA_parameters *rsa_parameters;
2195 + ending = public_key_path + strlen(public_key_path) - 3;
2196 + if (!strcmp(ending, "der"))
2197 + derkey = CH_load_der_key(public_key_path);
2198 + else if (!strcmp(ending, "pem"))
2199 + derkey = CH_load_pem_key(public_key_path);
2201 + fprintf(stderr, "Error: unknown key format\n");
2204 + if (derkey == NULL) {
2205 + fprintf(stderr, "Error: opening key file '%s'\n", public_key_path);
2208 + if ((asn1_get_public_key(derkey->buffer, derkey->size, &rsa_parameters)) != 0) {
2209 + fprintf(stderr, "Error: Extracting public key from file\n");
2212 + CH_free_der_key(derkey);
2213 + return (void *)rsa_parameters;
2217 +FONRSA_ERROR FR_end(void *handle)
2219 + RSA_parameters *rsa_parameters = (RSA_parameters *)handle;
2221 + free(rsa_parameters);
2225 +/* FR_decrypt_buffer */
2226 +FONRSA_ERROR FR_decrypt_buffer(void *handler, unsigned char *cryptext, int cryptext_size,
2227 + unsigned char *plaintext, int plaintext_buffer_size, int *plaintext_size)
2229 + RSA_parameters *rsaparms = (RSA_parameters *)handler;
2231 + if (cryptext_size != rsaparms->num_octets) {
2232 + return FONRSA_SIZE;
2234 + if (plaintext_buffer_size < cryptext_size) {
2235 + return FONRSA_SIZE;
2237 + if (CH_decrypt(rsaparms, (uint8_t *)cryptext, (uint8_t *)plaintext)) {
2238 + return FONRSA_DECRYPT;
2240 + *plaintext_size = cryptext_size;
2244 +FONRSA_ERROR FR_decrypt_buffer_v2(void *handler, unsigned char *cryptext, int cryptext_size,
2245 + unsigned char *plaintext, int plaintext_buffer_size, int *plaintext_size)
2247 + unsigned char* AuxBuffer;
2250 + AuxBuffer = (unsigned char*)malloc(cryptext_size);
2252 + RSA_parameters *rsaparms = (RSA_parameters *)handler;
2254 + if (cryptext_size != rsaparms->num_octets) {
2255 + return FONRSA_SIZE;
2257 + if (plaintext_buffer_size < cryptext_size) {
2258 + return FONRSA_SIZE;
2260 + if (CH_decrypt(rsaparms, (uint8_t *)cryptext, (uint8_t *)AuxBuffer)) {
2261 + return FONRSA_DECRYPT;
2263 + if ((AuxSize = ExtractPadding((uint8_t*)plaintext, (uint8_t*)AuxBuffer, cryptext_size)) < 0)
2265 + printf("Incorrect Padding decrypting buffer");
2266 + return FONRSA_DECRYPT;
2268 + *plaintext_size = AuxSize;
2274 + * Implementation of PKCS 1.5 padding, borrowed from
2275 + * Tom's code (public domain)
2278 +/* Standalone FR_verify_file */
2279 +FONRSA_ERROR FR_verify_file(void *handler, char *file_path, char *signature_file_path)
2284 + uint8_t *decrypted;
2285 + RSA_parameters *rsa_parameters = (RSA_parameters *)handler;
2286 + char *signature_buffer;
2287 + int signature_size;
2289 + /* Calculates the RIPEMD-160 hash of the file */
2290 + hashcode = RMDbinary (file_path);
2291 + /* Decrypts the signature file using the RSA public key */
2292 + signature_buffer = load_file_in_buffer(signature_file_path, &signature_size);
2293 + if (signature_buffer == NULL)
2294 + return FONRSA_OPENKEY;
2296 + if (rsa_parameters->num_octets != signature_size)
2297 + return FONRSA_SIZE;
2298 + decrypted = (uint8_t *)malloc(rsa_parameters->num_octets);
2299 + if (CH_decrypt(rsa_parameters, signature_buffer, decrypted)) {
2300 + fprintf(stderr, "Error: Decrypting signature\n");
2303 + memcpy(hash, decrypted + 492, 20);
2305 + free(signature_buffer);
2306 + for (j = 0; j < RMDsize/8; j++) {
2307 + if (hash[j] != hashcode[j])
2308 + return FONRSA_VERIFICATION_FAILURE;
2313 +/* FR_decrypt_file */
2314 +FONRSA_ERROR FR_decrypt_file(void *handle, char *crypted_file_path, char *plaintext_file_path)
2319 + char crypted[1024];
2322 + if ((filebuffer = load_file_in_buffer(crypted_file_path, &size)) == NULL) {
2323 + return FONRSA_LOADFILE;
2326 + ret = FR_decrypt_buffer(handle, filebuffer, size, crypted, 1024, &crypted_size);
2327 + if (ret != FONRSA_OK) {
2333 + if (save_file_from_buffer(plaintext_file_path, crypted, crypted_size)) {
2334 + printf("Error writing %lu bytes into %s", crypted_size, plaintext_file_path);
2335 + return FONRSA_SAVEFILE;
2340 +int ExtractPadding(uint8_t* OutputBuffer, uint8_t* InputBuffer, int LengthOfInputBuffer)
2344 + //First typical checks...
2345 + if (LengthOfInputBuffer < MINIMUM_PADING_BYTES_PKCS_1_5)
2347 + fprintf(stderr, "Error:ExtractPadding: Error, Length of input buffer is too short.\n");
2350 + else if((InputBuffer[0] != 0) || (InputBuffer[1] > 2)) //Necessary header of Padding...
2352 + fprintf(stderr, "Error:ExtractPadding: Error, Padding header is incorrect.\n");
2355 + for (i=2; i < LengthOfInputBuffer; i++) //Variable size of non-zero padding....
2357 + if (InputBuffer[i] == 0) break; //This is the end of Padding.
2359 + //We need to evaluate if there is an existing message...
2360 + if (i < LengthOfInputBuffer - 2)
2361 + {//Ok, Padding is extracted... copying the message and finishing...
2362 + memcpy(OutputBuffer, &(InputBuffer[i + 1]), LengthOfInputBuffer - (i + 1));
2363 + return LengthOfInputBuffer - (i + 1);
2365 + //If we have reached to this point, then an error has occurred...
2369 +#ifdef __MAINTEST__
2370 +int main(int argc, char **argv)
2372 + void *handle = NULL;
2374 + char *filebuffer = NULL;
2375 + char crypted[1024];
2376 + int size, crypted_size;
2379 + printf("Usage: %s <key_file> <crypted_file> <output_file>\n", argv[0]);
2383 + handle = FR_init(argv[1]);
2384 + if (handle == NULL) {
2385 + printf("Error loading keys\n");
2388 + ret = FR_decrypt_file(handle, argv[2], argv[3]);
2389 + if (ret != FONRSA_OK) {
2390 + printf("FR_decrypt_file returns %d\n", ret);
2400 +++ b/net/rsa/fonrsa.h
2403 + * FONSM RSA handling library, used by fonsmcd and foncheckrsa
2405 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
2407 + * This library is free software; you can redistribute it and/or modify
2408 + * it under the terms of the GNU Lesser General Public License as published by
2409 + * the Free Software Foundation; either version 2 of the License, or
2410 + * (at your option) any later version.
2412 + * This library is distributed in the hope that it will be useful,
2413 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2414 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2415 + * GNU Lesser General Public License for more details.
2417 + * You should have received a copy of the GNU Lesser General Public License
2418 + * along with this library; if not, write to the Free Software
2419 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2421 + * Created: 20070306 Pablo Martin Medrano <pablo@fon.com>
2423 + * $Id: fonrsa.h 404 2007-09-17 10:41:31Z jesus.pico $
2428 +#define MINIMUM_PADING_BYTES_PKCS_1_5 3
2432 + FONRSA_VERIFICATION_FAILURE = 1,
2433 + FONRSA_OPENKEY = 2,
2435 + FONRSA_LOADFILE = 4,
2437 + FONRSA_DECRYPT = 6,
2438 + FONRSA_SAVEFILE = 7,
2443 +void *FR_init(char *public_key_path);
2444 +FONRSA_ERROR FR_end(void *handle);
2445 +FONRSA_ERROR FR_decrypt_buffer(void *handler, unsigned char *cryptext, int cryptext_size,
2446 + unsigned char *plaintext, int plaintext_buffer_size, int *plaintext_size);
2447 +FONRSA_ERROR FR_decrypt_buffer_v2(void *handler, unsigned char *cryptext, int cryptext_size,
2448 + unsigned char *plaintext, int plaintext_buffer_size, int *plaintext_size);
2449 +FONRSA_ERROR FR_verify_file(void *handler, char *file_path, char *signature_file_path);
2450 +FONRSA_ERROR FR_decrypt_file(void *handler, char *crypted_file_path, char *plaintext_file_path);
2459 + * Fonsm log module. Used inside the fonsm backend module and on the client.
2461 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
2463 + * Created: 20070202 Pablo Martin Medrano <pablo@fon.com>
2465 + * $Id: log.c 392 2007-06-25 16:24:51Z pablo.martin $
2468 +#include <string.h>
2469 +#include <stdlib.h>
2470 +#include <stdarg.h>
2471 +#include <unistd.h>
2473 +#include <syslog.h>
2477 +#define MAX_SESSION_PREFIX 256
2481 + LG_LEVEL watermark;
2483 + LG_log_function_pointer function;
2486 +static ST_fslog fslog;
2487 +static void LG_log_string(LG_LEVEL level, const char *message);
2490 + \brief Starts the log subsystem, redirecting glog() to stderr/syslog depending
2492 + \retval FSLOG_ERROR : FSLOG_OK if everything goes well
2493 + \param lg : handle that will be returned
2494 + \param ident : program identifier, any string
2495 + \param low_watermark : if the log level is less than this value, it will not be logged
2496 + \param mode : FSLOG_MODE_SYSLOG (log to syslog) or FSLOG_MODE_STDERR (log to stderr)
2498 +FSLOG_ERROR LG_start(const char *domain, LG_LEVEL watermark, int mode,
2499 + LG_log_function_pointer log_function, int facility)
2502 + strncpy(fslog.domain, domain, MAX_LG_DOMAIN);
2503 + fslog.domain[MAX_LG_DOMAIN - 1] = '\0';
2504 + fslog.watermark = watermark;
2505 + fslog.mode = mode;
2506 + fslog.function = log_function?log_function:LG_log_string;
2507 + if (fslog.mode & LG_SYSLOG)
2508 + openlog(domain, LOG_NDELAY, facility);
2516 + \brief Set the low watermark
2517 + \retval FSLOG_ERROR : FSLOG_OK
2518 + \param lg : log handle
2519 + \param low_watermark : new watermark
2521 +FSLOG_ERROR LG_set_loglevel(LG_LEVEL watermark)
2523 + fslog.watermark = watermark;
2528 + \brief Ends the log subsystem, unregisteing glog handle
2529 + \retval FSLOG_ERROR : FSLOG_OK if everything goes well
2530 + \param handle : log handle to free
2532 +FSLOG_ERROR LG_end(void)
2535 + if (fslog.mode & LG_SYSLOG)
2542 +void LG_log(LG_LEVEL loglevel, const char *message, ...)
2546 + char buffer[4096];
2549 + va_start(ap, message);
2550 + n = vsnprintf(buffer, MAX_LOG_STRING, message, ap);
2552 + if (n > -1 && n < MAX_LOG_STRING)
2553 + fslog.function(loglevel, buffer);
2555 + fon_critical("%s: Message too big to be logged", __FUNCTION__);
2561 +/* Default log function (when mode is LG_SYSLOG or LG_STDERR) */
2562 +static void LG_log_string(LG_LEVEL level, const char *message)
2568 + } fonlog_to_syslog[] = {
2569 + [LG_DEBUG] = {LOG_ERR, "DEBUG"},
2570 + [LG_MESSAGE] = {LOG_ERR, "MESSAGE"},
2571 + [LG_WARNING] = {LOG_ERR, "WARNING"},
2572 + [LG_CRITICAL] = {LOG_ERR, "CRITICAL"},
2573 + [LG_ERROR] = {LOG_ERR, "ERROR"}
2576 + if (level < fslog.watermark)
2578 + if (fslog.mode & LG_SYSLOG) {
2579 + if (level == LG_MESSAGE) {
2580 + syslog(LOG_INFO, "%s", message);
2582 + syslog(fonlog_to_syslog[level].syslog_level, "%s: %s", fonlog_to_syslog[level].log_string, message);
2585 + if (fslog.mode & LG_STDERR) {
2586 + fprintf(stderr, "%s[%d]: %8.8s: %s\n", fslog.domain,
2587 + getpid(), fonlog_to_syslog[level].log_string,
2600 + * Fonsm log module. Used inside the fonsm backend module and on the client.
2602 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
2604 + * Created: 20070202 Pablo Martin Medrano <pablo@fon.com>
2606 + * $Id: log.h 379 2007-05-28 09:17:48Z pablo.martin $
2615 +#include <stdarg.h>
2619 + FSLOG_UNKNOWN = -1
2623 +typedef void * LG_HANDLE;
2634 +#define LG_SYSLOG 0x01
2635 +#define LG_STDERR 0x02
2636 +#define LG_CUSTOM 0x04
2638 +#define MAX_LG_DOMAIN 256
2639 +#define MAX_LOG_STRING 4096
2643 +#define fon_debug(...) LG_log (LG_DEBUG, __VA_ARGS__)
2645 +#define fon_debug(...) { printf("DEBUG: "); printf(__VA_ARGS__); printf("\n"); }
2648 +/* fon_debug evaluates to nothing when NDEBUG is defined */
2649 +#define fon_debug(...)
2652 +#define fon_message(...) LG_log (LG_MESSAGE, __VA_ARGS__)
2653 +#define fon_warning(...) LG_log (LG_WARNING, __VA_ARGS__)
2654 +#define fon_critical(...) LG_log (LG_CRITICAL, __VA_ARGS__)
2655 +#define fon_error(...) LG_log (LG_ERROR, __VA_ARGS__)
2657 +#define fon_message(...) { printf("MESSAGE: "); printf(__VA_ARGS__); printf("\n"); }
2658 +#define fon_warning(...) { printf("WARNING: "); printf(__VA_ARGS__); printf("\n"); }
2659 +#define fon_critical(...) { printf("CRITICAL: "); printf(__VA_ARGS__); printf("\n"); }
2660 +#define fon_error(...) { printf("ERROR: "); printf(__VA_ARGS__); printf("\n"); }
2663 +typedef void (*LG_log_function_pointer)(LG_LEVEL level, const char *message);
2665 +FSLOG_ERROR LG_start(const char *domain, LG_LEVEL watermark, int mode, LG_log_function_pointer log_function, int facility);
2666 +FSLOG_ERROR LG_set_loglevel(LG_LEVEL watermark);
2667 +FSLOG_ERROR LG_end(void);
2668 +void LG_log(LG_LEVEL loglevel, const char *message, ...);
2677 +++ b/net/rsa/Makefile
2680 +# FONRSA & FONSIGN libraries unit testing
2682 +# This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
2684 +# Execute in this directory
2686 +# Created: 20070422 Pablo Martin Medrano <pablo@fon.com>
2688 +# $Id: Makefile 389 2007-06-11 08:29:56Z pablo.martin $
2690 +# FIXME: Put this in the main Makefile.am
2692 +all: fonsign dump_key
2695 + gcc -g sign_openssl.c -D__MAINTEST__ -o fonsign -lssl
2698 + gcc -o dump_key dump_key.c
2699 + ./dump_key > public_key.h
2702 + gcc -g bigint.c fonrsa.c rmd160.c foncheckrsa.c base64.c log.c -o foncheckrsa
2704 +#private_fon_rsa_key.pem:
2705 +# openssl genrsa -out private_fon_rsa_key.pem 4096
2706 +# openssl rsa -in private_fon_rsa_key.pem -pubout -out public_fon_rsa_key.pem
2709 + rm fonsign dump_key
2711 +++ b/net/rsa/public_key.h
2713 +unsigned char public_key[] = {
2714 + 0x2D,0x2D,0x2D,0x2D,0x2D,0x42,0x45,0x47,0x49,0x4E,0x20,0x50,0x55,0x42,0x4C,0x49,
2715 + 0x43,0x20,0x4B,0x45,0x59,0x2D,0x2D,0x2D,0x2D,0x2D,0x0A,0x4D,0x49,0x49,0x43,0x49,
2716 + 0x6A,0x41,0x4E,0x42,0x67,0x6B,0x71,0x68,0x6B,0x69,0x47,0x39,0x77,0x30,0x42,0x41,
2717 + 0x51,0x45,0x46,0x41,0x41,0x4F,0x43,0x41,0x67,0x38,0x41,0x4D,0x49,0x49,0x43,0x43,
2718 + 0x67,0x4B,0x43,0x41,0x67,0x45,0x41,0x34,0x4C,0x42,0x76,0x59,0x43,0x4B,0x38,0x38,
2719 + 0x6D,0x75,0x57,0x61,0x73,0x31,0x4F,0x53,0x73,0x71,0x30,0x0A,0x38,0x39,0x38,0x79,
2720 + 0x76,0x54,0x4B,0x71,0x41,0x6E,0x4F,0x37,0x78,0x2F,0x44,0x53,0x57,0x72,0x46,0x53,
2721 + 0x30,0x42,0x72,0x47,0x53,0x51,0x31,0x52,0x69,0x44,0x39,0x55,0x62,0x78,0x77,0x6F,
2722 + 0x64,0x76,0x36,0x65,0x51,0x4B,0x55,0x30,0x67,0x36,0x52,0x6B,0x2F,0x39,0x54,0x70,
2723 + 0x4C,0x6E,0x4F,0x2F,0x76,0x51,0x4B,0x70,0x69,0x41,0x30,0x30,0x0A,0x2B,0x32,0x59,
2724 + 0x30,0x74,0x6B,0x4C,0x39,0x73,0x6A,0x37,0x64,0x33,0x57,0x4B,0x47,0x39,0x62,0x6A,
2725 + 0x64,0x51,0x58,0x2F,0x43,0x49,0x35,0x57,0x46,0x42,0x42,0x64,0x77,0x57,0x73,0x74,
2726 + 0x4D,0x43,0x38,0x77,0x74,0x4C,0x6A,0x6A,0x45,0x59,0x79,0x43,0x58,0x46,0x32,0x31,
2727 + 0x30,0x39,0x7A,0x31,0x47,0x54,0x4C,0x73,0x53,0x44,0x34,0x57,0x4F,0x0A,0x45,0x50,
2728 + 0x6D,0x45,0x37,0x34,0x63,0x6E,0x6F,0x35,0x78,0x53,0x43,0x71,0x71,0x33,0x74,0x54,
2729 + 0x49,0x6D,0x38,0x50,0x78,0x49,0x77,0x54,0x46,0x6D,0x46,0x6F,0x6D,0x6A,0x76,0x31,
2730 + 0x4F,0x56,0x50,0x32,0x73,0x42,0x49,0x70,0x35,0x4E,0x2B,0x59,0x6F,0x56,0x61,0x53,
2731 + 0x58,0x6A,0x47,0x66,0x4E,0x63,0x54,0x36,0x4E,0x6B,0x39,0x76,0x6B,0x56,0x0A,0x57,
2732 + 0x69,0x67,0x39,0x30,0x71,0x50,0x4E,0x4C,0x58,0x6E,0x39,0x39,0x50,0x78,0x48,0x61,
2733 + 0x49,0x31,0x36,0x52,0x7A,0x78,0x48,0x4C,0x39,0x54,0x42,0x2B,0x50,0x43,0x33,0x68,
2734 + 0x33,0x61,0x58,0x33,0x71,0x57,0x30,0x4B,0x4C,0x4A,0x41,0x66,0x6F,0x35,0x70,0x48,
2735 + 0x6C,0x39,0x79,0x75,0x55,0x70,0x37,0x66,0x46,0x65,0x6A,0x4A,0x2B,0x41,0x58,0x0A,
2736 + 0x51,0x4F,0x4A,0x62,0x53,0x45,0x67,0x56,0x74,0x76,0x72,0x68,0x44,0x39,0x73,0x55,
2737 + 0x6D,0x4B,0x30,0x74,0x36,0x63,0x51,0x44,0x65,0x32,0x32,0x4E,0x4E,0x6E,0x77,0x37,
2738 + 0x43,0x4F,0x4F,0x61,0x59,0x49,0x57,0x55,0x55,0x6A,0x79,0x6A,0x68,0x35,0x50,0x4B,
2739 + 0x64,0x64,0x45,0x4B,0x5A,0x38,0x68,0x62,0x62,0x47,0x65,0x61,0x5A,0x4A,0x6F,0x76,
2740 + 0x0A,0x63,0x6F,0x51,0x64,0x55,0x56,0x51,0x6D,0x71,0x44,0x53,0x2B,0x6B,0x63,0x2F,
2741 + 0x41,0x51,0x6C,0x65,0x55,0x36,0x68,0x51,0x6A,0x63,0x55,0x4C,0x57,0x44,0x6B,0x4E,
2742 + 0x2F,0x6F,0x4F,0x6C,0x33,0x43,0x53,0x65,0x70,0x67,0x54,0x37,0x6B,0x67,0x73,0x52,
2743 + 0x63,0x63,0x47,0x74,0x66,0x4B,0x65,0x37,0x77,0x4D,0x70,0x35,0x66,0x59,0x4A,0x2B,
2744 + 0x41,0x0A,0x43,0x46,0x44,0x41,0x6F,0x4C,0x6E,0x58,0x4E,0x6A,0x4E,0x56,0x6C,0x65,
2745 + 0x73,0x43,0x6B,0x78,0x74,0x6A,0x62,0x4C,0x62,0x49,0x72,0x66,0x32,0x6E,0x43,0x62,
2746 + 0x32,0x61,0x4D,0x65,0x64,0x31,0x5A,0x48,0x4E,0x4A,0x51,0x75,0x6F,0x4E,0x58,0x67,
2747 + 0x72,0x43,0x41,0x44,0x31,0x71,0x2B,0x58,0x6E,0x66,0x77,0x63,0x69,0x6D,0x57,0x50,
2748 + 0x64,0x51,0x0A,0x44,0x59,0x6A,0x6D,0x65,0x44,0x70,0x35,0x77,0x36,0x41,0x4A,0x33,
2749 + 0x2F,0x35,0x59,0x39,0x55,0x74,0x78,0x47,0x34,0x72,0x51,0x72,0x61,0x68,0x78,0x53,
2750 + 0x42,0x77,0x43,0x4B,0x57,0x39,0x4B,0x79,0x53,0x31,0x71,0x53,0x76,0x73,0x37,0x7A,
2751 + 0x59,0x2F,0x52,0x59,0x37,0x4A,0x66,0x36,0x63,0x56,0x6B,0x54,0x43,0x78,0x69,0x33,
2752 + 0x7A,0x32,0x53,0x0A,0x50,0x46,0x33,0x51,0x64,0x6B,0x30,0x50,0x44,0x2F,0x73,0x2B,
2753 + 0x6B,0x77,0x39,0x71,0x4F,0x4E,0x79,0x69,0x33,0x67,0x6E,0x61,0x42,0x46,0x6E,0x54,
2754 + 0x77,0x48,0x7A,0x59,0x69,0x77,0x34,0x2F,0x77,0x6A,0x46,0x33,0x64,0x47,0x68,0x47,
2755 + 0x4E,0x6B,0x78,0x36,0x70,0x63,0x4E,0x4F,0x52,0x55,0x46,0x4E,0x65,0x4F,0x7A,0x59,
2756 + 0x76,0x39,0x6F,0x6A,0x0A,0x51,0x59,0x70,0x73,0x55,0x31,0x33,0x6A,0x6D,0x30,0x33,
2757 + 0x42,0x6F,0x45,0x2B,0x42,0x31,0x64,0x38,0x50,0x47,0x75,0x57,0x2B,0x49,0x7A,0x2F,
2758 + 0x41,0x4F,0x44,0x7A,0x6B,0x6F,0x56,0x6B,0x39,0x2B,0x57,0x79,0x49,0x33,0x37,0x50,
2759 + 0x30,0x53,0x7A,0x47,0x4B,0x72,0x2B,0x53,0x33,0x72,0x72,0x74,0x61,0x50,0x6C,0x41,
2760 + 0x70,0x71,0x4B,0x48,0x55,0x0A,0x6E,0x64,0x35,0x6C,0x30,0x63,0x76,0x75,0x59,0x66,
2761 + 0x31,0x4C,0x37,0x45,0x52,0x75,0x49,0x58,0x64,0x47,0x4C,0x6A,0x30,0x43,0x41,0x77,
2762 + 0x45,0x41,0x41,0x51,0x3D,0x3D,0x0A,0x2D,0x2D,0x2D,0x2D,0x2D,0x45,0x4E,0x44,0x20,
2763 + 0x50,0x55,0x42,0x4C,0x49,0x43,0x20,0x4B,0x45,0x59,0x2D,0x2D,0x2D,0x2D,0x2D,0x0A,
2766 +++ b/net/rsa/rmd160.c
2768 +/********************************************************************\
2772 + * CONTENTS: A sample C-implementation of the RIPEMD-160
2774 + * TARGET: any computer with an ANSI C compiler
2776 + * AUTHOR: Antoon Bosselaers, ESAT-COSIC
2777 + * DATE: 1 March 1996
2780 + * Copyright (c) Katholieke Universiteit Leuven
2781 + * 1996, All Rights Reserved
2783 + * Conditions for use of the RIPEMD-160 Software
2785 + * The RIPEMD-160 software is freely available for use under the terms and
2786 + * conditions described hereunder, which shall be deemed to be accepted by
2787 + * any user of the software and applicable on any use of the software:
2789 + * 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
2790 + * all purposes be considered the owner of the RIPEMD-160 software and of
2791 + * all copyright, trade secret, patent or other intellectual property
2793 + * 2. The RIPEMD-160 software is provided on an "as is" basis without
2794 + * warranty of any sort, express or implied. K.U.Leuven makes no
2795 + * representation that the use of the software will not infringe any
2796 + * patent or proprietary right of third parties. User will indemnify
2797 + * K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
2798 + * which may arise as a result of its use of the software. In no
2799 + * circumstances K.U.Leuven R&D will be held liable for any deficiency,
2800 + * fault or other mishappening with regard to the use or performance of
2802 + * 3. User agrees to give due credit to K.U.Leuven in scientific publications
2803 + * or communications in relation with the use of the RIPEMD-160 software
2804 + * as follows: RIPEMD-160 software written by Antoon Bosselaers,
2805 + * available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
2807 +\********************************************************************/
2810 +#include <common.h>
2811 +#include "rmd160.h"
2813 +/********************************************************************/
2815 +void MDinit(dword *MDbuf)
2817 + MDbuf[0] = 0x67452301UL;
2818 + MDbuf[1] = 0xefcdab89UL;
2819 + MDbuf[2] = 0x98badcfeUL;
2820 + MDbuf[3] = 0x10325476UL;
2821 + MDbuf[4] = 0xc3d2e1f0UL;
2826 +/********************************************************************/
2828 +void compress(dword *MDbuf, dword *X)
2830 + dword aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2],
2831 + dd = MDbuf[3], ee = MDbuf[4];
2832 + dword aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2],
2833 + ddd = MDbuf[3], eee = MDbuf[4];
2836 + FF(aa, bb, cc, dd, ee, X[ 0], 11);
2837 + FF(ee, aa, bb, cc, dd, X[ 1], 14);
2838 + FF(dd, ee, aa, bb, cc, X[ 2], 15);
2839 + FF(cc, dd, ee, aa, bb, X[ 3], 12);
2840 + FF(bb, cc, dd, ee, aa, X[ 4], 5);
2841 + FF(aa, bb, cc, dd, ee, X[ 5], 8);
2842 + FF(ee, aa, bb, cc, dd, X[ 6], 7);
2843 + FF(dd, ee, aa, bb, cc, X[ 7], 9);
2844 + FF(cc, dd, ee, aa, bb, X[ 8], 11);
2845 + FF(bb, cc, dd, ee, aa, X[ 9], 13);
2846 + FF(aa, bb, cc, dd, ee, X[10], 14);
2847 + FF(ee, aa, bb, cc, dd, X[11], 15);
2848 + FF(dd, ee, aa, bb, cc, X[12], 6);
2849 + FF(cc, dd, ee, aa, bb, X[13], 7);
2850 + FF(bb, cc, dd, ee, aa, X[14], 9);
2851 + FF(aa, bb, cc, dd, ee, X[15], 8);
2854 + GG(ee, aa, bb, cc, dd, X[ 7], 7);
2855 + GG(dd, ee, aa, bb, cc, X[ 4], 6);
2856 + GG(cc, dd, ee, aa, bb, X[13], 8);
2857 + GG(bb, cc, dd, ee, aa, X[ 1], 13);
2858 + GG(aa, bb, cc, dd, ee, X[10], 11);
2859 + GG(ee, aa, bb, cc, dd, X[ 6], 9);
2860 + GG(dd, ee, aa, bb, cc, X[15], 7);
2861 + GG(cc, dd, ee, aa, bb, X[ 3], 15);
2862 + GG(bb, cc, dd, ee, aa, X[12], 7);
2863 + GG(aa, bb, cc, dd, ee, X[ 0], 12);
2864 + GG(ee, aa, bb, cc, dd, X[ 9], 15);
2865 + GG(dd, ee, aa, bb, cc, X[ 5], 9);
2866 + GG(cc, dd, ee, aa, bb, X[ 2], 11);
2867 + GG(bb, cc, dd, ee, aa, X[14], 7);
2868 + GG(aa, bb, cc, dd, ee, X[11], 13);
2869 + GG(ee, aa, bb, cc, dd, X[ 8], 12);
2872 + HH(dd, ee, aa, bb, cc, X[ 3], 11);
2873 + HH(cc, dd, ee, aa, bb, X[10], 13);
2874 + HH(bb, cc, dd, ee, aa, X[14], 6);
2875 + HH(aa, bb, cc, dd, ee, X[ 4], 7);
2876 + HH(ee, aa, bb, cc, dd, X[ 9], 14);
2877 + HH(dd, ee, aa, bb, cc, X[15], 9);
2878 + HH(cc, dd, ee, aa, bb, X[ 8], 13);
2879 + HH(bb, cc, dd, ee, aa, X[ 1], 15);
2880 + HH(aa, bb, cc, dd, ee, X[ 2], 14);
2881 + HH(ee, aa, bb, cc, dd, X[ 7], 8);
2882 + HH(dd, ee, aa, bb, cc, X[ 0], 13);
2883 + HH(cc, dd, ee, aa, bb, X[ 6], 6);
2884 + HH(bb, cc, dd, ee, aa, X[13], 5);
2885 + HH(aa, bb, cc, dd, ee, X[11], 12);
2886 + HH(ee, aa, bb, cc, dd, X[ 5], 7);
2887 + HH(dd, ee, aa, bb, cc, X[12], 5);
2890 + II(cc, dd, ee, aa, bb, X[ 1], 11);
2891 + II(bb, cc, dd, ee, aa, X[ 9], 12);
2892 + II(aa, bb, cc, dd, ee, X[11], 14);
2893 + II(ee, aa, bb, cc, dd, X[10], 15);
2894 + II(dd, ee, aa, bb, cc, X[ 0], 14);
2895 + II(cc, dd, ee, aa, bb, X[ 8], 15);
2896 + II(bb, cc, dd, ee, aa, X[12], 9);
2897 + II(aa, bb, cc, dd, ee, X[ 4], 8);
2898 + II(ee, aa, bb, cc, dd, X[13], 9);
2899 + II(dd, ee, aa, bb, cc, X[ 3], 14);
2900 + II(cc, dd, ee, aa, bb, X[ 7], 5);
2901 + II(bb, cc, dd, ee, aa, X[15], 6);
2902 + II(aa, bb, cc, dd, ee, X[14], 8);
2903 + II(ee, aa, bb, cc, dd, X[ 5], 6);
2904 + II(dd, ee, aa, bb, cc, X[ 6], 5);
2905 + II(cc, dd, ee, aa, bb, X[ 2], 12);
2908 + JJ(bb, cc, dd, ee, aa, X[ 4], 9);
2909 + JJ(aa, bb, cc, dd, ee, X[ 0], 15);
2910 + JJ(ee, aa, bb, cc, dd, X[ 5], 5);
2911 + JJ(dd, ee, aa, bb, cc, X[ 9], 11);
2912 + JJ(cc, dd, ee, aa, bb, X[ 7], 6);
2913 + JJ(bb, cc, dd, ee, aa, X[12], 8);
2914 + JJ(aa, bb, cc, dd, ee, X[ 2], 13);
2915 + JJ(ee, aa, bb, cc, dd, X[10], 12);
2916 + JJ(dd, ee, aa, bb, cc, X[14], 5);
2917 + JJ(cc, dd, ee, aa, bb, X[ 1], 12);
2918 + JJ(bb, cc, dd, ee, aa, X[ 3], 13);
2919 + JJ(aa, bb, cc, dd, ee, X[ 8], 14);
2920 + JJ(ee, aa, bb, cc, dd, X[11], 11);
2921 + JJ(dd, ee, aa, bb, cc, X[ 6], 8);
2922 + JJ(cc, dd, ee, aa, bb, X[15], 5);
2923 + JJ(bb, cc, dd, ee, aa, X[13], 6);
2925 + /* parallel round 1 */
2926 + JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
2927 + JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
2928 + JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
2929 + JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
2930 + JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
2931 + JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
2932 + JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
2933 + JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
2934 + JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
2935 + JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
2936 + JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
2937 + JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
2938 + JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
2939 + JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
2940 + JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
2941 + JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
2943 + /* parallel round 2 */
2944 + III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
2945 + III(ddd, eee, aaa, bbb, ccc, X[11], 13);
2946 + III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
2947 + III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
2948 + III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
2949 + III(eee, aaa, bbb, ccc, ddd, X[13], 8);
2950 + III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
2951 + III(ccc, ddd, eee, aaa, bbb, X[10], 11);
2952 + III(bbb, ccc, ddd, eee, aaa, X[14], 7);
2953 + III(aaa, bbb, ccc, ddd, eee, X[15], 7);
2954 + III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
2955 + III(ddd, eee, aaa, bbb, ccc, X[12], 7);
2956 + III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
2957 + III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
2958 + III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
2959 + III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
2961 + /* parallel round 3 */
2962 + HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
2963 + HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
2964 + HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
2965 + HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
2966 + HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
2967 + HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
2968 + HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
2969 + HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
2970 + HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
2971 + HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
2972 + HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
2973 + HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
2974 + HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
2975 + HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
2976 + HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
2977 + HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
2979 + /* parallel round 4 */
2980 + GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
2981 + GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
2982 + GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
2983 + GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
2984 + GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
2985 + GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
2986 + GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
2987 + GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
2988 + GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
2989 + GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
2990 + GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
2991 + GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
2992 + GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
2993 + GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
2994 + GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
2995 + GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
2997 + /* parallel round 5 */
2998 + FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
2999 + FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
3000 + FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
3001 + FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
3002 + FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
3003 + FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
3004 + FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
3005 + FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
3006 + FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
3007 + FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
3008 + FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
3009 + FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
3010 + FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
3011 + FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
3012 + FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
3013 + FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
3015 + /* combine results */
3016 + ddd += cc + MDbuf[1]; /* final result for MDbuf[0] */
3017 + MDbuf[1] = MDbuf[2] + dd + eee;
3018 + MDbuf[2] = MDbuf[3] + ee + aaa;
3019 + MDbuf[3] = MDbuf[4] + aa + bbb;
3020 + MDbuf[4] = MDbuf[0] + bb + ccc;
3026 +/********************************************************************/
3028 +void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen)
3030 + unsigned int i; /* counter */
3031 + dword X[16]; /* message words */
3033 + memset(X, 0, 16*sizeof(dword));
3035 + /* put bytes from strptr into X */
3036 + for (i=0; i<(lswlen&63); i++) {
3037 + /* byte i goes into word X[i div 4] at pos. 8*(i mod 4) */
3038 + X[i>>2] ^= (dword) *strptr++ << (8 * (i&3));
3041 + /* append the bit m_n == 1 */
3042 + X[(lswlen>>2)&15] ^= (dword)1 << (8*(lswlen&3) + 7);
3044 + if ((lswlen & 63) > 55) {
3045 + /* length goes to next block */
3046 + compress(MDbuf, X);
3047 + memset(X, 0, 16*sizeof(dword));
3050 + /* append length in bits*/
3051 + X[14] = lswlen << 3;
3052 + X[15] = (lswlen >> 29) | (mswlen << 3);
3053 + compress(MDbuf, X);
3058 +/************************ end of file rmd160.c **********************/
3061 +++ b/net/rsa/rmd160.h
3063 +/********************************************************************\
3067 + * CONTENTS: Header file for a sample C-implementation of the
3068 + * RIPEMD-160 hash-function.
3069 + * TARGET: any computer with an ANSI C compiler
3071 + * AUTHOR: Antoon Bosselaers, ESAT-COSIC
3072 + * DATE: 1 March 1996
3075 + * Copyright (c) Katholieke Universiteit Leuven
3076 + * 1996, All Rights Reserved
3078 + * Conditions for use of the RIPEMD-160 Software
3080 + * The RIPEMD-160 software is freely available for use under the terms and
3081 + * conditions described hereunder, which shall be deemed to be accepted by
3082 + * any user of the software and applicable on any use of the software:
3084 + * 1. K.U.Leuven Department of Electrical Engineering-ESAT/COSIC shall for
3085 + * all purposes be considered the owner of the RIPEMD-160 software and of
3086 + * all copyright, trade secret, patent or other intellectual property
3088 + * 2. The RIPEMD-160 software is provided on an "as is" basis without
3089 + * warranty of any sort, express or implied. K.U.Leuven makes no
3090 + * representation that the use of the software will not infringe any
3091 + * patent or proprietary right of third parties. User will indemnify
3092 + * K.U.Leuven and hold K.U.Leuven harmless from any claims or liabilities
3093 + * which may arise as a result of its use of the software. In no
3094 + * circumstances K.U.Leuven R&D will be held liable for any deficiency,
3095 + * fault or other mishappening with regard to the use or performance of
3097 + * 3. User agrees to give due credit to K.U.Leuven in scientific publications
3098 + * or communications in relation with the use of the RIPEMD-160 software
3099 + * as follows: RIPEMD-160 software written by Antoon Bosselaers,
3100 + * available at http://www.esat.kuleuven.be/~cosicart/ps/AB-9601/.
3102 +\********************************************************************/
3104 +#ifndef RMD160H /* make sure this file is read only once */
3107 +/********************************************************************/
3109 +/* typedef 8 and 32 bit types, resp. */
3110 +/* adapt these, if necessary,
3111 + for your operating system and compiler */
3112 +typedef unsigned char byte;
3113 +typedef unsigned long dword;
3116 +/********************************************************************/
3118 +/* macro definitions */
3120 +/* collect four bytes into one word: */
3121 +#define BYTES_TO_DWORD(strptr) \
3122 + (((dword) *((strptr)+3) << 24) | \
3123 + ((dword) *((strptr)+2) << 16) | \
3124 + ((dword) *((strptr)+1) << 8) | \
3125 + ((dword) *(strptr)))
3127 +/* ROL(x, n) cyclically rotates x over n bits to the left */
3128 +/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
3129 +#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
3131 +/* the five basic functions F(), G() and H() */
3132 +#define F(x, y, z) ((x) ^ (y) ^ (z))
3133 +#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
3134 +#define H(x, y, z) (((x) | ~(y)) ^ (z))
3135 +#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
3136 +#define J(x, y, z) ((x) ^ ((y) | ~(z)))
3138 +/* the ten basic operations FF() through III() */
3139 +#define FF(a, b, c, d, e, x, s) {\
3140 + (a) += F((b), (c), (d)) + (x);\
3141 + (a) = ROL((a), (s)) + (e);\
3142 + (c) = ROL((c), 10);\
3144 +#define GG(a, b, c, d, e, x, s) {\
3145 + (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
3146 + (a) = ROL((a), (s)) + (e);\
3147 + (c) = ROL((c), 10);\
3149 +#define HH(a, b, c, d, e, x, s) {\
3150 + (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
3151 + (a) = ROL((a), (s)) + (e);\
3152 + (c) = ROL((c), 10);\
3154 +#define II(a, b, c, d, e, x, s) {\
3155 + (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
3156 + (a) = ROL((a), (s)) + (e);\
3157 + (c) = ROL((c), 10);\
3159 +#define JJ(a, b, c, d, e, x, s) {\
3160 + (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
3161 + (a) = ROL((a), (s)) + (e);\
3162 + (c) = ROL((c), 10);\
3164 +#define FFF(a, b, c, d, e, x, s) {\
3165 + (a) += F((b), (c), (d)) + (x);\
3166 + (a) = ROL((a), (s)) + (e);\
3167 + (c) = ROL((c), 10);\
3169 +#define GGG(a, b, c, d, e, x, s) {\
3170 + (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
3171 + (a) = ROL((a), (s)) + (e);\
3172 + (c) = ROL((c), 10);\
3174 +#define HHH(a, b, c, d, e, x, s) {\
3175 + (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
3176 + (a) = ROL((a), (s)) + (e);\
3177 + (c) = ROL((c), 10);\
3179 +#define III(a, b, c, d, e, x, s) {\
3180 + (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
3181 + (a) = ROL((a), (s)) + (e);\
3182 + (c) = ROL((c), 10);\
3184 +#define JJJ(a, b, c, d, e, x, s) {\
3185 + (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
3186 + (a) = ROL((a), (s)) + (e);\
3187 + (c) = ROL((c), 10);\
3190 +/********************************************************************/
3192 +/* function prototypes */
3194 +void MDinit(dword *MDbuf);
3196 + * initializes MDbuffer to "magic constants"
3199 +void compress(dword *MDbuf, dword *X);
3201 + * the compression function.
3202 + * transforms MDbuf using message bytes X[0] through X[15]
3205 +void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen);
3207 + * puts bytes from strptr into X and pad out; appends length
3208 + * and finally, compresses the last block(s)
3209 + * note: length in bits == 8 * (lswlen + 2^32 mswlen).
3210 + * note: there are (lswlen mod 64) bytes left in strptr.
3213 +#endif /* RMD160H */
3215 +/*********************** end of file rmd160.h ***********************/
3221 + * FONSM RSA handling library
3223 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
3225 + * This library is free software; you can redistribute it and/or modify
3226 + * it under the terms of the GNU Lesser General Public License as published by
3227 + * the Free Software Foundation; either version 2 of the License, or
3228 + * (at your option) any later version.
3230 + * This library is distributed in the hope that it will be useful,
3231 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3232 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3233 + * GNU Lesser General Public License for more details.
3235 + * You should have received a copy of the GNU Lesser General Public License
3236 + * along with this library; if not, write to the Free Software
3237 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3239 + * Created: 20070306 Pablo Martin Medrano <pablo@fon.com>
3243 + * $Id: fonrsa.c 405 2007-09-19 15:26:17Z jesus.pico $
3245 +#include <malloc.h>
3246 +#include <common.h>
3248 +#include "rmd160.h"
3249 +#include "bigint.h"
3250 +#include "base64.h"
3252 +#include "public_key.h"
3254 +#define RMDsize 160 /* A RIPEMD-160 hash has 160 bits */
3262 + bigint *m; /* modulus */
3263 + bigint *e; /* public exponent */
3265 + BI_CTX *bi_ctx; /* big integer handle */
3270 + RSA_parameters *rsaparms;
3273 +static void CH_free_der_key(DER_key *key)
3275 + free(key->buffer);
3279 +int CH_decrypt(RSA_parameters *rsa, uint8_t *buffer_in, uint8_t *buffer_out)
3282 + bigint *decrypted_bi;
3285 + byte_size = rsa->num_octets;
3286 + dat_bi = bi_import(rsa->bi_ctx, buffer_in, byte_size);
3287 + rsa->bi_ctx->mod_offset = BIGINT_M_OFFSET;
3289 + decrypted_bi = bi_mod_power(rsa->bi_ctx, dat_bi, rsa->e);
3290 + bi_export(rsa->bi_ctx, decrypted_bi, buffer_out, byte_size);
3294 +byte *RMDbinary(char *buffer, int len)
3296 + byte data[1024]; /* contains current mess. block */
3297 + dword nbytes; /* length of this block */
3298 + dword MDbuf[RMDsize / 32]; /* contains (A, B, C, D(, E)) */
3299 + static byte hashcode[RMDsize / 8]; /* for final hash-value */
3300 + dword X[16]; /* current 16-word chunk */
3301 + unsigned int i, j; /* counters */
3302 + dword length[2]; /* length in bytes of message */
3303 + dword offset; /* # of unprocessed bytes at */
3304 + /* call of MDfinish */
3306 + char *tmp = buffer;
3311 + memcpy(data, tmp, 1024);
3321 + /* process all complete blocks */
3322 + for (i = 0; i < (nbytes >> 6); i++) {
3323 + for (j = 0; j < 16; j++)
3324 + X[j] = BYTES_TO_DWORD(data + 64 * i + 4 * j);
3325 + compress(MDbuf, X);
3327 + /* update length[] */
3328 + if (length[0] + nbytes < length[0])
3329 + length[1]++; /* overflow to msb of length */
3330 + length[0] += nbytes;
3333 + offset = length[0] & 0x3C0; /* extract bytes 6 to 10 inclusive */
3334 + MDfinish(MDbuf, data + offset, length[0], length[1]);
3336 + for (i = 0; i < RMDsize / 8; i += 4) {
3337 + hashcode[i] = MDbuf[i >> 2];
3338 + hashcode[i + 1] = (MDbuf[i >> 2] >> 8);
3339 + hashcode[i + 2] = (MDbuf[i >> 2] >> 16);
3340 + hashcode[i + 3] = (MDbuf[i >> 2] >> 24);
3343 + return (byte *) hashcode;
3346 +static DER_key *CH_load_pem_key(void)
3355 + if ((ret = (DER_key *)malloc(sizeof(DER_key))) == NULL)
3357 + buffer = public_key;
3358 + p = (char *)buffer;
3359 + while ((*p != '\n') && (*p != '\0'))
3367 + while((p - b64) <= filesize) {
3368 + if ((*p == '-')) {
3370 + } else if ((*p != '\n') && (*p != ' ') && (*p != '\t')) {
3377 + size = B64_decode(b64, key, strlen(b64), 1024);
3384 + ret->buffer = (char *)malloc(size);
3386 + memcpy((void *)ret->buffer, (void *)key, size);
3391 + * Similar to RSA_pub_key_new, rewritten to make this program depend only on bi module
3393 +void CH_pub_key_new(RSA_parameters **rsa, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len)
3395 + RSA_parameters *rsa_parameters;
3397 + BI_CTX *bi_ctx = bi_initialize();
3398 + *rsa = (RSA_parameters *)calloc(1, sizeof(RSA_parameters));
3399 + rsa_parameters = *rsa;
3400 + rsa_parameters->bi_ctx = bi_ctx;
3401 + rsa_parameters->num_octets = (mod_len & 0xFFF0);
3402 + rsa_parameters->m = bi_import(bi_ctx, modulus, mod_len);
3403 + bi_set_mod(bi_ctx, rsa_parameters->m, BIGINT_M_OFFSET);
3404 + rsa_parameters->e = bi_import(bi_ctx, pub_exp, pub_len);
3405 + bi_permanent(rsa_parameters->e);
3409 + * Get the public key specifics from an ASN.1 encoded file
3410 + * A function lacking in the exported axTLS API
3412 + * This is a really weird hack that only works with RSA public key
3415 +static int asn1_get_public_key(const uint8_t *buf, int len, RSA_parameters **rsa_parameters)
3417 + uint8_t *modulus, *pub_exp;
3418 + int mod_len, pub_len;
3421 + mod_len = len - 37;
3422 + if (buf[0] != 0x30) {
3426 + pub_exp = (uint8_t *)malloc(3);
3427 + modulus = (uint8_t *)malloc(mod_len);
3428 + memcpy(modulus, buf + 32 , mod_len);
3429 + memcpy(pub_exp, buf + 34 + mod_len, 3);
3430 + if (mod_len <= 0 || pub_len <= 0 )
3432 + CH_pub_key_new(rsa_parameters, modulus, mod_len, pub_exp, pub_len);
3441 +void *FR_init(void)
3444 + RSA_parameters *rsa_parameters;
3446 + derkey = CH_load_pem_key();
3447 + if ((asn1_get_public_key(derkey->buffer, derkey->size, &rsa_parameters)) != 0) {
3448 + fprintf(stderr, "Error: Extracting public key from file\n");
3451 + CH_free_der_key(derkey);
3452 + return (void *)rsa_parameters;
3455 +FONRSA_ERROR FR_end(void *handle)
3457 + RSA_parameters *rsa_parameters = (RSA_parameters *)handle;
3459 + free(rsa_parameters);
3465 + * Implementation of PKCS 1.5 padding, borrowed from
3466 + * Tom's code (public domain)
3469 +/* Standalone FR_verify_file */
3470 +FONRSA_ERROR FR_verify_file(void *handler, char *file_data, int file_len,
3471 + char *signature_buffer, int signature_size)
3476 + uint8_t *decrypted;
3477 + RSA_parameters *rsa_parameters = (RSA_parameters *)handler;
3479 + /* Calculates the RIPEMD-160 hash of the file */
3480 + hashcode = RMDbinary (file_data, file_len);
3482 + if (rsa_parameters->num_octets != signature_size)
3483 + return FONRSA_SIZE;
3484 + decrypted = (uint8_t *)malloc(rsa_parameters->num_octets);
3485 + if (CH_decrypt(rsa_parameters, signature_buffer, decrypted)) {
3486 + printf("Error: Decrypting signature\n");
3487 + return FONRSA_VERIFICATION_FAILURE;
3489 + memcpy(hash, decrypted + 492, 20);
3490 + //free(decrypted);
3491 + //free(signature_buffer);
3492 + for (j = 0; j < RMDsize/8; j++) {
3493 + if (hash[j] != hashcode[j])
3494 + return FONRSA_VERIFICATION_FAILURE;
3499 +int rsa_check_signature(char *signature, int signature_len, char *buffer, int buffer_len)
3501 + FONRSA_ERROR fonrsa_error;
3503 + handle = FR_init();
3504 + if (handle == NULL) {
3505 + printf("Error loading keys\n");
3508 + fonrsa_error = FR_verify_file(handle, buffer, buffer_len, signature, signature_len);
3510 + switch (fonrsa_error) {
3512 + printf("Verified OK\n");
3514 + case FONRSA_VERIFICATION_FAILURE:
3515 + printf("Verification failure\n");
3518 + printf("Verification error\n");
3527 + * FONSM RSA handling library, used by fonsmcd and foncheckrsa
3529 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
3531 + * This library is free software; you can redistribute it and/or modify
3532 + * it under the terms of the GNU Lesser General Public License as published by
3533 + * the Free Software Foundation; either version 2 of the License, or
3534 + * (at your option) any later version.
3536 + * This library is distributed in the hope that it will be useful,
3537 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3538 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3539 + * GNU Lesser General Public License for more details.
3541 + * You should have received a copy of the GNU Lesser General Public License
3542 + * along with this library; if not, write to the Free Software
3543 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3545 + * Created: 20070306 Pablo Martin Medrano <pablo@fon.com>
3547 + * $Id: fonrsa.h 404 2007-09-17 10:41:31Z jesus.pico $
3552 +#define MINIMUM_PADING_BYTES_PKCS_1_5 3
3556 + FONRSA_VERIFICATION_FAILURE = 1,
3557 + FONRSA_OPENKEY = 2,
3559 + FONRSA_LOADFILE = 4,
3561 + FONRSA_DECRYPT = 6,
3562 + FONRSA_SAVEFILE = 7,
3567 +int rsa_check_signature(char *signature, int signature_len, char *buffer, int buffer_len);
3573 +++ b/net/rsa/sign.h
3576 + * Signature interface
3578 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
3580 + * Created: 20070417 Pablo MartÃn Medrano <pablo@fon.com>
3582 + * $Id: sign.h 389 2007-06-11 08:29:56Z pablo.martin $
3590 +void SG_init(void);
3591 +void *SG_start(char *private_key_path, char *public_key_path);
3592 +void SG_stop(void *handle);
3593 +int SG_crypt(void *data, unsigned char *text, int size_text, unsigned char *crypted_text,
3594 + unsigned int crypted_text_buffer_size, int *crypted_size);
3595 +int SG_crypt_v2(void *data, unsigned char *text, int size_text, unsigned char *crypted_text,
3596 + unsigned int crypted_text_buffer_size, int *crypted_size);
3600 +#endif /* #ifdef __SIGN_H__ */
3603 +++ b/net/rsa/sign_openssl.c
3606 + * Signature using OpenSSL
3608 + * This file is part of FONUCS. Copyright (C) 2007 FON Wireless Ltd.
3610 + * Created: 20070417 Pablo MartÃn Medrano <pablo@fon.com>
3612 + * $Id: sign_openssl.c 346 2007-05-10 19:51:38Z pablo.martin $
3616 + * How the RSA public and private key was generated
3617 + * To check .FON files
3618 + * openssl genrsa -out private_fon_rsa_key.pem 4096
3619 + * openssl rsa -in private_fon_rsa_key.pem -pubout -out public_fon_rsa_key.pem
3621 + * How the Status Manager public and private key was generated
3622 + * openssl genrsa -out private_sm_rsa_key.pem 2048
3623 + * openssl rsa -in private_sm_rsa_key.pem -pubout -out public_sm_rsa_key.pem
3625 + * How to sign using the RSA private key (This is what fonsign does)
3626 + * openssl dgst -rmd160 -sign private_fon_rsa_key.pem FILE > SIGNATURE
3627 + * How to verify using the RSA public key (This is what fonverify + foncheckrsa does)
3628 + * openssl dgst -rmd160 -verify public_fon_rsa_key.pem -signature SIGNATURE FILE
3629 + * Convert to DER file (to use it in La Fonera)
3630 + * openssl rsa -inform PEM -outform DER -pubin -in public_fon_rsa_key.pem -pubout -out public_fon_rsa_key.der
3632 +#include <openssl/rsa.h>
3633 +#include <openssl/ssl.h>
3634 +#include <openssl/bn.h>
3635 +#include <openssl/pem.h>
3636 +#include <openssl/evp.h>
3637 +#include <sys/stat.h>
3639 +#ifndef __MAINTEST__
3642 +#define fon_warning printf
3643 +#define fon_debug printf
3644 +#define fon_critical printf
3654 + int public_rsa_size;
3655 + EVP_PKEY *public_pkey;
3656 + int public_pkey_size;
3664 +static EVP_PKEY *SG_load_key(char *key_path, KEY_TYPE type);
3665 +static unsigned char *load_file_in_buffer(char *path, int *size);
3666 +static int save_file_from_buffer(char *path, char *buffer, int size);
3670 + SSL_load_error_strings();
3671 + SSL_library_init();
3672 + OpenSSL_add_all_algorithms();
3673 + OpenSSL_add_all_ciphers();
3674 + OpenSSL_add_all_digests();
3677 +static unsigned char *load_file_in_buffer(char *path, int *size)
3683 + if (stat(path, &st))
3685 + buffer = (char *)malloc(st.st_size);
3686 + if (buffer == NULL)
3688 + if ((fd = open(path, O_RDONLY)) == -1) {
3692 + if (read(fd,buffer,st.st_size) != (ssize_t)st.st_size) {
3697 + *size = (int)st.st_size;
3702 +static int save_file_from_buffer(char *path, char *buffer, int size)
3706 + if ((fd = open(path, O_WRONLY | O_CREAT, 0644)) == -1)
3708 + if (write(fd, buffer, (size_t)size) != ((ssize_t)size)) {
3716 +static EVP_PKEY *SG_load_key(char *key_path, KEY_TYPE type)
3721 + if ((key = BIO_new(BIO_s_file())) == NULL) {
3722 + //ERR_print_errors(err);
3723 + fon_warning("%s: Error calling BIO_new()\n", __FUNCTION__);
3726 + if (BIO_read_filename(key, key_path) <= 0) {
3727 + fon_warning("%s: Error opening %s\n", __FUNCTION__, key_path);
3728 + // ERR_print_errors(err);
3731 + if (type == KEY_PUBLIC) {
3732 + pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
3733 + } else if (type == KEY_PRIVATE) {
3734 + pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
3739 + if (pkey == NULL) {
3740 + fon_warning("%s: Error reading %s\n", __FUNCTION__, key_path);
3748 +void *SG_start(char *private_key_path, char *public_key_path)
3752 + if ((sign = (Sign *)malloc(sizeof(Sign))) == NULL)
3754 + memset(sign, 0, sizeof(Sign));
3755 + if (private_key_path != NULL) {
3756 + if ((sign->pkey = SG_load_key(private_key_path, KEY_PRIVATE)) == NULL) {
3757 + fon_warning("%s: Error loading %s", __FUNCTION__, private_key_path);
3761 + if (public_key_path != NULL) {
3762 + if ((sign->public_pkey = SG_load_key(public_key_path, KEY_PUBLIC)) == NULL) {
3763 + fon_warning("%s: Error loading %s", __FUNCTION__, public_key_path);
3767 + if (sign->pkey != NULL) {
3768 + sign->pkey_size = EVP_PKEY_size(sign->pkey);
3769 + if ((sign->rsa = EVP_PKEY_get1_RSA(sign->pkey)) == NULL) {
3770 + EVP_PKEY_free(sign->pkey);
3774 + if (sign->public_pkey != NULL) {
3775 + sign->public_pkey_size = EVP_PKEY_size(sign->public_pkey);
3776 + if ((sign->public_rsa = EVP_PKEY_get1_RSA(sign->public_pkey)) == NULL) {
3777 + EVP_PKEY_free(sign->pkey);
3781 + if (((sign->rsa == NULL) && (private_key_path != NULL)) ||
3782 + ((sign->public_rsa == NULL) && (public_key_path != NULL))) {
3783 + fon_warning("%s: Error calling EVP_PKEY_get1_RSA()", __FUNCTION__);
3786 + if (sign->rsa != NULL) {
3787 + sign->rsa_size = RSA_size(sign->rsa);
3788 + if (RSA_check_key(sign->rsa) != 1) {
3789 + fon_warning("%s: RSA key failure", __FUNCTION__);
3794 + return (void *)sign;
3797 +void SG_stop(void *handle)
3799 + Sign *sign = (Sign *)handle;
3801 + EVP_PKEY_free(sign->pkey);
3802 + EVP_PKEY_free(sign->public_pkey);
3803 + if (sign->rsa != NULL)
3804 + RSA_free(sign->rsa);
3805 + if (sign->public_rsa != NULL)
3806 + RSA_free(sign->public_rsa);
3810 +int SG_verify(void *data, unsigned char *text, unsigned int size_text,
3811 + unsigned char *signature, unsigned int size_signature)
3817 + Sign *sign = (Sign *)data;
3819 + md = (EVP_MD *)EVP_ripemd160();
3820 + if(!EVP_VerifyInit(&mdctx, md))
3822 + if (!EVP_VerifyUpdate(&mdctx, (const void *)text, (unsigned int)size_text)) {
3825 + ret = EVP_VerifyFinal(&mdctx, (const char *)signature, size_signature, sign->public_pkey);
3826 + EVP_PKEY_free(pkey);
3827 + EVP_MD_CTX_cleanup(&mdctx);
3831 +int SG_sign(void *data, void *text, unsigned int size_text, void *signature_buffer,
3832 + unsigned int size_signature_buffer, unsigned int *size_signature)
3834 + unsigned char *digest[EVP_MAX_MD_SIZE];
3838 + Sign *sign = (Sign *)data;
3840 + if (size_signature_buffer < sign->pkey_size)
3843 + md = (EVP_MD *)EVP_ripemd160();
3844 + EVP_SignInit(&mdctx, md);
3845 + if (!EVP_SignUpdate(&mdctx, (const void *)text, (unsigned int)size_text)) {
3848 + if (!EVP_SignFinal(&mdctx, (unsigned char *)signature_buffer, (unsigned int *)size_signature, sign->pkey)) {
3851 + EVP_MD_CTX_cleanup(&mdctx);
3857 + * It's not advised to crypt using RAW ... unless you have crypted the buffer using AES before.
3859 +int SG_crypt(void *data, unsigned char *text, int size_text,
3860 + unsigned char *crypted_text, unsigned int crypted_text_buffer_size,
3861 + int *crypted_size)
3866 + Sign *sign = (Sign *)data;
3868 + if (crypted_text_buffer_size < sign->pkey_size) {
3869 + fon_critical("%s: size_signature_buffer [%u] < %u", __FUNCTION__, size_text, sign->pkey_size);
3872 + if (size_text != sign->pkey_size) {
3873 + fon_critical("%s: size_text [%u] != %u", __FUNCTION__, size_text, sign->pkey_size);
3876 + /* The buffer is pre-padded with random data ... */
3877 + fon_debug("%s: About to call RSA_private_encrypt(%d, %x, %x, %x, %d)",
3878 + __FUNCTION__, size_text, crypted_text, sign->rsa, RSA_NO_PADDING);
3879 + retsize = RSA_private_encrypt(size_text, text, crypted_text, sign->rsa, RSA_NO_PADDING);
3880 + if (retsize == -1) {
3881 + fon_critical("%s: Error calling RSA_private_encrypt(%d, %x, %x, %x, %d)",
3882 + __FUNCTION__, size_text, crypted_text, sign->rsa, RSA_NO_PADDING);
3885 + *crypted_size = retsize;
3890 +int SG_decrypt(void *data, unsigned char *cryptext, int cryptext_size, unsigned char *plaintext,
3891 + int plaintext_buffer_size, int *plaintext_size)
3896 + Sign *sign = (Sign *)data;
3898 + if (plaintext_buffer_size < sign->public_pkey_size) {
3899 + fon_critical("%s: plaintext_buffer_size [%u] < %u", __FUNCTION__, plaintext_buffer_size, sign->public_pkey_size);
3902 + if (cryptext_size != sign->public_pkey_size) {
3903 + fon_critical("%s: cryptext_size [%u] != %u", __FUNCTION__, cryptext_size, sign->public_pkey_size);
3906 + retsize = RSA_public_decrypt(cryptext_size, cryptext, plaintext, sign->public_rsa, RSA_NO_PADDING);
3907 + if (retsize == -1)
3909 + *plaintext_size = retsize;
3913 +#ifdef __MAINTEST__
3914 +int main(int argc, char **argv)
3916 + size_t argv0_size;
3919 + argv0_size = strlen(argv[0]);
3920 + if (argv0_size < 7) {
3921 + fprintf(stderr, "%s?", argv[0]);
3924 + token = argv[0] + argv0_size - 7;
3927 + if (!strcmp(token, "fonsign")) {
3928 + return main_fonsign(argc, argv);
3929 + } else if (!strcmp(token, "foncryp")) {
3930 + return main_foncryp(argc, argv);
3932 + fprintf(stderr, "%s?", argv[0]);
3936 +int main_foncryp(int argc, char **argv)
3938 + void *handle = NULL;
3940 + char *filebuffer = NULL;
3941 + char crypted[1024];
3942 + int size, crypted_size, ret;
3945 + printf("Usage: %s encrypt|decrypt <key_file> <file> <crypted_file>\n", argv[0]);
3948 + if (!strcmp(argv[1], "encrypt")) {
3949 + printf("Encryption mode\n");
3952 + printf("Decryption mode\n");
3954 + handle = SG_start(argv[2], NULL);
3956 + handle = SG_start(NULL, argv[2]);
3957 + if (handle == NULL) {
3958 + printf("Error loading keys\n");
3962 + filebuffer = load_file_in_buffer(argv[3], &size);
3963 + if (filebuffer == NULL) {
3964 + printf("Error reading %s\n", argv[3]);
3969 + ret = SG_crypt(handle, filebuffer, size, crypted, 1024, &crypted_size);
3971 + ret = SG_decrypt(handle, filebuffer, size, crypted, 1024, &crypted_size);
3974 + printf("Error crypting %d bytes\n", size);
3978 + printf("Crypted size %d\n", crypted_size);
3979 + if (save_file_from_buffer(argv[4], crypted, crypted_size)) {
3980 + printf("Error saving file\n");
3989 +int main_fonsign(int argc, char **argv)
3991 + void *handle = NULL;
3992 + char signature_buffer[4096];
3994 + unsigned int signature_size;
3996 + char *filebuffer = NULL;
4001 + fprintf(stderr, "usage: %s <private_key_file> <public_key_file> <file_to_sign> <signature_file>\n", argv[0]);
4004 + handle = SG_start(argv[1], argv[2]);
4005 + if (handle == NULL) {
4006 + fprintf(stderr, "Error calling SG_start(%s)\n", argv[1]);
4009 + filebuffer = load_file_in_buffer(argv[3], &size);
4010 + if (filebuffer == NULL) {
4011 + fprintf(stderr, "Error reading %s\n", argv[3]);
4014 + if (SG_sign(handle, filebuffer, size, signature_buffer, 4096, &signature_size)) {
4015 + fprintf(stderr, "Error calling SG_sign()\n");
4018 + ret = SG_verify(handle, filebuffer, size, signature_buffer, signature_size);
4020 + fprintf(stderr, "signature failure\n");
4021 + else if (ret == 1) {
4022 + fprintf(stderr, "signature ok\n");
4025 + fprintf(stderr, "signature error\n");
4028 + if (save_file_from_buffer(argv[4], signature_buffer, signature_size)) {
4029 + fprintf(stderr, "Error writing to %s\n", argv[4]);
4034 + if (filebuffer != NULL)
4036 + if (handle != NULL)
4043 +++ b/net/uip-0.9/fs.c
4046 + * \addtogroup httpd
4052 + * HTTP server read-only file system code.
4053 + * \author Adam Dunkels <adam@dunkels.com>
4055 + * A simple read-only filesystem.
4059 + * Copyright (c) 2001, Swedish Institute of Computer Science.
4060 + * All rights reserved.
4062 + * Redistribution and use in source and binary forms, with or without
4063 + * modification, are permitted provided that the following conditions
4065 + * 1. Redistributions of source code must retain the above copyright
4066 + * notice, this list of conditions and the following disclaimer.
4067 + * 2. Redistributions in binary form must reproduce the above copyright
4068 + * notice, this list of conditions and the following disclaimer in the
4069 + * documentation and/or other materials provided with the distribution.
4070 + * 3. Neither the name of the Institute nor the names of its contributors
4071 + * may be used to endorse or promote products derived from this software
4072 + * without specific prior written permission.
4074 + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
4075 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4076 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4077 + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
4078 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4079 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4080 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4081 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4082 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4083 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4086 + * This file is part of the lwIP TCP/IP stack.
4088 + * Author: Adam Dunkels <adam@sics.se>
4090 + * $Id: fs.c,v 1.7.2.3 2003/10/07 13:22:27 adam Exp $
4096 +#include "fsdata.h"
4098 +#include "fsdata.c"
4100 +#ifdef FS_STATISTICS
4101 +#if FS_STATISTICS == 1
4102 +static u16_t count[FS_NUMFILES];
4103 +#endif /* FS_STATISTICS */
4104 +#endif /* FS_STATISTICS */
4106 +/*-----------------------------------------------------------------------------------*/
4108 +fs_strcmp(const char *str1, const char *str2)
4114 + if(str2[i] == 0 ||
4115 + str1[i] == '\r' ||
4116 + str1[i] == '\n') {
4120 + if(str1[i] != str2[i]) {
4128 +/*-----------------------------------------------------------------------------------*/
4130 +fs_open(const char *name, struct fs_file *file)
4132 +#ifdef FS_STATISTICS
4133 +#if FS_STATISTICS == 1
4135 +#endif /* FS_STATISTICS */
4136 +#endif /* FS_STATISTICS */
4137 + struct fsdata_file_noconst *f;
4139 + for(f = (struct fsdata_file_noconst *)FS_ROOT;
4141 + f = (struct fsdata_file_noconst *)f->next) {
4143 + if(fs_strcmp(name, f->name) == 0) {
4144 + file->data = f->data;
4145 + file->len = f->len;
4146 +#ifdef FS_STATISTICS
4147 +#if FS_STATISTICS == 1
4149 +#endif /* FS_STATISTICS */
4150 +#endif /* FS_STATISTICS */
4153 +#ifdef FS_STATISTICS
4154 +#if FS_STATISTICS == 1
4156 +#endif /* FS_STATISTICS */
4157 +#endif /* FS_STATISTICS */
4162 +/*-----------------------------------------------------------------------------------*/
4166 +#ifdef FS_STATISTICS
4167 +#if FS_STATISTICS == 1
4169 + for(i = 0; i < FS_NUMFILES; i++) {
4172 +#endif /* FS_STATISTICS */
4173 +#endif /* FS_STATISTICS */
4175 +/*-----------------------------------------------------------------------------------*/
4176 +#ifdef FS_STATISTICS
4177 +#if FS_STATISTICS == 1
4181 + struct fsdata_file_noconst *f;
4185 + for(f = (struct fsdata_file_noconst *)FS_ROOT;
4187 + f = (struct fsdata_file_noconst *)f->next) {
4189 + if(fs_strcmp(name, f->name) == 0) {
4196 +#endif /* FS_STATISTICS */
4197 +#endif /* FS_STATISTICS */
4198 +/*-----------------------------------------------------------------------------------*/
4200 +++ b/net/uip-0.9/fsdata.c
4202 +static const char data_flashing_html[] = {
4203 + /* /flashing.html */
4204 + 0x2f, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
4205 + 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32,
4206 + 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x53, 0x65, 0x72,
4207 + 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x30,
4208 + 0x2e, 0x39, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
4209 + 0x2f, 0x64, 0x75, 0x6e, 0x6b, 0x65, 0x6c, 0x73, 0x2e, 0x63,
4210 + 0x6f, 0x6d, 0x2f, 0x61, 0x64, 0x61, 0x6d, 0x2f, 0x75, 0x69,
4211 + 0x70, 0x2f, 0x29, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65,
4212 + 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
4213 + 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0xd, 0xa,
4214 + 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x3c, 0x62,
4215 + 0x6f, 0x64, 0x79, 0x20, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3d,
4216 + 0x22, 0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x30,
4217 + 0x70, 0x74, 0x20, 0x61, 0x75, 0x74, 0x6f, 0x3b, 0x20, 0x68,
4218 + 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x31, 0x30, 0x30, 0x25,
4219 + 0x3b, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x23,
4220 + 0x66, 0x66, 0x66, 0x3b, 0x20, 0x62, 0x61, 0x63, 0x6b, 0x67,
4221 + 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f, 0x6c, 0x6f,
4222 + 0x72, 0x3a, 0x20, 0x23, 0x66, 0x62, 0x62, 0x30, 0x33, 0x34,
4223 + 0x3b, 0x22, 0x3e, 0x3c, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72,
4224 + 0x3e, 0x3c, 0x68, 0x31, 0x3e, 0x55, 0x70, 0x67, 0x72, 0x61,
4225 + 0x64, 0x69, 0x6e, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65,
4226 + 0x6d, 0x20, 0x2e, 0x2e, 0x2e, 0x2e, 0x3c, 0x2f, 0x68, 0x31,
4227 + 0x3e, 0x3c, 0x2f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3e,
4228 + 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x2f, 0x68,
4229 + 0x74, 0x6d, 0x6c, 0x3e, 0xa, };
4231 +static const char data_fail_html[] = {
4233 + 0x2f, 0x66, 0x61, 0x69, 0x6c, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
4234 + 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32,
4235 + 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x53, 0x65, 0x72,
4236 + 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x30,
4237 + 0x2e, 0x39, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
4238 + 0x2f, 0x64, 0x75, 0x6e, 0x6b, 0x65, 0x6c, 0x73, 0x2e, 0x63,
4239 + 0x6f, 0x6d, 0x2f, 0x61, 0x64, 0x61, 0x6d, 0x2f, 0x75, 0x69,
4240 + 0x70, 0x2f, 0x29, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65,
4241 + 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
4242 + 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0xd, 0xa,
4243 + 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x9,
4244 + 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0xa, 0x9, 0x9, 0x3c,
4245 + 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0xa, 0x9, 0x9, 0x9,
4246 + 0x4c, 0x61, 0x46, 0x6f, 0x6e, 0x65, 0x72, 0x61, 0x20, 0x46,
4247 + 0x61, 0x69, 0x6c, 0x73, 0x61, 0x66, 0x65, 0x20, 0x55, 0x49,
4248 + 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65,
4249 + 0x3e, 0xa, 0x9, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa,
4250 + 0x9, 0x9, 0x3c, 0x68, 0x31, 0x3e, 0x46, 0x6c, 0x61, 0x73,
4251 + 0x68, 0x69, 0x6e, 0x67, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65,
4252 + 0x64, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0xa, 0x9, 0x9, 0x45,
4253 + 0x52, 0x52, 0x4f, 0x52, 0x20, 0x2d, 0x20, 0x74, 0x68, 0x65,
4254 + 0x20, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x20, 0x79, 0x6f, 0x75,
4255 + 0x20, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x64, 0x20,
4256 + 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20,
4257 + 0x70, 0x61, 0x73, 0x73, 0x20, 0x76, 0x65, 0x72, 0x69, 0x66,
4258 + 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x20, 0x50,
4259 + 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x6d, 0x61, 0x6b, 0x65,
4260 + 0x20, 0x73, 0x75, 0x72, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x75,
4261 + 0x73, 0x65, 0x20, 0x61, 0x6e, 0x20, 0x6f, 0x66, 0x66, 0x69,
4262 + 0x63, 0x69, 0x61, 0x6c, 0x20, 0x75, 0x70, 0x64, 0x61, 0x74,
4263 + 0x65, 0x20, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x64,
4264 + 0x20, 0x62, 0x79, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
4265 + 0x2f, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x2e,
4266 + 0x66, 0x6f, 0x6e, 0x6f, 0x73, 0x66, 0x65, 0x72, 0x61, 0x2e,
4267 + 0x6f, 0x72, 0x67, 0x2f, 0xa, 0x9, 0x3c, 0x2f, 0x62, 0x6f,
4268 + 0x64, 0x79, 0x3e, 0xa, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c,
4271 +static const char data_404_html[] = {
4273 + 0x2f, 0x34, 0x30, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
4274 + 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x34,
4275 + 0x30, 0x34, 0x20, 0x46, 0x69, 0x6c, 0x65, 0x20, 0x6e, 0x6f,
4276 + 0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0xd, 0xa, 0x53,
4277 + 0x65, 0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50,
4278 + 0x2f, 0x30, 0x2e, 0x39, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70,
4279 + 0x3a, 0x2f, 0x2f, 0x64, 0x75, 0x6e, 0x6b, 0x65, 0x6c, 0x73,
4280 + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x64, 0x61, 0x6d, 0x2f,
4281 + 0x75, 0x69, 0x70, 0x2f, 0x29, 0xd, 0xa, 0x43, 0x6f, 0x6e,
4282 + 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a,
4283 + 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c,
4284 + 0xd, 0xa, 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e,
4285 + 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x62, 0x67, 0x63, 0x6f,
4286 + 0x6c, 0x6f, 0x72, 0x3d, 0x22, 0x77, 0x68, 0x69, 0x74, 0x65,
4287 + 0x22, 0x3e, 0x3c, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3e,
4288 + 0x3c, 0x68, 0x31, 0x3e, 0x34, 0x30, 0x34, 0x20, 0x2d, 0x20,
4289 + 0x66, 0x69, 0x6c, 0x65, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x66,
4290 + 0x6f, 0x75, 0x6e, 0x64, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0x3c,
4291 + 0x2f, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x72, 0x3e, 0x3c, 0x2f,
4292 + 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c, 0x2f, 0x68, 0x74, 0x6d,
4295 +static const char data_index_html[] = {
4297 + 0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
4298 + 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32,
4299 + 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x53, 0x65, 0x72,
4300 + 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x30,
4301 + 0x2e, 0x39, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
4302 + 0x2f, 0x64, 0x75, 0x6e, 0x6b, 0x65, 0x6c, 0x73, 0x2e, 0x63,
4303 + 0x6f, 0x6d, 0x2f, 0x61, 0x64, 0x61, 0x6d, 0x2f, 0x75, 0x69,
4304 + 0x70, 0x2f, 0x29, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65,
4305 + 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
4306 + 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0xd, 0xa,
4307 + 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x9,
4308 + 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0xa, 0x9, 0x9, 0x3c,
4309 + 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0xa, 0x9, 0x9, 0x9,
4310 + 0x4c, 0x61, 0x46, 0x6f, 0x6e, 0x65, 0x72, 0x61, 0x20, 0x46,
4311 + 0x61, 0x69, 0x6c, 0x73, 0x61, 0x66, 0x65, 0x20, 0x55, 0x49,
4312 + 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65,
4313 + 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e,
4314 + 0xa, 0x9, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x73, 0x74,
4315 + 0x79, 0x6c, 0x65, 0x3d, 0x22, 0x6d, 0x61, 0x72, 0x67, 0x69,
4316 + 0x6e, 0x3a, 0x20, 0x30, 0x70, 0x74, 0x20, 0x61, 0x75, 0x74,
4317 + 0x6f, 0x3b, 0x20, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a,
4318 + 0x31, 0x30, 0x30, 0x25, 0x3b, 0x20, 0x63, 0x6f, 0x6c, 0x6f,
4319 + 0x72, 0x3a, 0x20, 0x23, 0x30, 0x30, 0x30, 0x3b, 0x20, 0x62,
4320 + 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d,
4321 + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x23, 0x66, 0x62,
4322 + 0x62, 0x30, 0x33, 0x34, 0x3b, 0x22, 0x3e, 0xa, 0x9, 0x9,
4323 + 0x3c, 0x68, 0x31, 0x3e, 0x4c, 0x61, 0x46, 0x6f, 0x6e, 0x65,
4324 + 0x72, 0x61, 0x20, 0x46, 0x61, 0x69, 0x6c, 0x73, 0x61, 0x66,
4325 + 0x65, 0x20, 0x55, 0x49, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0xa,
4326 + 0x9, 0x9, 0x3c, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x65,
4327 + 0x74, 0x68, 0x6f, 0x64, 0x3d, 0x22, 0x70, 0x6f, 0x73, 0x74,
4328 + 0x22, 0x20, 0x65, 0x6e, 0x63, 0x74, 0x79, 0x70, 0x65, 0x3d,
4329 + 0x22, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74,
4330 + 0x2f, 0x66, 0x6f, 0x72, 0x6d, 0x2d, 0x64, 0x61, 0x74, 0x61,
4331 + 0x22, 0x3e, 0xa, 0x9, 0x9, 0x9, 0x3c, 0x69, 0x6e, 0x70,
4332 + 0x75, 0x74, 0x20, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x66, 0x69,
4333 + 0x6c, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x66, 0x69,
4334 + 0x72, 0x6d, 0x77, 0x61, 0x72, 0x65, 0x3e, 0xa, 0x9, 0x9,
4335 + 0x9, 0x3c, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x74, 0x79,
4336 + 0x70, 0x65, 0x3d, 0x73, 0x75, 0x62, 0x6d, 0x69, 0x74, 0x3e,
4337 + 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x66, 0x6f, 0x72, 0x6d, 0x3e,
4338 + 0xa, 0x9, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa,
4339 + 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, };
4341 +static const char data_flash_html[] = {
4343 + 0x2f, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0,
4344 + 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32,
4345 + 0x30, 0x30, 0x20, 0x4f, 0x4b, 0xd, 0xa, 0x53, 0x65, 0x72,
4346 + 0x76, 0x65, 0x72, 0x3a, 0x20, 0x75, 0x49, 0x50, 0x2f, 0x30,
4347 + 0x2e, 0x39, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
4348 + 0x2f, 0x64, 0x75, 0x6e, 0x6b, 0x65, 0x6c, 0x73, 0x2e, 0x63,
4349 + 0x6f, 0x6d, 0x2f, 0x61, 0x64, 0x61, 0x6d, 0x2f, 0x75, 0x69,
4350 + 0x70, 0x2f, 0x29, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65,
4351 + 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
4352 + 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0xd, 0xa,
4353 + 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x9,
4354 + 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0xa, 0x9, 0x9, 0x3c,
4355 + 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0xa, 0x9, 0x9, 0x9,
4356 + 0x4c, 0x61, 0x46, 0x6f, 0x6e, 0x65, 0x72, 0x61, 0x20, 0x46,
4357 + 0x61, 0x69, 0x6c, 0x73, 0x61, 0x66, 0x65, 0x20, 0x55, 0x49,
4358 + 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65,
4359 + 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e,
4360 + 0xa, 0x9, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x73, 0x74,
4361 + 0x79, 0x6c, 0x65, 0x3d, 0x22, 0x6d, 0x61, 0x72, 0x67, 0x69,
4362 + 0x6e, 0x3a, 0x20, 0x30, 0x70, 0x74, 0x20, 0x61, 0x75, 0x74,
4363 + 0x6f, 0x3b, 0x20, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a,
4364 + 0x31, 0x30, 0x30, 0x25, 0x3b, 0x20, 0x63, 0x6f, 0x6c, 0x6f,
4365 + 0x72, 0x3a, 0x20, 0x23, 0x30, 0x30, 0x30, 0x3b, 0x20, 0x62,
4366 + 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d,
4367 + 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x23, 0x66, 0x62,
4368 + 0x62, 0x30, 0x33, 0x34, 0x3b, 0x22, 0x3e, 0xa, 0x9, 0x9,
4369 + 0x3c, 0x68, 0x31, 0x3e, 0x46, 0x6c, 0x61, 0x73, 0x68, 0x69,
4370 + 0x6e, 0x67, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0xa, 0x9, 0x9,
4371 + 0x54, 0x68, 0x65, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d,
4372 + 0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x77, 0x20, 0x74, 0x72,
4373 + 0x79, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x66, 0x6c,
4374 + 0x61, 0x73, 0x68, 0x2e, 0x20, 0x49, 0x66, 0x20, 0x74, 0x68,
4375 + 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x70,
4376 + 0x72, 0x6f, 0x62, 0x6c, 0x65, 0x6d, 0x2c, 0x20, 0x74, 0x68,
4377 + 0x65, 0x20, 0x6c, 0x65, 0x64, 0x73, 0x20, 0x77, 0x69, 0x6c,
4378 + 0x6c, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x20, 0x74, 0x6f,
4379 + 0x20, 0x62, 0x6c, 0x69, 0x6e, 0x6b, 0x2e, 0xa, 0xa, 0x9,
4380 + 0x9, 0x41, 0x66, 0x74, 0x65, 0x72, 0x20, 0x61, 0x20, 0x73,
4381 + 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x66, 0x75, 0x6c, 0x6c,
4382 + 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20, 0x74, 0x68,
4383 + 0x65, 0x20, 0x62, 0x6f, 0x78, 0x20, 0x77, 0x69, 0x6c, 0x6c,
4384 + 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f, 0x74, 0xa, 0x9, 0x3c,
4385 + 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa, 0x3c, 0x2f, 0x68,
4386 + 0x74, 0x6d, 0x6c, 0x3e, 0xa, };
4388 +const struct fsdata_file file_flashing_html[] = {{NULL, data_flashing_html, data_flashing_html + 15, sizeof(data_flashing_html) - 15}};
4390 +const struct fsdata_file file_fail_html[] = {{file_flashing_html, data_fail_html, data_fail_html + 11, sizeof(data_fail_html) - 11}};
4392 +const struct fsdata_file file_404_html[] = {{file_fail_html, data_404_html, data_404_html + 10, sizeof(data_404_html) - 10}};
4394 +const struct fsdata_file file_index_html[] = {{file_404_html, data_index_html, data_index_html + 12, sizeof(data_index_html) - 12}};
4396 +const struct fsdata_file file_flash_html[] = {{file_index_html, data_flash_html, data_flash_html + 12, sizeof(data_flash_html) - 12}};
4398 +#define FS_ROOT file_flash_html
4400 +#define FS_NUMFILES 5
4401 \ No newline at end of file
4403 +++ b/net/uip-0.9/fsdata.h
4406 + * Copyright (c) 2001, Swedish Institute of Computer Science.
4407 + * All rights reserved.
4409 + * Redistribution and use in source and binary forms, with or without
4410 + * modification, are permitted provided that the following conditions
4412 + * 1. Redistributions of source code must retain the above copyright
4413 + * notice, this list of conditions and the following disclaimer.
4414 + * 2. Redistributions in binary form must reproduce the above copyright
4415 + * notice, this list of conditions and the following disclaimer in the
4416 + * documentation and/or other materials provided with the distribution.
4417 + * 3. Neither the name of the Institute nor the names of its contributors
4418 + * may be used to endorse or promote products derived from this software
4419 + * without specific prior written permission.
4421 + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
4422 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4423 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4424 + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
4425 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4426 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4427 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4428 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4429 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4430 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4433 + * This file is part of the lwIP TCP/IP stack.
4435 + * Author: Adam Dunkels <adam@sics.se>
4437 + * $Id: fsdata.h,v 1.4.2.1 2003/10/04 22:54:06 adam Exp $
4439 +#ifndef __FSDATA_H__
4440 +#define __FSDATA_H__
4442 +#include "uipopt.h"
4444 +struct fsdata_file {
4445 + const struct fsdata_file *next;
4449 +#ifdef FS_STATISTICS
4450 +#if FS_STATISTICS == 1
4452 +#endif /* FS_STATISTICS */
4453 +#endif /* FS_STATISTICS */
4456 +struct fsdata_file_noconst {
4457 + struct fsdata_file *next;
4461 +#ifdef FS_STATISTICS
4462 +#if FS_STATISTICS == 1
4464 +#endif /* FS_STATISTICS */
4465 +#endif /* FS_STATISTICS */
4468 +#endif /* __FSDATA_H__ */
4470 +++ b/net/uip-0.9/fs.h
4473 + * \addtogroup httpd
4479 + * HTTP server read-only file system header file.
4480 + * \author Adam Dunkels <adam@dunkels.com>
4484 + * Copyright (c) 2001, Swedish Institute of Computer Science.
4485 + * All rights reserved.
4487 + * Redistribution and use in source and binary forms, with or without
4488 + * modification, are permitted provided that the following conditions
4490 + * 1. Redistributions of source code must retain the above copyright
4491 + * notice, this list of conditions and the following disclaimer.
4492 + * 2. Redistributions in binary form must reproduce the above copyright
4493 + * notice, this list of conditions and the following disclaimer in the
4494 + * documentation and/or other materials provided with the distribution.
4495 + * 3. Neither the name of the Institute nor the names of its contributors
4496 + * may be used to endorse or promote products derived from this software
4497 + * without specific prior written permission.
4499 + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
4500 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4501 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4502 + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
4503 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4504 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4505 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4506 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4507 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4508 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4511 + * This file is part of the lwIP TCP/IP stack.
4513 + * Author: Adam Dunkels <adam@sics.se>
4515 + * $Id: fs.h,v 1.6.2.3 2003/10/07 13:22:27 adam Exp $
4523 + * An open file in the read-only file system.
4526 + char *data; /**< The actual file data. */
4527 + int len; /**< The length of the file data. */
4531 + * Open a file in the read-only file system.
4533 + * \param name The name of the file.
4535 + * \param file The file pointer, which must be allocated by caller and
4536 + * will be filled in by the function.
4538 +int fs_open(const char *name, struct fs_file *file);
4540 +#ifdef FS_STATISTICS
4541 +#if FS_STATISTICS == 1
4542 +u16_t fs_count(char *name);
4543 +#endif /* FS_STATISTICS */
4544 +#endif /* FS_STATISTICS */
4547 + * Initialize the read-only file system.
4549 +void fs_init(void);
4551 +#endif /* __FS_H__ */
4553 +++ b/net/uip-0.9/httpd.c
4558 +#include "fsdata.h"
4559 +#include <asm/addrspace.h>
4561 +#define HTTP_NONE 0
4562 +#define HTTP_FILE 1
4563 +#define HTTP_FIRMWARE 2
4565 +#define PRINT(x) printf("%s", x)
4566 +#define PRINTLN(x) printf("%s\n", x)
4568 +extern unsigned long do_http_tmp_address(void);
4570 +struct httpd_state *hs;
4572 +extern const struct fsdata_file file_index_html;
4573 +extern const struct fsdata_file file_404_html;
4574 +extern const struct fsdata_file file_flash_html;
4575 +extern int httpd_upload_complete;
4576 +extern unsigned char *httpd_upload_data;
4577 +unsigned char *upload_data;
4578 +extern ulong NetBootFileXferSize;
4579 +int upload_running = 0;
4588 +#define ISO_slash 0x2f
4592 +#define ISO_space 0x20
4593 +#define ISO_nl 0x0a
4594 +#define ISO_cr 0x0d
4597 +#define ISO_hash 0x23
4598 +#define ISO_period 0x2e
4600 +static char eol[3] = { 0x0d, 0x0a, 0x00 };
4601 +static char eol2[5] = { 0x0d, 0x0a, 0x0d, 0x0a, 0x00 };
4602 +static char boundary[128];
4603 +static int boundary_len = 0;
4605 +/* we use this so that we can do without the ctype library */
4606 +#define is_digit(c) ((c) >= '0' && (c) <= '9')
4607 +static int atoi(const char *s)
4611 + while (is_digit(*s))
4612 + i = i*10 + *(s++) - '0';
4620 + uip_listen(HTONS(80));
4624 +httpd_appcall(void)
4626 + struct fs_file fsfile;
4628 + switch(uip_conn->lport) {
4630 + hs = (struct httpd_state *)(uip_conn->appstate);
4631 + if(uip_connected())
4633 + hs->state = HTTP_NONE;
4636 + } else if(uip_poll())
4638 + if(hs->count++ >= 1000) {
4642 + } else if(uip_newdata() && hs->state == HTTP_NONE)
4644 + if(uip_appdata[0] == ISO_G &&
4645 + uip_appdata[1] == ISO_E &&
4646 + uip_appdata[2] == ISO_T &&
4647 + uip_appdata[3] == ISO_space)
4649 + hs->state = HTTP_FILE;
4651 + if(uip_appdata[0] == ISO_P &&
4652 + uip_appdata[1] == ISO_O &&
4653 + uip_appdata[2] == ISO_S &&
4654 + uip_appdata[3] == ISO_T &&
4655 + uip_appdata[4] == ISO_space)
4657 + hs->state = HTTP_FIRMWARE;
4659 + if(hs->state == HTTP_NONE)
4664 + if(hs->state == HTTP_FILE)
4666 + for(i = 4; i < 40; ++i)
4668 + if(uip_appdata[i] == ISO_space ||
4669 + uip_appdata[i] == ISO_cr ||
4670 + uip_appdata[i] == ISO_nl)
4672 + uip_appdata[i] = 0;
4677 + PRINT("request for file ");
4678 + PRINTLN(&uip_appdata[4]);
4679 + if(uip_appdata[4] == ISO_slash &&
4680 + uip_appdata[5] == 0)
4682 + fs_open(file_index_html.name, &fsfile);
4684 + if(!fs_open((const char *)&uip_appdata[4], &fsfile))
4686 + PRINTLN("couldn't open file");
4687 + fs_open(file_index_html.name, &fsfile);
4691 + hs->state = HTTP_FILE;
4692 + hs->dataptr = fsfile.data;
4693 + hs->count = fsfile.len;
4695 + if(hs->state == HTTP_FIRMWARE)
4697 + unsigned char *start = (unsigned char*)uip_appdata;
4698 + char *clen = strstr(start, "Content-Length:");
4700 + unsigned char *next, *end;
4701 + unsigned char *boundary_start;
4703 + uip_appdata[uip_len] = '\0';
4706 + clen += sizeof("Content-Length:");
4707 + next = strstr(clen, eol);
4712 + printf("expecting %d bytes\n", len);
4713 + upload_data = httpd_upload_data = (unsigned char *)do_http_tmp_address();
4714 + printf("received data will be stored at 0x%08X\n", upload_data);
4717 + printf("failed to allocate memory\n");
4726 + if(len < 4 * 1024)
4731 + boundary_start = strstr(next, "---");
4732 + if(!boundary_start)
4737 + end = strstr(boundary_start, eol);
4743 + boundary_len = end - boundary_start;
4744 + memcpy(boundary, boundary_start, boundary_len);
4745 + boundary[boundary_len] = 0;
4746 + next = strstr(boundary_start, "name=\"firmware\";");
4752 + next = strstr(next, eol2);
4755 + printf("could not find start of data\n");
4761 + hs->state = HTTP_FIRMWARE;
4762 + hs->upload = uip_len - (next - start);
4763 + hs->upload_total = len - (int)(next - boundary_start);
4764 + hs->upload_total -= (strlen(boundary) + 6);
4765 + //printf("storing %d bytes at %p\n", (int)hs->upload, upload_data);
4766 + for(i = 0; i < hs->upload; i++)
4767 + upload_data[i] = next[i];
4768 + upload_data += (int)hs->upload;
4769 + printf("%d / %d\n", (int)hs->upload, hs->upload_total);
4775 + if(hs->state == HTTP_FIRMWARE)
4781 + uip_appdata[uip_len] = '\0';
4782 + hs->upload += uip_len;
4783 + //printf("storing %d bytes at %p\n", uip_len, upload_data);
4784 + printf("%d / %d\n", (int)hs->upload, hs->upload_total);
4785 + for(i = 0; i < uip_len; i++)
4786 + upload_data[i] = uip_appdata[i];
4787 + upload_data += uip_len;
4789 + if(hs->upload >= hs->upload_total)
4791 + upload_running = 1;
4792 + NetBootFileXferSize = hs->upload_total;
4793 + fs_open(file_flash_html.name, &fsfile);
4795 + hs->state = HTTP_FILE;
4796 + hs->dataptr = fsfile.data;
4797 + hs->count = fsfile.len;
4801 + if(hs->state == HTTP_FILE)
4805 + if(hs->count >= uip_conn->len)
4807 + hs->count -= uip_conn->len;
4808 + hs->dataptr += uip_conn->len;
4812 + if(hs->count == 0)
4814 + if(upload_running)
4817 + httpd_upload_complete = 1;
4818 + // for(i = 0; i < hs->upload_total; i++)
4819 + // printf("%c", httpd_upload_data[i]);
4824 + uip_send(hs->dataptr, hs->count);
4834 +++ b/net/uip-0.9/httpd.h
4837 + * \addtogroup httpd
4843 + * HTTP server header file.
4844 + * \author Adam Dunkels <adam@dunkels.com>
4848 + * Copyright (c) 2001, Adam Dunkels.
4849 + * All rights reserved.
4851 + * Redistribution and use in source and binary forms, with or without
4852 + * modification, are permitted provided that the following conditions
4854 + * 1. Redistributions of source code must retain the above copyright
4855 + * notice, this list of conditions and the following disclaimer.
4856 + * 2. Redistributions in binary form must reproduce the above copyright
4857 + * notice, this list of conditions and the following disclaimer in the
4858 + * documentation and/or other materials provided with the distribution.
4859 + * 3. The name of the author may not be used to endorse or promote
4860 + * products derived from this software without specific prior
4861 + * written permission.
4863 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
4864 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
4865 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4866 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
4867 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4868 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
4869 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4870 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4871 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
4872 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4873 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4875 + * This file is part of the uIP TCP/IP stack.
4877 + * $Id: httpd.h,v 1.4.2.3 2003/10/06 22:56:44 adam Exp $
4881 +#ifndef __HTTPD_H__
4882 +#define __HTTPD_H__
4884 +void httpd_init(void);
4885 +void httpd_appcall(void);
4887 +/* UIP_APPCALL: the name of the application function. This function
4888 + must return void and take no arguments (i.e., C type "void
4889 + appfunc(void)"). */
4890 +#ifndef UIP_APPCALL
4891 +#define UIP_APPCALL httpd_appcall
4894 +struct httpd_state {
4899 + unsigned int upload;
4900 + unsigned int upload_total;
4904 +/* UIP_APPSTATE_SIZE: The size of the application-specific state
4905 + stored in the uip_conn structure. */
4906 +#ifndef UIP_APPSTATE_SIZE
4907 +#define UIP_APPSTATE_SIZE (sizeof(struct httpd_state))
4910 +#define FS_STATISTICS 1
4912 +extern struct httpd_state *hs;
4915 +/* we copy the data to RAM+10MB */
4916 +#define TMP_DATA 0x8A100000
4918 +#endif /* __HTTPD_H__ */
4920 +++ b/net/uip-0.9/main.c
4923 + * Copyright (c) 2001-2003, Adam Dunkels.
4924 + * All rights reserved.
4926 + * Redistribution and use in source and binary forms, with or without
4927 + * modification, are permitted provided that the following conditions
4929 + * 1. Redistributions of source code must retain the above copyright
4930 + * notice, this list of conditions and the following disclaimer.
4931 + * 2. Redistributions in binary form must reproduce the above copyright
4932 + * notice, this list of conditions and the following disclaimer in the
4933 + * documentation and/or other materials provided with the distribution.
4934 + * 3. The name of the author may not be used to endorse or promote
4935 + * products derived from this software without specific prior
4936 + * written permission.
4938 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
4939 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
4940 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4941 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
4942 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4943 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
4944 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4945 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
4946 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
4947 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4948 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4950 + * This file is part of the uIP TCP/IP stack.
4952 + * $Id: main.c,v 1.10.2.1 2003/10/04 22:54:17 adam Exp $
4958 +#include "uip_arp.h"
4959 +#include "tapdev.h"
4962 +#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
4965 +#define NULL (void *)0
4968 +/*-----------------------------------------------------------------------------------*/
4978 + uip_len = tapdev_read();
4979 + if(uip_len == 0) {
4980 + for(i = 0; i < UIP_CONNS; i++) {
4988 + if(++arptimer == 20) {
4993 + if(BUF->type == htons(UIP_ETHTYPE_IP)) {
5000 + } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
5011 +++ b/net/uip-0.9/Makefile
5013 +# Copyright (c) 2001, Adam Dunkels.
5014 +# All rights reserved.
5016 +# Redistribution and use in source and binary forms, with or without
5017 +# modification, are permitted provided that the following conditions
5019 +# 1. Redistributions of source code must retain the above copyright
5020 +# notice, this list of conditions and the following disclaimer.
5021 +# 2. Redistributions in binary form must reproduce the above copyright
5022 +# notice, this list of conditions and the following disclaimer in the
5023 +# documentation and/or other materials provided with the distribution.
5024 +# 3. All advertising materials mentioning features or use of this software
5025 +# must display the following acknowledgement:
5026 +# This product includes software developed by Adam Dunkels.
5027 +# 4. The name of the author may not be used to endorse or promote
5028 +# products derived from this software without specific prior
5029 +# written permission.
5031 +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
5032 +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5033 +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5034 +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
5035 +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5036 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5037 +# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5038 +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5039 +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5040 +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5041 +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5043 +# This file is part of the uIP TCP/IP stack.
5045 +# $Id: Makefile,v 1.8.2.2 2003/10/04 22:54:17 adam Exp $
5049 +CFLAGS=-Wall -fpack-struct -DDUMP=0
5053 +uip: uip.o uip_arch.o tapdev.o httpd.o main.o fs.o uip_arp.o
5054 + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
5057 + $(CC) $(CFLAGS) -c $^ -o $@
5060 + rm -f *.o *~ *core uip
5068 +++ b/net/uip-0.9/tapdev.c
5071 + * Copyright (c) 2001, Swedish Institute of Computer Science.
5072 + * All rights reserved.
5074 + * Redistribution and use in source and binary forms, with or without
5075 + * modification, are permitted provided that the following conditions
5078 + * 1. Redistributions of source code must retain the above copyright
5079 + * notice, this list of conditions and the following disclaimer.
5081 + * 2. Redistributions in binary form must reproduce the above copyright
5082 + * notice, this list of conditions and the following disclaimer in the
5083 + * documentation and/or other materials provided with the distribution.
5085 + * 3. Neither the name of the Institute nor the names of its contributors
5086 + * may be used to endorse or promote products derived from this software
5087 + * without specific prior written permission.
5089 + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
5090 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5091 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5092 + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
5093 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5094 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5095 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5096 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5097 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5098 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5101 + * Author: Adam Dunkels <adam@sics.se>
5103 + * $Id: tapdev.c,v 1.7.2.1 2003/10/07 13:23:19 adam Exp $
5108 +#include <stdlib.h>
5110 +#include <unistd.h>
5111 +#include <string.h>
5112 +#include <sys/ioctl.h>
5113 +#include <sys/socket.h>
5114 +#include <sys/types.h>
5115 +#include <sys/time.h>
5116 +#include <sys/uio.h>
5117 +#include <sys/socket.h>
5120 +#include <sys/ioctl.h>
5121 +#include <linux/if.h>
5122 +#include <linux/if_tun.h>
5123 +#define DEVTAP "/dev/net/tun"
5125 +#define DEVTAP "/dev/tap0"
5132 +static unsigned long lasttime;
5133 +static struct timezone tz;
5135 +/*-----------------------------------------------------------------------------------*/
5141 + fd = open(DEVTAP, O_RDWR);
5143 + perror("tapdev: tapdev_init: open");
5150 + memset(&ifr, 0, sizeof(ifr));
5151 + ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
5152 + if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
5159 + snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d",
5160 + UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3);
5166 +void dump_mem(int type, int len)
5170 + for(i = 0; i < len; i++)
5171 + printf("%c", uip_buf[i]);
5174 + printf("\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01");
5175 + printf("\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01");
5177 + printf("\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02");
5178 + printf("\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02");
5184 +/*-----------------------------------------------------------------------------------*/
5189 + struct timeval tv, now;
5192 + if(lasttime >= 500000) {
5198 + tv.tv_usec = 500000 - lasttime;
5202 + FD_SET(fd, &fdset);
5204 + gettimeofday(&now, &tz);
5205 + ret = select(fd + 1, &fdset, NULL, NULL, &tv);
5210 + ret = read(fd, uip_buf, UIP_BUFSIZE);
5212 + perror("tap_dev: tapdev_read: read");
5214 + gettimeofday(&tv, &tz);
5215 + lasttime += (tv.tv_sec - now.tv_sec) * 1000000 + (tv.tv_usec - now.tv_usec);
5219 +/*-----------------------------------------------------------------------------------*/
5224 + struct iovec iov[2];
5228 + char tmpbuf[UIP_BUFSIZE];
5231 + for(i = 0; i < 40 + UIP_LLH_LEN; i++) {
5232 + tmpbuf[i] = uip_buf[i];
5235 + for(; i < uip_len; i++) {
5236 + tmpbuf[i] = uip_appdata[i - 40 - UIP_LLH_LEN];
5239 + ret = write(fd, tmpbuf, uip_len);
5243 + if(uip_len < 40 + UIP_LLH_LEN) {
5244 + ret = write(fd, uip_buf, uip_len + UIP_LLH_LEN);
5246 + iov[0].iov_base = uip_buf;
5247 + iov[0].iov_len = 40 + UIP_LLH_LEN;
5248 + iov[1].iov_base = (char *)uip_appdata;
5249 + iov[1].iov_len = uip_len - (40 + UIP_LLH_LEN);
5251 + ret = writev(fd, iov, 2);
5257 + perror("tap_dev: tapdev_send: writev");
5261 +/*-----------------------------------------------------------------------------------*/
5263 +++ b/net/uip-0.9/tapdev.h
5266 + * Copyright (c) 2001, Adam Dunkels.
5267 + * All rights reserved.
5269 + * Redistribution and use in source and binary forms, with or without
5270 + * modification, are permitted provided that the following conditions
5272 + * 1. Redistributions of source code must retain the above copyright
5273 + * notice, this list of conditions and the following disclaimer.
5274 + * 2. Redistributions in binary form must reproduce the above copyright
5275 + * notice, this list of conditions and the following disclaimer in the
5276 + * documentation and/or other materials provided with the distribution.
5277 + * 3. The name of the author may not be used to endorse or promote
5278 + * products derived from this software without specific prior
5279 + * written permission.
5281 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
5282 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5283 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5284 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
5285 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5286 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5287 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5288 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5289 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5290 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5291 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5293 + * This file is part of the uIP TCP/IP stack.
5295 + * $Id: tapdev.h,v 1.1.2.1 2003/10/04 22:54:17 adam Exp $
5299 +#ifndef __TAPDEV_H__
5300 +#define __TAPDEV_H__
5302 +void tapdev_init(void);
5303 +unsigned int tapdev_read(void);
5304 +void tapdev_send(void);
5306 +#endif /* __TAPDEV_H__ */
5308 +++ b/net/uip-0.9/uip_arch.c
5311 + * Copyright (c) 2001, Adam Dunkels.
5312 + * All rights reserved.
5314 + * Redistribution and use in source and binary forms, with or without
5315 + * modification, are permitted provided that the following conditions
5317 + * 1. Redistributions of source code must retain the above copyright
5318 + * notice, this list of conditions and the following disclaimer.
5319 + * 2. Redistributions in binary form must reproduce the above copyright
5320 + * notice, this list of conditions and the following disclaimer in the
5321 + * documentation and/or other materials provided with the distribution.
5322 + * 3. The name of the author may not be used to endorse or promote
5323 + * products derived from this software without specific prior
5324 + * written permission.
5326 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
5327 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5328 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5329 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
5330 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5331 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5332 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5333 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5334 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5335 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5336 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5338 + * This file is part of the uIP TCP/IP stack.
5340 + * $Id: uip_arch.c,v 1.2.2.1 2003/10/04 22:54:17 adam Exp $
5346 +#include "uip_arch.h"
5348 +#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
5349 +#define IP_PROTO_TCP 6
5351 +/*-----------------------------------------------------------------------------------*/
5353 +uip_add32(u8_t *op32, u16_t op16)
5356 + uip_acc32[3] = op32[3] + (op16 & 0xff);
5357 + uip_acc32[2] = op32[2] + (op16 >> 8);
5358 + uip_acc32[1] = op32[1];
5359 + uip_acc32[0] = op32[0];
5361 + if(uip_acc32[2] < (op16 >> 8)) {
5363 + if(uip_acc32[1] == 0) {
5369 + if(uip_acc32[3] < (op16 & 0xff)) {
5371 + if(uip_acc32[2] == 0) {
5373 + if(uip_acc32[1] == 0) {
5379 +/*-----------------------------------------------------------------------------------*/
5381 +uip_chksum(u16_t *sdata, u16_t len)
5385 + for(acc = 0; len > 1; len -= 2) {
5387 + if(acc < *sdata) {
5388 + /* Overflow, so we add the carry to acc (i.e., increase by
5395 + /* add up any odd byte */
5397 + acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
5398 + if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
5405 +/*-----------------------------------------------------------------------------------*/
5409 + return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], 20);
5411 +/*-----------------------------------------------------------------------------------*/
5413 +uip_tcpchksum(void)
5418 + /* Compute the checksum of the TCP header. */
5419 + hsum = uip_chksum((u16_t *)&uip_buf[20 + UIP_LLH_LEN], 20);
5421 + /* Compute the checksum of the data in the TCP packet and add it to
5422 + the TCP header checksum. */
5423 + sum = uip_chksum((u16_t *)uip_appdata,
5424 + (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 40)));
5426 + if((sum += hsum) < hsum) {
5430 + if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
5433 + if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
5436 + if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
5439 + if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
5442 + if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
5446 + hsum = (u16_t)htons((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 20);
5448 + if((sum += hsum) < hsum) {
5454 +/*-----------------------------------------------------------------------------------*/
5456 +++ b/net/uip-0.9/uip_arch.h
5459 + * \defgroup uiparch Architecture specific uIP functions
5462 + * The functions in the architecture specific module implement the IP
5463 + * check sum and 32-bit additions.
5465 + * The IP checksum calculation is the most computationally expensive
5466 + * operation in the TCP/IP stack and it therefore pays off to
5467 + * implement this in efficient assembler. The purpose of the uip-arch
5468 + * module is to let the checksum functions to be implemented in
5469 + * architecture specific assembler.
5475 + * Declarations of architecture specific functions.
5476 + * \author Adam Dunkels <adam@dunkels.com>
5480 + * Copyright (c) 2001, Adam Dunkels.
5481 + * All rights reserved.
5483 + * Redistribution and use in source and binary forms, with or without
5484 + * modification, are permitted provided that the following conditions
5486 + * 1. Redistributions of source code must retain the above copyright
5487 + * notice, this list of conditions and the following disclaimer.
5488 + * 2. Redistributions in binary form must reproduce the above copyright
5489 + * notice, this list of conditions and the following disclaimer in the
5490 + * documentation and/or other materials provided with the distribution.
5491 + * 3. The name of the author may not be used to endorse or promote
5492 + * products derived from this software without specific prior
5493 + * written permission.
5495 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
5496 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5497 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5498 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
5499 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5500 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5501 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5502 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5503 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5504 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5505 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5507 + * This file is part of the uIP TCP/IP stack.
5509 + * $Id: uip_arch.h,v 1.1.2.2 2003/10/06 15:10:22 adam Exp $
5513 +#ifndef __UIP_ARCH_H__
5514 +#define __UIP_ARCH_H__
5519 + * Carry out a 32-bit addition.
5521 + * Because not all architectures for which uIP is intended has native
5522 + * 32-bit arithmetic, uIP uses an external C function for doing the
5523 + * required 32-bit additions in the TCP protocol processing. This
5524 + * function should add the two arguments and place the result in the
5525 + * global variable uip_acc32.
5527 + * \note The 32-bit integer pointed to by the op32 parameter and the
5528 + * result in the uip_acc32 variable are in network byte order (big
5531 + * \param op32 A pointer to a 4-byte array representing a 32-bit
5532 + * integer in network byte order (big endian).
5534 + * \param op16 A 16-bit integer in host byte order.
5536 +void uip_add32(u8_t *op32, u16_t op16);
5539 + * Calculate the Internet checksum over a buffer.
5541 + * The Internet checksum is the one's complement of the one's
5542 + * complement sum of all 16-bit words in the buffer.
5546 + * \note This function is not called in the current version of uIP,
5547 + * but future versions might make use of it.
5549 + * \param buf A pointer to the buffer over which the checksum is to be
5552 + * \param len The length of the buffer over which the checksum is to
5555 + * \return The Internet checksum of the buffer.
5557 +u16_t uip_chksum(u16_t *buf, u16_t len);
5560 + * Calculate the IP header checksum of the packet header in uip_buf.
5562 + * The IP header checksum is the Internet checksum of the 20 bytes of
5565 + * \return The IP header checksum of the IP header in the uip_buf
5568 +u16_t uip_ipchksum(void);
5571 + * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
5573 + * The TCP checksum is the Internet checksum of data contents of the
5574 + * TCP segment, and a pseudo-header as defined in RFC793.
5576 + * \note The uip_appdata pointer that points to the packet data may
5577 + * point anywhere in memory, so it is not possible to simply calculate
5578 + * the Internet checksum of the contents of the uip_buf buffer.
5580 + * \return The TCP checksum of the TCP segment in uip_buf and pointed
5581 + * to by uip_appdata.
5583 +u16_t uip_tcpchksum(void);
5587 +#endif /* __UIP_ARCH_H__ */
5589 +++ b/net/uip-0.9/uip_arp.c
5597 + * \defgroup uiparp uIP Address Resolution Protocol
5600 + * The Address Resolution Protocol ARP is used for mapping between IP
5601 + * addresses and link level addresses such as the Ethernet MAC
5602 + * addresses. ARP uses broadcast queries to ask for the link level
5603 + * address of a known IP address and the host which is configured with
5604 + * the IP address for which the query was meant, will respond with its
5605 + * link level address.
5607 + * \note This ARP implementation only supports Ethernet.
5612 + * Implementation of the ARP Address Resolution Protocol.
5613 + * \author Adam Dunkels <adam@dunkels.com>
5618 + * Copyright (c) 2001-2003, Adam Dunkels.
5619 + * All rights reserved.
5621 + * Redistribution and use in source and binary forms, with or without
5622 + * modification, are permitted provided that the following conditions
5624 + * 1. Redistributions of source code must retain the above copyright
5625 + * notice, this list of conditions and the following disclaimer.
5626 + * 2. Redistributions in binary form must reproduce the above copyright
5627 + * notice, this list of conditions and the following disclaimer in the
5628 + * documentation and/or other materials provided with the distribution.
5629 + * 3. The name of the author may not be used to endorse or promote
5630 + * products derived from this software without specific prior
5631 + * written permission.
5633 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
5634 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5635 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5636 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
5637 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5638 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5639 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5640 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5641 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5642 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5643 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5645 + * This file is part of the uIP TCP/IP stack.
5647 + * $Id: uip_arp.c,v 1.7.2.3 2003/10/06 22:42:30 adam Exp $
5652 +#include "uip_arp.h"
5655 + struct uip_eth_hdr ethhdr;
5661 + struct uip_eth_addr shwaddr;
5663 + struct uip_eth_addr dhwaddr;
5668 + struct uip_eth_hdr ethhdr;
5678 + u16_t srcipaddr[2],
5682 +#define ARP_REQUEST 1
5683 +#define ARP_REPLY 2
5685 +#define ARP_HWTYPE_ETH 1
5689 + struct uip_eth_addr ethaddr;
5693 +struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
5700 +static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
5701 +static u16_t ipaddr[2];
5704 +static u8_t arptime;
5705 +static u8_t tmpage;
5707 +#define BUF ((struct arp_hdr *)&uip_buf[0])
5708 +#define IPBUF ((struct ethip_hdr *)&uip_buf[0])
5709 +/*-----------------------------------------------------------------------------------*/
5711 + * Initialize the ARP module.
5714 +/*-----------------------------------------------------------------------------------*/
5718 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5719 + memset(arp_table[i].ipaddr, 0, 4);
5722 +/*-----------------------------------------------------------------------------------*/
5724 + * Periodic ARP processing function.
5726 + * This function performs periodic timer processing in the ARP module
5727 + * and should be called at regular intervals. The recommended interval
5728 + * is 10 seconds between the calls.
5731 +/*-----------------------------------------------------------------------------------*/
5733 +uip_arp_timer(void)
5735 + struct arp_entry *tabptr;
5738 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5739 + tabptr = &arp_table[i];
5740 + if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
5741 + arptime - tabptr->time >= UIP_ARP_MAXAGE) {
5742 + memset(tabptr->ipaddr, 0, 4);
5747 +/*-----------------------------------------------------------------------------------*/
5749 +uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
5751 + register struct arp_entry *tabptr;
5752 + /* Walk through the ARP mapping table and try to find an entry to
5753 + update. If none is found, the IP -> MAC address mapping is
5754 + inserted in the ARP table. */
5755 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5757 + tabptr = &arp_table[i];
5758 + /* Only check those entries that are actually in use. */
5759 + if(tabptr->ipaddr[0] != 0 &&
5760 + tabptr->ipaddr[1] != 0) {
5762 + /* Check if the source IP address of the incoming packet matches
5763 + the IP address in this ARP table entry. */
5764 + if(ipaddr[0] == tabptr->ipaddr[0] &&
5765 + ipaddr[1] == tabptr->ipaddr[1]) {
5767 + /* An old entry found, update this and return. */
5768 + memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
5769 + tabptr->time = arptime;
5776 + /* If we get here, no existing ARP table entry was found, so we
5779 + /* First, we try to find an unused entry in the ARP table. */
5780 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5781 + tabptr = &arp_table[i];
5782 + if(tabptr->ipaddr[0] == 0 &&
5783 + tabptr->ipaddr[1] == 0) {
5788 + /* If no unused entry is found, we try to find the oldest entry and
5790 + if(i == UIP_ARPTAB_SIZE) {
5793 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5794 + tabptr = &arp_table[i];
5795 + if(arptime - tabptr->time > tmpage) {
5796 + tmpage = arptime - tabptr->time;
5803 + /* Now, i is the ARP table entry which we will fill with the new
5805 + memcpy(tabptr->ipaddr, ipaddr, 4);
5806 + memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
5807 + tabptr->time = arptime;
5809 +/*-----------------------------------------------------------------------------------*/
5811 + * ARP processing for incoming IP packets
5813 + * This function should be called by the device driver when an IP
5814 + * packet has been received. The function will check if the address is
5815 + * in the ARP cache, and if so the ARP cache entry will be
5816 + * refreshed. If no ARP cache entry was found, a new one is created.
5818 + * This function expects an IP packet with a prepended Ethernet header
5819 + * in the uip_buf[] buffer, and the length of the packet in the global
5820 + * variable uip_len.
5822 +/*-----------------------------------------------------------------------------------*/
5826 + uip_len -= sizeof(struct uip_eth_hdr);
5828 + /* Only insert/update an entry if the source IP address of the
5829 + incoming IP packet comes from a host on the local network. */
5830 + if((IPBUF->srcipaddr[0] & uip_arp_netmask[0]) !=
5831 + (uip_hostaddr[0] & uip_arp_netmask[0])) {
5834 + if((IPBUF->srcipaddr[1] & uip_arp_netmask[1]) !=
5835 + (uip_hostaddr[1] & uip_arp_netmask[1])) {
5838 + uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
5842 +/*-----------------------------------------------------------------------------------*/
5844 + * ARP processing for incoming ARP packets.
5846 + * This function should be called by the device driver when an ARP
5847 + * packet has been received. The function will act differently
5848 + * depending on the ARP packet type: if it is a reply for a request
5849 + * that we previously sent out, the ARP cache will be filled in with
5850 + * the values from the ARP reply. If the incoming ARP packet is an ARP
5851 + * request for our IP address, an ARP reply packet is created and put
5852 + * into the uip_buf[] buffer.
5854 + * When the function returns, the value of the global variable uip_len
5855 + * indicates whether the device driver should send out a packet or
5856 + * not. If uip_len is zero, no packet should be sent. If uip_len is
5857 + * non-zero, it contains the length of the outbound packet that is
5858 + * present in the uip_buf[] buffer.
5860 + * This function expects an ARP packet with a prepended Ethernet
5861 + * header in the uip_buf[] buffer, and the length of the packet in the
5862 + * global variable uip_len.
5864 +/*-----------------------------------------------------------------------------------*/
5866 +uip_arp_arpin(void)
5869 + if(uip_len < sizeof(struct arp_hdr)) {
5876 + switch(BUF->opcode) {
5877 + case HTONS(ARP_REQUEST):
5878 + /* ARP request. If it asked for our address, we send out a
5880 + if(BUF->dipaddr[0] == uip_hostaddr[0] &&
5881 + BUF->dipaddr[1] == uip_hostaddr[1]) {
5882 + /* The reply opcode is 2. */
5883 + BUF->opcode = HTONS(2);
5885 + memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
5886 + memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
5887 + memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
5888 + memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
5890 + BUF->dipaddr[0] = BUF->sipaddr[0];
5891 + BUF->dipaddr[1] = BUF->sipaddr[1];
5892 + BUF->sipaddr[0] = uip_hostaddr[0];
5893 + BUF->sipaddr[1] = uip_hostaddr[1];
5895 + BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
5896 + uip_len = sizeof(struct arp_hdr);
5899 + case HTONS(ARP_REPLY):
5900 + /* ARP reply. We insert or update the ARP table if it was meant
5902 + if(BUF->dipaddr[0] == uip_hostaddr[0] &&
5903 + BUF->dipaddr[1] == uip_hostaddr[1]) {
5905 + uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
5912 +/*-----------------------------------------------------------------------------------*/
5914 + * Prepend Ethernet header to an outbound IP packet and see if we need
5915 + * to send out an ARP request.
5917 + * This function should be called before sending out an IP packet. The
5918 + * function checks the destination IP address of the IP packet to see
5919 + * what Ethernet MAC address that should be used as a destination MAC
5920 + * address on the Ethernet.
5922 + * If the destination IP address is in the local network (determined
5923 + * by logical ANDing of netmask and our IP address), the function
5924 + * checks the ARP cache to see if an entry for the destination IP
5925 + * address is found. If so, an Ethernet header is prepended and the
5926 + * function returns. If no ARP cache entry is found for the
5927 + * destination IP address, the packet in the uip_buf[] is replaced by
5928 + * an ARP request packet for the IP address. The IP packet is dropped
5929 + * and it is assumed that they higher level protocols (e.g., TCP)
5930 + * eventually will retransmit the dropped packet.
5932 + * If the destination IP address is not on the local network, the IP
5933 + * address of the default router is used instead.
5935 + * When the function returns, a packet is present in the uip_buf[]
5936 + * buffer, and the length of the packet is in the global variable
5939 +/*-----------------------------------------------------------------------------------*/
5943 + struct arp_entry *tabptr;
5944 + /* Find the destination IP address in the ARP table and construct
5945 + the Ethernet header. If the destination IP addres isn't on the
5946 + local network, we use the default router's IP address instead.
5948 + If not ARP table entry is found, we overwrite the original IP
5949 + packet with an ARP request for the IP address. */
5951 + /* Check if the destination address is on the local network. */
5952 + if((IPBUF->destipaddr[0] & uip_arp_netmask[0]) !=
5953 + (uip_hostaddr[0] & uip_arp_netmask[0]) ||
5954 + (IPBUF->destipaddr[1] & uip_arp_netmask[1]) !=
5955 + (uip_hostaddr[1] & uip_arp_netmask[1])) {
5956 + /* Destination address was not on the local network, so we need to
5957 + use the default router's IP address instead of the destination
5958 + address when determining the MAC address. */
5959 + ipaddr[0] = uip_arp_draddr[0];
5960 + ipaddr[1] = uip_arp_draddr[1];
5962 + /* Else, we use the destination IP address. */
5963 + ipaddr[0] = IPBUF->destipaddr[0];
5964 + ipaddr[1] = IPBUF->destipaddr[1];
5967 + for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
5968 + tabptr = &arp_table[i];
5969 + if(ipaddr[0] == tabptr->ipaddr[0] &&
5970 + ipaddr[1] == tabptr->ipaddr[1])
5974 + if(i == UIP_ARPTAB_SIZE) {
5975 + /* The destination address was not in our ARP table, so we
5976 + overwrite the IP packet with an ARP request. */
5978 + memset(BUF->ethhdr.dest.addr, 0xff, 6);
5979 + memset(BUF->dhwaddr.addr, 0x00, 6);
5980 + memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
5981 + memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
5983 + BUF->dipaddr[0] = ipaddr[0];
5984 + BUF->dipaddr[1] = ipaddr[1];
5985 + BUF->sipaddr[0] = uip_hostaddr[0];
5986 + BUF->sipaddr[1] = uip_hostaddr[1];
5987 + BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
5988 + BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
5989 + BUF->protocol = HTONS(UIP_ETHTYPE_IP);
5991 + BUF->protolen = 4;
5992 + BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
5994 + uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
5996 + uip_len = sizeof(struct arp_hdr);
6000 + /* Build an ethernet header. */
6001 + memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
6002 + memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
6004 + IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
6006 + uip_len += sizeof(struct uip_eth_hdr);
6008 +/*-----------------------------------------------------------------------------------*/
6013 +++ b/net/uip-0.9/uip_arp.h
6021 + * \addtogroup uiparp
6027 + * Macros and definitions for the ARP module.
6028 + * \author Adam Dunkels <adam@dunkels.com>
6033 + * Copyright (c) 2001-2003, Adam Dunkels.
6034 + * All rights reserved.
6036 + * Redistribution and use in source and binary forms, with or without
6037 + * modification, are permitted provided that the following conditions
6039 + * 1. Redistributions of source code must retain the above copyright
6040 + * notice, this list of conditions and the following disclaimer.
6041 + * 2. Redistributions in binary form must reproduce the above copyright
6042 + * notice, this list of conditions and the following disclaimer in the
6043 + * documentation and/or other materials provided with the distribution.
6044 + * 3. The name of the author may not be used to endorse or promote
6045 + * products derived from this software without specific prior
6046 + * written permission.
6048 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
6049 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
6050 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6051 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
6052 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6053 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
6054 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6055 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
6056 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
6057 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
6058 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6060 + * This file is part of the uIP TCP/IP stack.
6062 + * $Id: uip_arp.h,v 1.3.2.2 2003/10/06 15:10:22 adam Exp $
6066 +#ifndef __UIP_ARP_H__
6067 +#define __UIP_ARP_H__
6073 + * Representation of a 48-bit Ethernet address.
6075 +struct uip_eth_addr {
6079 +extern struct uip_eth_addr uip_ethaddr;
6082 + * The Ethernet header.
6084 +struct uip_eth_hdr {
6085 + struct uip_eth_addr dest;
6086 + struct uip_eth_addr src;
6090 +#define UIP_ETHTYPE_ARP 0x0806
6091 +#define UIP_ETHTYPE_IP 0x0800
6092 +#define UIP_ETHTYPE_IP6 0x86dd
6095 +/* The uip_arp_init() function must be called before any of the other
6097 +void uip_arp_init(void);
6099 +/* The uip_arp_ipin() function should be called whenever an IP packet
6100 + arrives from the Ethernet. This function refreshes the ARP table or
6101 + inserts a new mapping if none exists. The function assumes that an
6102 + IP packet with an Ethernet header is present in the uip_buf buffer
6103 + and that the length of the packet is in the uip_len variable. */
6104 +void uip_arp_ipin(void);
6106 +/* The uip_arp_arpin() should be called when an ARP packet is received
6107 + by the Ethernet driver. This function also assumes that the
6108 + Ethernet frame is present in the uip_buf buffer. When the
6109 + uip_arp_arpin() function returns, the contents of the uip_buf
6110 + buffer should be sent out on the Ethernet if the uip_len variable
6112 +void uip_arp_arpin(void);
6114 +/* The uip_arp_out() function should be called when an IP packet
6115 + should be sent out on the Ethernet. This function creates an
6116 + Ethernet header before the IP header in the uip_buf buffer. The
6117 + Ethernet header will have the correct Ethernet MAC destination
6118 + address filled in if an ARP table entry for the destination IP
6119 + address (or the IP address of the default router) is present. If no
6120 + such table entry is found, the IP packet is overwritten with an ARP
6121 + request and we rely on TCP to retransmit the packet that was
6122 + overwritten. In any case, the uip_len variable holds the length of
6123 + the Ethernet frame that should be transmitted. */
6124 +void uip_arp_out(void);
6126 +/* The uip_arp_timer() function should be called every ten seconds. It
6127 + is responsible for flushing old entries in the ARP table. */
6128 +void uip_arp_timer(void);
6133 + * \addtogroup uipconffunc
6138 + * Set the default router's IP address.
6140 + * \param addr A pointer to a 4-byte array containing the IP address
6141 + * of the default router.
6143 + * \hideinitializer
6145 +#define uip_setdraddr(addr) do { uip_arp_draddr[0] = addr[0]; \
6146 + uip_arp_draddr[1] = addr[1]; } while(0)
6149 + * Set the netmask.
6151 + * \param addr A pointer to a 4-byte array containing the IP address
6154 + * \hideinitializer
6156 +#define uip_setnetmask(addr) do { uip_arp_netmask[0] = addr[0]; \
6157 + uip_arp_netmask[1] = addr[1]; } while(0)
6161 + * Get the default router's IP address.
6163 + * \param addr A pointer to a 4-byte array that will be filled in with
6164 + * the IP address of the default router.
6166 + * \hideinitializer
6168 +#define uip_getdraddr(addr) do { addr[0] = uip_arp_draddr[0]; \
6169 + addr[1] = uip_arp_draddr[1]; } while(0)
6172 + * Get the netmask.
6174 + * \param addr A pointer to a 4-byte array that will be filled in with
6175 + * the value of the netmask.
6177 + * \hideinitializer
6179 +#define uip_getnetmask(addr) do { addr[0] = uip_arp_netmask[0]; \
6180 + addr[1] = uip_arp_netmask[1]; } while(0)
6184 + * Specifiy the Ethernet MAC address.
6186 + * The ARP code needs to know the MAC address of the Ethernet card in
6187 + * order to be able to respond to ARP queries and to generate working
6188 + * Ethernet headers.
6190 + * \note This macro only specifies the Ethernet MAC address to the ARP
6191 + * code. It cannot be used to change the MAC address of the Ethernet
6194 + * \param eaddr A pointer to a struct uip_eth_addr containing the
6195 + * Ethernet MAC address of the Ethernet card.
6197 + * \hideinitializer
6199 +#define uip_setethaddr(eaddr) do {uip_ethaddr.addr[0] = eaddr.addr[0]; \
6200 + uip_ethaddr.addr[1] = eaddr.addr[1];\
6201 + uip_ethaddr.addr[2] = eaddr.addr[2];\
6202 + uip_ethaddr.addr[3] = eaddr.addr[3];\
6203 + uip_ethaddr.addr[4] = eaddr.addr[4];\
6204 + uip_ethaddr.addr[5] = eaddr.addr[5];} while(0)
6209 + * \internal Internal variables that are set using the macros
6210 + * uip_setdraddr and uip_setnetmask.
6212 +extern u16_t uip_arp_draddr[2], uip_arp_netmask[2];
6213 +#endif /* __UIP_ARP_H__ */
6217 +++ b/net/uip-0.9/uip.c
6226 + * The uIP TCP/IP stack code.
6227 + * \author Adam Dunkels <adam@dunkels.com>
6231 + * Copyright (c) 2001-2003, Adam Dunkels.
6232 + * All rights reserved.
6234 + * Redistribution and use in source and binary forms, with or without
6235 + * modification, are permitted provided that the following conditions
6237 + * 1. Redistributions of source code must retain the above copyright
6238 + * notice, this list of conditions and the following disclaimer.
6239 + * 2. Redistributions in binary form must reproduce the above copyright
6240 + * notice, this list of conditions and the following disclaimer in the
6241 + * documentation and/or other materials provided with the distribution.
6242 + * 3. The name of the author may not be used to endorse or promote
6243 + * products derived from this software without specific prior
6244 + * written permission.
6246 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
6247 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
6248 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6249 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
6250 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6251 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
6252 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6253 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
6254 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
6255 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
6256 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6258 + * This file is part of the uIP TCP/IP stack.
6260 + * $Id: uip.c,v 1.62.2.10 2003/10/07 13:23:01 adam Exp $
6265 +This is a small implementation of the IP and TCP protocols (as well as
6266 +some basic ICMP stuff). The implementation couples the IP, TCP and the
6267 +application layers very tightly. To keep the size of the compiled code
6268 +down, this code also features heavy usage of the goto statement.
6270 +The principle is that we have a small buffer, called the uip_buf, in
6271 +which the device driver puts an incoming packet. The TCP/IP stack
6272 +parses the headers in the packet, and calls upon the application. If
6273 +the remote host has sent data to the application, this data is present
6274 +in the uip_buf and the application read the data from there. It is up
6275 +to the application to put this data into a byte stream if needed. The
6276 +application will not be fed with data that is out of sequence.
6278 +If the application whishes to send data to the peer, it should put its
6279 +data into the uip_buf, 40 bytes from the start of the buffer. The
6280 +TCP/IP stack will calculate the checksums, and fill in the necessary
6281 +header fields and finally send the packet back to the peer.
6285 +#include "uipopt.h"
6286 +#include "uip_arch.h"
6288 +/*-----------------------------------------------------------------------------------*/
6289 +/* Variable definitions. */
6292 +/* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set here. Otherwise, the address */
6293 +#if UIP_FIXEDADDR > 0
6294 +const unsigned short int uip_hostaddr[2] =
6295 + {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
6296 + HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
6297 +const unsigned short int uip_arp_draddr[2] =
6298 + {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
6299 + HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
6300 +const unsigned short int uip_arp_netmask[2] =
6301 + {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
6302 + HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
6304 +unsigned short int uip_hostaddr[2];
6305 +unsigned short int uip_arp_draddr[2], uip_arp_netmask[2];
6306 +#endif /* UIP_FIXEDADDR */
6308 +u8_t uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
6309 + incoming packets. */
6310 +volatile u8_t *uip_appdata; /* The uip_appdata pointer points to
6311 + application data. */
6312 +volatile u8_t *uip_sappdata; /* The uip_appdata pointer points to the
6313 + application data which is to be sent. */
6314 +#if UIP_URGDATA > 0
6315 +volatile u8_t *uip_urgdata; /* The uip_urgdata pointer points to
6316 + urgent data (out-of-band data), if
6318 +volatile u8_t uip_urglen, uip_surglen;
6319 +#endif /* UIP_URGDATA > 0 */
6321 +volatile unsigned short int uip_len, uip_slen;
6322 + /* The uip_len is either 8 or 16 bits,
6323 + depending on the maximum packet
6326 +volatile u8_t uip_flags; /* The uip_flags variable is used for
6327 + communication between the TCP/IP stack
6328 + and the application program. */
6329 +struct uip_conn *uip_conn; /* uip_conn always points to the current
6332 +struct uip_conn uip_conns[UIP_CONNS];
6333 + /* The uip_conns array holds all TCP
6335 +unsigned short int uip_listenports[UIP_LISTENPORTS];
6336 + /* The uip_listenports list all currently
6337 + listning ports. */
6339 +struct uip_udp_conn *uip_udp_conn;
6340 +struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
6341 +#endif /* UIP_UDP */
6344 +static unsigned short int ipid; /* Ths ipid variable is an increasing
6345 + number that is used for the IP ID
6348 +static u8_t iss[4]; /* The iss variable is used for the TCP
6349 + initial sequence number. */
6351 +#if UIP_ACTIVE_OPEN
6352 +static unsigned short int lastport; /* Keeps track of the last port used for
6353 + a new connection. */
6354 +#endif /* UIP_ACTIVE_OPEN */
6356 +/* Temporary variables. */
6357 +volatile u8_t uip_acc32[4];
6358 +static u8_t c, opt;
6359 +static unsigned short int tmp16;
6361 +/* Structures and definitions. */
6362 +#define TCP_FIN 0x01
6363 +#define TCP_SYN 0x02
6364 +#define TCP_RST 0x04
6365 +#define TCP_PSH 0x08
6366 +#define TCP_ACK 0x10
6367 +#define TCP_URG 0x20
6368 +#define TCP_CTL 0x3f
6370 +#define ICMP_ECHO_REPLY 0
6371 +#define ICMP_ECHO 8
6374 +#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
6375 +#define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
6376 +#define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
6377 +#define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
6379 +#if UIP_STATISTICS == 1
6380 +struct uip_stats uip_stat;
6381 +#define UIP_STAT(s) s
6383 +#define UIP_STAT(s)
6384 +#endif /* UIP_STATISTICS == 1 */
6386 +#if UIP_LOGGING == 1
6387 +extern void puts(const char *s);
6388 +#define UIP_LOG(m) puts(m)
6391 +#endif /* UIP_LOGGING == 1 */
6393 +/*-----------------------------------------------------------------------------------*/
6397 + for(c = 0; c < UIP_LISTENPORTS; ++c) {
6398 + uip_listenports[c] = 0;
6400 + for(c = 0; c < UIP_CONNS; ++c) {
6401 + uip_conns[c].tcpstateflags = CLOSED;
6403 +#if UIP_ACTIVE_OPEN
6405 +#endif /* UIP_ACTIVE_OPEN */
6408 + for(c = 0; c < UIP_UDP_CONNS; ++c) {
6409 + uip_udp_conns[c].lport = 0;
6411 +#endif /* UIP_UDP */
6414 + /* IPv4 initialization. */
6415 +#if UIP_FIXEDADDR == 0
6416 + uip_hostaddr[0] = uip_hostaddr[1] = 0;
6417 +#endif /* UIP_FIXEDADDR */
6420 +/*-----------------------------------------------------------------------------------*/
6421 +#if UIP_ACTIVE_OPEN
6423 +uip_connect(unsigned short int *ripaddr, unsigned short int rport)
6425 + register struct uip_conn *conn, *cconn;
6427 + /* Find an unused local port. */
6431 + if(lastport >= 32000) {
6435 + /* Check if this port is already in use, and if so try to find
6437 + for(c = 0; c < UIP_CONNS; ++c) {
6438 + conn = &uip_conns[c];
6439 + if(conn->tcpstateflags != CLOSED &&
6440 + conn->lport == htons(lastport)) {
6447 + for(c = 0; c < UIP_CONNS; ++c) {
6448 + cconn = &uip_conns[c];
6449 + if(cconn->tcpstateflags == CLOSED) {
6453 + if(cconn->tcpstateflags == TIME_WAIT) {
6455 + cconn->timer > uip_conn->timer) {
6465 + conn->tcpstateflags = SYN_SENT;
6467 + conn->snd_nxt[0] = iss[0];
6468 + conn->snd_nxt[1] = iss[1];
6469 + conn->snd_nxt[2] = iss[2];
6470 + conn->snd_nxt[3] = iss[3];
6472 + conn->initialmss = conn->mss = UIP_TCP_MSS;
6474 + conn->len = 1; /* TCP length of the SYN is one. */
6476 + conn->timer = 1; /* Send the SYN next time around. */
6477 + conn->rto = UIP_RTO;
6480 + conn->lport = htons(lastport);
6481 + conn->rport = rport;
6482 + conn->ripaddr[0] = ripaddr[0];
6483 + conn->ripaddr[1] = ripaddr[1];
6487 +#endif /* UIP_ACTIVE_OPEN */
6488 +/*-----------------------------------------------------------------------------------*/
6490 +struct uip_udp_conn *
6491 +uip_udp_new(unsigned short int *ripaddr, unsigned short int rport)
6493 + register struct uip_udp_conn *conn;
6495 + /* Find an unused local port. */
6499 + if(lastport >= 32000) {
6503 + for(c = 0; c < UIP_UDP_CONNS; ++c) {
6504 + if(uip_udp_conns[c].lport == lastport) {
6511 + for(c = 0; c < UIP_UDP_CONNS; ++c) {
6512 + if(uip_udp_conns[c].lport == 0) {
6513 + conn = &uip_udp_conns[c];
6522 + conn->lport = HTONS(lastport);
6523 + conn->rport = HTONS(rport);
6524 + conn->ripaddr[0] = ripaddr[0];
6525 + conn->ripaddr[1] = ripaddr[1];
6529 +#endif /* UIP_UDP */
6530 +/*-----------------------------------------------------------------------------------*/
6532 +uip_unlisten(unsigned short int port)
6534 + for(c = 0; c < UIP_LISTENPORTS; ++c) {
6535 + if(uip_listenports[c] == port) {
6536 + uip_listenports[c] = 0;
6541 +/*-----------------------------------------------------------------------------------*/
6543 +uip_listen(unsigned short int port)
6545 + for(c = 0; c < UIP_LISTENPORTS; ++c) {
6546 + if(uip_listenports[c] == 0) {
6547 + uip_listenports[c] = port;
6552 +/*-----------------------------------------------------------------------------------*/
6553 +/* XXX: IP fragment reassembly: not well-tested. */
6556 +#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
6557 +static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
6558 +static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
6559 +static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
6560 + 0x0f, 0x07, 0x03, 0x01};
6561 +static unsigned short int uip_reasslen;
6562 +static u8_t uip_reassflags;
6563 +#define UIP_REASS_FLAG_LASTFRAG 0x01
6564 +static u8_t uip_reasstmr;
6572 + unsigned short int offset, len;
6573 + unsigned short int i;
6575 + /* If ip_reasstmr is zero, no packet is present in the buffer, so we
6576 + write the IP header of the fragment into the reassembly
6577 + buffer. The timer is updated with the maximum age. */
6578 + if(uip_reasstmr == 0) {
6579 + memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
6580 + uip_reasstmr = UIP_REASS_MAXAGE;
6581 + uip_reassflags = 0;
6582 + /* Clear the bitmap. */
6583 + memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
6586 + /* Check if the incoming fragment matches the one currently present
6587 + in the reasembly buffer. If so, we proceed with copying the
6588 + fragment into the buffer. */
6589 + if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
6590 + BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
6591 + BUF->destipaddr[0] == FBUF->destipaddr[0] &&
6592 + BUF->destipaddr[1] == FBUF->destipaddr[1] &&
6593 + BUF->ipid[0] == FBUF->ipid[0] &&
6594 + BUF->ipid[1] == FBUF->ipid[1]) {
6596 + len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
6597 + offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
6599 + /* If the offset or the offset + fragment length overflows the
6600 + reassembly buffer, we discard the entire packet. */
6601 + if(offset > UIP_REASS_BUFSIZE ||
6602 + offset + len > UIP_REASS_BUFSIZE) {
6607 + /* Copy the fragment into the reassembly buffer, at the right
6609 + memcpy(&uip_reassbuf[IP_HLEN + offset],
6610 + (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
6613 + /* Update the bitmap. */
6614 + if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
6615 + /* If the two endpoints are in the same byte, we only update
6618 + uip_reassbitmap[offset / (8 * 8)] |=
6619 + bitmap_bits[(offset / 8 ) & 7] &
6620 + ~bitmap_bits[((offset + len) / 8 ) & 7];
6622 + /* If the two endpoints are in different bytes, we update the
6623 + bytes in the endpoints and fill the stuff inbetween with
6625 + uip_reassbitmap[offset / (8 * 8)] |=
6626 + bitmap_bits[(offset / 8 ) & 7];
6627 + for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
6628 + uip_reassbitmap[i] = 0xff;
6630 + uip_reassbitmap[(offset + len) / (8 * 8)] |=
6631 + ~bitmap_bits[((offset + len) / 8 ) & 7];
6634 + /* If this fragment has the More Fragments flag set to zero, we
6635 + know that this is the last fragment, so we can calculate the
6636 + size of the entire packet. We also set the
6637 + IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
6638 + the final fragment. */
6640 + if((BUF->ipoffset[0] & IP_MF) == 0) {
6641 + uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
6642 + uip_reasslen = offset + len;
6645 + /* Finally, we check if we have a full packet in the buffer. We do
6646 + this by checking if we have the last fragment and if all bits
6647 + in the bitmap are set. */
6648 + if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
6649 + /* Check all bytes up to and including all but the last byte in
6651 + for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
6652 + if(uip_reassbitmap[i] != 0xff) {
6656 + /* Check the last byte in the bitmap. It should contain just the
6657 + right amount of bits. */
6658 + if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
6659 + (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
6663 + /* If we have come this far, we have a full packet in the
6664 + buffer, so we allocate a pbuf and copy the packet into it. We
6665 + also reset the timer. */
6667 + memcpy(BUF, FBUF, uip_reasslen);
6669 + /* Pretend to be a "normal" (i.e., not fragmented) IP packet
6671 + BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
6672 + BUF->len[0] = uip_reasslen >> 8;
6673 + BUF->len[1] = uip_reasslen & 0xff;
6674 + BUF->ipchksum = 0;
6675 + BUF->ipchksum = ~(uip_ipchksum());
6677 + return uip_reasslen;
6684 +#endif /* UIP_REASSEMBL */
6685 +/*-----------------------------------------------------------------------------------*/
6687 +uip_add_rcv_nxt(unsigned short int n)
6689 + uip_add32(uip_conn->rcv_nxt, n);
6690 + uip_conn->rcv_nxt[0] = uip_acc32[0];
6691 + uip_conn->rcv_nxt[1] = uip_acc32[1];
6692 + uip_conn->rcv_nxt[2] = uip_acc32[2];
6693 + uip_conn->rcv_nxt[3] = uip_acc32[3];
6695 +/*-----------------------------------------------------------------------------------*/
6697 +uip_process(u8_t flag)
6699 + register struct uip_conn *uip_connr = uip_conn;
6701 + uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
6704 + /* Check if we were invoked because of the perodic timer fireing. */
6705 + if(flag == UIP_TIMER) {
6707 + if(uip_reasstmr != 0) {
6710 +#endif /* UIP_REASSEMBLY */
6711 + /* Increase the initial sequence number. */
6712 + if(++iss[3] == 0) {
6713 + if(++iss[2] == 0) {
6714 + if(++iss[1] == 0) {
6720 + if(uip_connr->tcpstateflags == TIME_WAIT ||
6721 + uip_connr->tcpstateflags == FIN_WAIT_2) {
6722 + ++(uip_connr->timer);
6723 + if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
6724 + uip_connr->tcpstateflags = CLOSED;
6726 + } else if(uip_connr->tcpstateflags != CLOSED) {
6727 + /* If the connection has outstanding data, we increase the
6728 + connection's timer and see if it has reached the RTO value
6729 + in which case we retransmit. */
6730 + if(uip_outstanding(uip_connr)) {
6731 + if(uip_connr->timer-- == 0) {
6732 + if(uip_connr->nrtx == UIP_MAXRTX ||
6733 + ((uip_connr->tcpstateflags == SYN_SENT ||
6734 + uip_connr->tcpstateflags == SYN_RCVD) &&
6735 + uip_connr->nrtx == UIP_MAXSYNRTX)) {
6736 + uip_connr->tcpstateflags = CLOSED;
6738 + /* We call UIP_APPCALL() with uip_flags set to
6739 + UIP_TIMEDOUT to inform the application that the
6740 + connection has timed out. */
6741 + uip_flags = UIP_TIMEDOUT;
6744 + /* We also send a reset packet to the remote host. */
6745 + BUF->flags = TCP_RST | TCP_ACK;
6746 + goto tcp_send_nodata;
6749 + /* Exponential backoff. */
6750 + uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
6753 + ++(uip_connr->nrtx);
6755 + /* Ok, so we need to retransmit. We do this differently
6756 + depending on which state we are in. In ESTABLISHED, we
6757 + call upon the application so that it may prepare the
6758 + data for the retransmit. In SYN_RCVD, we resend the
6759 + SYNACK that we sent earlier and in LAST_ACK we have to
6760 + retransmit our FINACK. */
6761 + UIP_STAT(++uip_stat.tcp.rexmit);
6762 + switch(uip_connr->tcpstateflags & TS_MASK) {
6764 + /* In the SYN_RCVD state, we should retransmit our
6766 + goto tcp_send_synack;
6768 +#if UIP_ACTIVE_OPEN
6770 + /* In the SYN_SENT state, we retransmit out SYN. */
6772 + goto tcp_send_syn;
6773 +#endif /* UIP_ACTIVE_OPEN */
6776 + /* In the ESTABLISHED state, we call upon the application
6777 + to do the actual retransmit after which we jump into
6778 + the code for sending out the packet (the apprexmit
6782 + uip_flags = UIP_REXMIT;
6789 + /* In all these states we should retransmit a FINACK. */
6790 + goto tcp_send_finack;
6794 + } else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED) {
6795 + /* If there was no need for a retransmission, we poll the
6796 + application for new data. */
6799 + uip_flags = UIP_POLL;
6807 + if(flag == UIP_UDP_TIMER) {
6808 + if(uip_udp_conn->lport != 0) {
6809 + uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
6810 + uip_len = uip_slen = 0;
6811 + uip_flags = UIP_POLL;
6812 + UIP_UDP_APPCALL();
6820 + /* This is where the input processing starts. */
6821 + UIP_STAT(++uip_stat.ip.recv);
6824 + /* Start of IPv4 input header processing code. */
6826 + /* Check validity of the IP header. */
6827 + if(BUF->vhl != 0x45) { /* IP version and header length. */
6828 + UIP_STAT(++uip_stat.ip.drop);
6829 + UIP_STAT(++uip_stat.ip.vhlerr);
6830 + UIP_LOG("ip: invalid version or header length.");
6834 + /* Check the size of the packet. If the size reported to us in
6835 + uip_len doesn't match the size reported in the IP header, there
6836 + has been a transmission error and we drop the packet. */
6838 + if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
6839 + uip_len = (uip_len & 0xff) | (BUF->len[0] << 8);
6841 + if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
6842 + uip_len = (uip_len & 0xff00) | BUF->len[1];
6845 + /* Check the fragment flag. */
6846 + if((BUF->ipoffset[0] & 0x3f) != 0 ||
6847 + BUF->ipoffset[1] != 0) {
6849 + uip_len = uip_reass();
6850 + if(uip_len == 0) {
6854 + UIP_STAT(++uip_stat.ip.drop);
6855 + UIP_STAT(++uip_stat.ip.fragerr);
6856 + UIP_LOG("ip: fragment dropped.");
6858 +#endif /* UIP_REASSEMBLY */
6861 + /* If we are configured to use ping IP address configuration and
6862 + hasn't been assigned an IP address yet, we accept all ICMP
6864 +#if UIP_PINGADDRCONF
6865 + if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
6866 + if(BUF->proto == UIP_PROTO_ICMP) {
6867 + UIP_LOG("ip: possible ping config packet received.");
6870 + UIP_LOG("ip: packet dropped since no address assigned.");
6874 +#endif /* UIP_PINGADDRCONF */
6876 + /* Check if the packet is destined for our IP address. */
6877 + if(BUF->destipaddr[0] != uip_hostaddr[0]) {
6878 + UIP_STAT(++uip_stat.ip.drop);
6879 + UIP_LOG("ip: packet not for us.");
6882 + if(BUF->destipaddr[1] != uip_hostaddr[1]) {
6883 + UIP_STAT(++uip_stat.ip.drop);
6884 + UIP_LOG("ip: packet not for us.");
6888 + if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
6890 + UIP_STAT(++uip_stat.ip.drop);
6891 + UIP_STAT(++uip_stat.ip.chkerr);
6892 + UIP_LOG("ip: bad checksum.");
6896 + if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump
6897 + to the tcp_input label. */
6901 + if(BUF->proto == UIP_PROTO_UDP)
6903 +#endif /* UIP_UDP */
6905 + if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
6907 + UIP_STAT(++uip_stat.ip.drop);
6908 + UIP_STAT(++uip_stat.ip.protoerr);
6909 + UIP_LOG("ip: neither tcp nor icmp.");
6914 + UIP_STAT(++uip_stat.icmp.recv);
6916 + /* ICMP echo (i.e., ping) processing. This is simple, we only change
6917 + the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
6918 + checksum before we return the packet. */
6919 + if(ICMPBUF->type != ICMP_ECHO) {
6920 + UIP_STAT(++uip_stat.icmp.drop);
6921 + UIP_STAT(++uip_stat.icmp.typeerr);
6922 + UIP_LOG("icmp: not icmp echo.");
6926 + /* If we are configured to use ping IP address assignment, we use
6927 + the destination IP address of this ping packet and assign it to
6929 +#if UIP_PINGADDRCONF
6930 + if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
6931 + uip_hostaddr[0] = BUF->destipaddr[0];
6932 + uip_hostaddr[1] = BUF->destipaddr[1];
6934 +#endif /* UIP_PINGADDRCONF */
6936 + ICMPBUF->type = ICMP_ECHO_REPLY;
6938 + if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
6939 + ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
6941 + ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
6944 + /* Swap IP addresses. */
6945 + tmp16 = BUF->destipaddr[0];
6946 + BUF->destipaddr[0] = BUF->srcipaddr[0];
6947 + BUF->srcipaddr[0] = tmp16;
6948 + tmp16 = BUF->destipaddr[1];
6949 + BUF->destipaddr[1] = BUF->srcipaddr[1];
6950 + BUF->srcipaddr[1] = tmp16;
6952 + UIP_STAT(++uip_stat.icmp.sent);
6955 + /* End of IPv4 input header processing code. */
6959 + /* UDP input processing. */
6961 + /* UDP processing is really just a hack. We don't do anything to the
6962 + UDP/IP headers, but let the UDP application do all the hard
6963 + work. If the application sets uip_slen, it has a packet to
6965 +#if UIP_UDP_CHECKSUMS
6966 + if(uip_udpchksum() != 0xffff) {
6967 + UIP_STAT(++uip_stat.udp.drop);
6968 + UIP_STAT(++uip_stat.udp.chkerr);
6969 + UIP_LOG("udp: bad checksum.");
6972 +#endif /* UIP_UDP_CHECKSUMS */
6974 + /* Demultiplex this UDP packet between the UDP "connections". */
6975 + for(uip_udp_conn = &uip_udp_conns[0];
6976 + uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
6978 + if(uip_udp_conn->lport != 0 &&
6979 + UDPBUF->destport == uip_udp_conn->lport &&
6980 + (uip_udp_conn->rport == 0 ||
6981 + UDPBUF->srcport == uip_udp_conn->rport) &&
6982 + BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] &&
6983 + BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) {
6990 + uip_len = uip_len - 28;
6991 + uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
6992 + uip_flags = UIP_NEWDATA;
6994 + UIP_UDP_APPCALL();
6996 + if(uip_slen == 0) {
6999 + uip_len = uip_slen + 28;
7001 + BUF->len[0] = (uip_len >> 8);
7002 + BUF->len[1] = (uip_len & 0xff);
7004 + BUF->proto = UIP_PROTO_UDP;
7006 + UDPBUF->udplen = HTONS(uip_slen + 8);
7007 + UDPBUF->udpchksum = 0;
7008 +#if UIP_UDP_CHECKSUMS
7009 + /* Calculate UDP checksum. */
7010 + UDPBUF->udpchksum = ~(uip_udpchksum());
7011 + if(UDPBUF->udpchksum == 0) {
7012 + UDPBUF->udpchksum = 0xffff;
7014 +#endif /* UIP_UDP_CHECKSUMS */
7016 + BUF->srcport = uip_udp_conn->lport;
7017 + BUF->destport = uip_udp_conn->rport;
7019 + BUF->srcipaddr[0] = uip_hostaddr[0];
7020 + BUF->srcipaddr[1] = uip_hostaddr[1];
7021 + BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
7022 + BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
7024 + uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
7025 + goto ip_send_nolen;
7026 +#endif /* UIP_UDP */
7028 + /* TCP input processing. */
7030 + UIP_STAT(++uip_stat.tcp.recv);
7032 + /* Start of TCP input header processing code. */
7034 + if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
7036 + UIP_STAT(++uip_stat.tcp.drop);
7037 + UIP_STAT(++uip_stat.tcp.chkerr);
7038 + UIP_LOG("tcp: bad checksum.");
7042 + /* Demultiplex this segment. */
7043 + /* First check any active connections. */
7044 + for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
7045 + if(uip_connr->tcpstateflags != CLOSED &&
7046 + BUF->destport == uip_connr->lport &&
7047 + BUF->srcport == uip_connr->rport &&
7048 + BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
7049 + BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
7054 + /* If we didn't find and active connection that expected the packet,
7055 + either this packet is an old duplicate, or this is a SYN packet
7056 + destined for a connection in LISTEN. If the SYN flag isn't set,
7057 + it is an old packet and we send a RST. */
7058 + if((BUF->flags & TCP_CTL) != TCP_SYN)
7061 + tmp16 = BUF->destport;
7062 + /* Next, check listening connections. */
7063 + for(c = 0; c < UIP_LISTENPORTS; ++c) {
7064 + if(tmp16 == uip_listenports[c])
7065 + goto found_listen;
7068 + /* No matching connection found, so we send a RST packet. */
7069 + UIP_STAT(++uip_stat.tcp.synrst);
7072 + /* We do not send resets in response to resets. */
7073 + if(BUF->flags & TCP_RST)
7076 + UIP_STAT(++uip_stat.tcp.rst);
7078 + BUF->flags = TCP_RST | TCP_ACK;
7080 + BUF->tcpoffset = 5 << 4;
7082 + /* Flip the seqno and ackno fields in the TCP header. */
7083 + c = BUF->seqno[3];
7084 + BUF->seqno[3] = BUF->ackno[3];
7085 + BUF->ackno[3] = c;
7087 + c = BUF->seqno[2];
7088 + BUF->seqno[2] = BUF->ackno[2];
7089 + BUF->ackno[2] = c;
7091 + c = BUF->seqno[1];
7092 + BUF->seqno[1] = BUF->ackno[1];
7093 + BUF->ackno[1] = c;
7095 + c = BUF->seqno[0];
7096 + BUF->seqno[0] = BUF->ackno[0];
7097 + BUF->ackno[0] = c;
7099 + /* We also have to increase the sequence number we are
7100 + acknowledging. If the least significant byte overflowed, we need
7101 + to propagate the carry to the other bytes as well. */
7102 + if(++BUF->ackno[3] == 0) {
7103 + if(++BUF->ackno[2] == 0) {
7104 + if(++BUF->ackno[1] == 0) {
7110 + /* Swap port numbers. */
7111 + tmp16 = BUF->srcport;
7112 + BUF->srcport = BUF->destport;
7113 + BUF->destport = tmp16;
7115 + /* Swap IP addresses. */
7116 + tmp16 = BUF->destipaddr[0];
7117 + BUF->destipaddr[0] = BUF->srcipaddr[0];
7118 + BUF->srcipaddr[0] = tmp16;
7119 + tmp16 = BUF->destipaddr[1];
7120 + BUF->destipaddr[1] = BUF->srcipaddr[1];
7121 + BUF->srcipaddr[1] = tmp16;
7124 + /* And send out the RST packet! */
7125 + goto tcp_send_noconn;
7127 + /* This label will be jumped to if we matched the incoming packet
7128 + with a connection in LISTEN. In that case, we should create a new
7129 + connection and send a SYNACK in return. */
7131 + /* First we check if there are any connections avaliable. Unused
7132 + connections are kept in the same table as used connections, but
7133 + unused ones have the tcpstate set to CLOSED. Also, connections in
7134 + TIME_WAIT are kept track of and we'll use the oldest one if no
7135 + CLOSED connections are found. Thanks to Eddie C. Dost for a very
7136 + nice algorithm for the TIME_WAIT search. */
7138 + for(c = 0; c < UIP_CONNS; ++c) {
7139 + if(uip_conns[c].tcpstateflags == CLOSED) {
7140 + uip_connr = &uip_conns[c];
7143 + if(uip_conns[c].tcpstateflags == TIME_WAIT) {
7144 + if(uip_connr == 0 ||
7145 + uip_conns[c].timer > uip_connr->timer) {
7146 + uip_connr = &uip_conns[c];
7151 + if(uip_connr == 0) {
7152 + /* All connections are used already, we drop packet and hope that
7153 + the remote end will retransmit the packet at a time when we
7154 + have more spare connections. */
7155 + UIP_STAT(++uip_stat.tcp.syndrop);
7156 + UIP_LOG("tcp: found no unused connections.");
7159 + uip_conn = uip_connr;
7161 + /* Fill in the necessary fields for the new connection. */
7162 + uip_connr->rto = uip_connr->timer = UIP_RTO;
7163 + uip_connr->sa = 0;
7164 + uip_connr->sv = 4;
7165 + uip_connr->nrtx = 0;
7166 + uip_connr->lport = BUF->destport;
7167 + uip_connr->rport = BUF->srcport;
7168 + uip_connr->ripaddr[0] = BUF->srcipaddr[0];
7169 + uip_connr->ripaddr[1] = BUF->srcipaddr[1];
7170 + uip_connr->tcpstateflags = SYN_RCVD;
7172 + uip_connr->snd_nxt[0] = iss[0];
7173 + uip_connr->snd_nxt[1] = iss[1];
7174 + uip_connr->snd_nxt[2] = iss[2];
7175 + uip_connr->snd_nxt[3] = iss[3];
7176 + uip_connr->len = 1;
7178 + /* rcv_nxt should be the seqno from the incoming packet + 1. */
7179 + uip_connr->rcv_nxt[3] = BUF->seqno[3];
7180 + uip_connr->rcv_nxt[2] = BUF->seqno[2];
7181 + uip_connr->rcv_nxt[1] = BUF->seqno[1];
7182 + uip_connr->rcv_nxt[0] = BUF->seqno[0];
7183 + uip_add_rcv_nxt(1);
7185 + /* Parse the TCP MSS option, if present. */
7186 + if((BUF->tcpoffset & 0xf0) > 0x50) {
7187 + for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
7188 + opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
7190 + /* End of options. */
7192 + } else if(opt == 0x01) {
7195 + } else if(opt == 0x02 &&
7196 + uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
7197 + /* An MSS option with the right option length. */
7198 + tmp16 = ((unsigned short int)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
7199 + (unsigned short int)uip_buf[40 + UIP_LLH_LEN + 3 + c];
7200 + uip_connr->initialmss = uip_connr->mss =
7201 + tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
7203 + /* And we are done processing options. */
7206 + /* All other options have a length field, so that we easily
7207 + can skip past them. */
7208 + if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
7209 + /* If the length field is zero, the options are malformed
7210 + and we don't process them further. */
7213 + c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
7218 + /* Our response will be a SYNACK. */
7219 +#if UIP_ACTIVE_OPEN
7221 + BUF->flags = TCP_ACK;
7224 + BUF->flags |= TCP_SYN;
7225 +#else /* UIP_ACTIVE_OPEN */
7227 + BUF->flags = TCP_SYN | TCP_ACK;
7228 +#endif /* UIP_ACTIVE_OPEN */
7230 + /* We send out the TCP Maximum Segment Size option with our
7232 + BUF->optdata[0] = 2;
7233 + BUF->optdata[1] = 4;
7234 + BUF->optdata[2] = (UIP_TCP_MSS) / 256;
7235 + BUF->optdata[3] = (UIP_TCP_MSS) & 255;
7237 + BUF->tcpoffset = 6 << 4;
7240 + /* This label will be jumped to if we found an active connection. */
7242 + uip_conn = uip_connr;
7245 + /* We do a very naive form of TCP reset processing; we just accept
7246 + any RST and kill our connection. We should in fact check if the
7247 + sequence number of this reset is wihtin our advertised window
7248 + before we accept the reset. */
7249 + if(BUF->flags & TCP_RST) {
7250 + uip_connr->tcpstateflags = CLOSED;
7251 + UIP_LOG("tcp: got reset, aborting connection.");
7252 + uip_flags = UIP_ABORT;
7256 + /* Calculated the length of the data, if the application has sent
7257 + any data to us. */
7258 + c = (BUF->tcpoffset >> 4) << 2;
7259 + /* uip_len will contain the length of the actual TCP data. This is
7260 + calculated by subtracing the length of the TCP header (in
7261 + c) and the length of the IP header (20 bytes). */
7262 + uip_len = uip_len - c - 20;
7264 + /* First, check if the sequence number of the incoming packet is
7265 + what we're expecting next. If not, we send out an ACK with the
7266 + correct numbers in. */
7268 + (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
7269 + BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
7270 + BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
7271 + BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
7272 + goto tcp_send_ack;
7275 + /* Next, check if the incoming segment acknowledges any outstanding
7276 + data. If so, we update the sequence number, reset the length of
7277 + the outstanding data, calculate RTT estimations, and reset the
7278 + retransmission timer. */
7279 + if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
7280 + uip_add32(uip_connr->snd_nxt, uip_connr->len);
7281 + if(BUF->ackno[0] == uip_acc32[0] &&
7282 + BUF->ackno[1] == uip_acc32[1] &&
7283 + BUF->ackno[2] == uip_acc32[2] &&
7284 + BUF->ackno[3] == uip_acc32[3]) {
7285 + /* Update sequence number. */
7286 + uip_connr->snd_nxt[0] = uip_acc32[0];
7287 + uip_connr->snd_nxt[1] = uip_acc32[1];
7288 + uip_connr->snd_nxt[2] = uip_acc32[2];
7289 + uip_connr->snd_nxt[3] = uip_acc32[3];
7292 + /* Do RTT estimation, unless we have done retransmissions. */
7293 + if(uip_connr->nrtx == 0) {
7295 + m = uip_connr->rto - uip_connr->timer;
7296 + /* This is taken directly from VJs original code in his paper */
7297 + m = m - (uip_connr->sa >> 3);
7298 + uip_connr->sa += m;
7302 + m = m - (uip_connr->sv >> 2);
7303 + uip_connr->sv += m;
7304 + uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
7307 + /* Set the acknowledged flag. */
7308 + uip_flags = UIP_ACKDATA;
7309 + /* Reset the retransmission timer. */
7310 + uip_connr->timer = uip_connr->rto;
7315 + /* Do different things depending on in what state the connection is. */
7316 + switch(uip_connr->tcpstateflags & TS_MASK) {
7317 + /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
7318 + implemented, since we force the application to close when the
7319 + peer sends a FIN (hence the application goes directly from
7320 + ESTABLISHED to LAST_ACK). */
7322 + /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
7323 + we are waiting for an ACK that acknowledges the data we sent
7324 + out the last time. Therefore, we want to have the UIP_ACKDATA
7325 + flag set. If so, we enter the ESTABLISHED state. */
7326 + if(uip_flags & UIP_ACKDATA) {
7327 + uip_connr->tcpstateflags = ESTABLISHED;
7328 + uip_flags = UIP_CONNECTED;
7329 + uip_connr->len = 0;
7331 + uip_flags |= UIP_NEWDATA;
7332 + uip_add_rcv_nxt(uip_len);
7339 +#if UIP_ACTIVE_OPEN
7341 + /* In SYN_SENT, we wait for a SYNACK that is sent in response to
7342 + our SYN. The rcv_nxt is set to sequence number in the SYNACK
7343 + plus one, and we send an ACK. We move into the ESTABLISHED
7345 + if((uip_flags & UIP_ACKDATA) &&
7346 + BUF->flags == (TCP_SYN | TCP_ACK)) {
7348 + /* Parse the TCP MSS option, if present. */
7349 + if((BUF->tcpoffset & 0xf0) > 0x50) {
7350 + for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
7351 + opt = uip_buf[40 + UIP_LLH_LEN + c];
7353 + /* End of options. */
7355 + } else if(opt == 0x01) {
7358 + } else if(opt == 0x02 &&
7359 + uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
7360 + /* An MSS option with the right option length. */
7361 + tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
7362 + uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
7363 + uip_connr->initialmss =
7364 + uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
7366 + /* And we are done processing options. */
7369 + /* All other options have a length field, so that we easily
7370 + can skip past them. */
7371 + if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
7372 + /* If the length field is zero, the options are malformed
7373 + and we don't process them further. */
7376 + c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
7380 + uip_connr->tcpstateflags = ESTABLISHED;
7381 + uip_connr->rcv_nxt[0] = BUF->seqno[0];
7382 + uip_connr->rcv_nxt[1] = BUF->seqno[1];
7383 + uip_connr->rcv_nxt[2] = BUF->seqno[2];
7384 + uip_connr->rcv_nxt[3] = BUF->seqno[3];
7385 + uip_add_rcv_nxt(1);
7386 + uip_flags = UIP_CONNECTED | UIP_NEWDATA;
7387 + uip_connr->len = 0;
7394 +#endif /* UIP_ACTIVE_OPEN */
7397 + /* In the ESTABLISHED state, we call upon the application to feed
7398 + data into the uip_buf. If the UIP_ACKDATA flag is set, the
7399 + application should put new data into the buffer, otherwise we are
7400 + retransmitting an old segment, and the application should put that
7401 + data into the buffer.
7403 + If the incoming packet is a FIN, we should close the connection on
7404 + this side as well, and we send out a FIN and enter the LAST_ACK
7405 + state. We require that there is no outstanding data; otherwise the
7406 + sequence numbers will be screwed up. */
7408 + if(BUF->flags & TCP_FIN) {
7409 + if(uip_outstanding(uip_connr)) {
7412 + uip_add_rcv_nxt(1 + uip_len);
7413 + uip_flags = UIP_CLOSE;
7415 + uip_flags |= UIP_NEWDATA;
7418 + uip_connr->len = 1;
7419 + uip_connr->tcpstateflags = LAST_ACK;
7420 + uip_connr->nrtx = 0;
7422 + BUF->flags = TCP_FIN | TCP_ACK;
7423 + goto tcp_send_nodata;
7426 + /* Check the URG flag. If this is set, the segment carries urgent
7427 + data that we must pass to the application. */
7428 + if(BUF->flags & TCP_URG) {
7429 +#if UIP_URGDATA > 0
7430 + uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
7431 + if(uip_urglen > uip_len) {
7432 + /* There is more urgent data in the next segment to come. */
7433 + uip_urglen = uip_len;
7435 + uip_add_rcv_nxt(uip_urglen);
7436 + uip_len -= uip_urglen;
7437 + uip_urgdata = uip_appdata;
7438 + uip_appdata += uip_urglen;
7441 +#endif /* UIP_URGDATA > 0 */
7442 + uip_appdata += (BUF->urgp[0] << 8) | BUF->urgp[1];
7443 + uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
7447 + /* If uip_len > 0 we have TCP data in the packet, and we flag this
7448 + by setting the UIP_NEWDATA flag and update the sequence number
7449 + we acknowledge. If the application has stopped the dataflow
7450 + using uip_stop(), we must not accept any data packets from the
7452 + if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
7453 + uip_flags |= UIP_NEWDATA;
7454 + uip_add_rcv_nxt(uip_len);
7457 + /* Check if the available buffer space advertised by the other end
7458 + is smaller than the initial MSS for this connection. If so, we
7459 + set the current MSS to the window size to ensure that the
7460 + application does not send more data than the other end can
7463 + If the remote host advertises a zero window, we set the MSS to
7464 + the initial MSS so that the application will send an entire MSS
7465 + of data. This data will not be acknowledged by the receiver,
7466 + and the application will retransmit it. This is called the
7467 + "persistent timer" and uses the retransmission mechanim.
7469 + tmp16 = ((unsigned short int)BUF->wnd[0] << 8) + (unsigned short int)BUF->wnd[1];
7470 + if(tmp16 > uip_connr->initialmss ||
7472 + tmp16 = uip_connr->initialmss;
7474 + uip_connr->mss = tmp16;
7476 + /* If this packet constitutes an ACK for outstanding data (flagged
7477 + by the UIP_ACKDATA flag, we should call the application since it
7478 + might want to send more data. If the incoming packet had data
7479 + from the peer (as flagged by the UIP_NEWDATA flag), the
7480 + application must also be notified.
7482 + When the application is called, the global variable uip_len
7483 + contains the length of the incoming data. The application can
7484 + access the incoming data through the global pointer
7485 + uip_appdata, which usually points 40 bytes into the uip_buf
7488 + If the application wishes to send any data, this data should be
7489 + put into the uip_appdata and the length of the data should be
7490 + put into uip_len. If the application don't have any data to
7491 + send, uip_len must be set to 0. */
7492 + if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
7498 + if(uip_flags & UIP_ABORT) {
7500 + uip_connr->tcpstateflags = CLOSED;
7501 + BUF->flags = TCP_RST | TCP_ACK;
7502 + goto tcp_send_nodata;
7505 + if(uip_flags & UIP_CLOSE) {
7507 + uip_connr->len = 1;
7508 + uip_connr->tcpstateflags = FIN_WAIT_1;
7509 + uip_connr->nrtx = 0;
7510 + BUF->flags = TCP_FIN | TCP_ACK;
7511 + goto tcp_send_nodata;
7514 + /* If uip_slen > 0, the application has data to be sent. */
7515 + if(uip_slen > 0) {
7517 + /* If the connection has acknowledged data, the contents of
7518 + the ->len variable should be discarded. */
7519 + if((uip_flags & UIP_ACKDATA) != 0) {
7520 + uip_connr->len = 0;
7523 + /* If the ->len variable is non-zero the connection has
7524 + already data in transit and cannot send anymore right
7526 + if(uip_connr->len == 0) {
7528 + /* The application cannot send more than what is allowed by
7529 + the mss (the minumum of the MSS and the available
7531 + if(uip_slen > uip_connr->mss) {
7532 + uip_slen = uip_connr->mss;
7535 + /* Remember how much data we send out now so that we know
7536 + when everything has been acknowledged. */
7537 + uip_connr->len = uip_slen;
7540 + /* If the application already had unacknowledged data, we
7541 + make sure that the application does not send (i.e.,
7542 + retransmit) out more than it previously sent out. */
7543 + uip_slen = uip_connr->len;
7546 + uip_connr->len = 0;
7548 + uip_connr->nrtx = 0;
7550 + uip_appdata = uip_sappdata;
7552 + /* If the application has data to be sent, or if the incoming
7553 + packet had new data in it, we must send out a packet. */
7554 + if(uip_slen > 0 && uip_connr->len > 0) {
7555 + /* Add the length of the IP and TCP headers. */
7556 + uip_len = uip_connr->len + UIP_TCPIP_HLEN;
7557 + /* We always set the ACK flag in response packets. */
7558 + BUF->flags = TCP_ACK | TCP_PSH;
7559 + /* Send the packet. */
7560 + goto tcp_send_noopts;
7562 + /* If there is no data to send, just send out a pure ACK if
7563 + there is newdata. */
7564 + if(uip_flags & UIP_NEWDATA) {
7565 + uip_len = UIP_TCPIP_HLEN;
7566 + BUF->flags = TCP_ACK;
7567 + goto tcp_send_noopts;
7572 + /* We can close this connection if the peer has acknowledged our
7573 + FIN. This is indicated by the UIP_ACKDATA flag. */
7574 + if(uip_flags & UIP_ACKDATA) {
7575 + uip_connr->tcpstateflags = CLOSED;
7576 + uip_flags = UIP_CLOSE;
7582 + /* The application has closed the connection, but the remote host
7583 + hasn't closed its end yet. Thus we do nothing but wait for a
7584 + FIN from the other side. */
7586 + uip_add_rcv_nxt(uip_len);
7588 + if(BUF->flags & TCP_FIN) {
7589 + if(uip_flags & UIP_ACKDATA) {
7590 + uip_connr->tcpstateflags = TIME_WAIT;
7591 + uip_connr->timer = 0;
7592 + uip_connr->len = 0;
7594 + uip_connr->tcpstateflags = CLOSING;
7596 + uip_add_rcv_nxt(1);
7597 + uip_flags = UIP_CLOSE;
7599 + goto tcp_send_ack;
7600 + } else if(uip_flags & UIP_ACKDATA) {
7601 + uip_connr->tcpstateflags = FIN_WAIT_2;
7602 + uip_connr->len = 0;
7606 + goto tcp_send_ack;
7612 + uip_add_rcv_nxt(uip_len);
7614 + if(BUF->flags & TCP_FIN) {
7615 + uip_connr->tcpstateflags = TIME_WAIT;
7616 + uip_connr->timer = 0;
7617 + uip_add_rcv_nxt(1);
7618 + uip_flags = UIP_CLOSE;
7620 + goto tcp_send_ack;
7623 + goto tcp_send_ack;
7628 + goto tcp_send_ack;
7631 + if(uip_flags & UIP_ACKDATA) {
7632 + uip_connr->tcpstateflags = TIME_WAIT;
7633 + uip_connr->timer = 0;
7639 + /* We jump here when we are ready to send the packet, and just want
7640 + to set the appropriate TCP sequence numbers in the TCP header. */
7642 + BUF->flags = TCP_ACK;
7646 + BUF->tcpoffset = 5 << 4;
7648 + /* We're done with the input processing. We are now ready to send a
7649 + reply. Our job is to fill in all the fields of the TCP and IP
7650 + headers before calculating the checksum and finally send the
7652 + BUF->ackno[0] = uip_connr->rcv_nxt[0];
7653 + BUF->ackno[1] = uip_connr->rcv_nxt[1];
7654 + BUF->ackno[2] = uip_connr->rcv_nxt[2];
7655 + BUF->ackno[3] = uip_connr->rcv_nxt[3];
7657 + BUF->seqno[0] = uip_connr->snd_nxt[0];
7658 + BUF->seqno[1] = uip_connr->snd_nxt[1];
7659 + BUF->seqno[2] = uip_connr->snd_nxt[2];
7660 + BUF->seqno[3] = uip_connr->snd_nxt[3];
7662 + BUF->proto = UIP_PROTO_TCP;
7664 + BUF->srcport = uip_connr->lport;
7665 + BUF->destport = uip_connr->rport;
7667 + BUF->srcipaddr[0] = uip_hostaddr[0];
7668 + BUF->srcipaddr[1] = uip_hostaddr[1];
7669 + BUF->destipaddr[0] = uip_connr->ripaddr[0];
7670 + BUF->destipaddr[1] = uip_connr->ripaddr[1];
7673 + if(uip_connr->tcpstateflags & UIP_STOPPED) {
7674 + /* If the connection has issued uip_stop(), we advertise a zero
7675 + window so that the remote host will stop sending data. */
7676 + BUF->wnd[0] = BUF->wnd[1] = 0;
7678 + BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
7679 + BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
7684 + BUF->len[0] = (uip_len >> 8);
7685 + BUF->len[1] = (uip_len & 0xff);
7687 + /* Calculate TCP checksum. */
7688 + BUF->tcpchksum = 0;
7689 + BUF->tcpchksum = ~(uip_tcpchksum());
7695 + BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
7696 + BUF->ttl = UIP_TTL;
7698 + BUF->ipid[0] = ipid >> 8;
7699 + BUF->ipid[1] = ipid & 0xff;
7701 + /* Calculate IP checksum. */
7702 + BUF->ipchksum = 0;
7703 + BUF->ipchksum = ~(uip_ipchksum());
7705 + UIP_STAT(++uip_stat.tcp.sent);
7707 + UIP_STAT(++uip_stat.ip.sent);
7708 + /* Return and let the caller do the actual transmission. */
7714 +/*-----------------------------------------------------------------------------------*/
7715 +/*unsigned short int
7716 +htons(unsigned short int val)
7718 + return HTONS(val);
7720 +/*-----------------------------------------------------------------------------------*/
7723 +++ b/net/uip-0.9/uip.h
7732 + * Header file for the uIP TCP/IP stack.
7733 + * \author Adam Dunkels <adam@dunkels.com>
7735 + * The uIP TCP/IP stack header file contains definitions for a number
7736 + * of C macros that are used by uIP programs as well as internal uIP
7737 + * structures, TCP/IP header structures and function declarations.
7743 + * Copyright (c) 2001-2003, Adam Dunkels.
7744 + * All rights reserved.
7746 + * Redistribution and use in source and binary forms, with or without
7747 + * modification, are permitted provided that the following conditions
7749 + * 1. Redistributions of source code must retain the above copyright
7750 + * notice, this list of conditions and the following disclaimer.
7751 + * 2. Redistributions in binary form must reproduce the above copyright
7752 + * notice, this list of conditions and the following disclaimer in the
7753 + * documentation and/or other materials provided with the distribution.
7754 + * 3. The name of the author may not be used to endorse or promote
7755 + * products derived from this software without specific prior
7756 + * written permission.
7758 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
7759 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
7760 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7761 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
7762 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7763 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
7764 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
7765 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
7766 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
7767 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
7768 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7770 + * This file is part of the uIP TCP/IP stack.
7772 + * $Id: uip.h,v 1.36.2.7 2003/10/07 13:47:51 adam Exp $
7778 +#include <linux/types.h>
7779 +#include <linux/string.h>
7780 +#include <linux/ctype.h>
7781 +#include <malloc.h>
7782 +#include <common.h>
7785 +#include "uipopt.h"
7787 +/*-----------------------------------------------------------------------------------*/
7788 +/* First, the functions that should be called from the
7789 + * system. Initialization, the periodic timer and incoming packets are
7790 + * handled by the following three functions.
7794 + * \defgroup uipconffunc uIP configuration functions
7797 + * The uIP configuration functions are used for setting run-time
7798 + * parameters in uIP such as IP addresses.
7802 + * Set the IP address of this host.
7804 + * The IP address is represented as a 4-byte array where the first
7805 + * octet of the IP address is put in the first member of the 4-byte
7808 + * \param addr A pointer to a 4-byte representation of the IP address.
7810 + * \hideinitializer
7812 +#define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \
7813 + uip_hostaddr[1] = addr[1]; } while(0)
7816 + * Get the IP address of this host.
7818 + * The IP address is represented as a 4-byte array where the first
7819 + * octet of the IP address is put in the first member of the 4-byte
7822 + * \param addr A pointer to a 4-byte array that will be filled in with
7823 + * the currently configured IP address.
7825 + * \hideinitializer
7827 +#define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \
7828 + addr[1] = uip_hostaddr[1]; } while(0)
7833 + * \defgroup uipinit uIP initialization functions
7836 + * The uIP initialization functions are used for booting uIP.
7840 + * uIP initialization function.
7842 + * This function should be called at boot up to initilize the uIP
7845 +void uip_init(void);
7850 + * \defgroup uipdevfunc uIP device driver functions
7853 + * These functions are used by a network device driver for interacting
7858 + * Process an incoming packet.
7860 + * This function should be called when the device driver has received
7861 + * a packet from the network. The packet from the device driver must
7862 + * be present in the uip_buf buffer, and the length of the packet
7863 + * should be placed in the uip_len variable.
7865 + * When the function returns, there may be an outbound packet placed
7866 + * in the uip_buf packet buffer. If so, the uip_len variable is set to
7867 + * the length of the packet. If no packet is to be sent out, the
7868 + * uip_len variable is set to 0.
7870 + * The usual way of calling the function is presented by the source
7873 + uip_len = devicedriver_poll();
7877 + devicedriver_send();
7882 + * \note If you are writing a uIP device driver that needs ARP
7883 + * (Address Resolution Protocol), e.g., when running uIP over
7884 + * Ethernet, you will need to call the uIP ARP code before calling
7887 + #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
7888 + uip_len = ethernet_devicedrver_poll();
7890 + if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
7895 + ethernet_devicedriver_send();
7897 + } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
7900 + ethernet_devicedriver_send();
7905 + * \hideinitializer
7907 +#define uip_input() uip_process(UIP_DATA)
7910 + * Periodic processing for a connection identified by its number.
7912 + * This function does the necessary periodic processing (timers,
7913 + * polling) for a uIP TCP conneciton, and should be called when the
7914 + * periodic uIP timer goes off. It should be called for every
7915 + * connection, regardless of whether they are open of closed.
7917 + * When the function returns, it may have an outbound packet waiting
7918 + * for service in the uIP packet buffer, and if so the uip_len
7919 + * variable is set to a value larger than zero. The device driver
7920 + * should be called to send out the packet.
7922 + * The ususal way of calling the function is through a for() loop like
7925 + for(i = 0; i < UIP_CONNS; ++i) {
7928 + devicedriver_send();
7933 + * \note If you are writing a uIP device driver that needs ARP
7934 + * (Address Resolution Protocol), e.g., when running uIP over
7935 + * Ethernet, you will need to call the uip_arp_out() function before
7936 + * calling the device driver:
7938 + for(i = 0; i < UIP_CONNS; ++i) {
7942 + ethernet_devicedriver_send();
7947 + * \param conn The number of the connection which is to be periodically polled.
7949 + * \hideinitializer
7951 +#define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
7952 + uip_process(UIP_TIMER); } while (0)
7955 + * Periodic processing for a connection identified by a pointer to its structure.
7957 + * Same as uip_periodic() but takes a pointer to the actual uip_conn
7958 + * struct instead of an integer as its argument. This function can be
7959 + * used to force periodic processing of a specific connection.
7961 + * \param conn A pointer to the uip_conn struct for the connection to
7964 + * \hideinitializer
7966 +#define uip_periodic_conn(conn) do { uip_conn = conn; \
7967 + uip_process(UIP_TIMER); } while (0)
7971 + * Periodic processing for a UDP connection identified by its number.
7973 + * This function is essentially the same as uip_prerioic(), but for
7974 + * UDP connections. It is called in a similar fashion as the
7975 + * uip_periodic() function:
7977 + for(i = 0; i < UIP_UDP_CONNS; i++) {
7978 + uip_udp_periodic(i);
7980 + devicedriver_send();
7985 + * \note As for the uip_periodic() function, special care has to be
7986 + * taken when using uIP together with ARP and Ethernet:
7988 + for(i = 0; i < UIP_UDP_CONNS; i++) {
7989 + uip_udp_periodic(i);
7992 + ethernet_devicedriver_send();
7997 + * \param conn The number of the UDP connection to be processed.
7999 + * \hideinitializer
8001 +#define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
8002 + uip_process(UIP_UDP_TIMER); } while (0)
8005 + * Periodic processing for a UDP connection identified by a pointer to
8008 + * Same as uip_udp_periodic() but takes a pointer to the actual
8009 + * uip_conn struct instead of an integer as its argument. This
8010 + * function can be used to force periodic processing of a specific
8013 + * \param conn A pointer to the uip_udp_conn struct for the connection
8014 + * to be processed.
8016 + * \hideinitializer
8018 +#define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
8019 + uip_process(UIP_UDP_TIMER); } while (0)
8022 +#endif /* UIP_UDP */
8025 + * The uIP packet buffer.
8027 + * The uip_buf array is used to hold incoming and outgoing
8028 + * packets. The device driver should place incoming data into this
8029 + * buffer. When sending data, the device driver should read the link
8030 + * level headers and the TCP/IP headers from this buffer. The size of
8031 + * the link level headers is configured by the UIP_LLH_LEN define.
8033 + * \note The application data need not be placed in this buffer, so
8034 + * the device driver must read it from the place pointed to by the
8035 + * uip_appdata pointer as illustrated by the following example:
8038 + devicedriver_send(void)
8040 + hwsend(&uip_buf[0], UIP_LLH_LEN);
8041 + hwsend(&uip_buf[UIP_LLH_LEN], 40);
8042 + hwsend(uip_appdata, uip_len - 40 - UIP_LLH_LEN);
8046 +extern u8_t uip_buf[UIP_BUFSIZE+2];
8050 +/*-----------------------------------------------------------------------------------*/
8051 +/* Functions that are used by the uIP application program. Opening and
8052 + * closing connections, sending and receiving data, etc. is all
8053 + * handled by the functions below.
8056 + * \defgroup uipappfunc uIP application functions
8059 + * Functions used by an application running of top of uIP.
8063 + * Start listening to the specified port.
8065 + * \note Since this function expects the port number in network byte
8066 + * order, a conversion using HTONS() or htons() is necessary.
8069 + uip_listen(HTONS(80));
8072 + * \param port A 16-bit port number in network byte order.
8074 +void uip_listen(u16_t port);
8077 + * Stop listening to the specified port.
8079 + * \note Since this function expects the port number in network byte
8080 + * order, a conversion using HTONS() or htons() is necessary.
8083 + uip_unlisten(HTONS(80));
8086 + * \param port A 16-bit port number in network byte order.
8088 +void uip_unlisten(u16_t port);
8091 + * Connect to a remote host using TCP.
8093 + * This function is used to start a new connection to the specified
8094 + * port on the specied host. It allocates a new connection identifier,
8095 + * sets the connection to the SYN_SENT state and sets the
8096 + * retransmission timer to 0. This will cause a TCP SYN segment to be
8097 + * sent out the next time this connection is periodically processed,
8098 + * which usually is done within 0.5 seconds after the call to
8101 + * \note This function is avaliable only if support for active open
8102 + * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
8104 + * \note Since this function requires the port number to be in network
8105 + * byte order, a convertion using HTONS() or htons() is necessary.
8110 + uip_ipaddr(ipaddr, 192,168,1,2);
8111 + uip_connect(ipaddr, HTONS(80));
8114 + * \param ripaddr A pointer to a 4-byte array representing the IP
8115 + * address of the remote hot.
8117 + * \param port A 16-bit port number in network byte order.
8119 + * \return A pointer to the uIP connection identifier for the new connection,
8120 + * or NULL if no connection could be allocated.
8123 +struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);
8130 + * Check if a connection has outstanding (i.e., unacknowledged) data.
8132 + * \param conn A pointer to the uip_conn structure for the connection.
8134 + * \hideinitializer
8136 +#define uip_outstanding(conn) ((conn)->len)
8139 + * Send data on the current connection.
8141 + * This function is used to send out a single segment of TCP
8142 + * data. Only applications that have been invoked by uIP for event
8143 + * processing can send data.
8145 + * The amount of data that actually is sent out after a call to this
8146 + * funcion is determined by the maximum amount of data TCP allows. uIP
8147 + * will automatically crop the data so that only the appropriate
8148 + * amount of data is sent. The function uip_mss() can be used to query
8149 + * uIP for the amount of data that actually will be sent.
8151 + * \note This function does not guarantee that the sent data will
8152 + * arrive at the destination. If the data is lost in the network, the
8153 + * application will be invoked with the uip_rexmit() event being
8154 + * set. The application will then have to resend the data using this
8157 + * \param data A pointer to the data which is to be sent.
8159 + * \param len The maximum amount of data bytes to be sent.
8161 + * \hideinitializer
8163 +#define uip_send(data, len) do { uip_sappdata = (data); uip_slen = (len);} while(0)
8166 + * The length of any incoming data that is currently avaliable (if avaliable)
8167 + * in the uip_appdata buffer.
8169 + * The test function uip_data() must first be used to check if there
8170 + * is any data available at all.
8172 + * \hideinitializer
8174 +#define uip_datalen() uip_len
8177 + * The length of any out-of-band data (urgent data) that has arrived
8178 + * on the connection.
8180 + * \note The configuration parameter UIP_URGDATA must be set for this
8181 + * function to be enabled.
8183 + * \hideinitializer
8185 +#define uip_urgdatalen() uip_urglen
8188 + * Close the current connection.
8190 + * This function will close the current connection in a nice way.
8192 + * \hideinitializer
8194 +#define uip_close() (uip_flags = UIP_CLOSE)
8197 + * Abort the current connection.
8199 + * This function will abort (reset) the current connection, and is
8200 + * usually used when an error has occured that prevents using the
8201 + * uip_close() function.
8203 + * \hideinitializer
8205 +#define uip_abort() (uip_flags = UIP_ABORT)
8208 + * Tell the sending host to stop sending data.
8210 + * This function will close our receiver's window so that we stop
8211 + * receiving data for the current connection.
8213 + * \hideinitializer
8215 +#define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
8218 + * Find out if the current connection has been previously stopped with
8221 + * \hideinitializer
8223 +#define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
8226 + * Restart the current connection, if is has previously been stopped
8227 + * with uip_stop().
8229 + * This function will open the receiver's window again so that we
8230 + * start receiving data for the current connection.
8232 + * \hideinitializer
8234 +#define uip_restart() do { uip_flags |= UIP_NEWDATA; \
8235 + uip_conn->tcpstateflags &= ~UIP_STOPPED; \
8239 +/* uIP tests that can be made to determine in what state the current
8240 + connection is, and what the application function should do. */
8243 + * Is new incoming data available?
8245 + * Will reduce to non-zero if there is new data for the application
8246 + * present at the uip_appdata pointer. The size of the data is
8247 + * avaliable through the uip_len variable.
8249 + * \hideinitializer
8251 +#define uip_newdata() (uip_flags & UIP_NEWDATA)
8254 + * Has previously sent data been acknowledged?
8256 + * Will reduce to non-zero if the previously sent data has been
8257 + * acknowledged by the remote host. This means that the application
8258 + * can send new data.
8260 + * \hideinitializer
8262 +#define uip_acked() (uip_flags & UIP_ACKDATA)
8265 + * Has the connection just been connected?
8267 + * Reduces to non-zero if the current connection has been connected to
8268 + * a remote host. This will happen both if the connection has been
8269 + * actively opened (with uip_connect()) or passively opened (with
8272 + * \hideinitializer
8274 +#define uip_connected() (uip_flags & UIP_CONNECTED)
8277 + * Has the connection been closed by the other end?
8279 + * Is non-zero if the connection has been closed by the remote
8280 + * host. The application may then do the necessary clean-ups.
8282 + * \hideinitializer
8284 +#define uip_closed() (uip_flags & UIP_CLOSE)
8287 + * Has the connection been aborted by the other end?
8289 + * Non-zero if the current connection has been aborted (reset) by the
8292 + * \hideinitializer
8294 +#define uip_aborted() (uip_flags & UIP_ABORT)
8297 + * Has the connection timed out?
8299 + * Non-zero if the current connection has been aborted due to too many
8300 + * retransmissions.
8302 + * \hideinitializer
8304 +#define uip_timedout() (uip_flags & UIP_TIMEDOUT)
8307 + * Do we need to retransmit previously data?
8309 + * Reduces to non-zero if the previously sent data has been lost in
8310 + * the network, and the application should retransmit it. The
8311 + * application should send the exact same data as it did the last
8312 + * time, using the uip_send() function.
8314 + * \hideinitializer
8316 +#define uip_rexmit() (uip_flags & UIP_REXMIT)
8319 + * Is the connection being polled by uIP?
8321 + * Is non-zero if the reason the application is invoked is that the
8322 + * current connection has been idle for a while and should be
8325 + * The polling event can be used for sending data without having to
8326 + * wait for the remote host to send data.
8328 + * \hideinitializer
8330 +#define uip_poll() (uip_flags & UIP_POLL)
8333 + * Get the initial maxium segment size (MSS) of the current
8336 + * \hideinitializer
8338 +#define uip_initialmss() (uip_conn->initialmss)
8341 + * Get the current maxium segment size that can be sent on the current
8344 + * The current maxiumum segment size that can be sent on the
8345 + * connection is computed from the receiver's window and the MSS of
8346 + * the connection (which also is available by calling
8347 + * uip_initialmss()).
8349 + * \hideinitializer
8351 +#define uip_mss() (uip_conn->mss)
8354 + * Set up a new UDP connection.
8356 + * \param ripaddr A pointer to a 4-byte structure representing the IP
8357 + * address of the remote host.
8359 + * \param rport The remote port number in network byte order.
8361 + * \return The uip_udp_conn structure for the new connection or NULL
8362 + * if no connection could be allocated.
8364 +struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);
8367 + * Removed a UDP connection.
8369 + * \param conn A pointer to the uip_udp_conn structure for the connection.
8371 + * \hideinitializer
8373 +#define uip_udp_remove(conn) (conn)->lport = 0
8376 + * Send a UDP datagram of length len on the current connection.
8378 + * This function can only be called in response to a UDP event (poll
8379 + * or newdata). The data must be present in the uip_buf buffer, at the
8380 + * place pointed to by the uip_appdata pointer.
8382 + * \param len The length of the data in the uip_buf buffer.
8384 + * \hideinitializer
8386 +#define uip_udp_send(len) uip_slen = (len)
8390 +/* uIP convenience and converting functions. */
8393 + * \defgroup uipconvfunc uIP conversion functions
8396 + * These functions can be used for converting between different data
8397 + * formats used by uIP.
8401 + * Pack an IP address into a 4-byte array which is used by uIP to
8402 + * represent IP addresses.
8408 + uip_ipaddr(&ipaddr, 192,168,1,2);
8411 + * \param addr A pointer to a 4-byte array that will be filled in with
8413 + * \param addr0 The first octet of the IP address.
8414 + * \param addr1 The second octet of the IP address.
8415 + * \param addr2 The third octet of the IP address.
8416 + * \param addr3 The forth octet of the IP address.
8418 + * \hideinitializer
8420 +#define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
8421 + (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \
8422 + (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \
8426 + * Convert 16-bit quantity from host byte order to network byte order.
8428 + * This macro is primarily used for converting constants from host
8429 + * byte order to network byte order. For converting variables to
8430 + * network byte order, use the htons() function instead.
8432 + * \hideinitializer
8435 +# if BYTE_ORDER == BIG_ENDIAN
8436 +# define HTONS(n) (n)
8437 +# else /* BYTE_ORDER == BIG_ENDIAN */
8438 +# define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))
8439 +# endif /* BYTE_ORDER == BIG_ENDIAN */
8443 + * Convert 16-bit quantity from host byte order to network byte order.
8445 + * This function is primarily used for converting variables from host
8446 + * byte order to network byte order. For converting constants to
8447 + * network byte order, use the HTONS() macro instead.
8450 +u16_t htons(u16_t val);
8456 + * Pointer to the application data in the packet buffer.
8458 + * This pointer points to the application data when the application is
8459 + * called. If the application wishes to send data, the application may
8460 + * use this space to write the data into before calling uip_send().
8462 +extern volatile u8_t *uip_appdata;
8463 +extern volatile u8_t *uip_sappdata;
8465 +#if UIP_URGDATA > 0
8466 +/* u8_t *uip_urgdata:
8468 + * This pointer points to any urgent data that has been received. Only
8469 + * present if compiled with support for urgent data (UIP_URGDATA).
8471 +extern volatile u8_t *uip_urgdata;
8472 +#endif /* UIP_URGDATA > 0 */
8475 +/* u[8|16]_t uip_len:
8477 + * When the application is called, uip_len contains the length of any
8478 + * new data that has been received from the remote host. The
8479 + * application should set this variable to the size of any data that
8480 + * the application wishes to send. When the network device driver
8481 + * output function is called, uip_len should contain the length of the
8482 + * outgoing packet.
8484 +extern volatile u16_t uip_len, uip_slen;
8486 +#if UIP_URGDATA > 0
8487 +extern volatile u8_t uip_urglen, uip_surglen;
8488 +#endif /* UIP_URGDATA > 0 */
8492 + * Representation of a uIP TCP connection.
8494 + * The uip_conn structure is used for identifying a connection. All
8495 + * but one field in the structure are to be considered read-only by an
8496 + * application. The only exception is the appstate field whos purpose
8497 + * is to let the application store application-specific state (e.g.,
8498 + * file pointers) for the connection. The size of this field is
8499 + * configured in the "uipopt.h" header file.
8502 + u16_t ripaddr[2]; /**< The IP address of the remote host. */
8504 + u16_t lport; /**< The local TCP port, in network byte order. */
8505 + u16_t rport; /**< The local remote TCP port, in network byte
8508 + u8_t rcv_nxt[4]; /**< The sequence number that we expect to
8510 + u8_t snd_nxt[4]; /**< The sequence number that was last sent by
8512 + u16_t len; /**< Length of the data that was previously sent. */
8513 + u16_t mss; /**< Current maximum segment size for the
8515 + u16_t initialmss; /**< Initial maximum segment size for the
8517 + u8_t sa; /**< Retransmission time-out calculation state
8519 + u8_t sv; /**< Retransmission time-out calculation state
8521 + u8_t rto; /**< Retransmission time-out. */
8522 + u8_t tcpstateflags; /**< TCP state and flags. */
8523 + u8_t timer; /**< The retransmission timer. */
8524 + u8_t nrtx; /**< The number of retransmissions for the last
8527 + /** The application state. */
8528 + u8_t appstate[UIP_APPSTATE_SIZE];
8532 +/* Pointer to the current connection. */
8533 +extern struct uip_conn *uip_conn;
8534 +/* The array containing all uIP connections. */
8535 +extern struct uip_conn uip_conns[UIP_CONNS];
8537 + * \addtogroup uiparch
8542 + * 4-byte array used for the 32-bit sequence number calculations.
8544 +extern volatile u8_t uip_acc32[4];
8551 + * Representation of a uIP UDP connection.
8553 +struct uip_udp_conn {
8554 + u16_t ripaddr[2]; /**< The IP address of the remote peer. */
8555 + u16_t lport; /**< The local port number in network byte order. */
8556 + u16_t rport; /**< The remote port number in network byte order. */
8559 +extern struct uip_udp_conn *uip_udp_conn;
8560 +extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
8561 +#endif /* UIP_UDP */
8564 + * The structure holding the TCP/IP statistics that are gathered if
8565 + * UIP_STATISTICS is set to 1.
8570 + uip_stats_t drop; /**< Number of dropped packets at the IP
8572 + uip_stats_t recv; /**< Number of received packets at the IP
8574 + uip_stats_t sent; /**< Number of sent packets at the IP
8576 + uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
8577 + IP version or header length. */
8578 + uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
8579 + IP length, high byte. */
8580 + uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
8581 + IP length, low byte. */
8582 + uip_stats_t fragerr; /**< Number of packets dropped since they
8583 + were IP fragments. */
8584 + uip_stats_t chkerr; /**< Number of packets dropped due to IP
8585 + checksum errors. */
8586 + uip_stats_t protoerr; /**< Number of packets dropped since they
8587 + were neither ICMP, UDP nor TCP. */
8588 + } ip; /**< IP statistics. */
8590 + uip_stats_t drop; /**< Number of dropped ICMP packets. */
8591 + uip_stats_t recv; /**< Number of received ICMP packets. */
8592 + uip_stats_t sent; /**< Number of sent ICMP packets. */
8593 + uip_stats_t typeerr; /**< Number of ICMP packets with a wrong
8595 + } icmp; /**< ICMP statistics. */
8597 + uip_stats_t drop; /**< Number of dropped TCP segments. */
8598 + uip_stats_t recv; /**< Number of recived TCP segments. */
8599 + uip_stats_t sent; /**< Number of sent TCP segments. */
8600 + uip_stats_t chkerr; /**< Number of TCP segments with a bad
8602 + uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK
8604 + uip_stats_t rst; /**< Number of recevied TCP RST (reset) segments. */
8605 + uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
8606 + uip_stats_t syndrop; /**< Number of dropped SYNs due to too few
8607 + connections was avaliable. */
8608 + uip_stats_t synrst; /**< Number of SYNs for closed ports,
8609 + triggering a RST. */
8610 + } tcp; /**< TCP statistics. */
8614 + * The uIP TCP/IP statistics.
8616 + * This is the variable in which the uIP TCP/IP statistics are gathered.
8618 +extern struct uip_stats uip_stat;
8621 +/*-----------------------------------------------------------------------------------*/
8622 +/* All the stuff below this point is internal to uIP and should not be
8623 + * used directly by an application or by a device driver.
8625 +/*-----------------------------------------------------------------------------------*/
8628 + * When the application is called, uip_flags will contain the flags
8629 + * that are defined in this file. Please read below for more
8632 +extern volatile u8_t uip_flags;
8634 +/* The following flags may be set in the global variable uip_flags
8635 + before calling the application callback. The UIP_ACKDATA and
8636 + UIP_NEWDATA flags may both be set at the same time, whereas the
8637 + others are mutualy exclusive. Note that these flags should *NOT* be
8638 + accessed directly, but through the uIP functions/macros. */
8640 +#define UIP_ACKDATA 1 /* Signifies that the outstanding data was
8641 + acked and the application should send
8642 + out new data instead of retransmitting
8644 +#define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
8646 +#define UIP_REXMIT 4 /* Tells the application to retransmit the
8647 + data that was last sent. */
8648 +#define UIP_POLL 8 /* Used for polling the application, to
8649 + check if the application has data that
8650 + it wants to send. */
8651 +#define UIP_CLOSE 16 /* The remote host has closed the
8652 + connection, thus the connection has
8653 + gone away. Or the application signals
8654 + that it wants to close the
8656 +#define UIP_ABORT 32 /* The remote host has aborted the
8657 + connection, thus the connection has
8658 + gone away. Or the application signals
8659 + that it wants to abort the
8661 +#define UIP_CONNECTED 64 /* We have got a connection from a remote
8662 + host and have set up a new connection
8663 + for it, or an active connection has
8664 + been successfully established. */
8666 +#define UIP_TIMEDOUT 128 /* The connection has been aborted due to
8667 + too many retransmissions. */
8670 +/* uip_process(flag):
8672 + * The actual uIP function which does all the work.
8674 +void uip_process(u8_t flag);
8676 +/* The following flags are passed as an argument to the uip_process()
8677 + function. They are used to distinguish between the two cases where
8678 + uip_process() is called. It can be called either because we have
8679 + incoming data that should be processed, or because the periodic
8680 + timer has fired. */
8682 +#define UIP_DATA 1 /* Tells uIP that there is incoming data in
8683 + the uip_buf buffer. The length of the
8684 + data is stored in the global variable
8686 +#define UIP_TIMER 2 /* Tells uIP that the periodic timer has
8689 +#define UIP_UDP_TIMER 3
8690 +#endif /* UIP_UDP */
8692 +/* The TCP states used in the uip_conn->tcpstateflags. */
8696 +#define ESTABLISHED 3
8697 +#define FIN_WAIT_1 4
8698 +#define FIN_WAIT_2 5
8700 +#define TIME_WAIT 7
8704 +#define UIP_STOPPED 16
8706 +#define UIP_TCPIP_HLEN 40
8708 +/* The TCP and IP headers. */
8719 + u16_t srcipaddr[2],
8735 +/* The ICMP and IP headers. */
8746 + u16_t srcipaddr[2],
8748 + /* ICMP (echo) header. */
8755 +/* The UDP and IP headers. */
8766 + u16_t srcipaddr[2],
8776 +#define UIP_PROTO_ICMP 1
8777 +#define UIP_PROTO_TCP 6
8778 +#define UIP_PROTO_UDP 17
8781 +extern const u16_t uip_hostaddr[2];
8782 +#else /* UIP_FIXEDADDR */
8783 +extern u16_t uip_hostaddr[2];
8784 +#endif /* UIP_FIXEDADDR */
8786 +#endif /* __UIP_H__ */
8792 +++ b/net/uip-0.9/uipopt.h
8795 + * \defgroup uipopt Configuration options for uIP
8798 + * uIP is configured using the per-project configuration file
8799 + * "uipopt.h". This file contains all compile-time options for uIP and
8800 + * should be tweaked to match each specific project. The uIP
8801 + * distribution contains a documented example "uipopt.h" that can be
8802 + * copied and modified for each project.
8807 + * Configuration options for uIP.
8808 + * \author Adam Dunkels <adam@dunkels.com>
8810 + * This file is used for tweaking various configuration options for
8811 + * uIP. You should make a copy of this file into one of your project's
8812 + * directories instead of editing this example "uipopt.h" file that
8813 + * comes with the uIP distribution.
8817 + * Copyright (c) 2001-2003, Adam Dunkels.
8818 + * All rights reserved.
8820 + * Redistribution and use in source and binary forms, with or without
8821 + * modification, are permitted provided that the following conditions
8823 + * 1. Redistributions of source code must retain the above copyright
8824 + * notice, this list of conditions and the following disclaimer.
8825 + * 2. Redistributions in binary form must reproduce the above copyright
8826 + * notice, this list of conditions and the following disclaimer in the
8827 + * documentation and/or other materials provided with the distribution.
8828 + * 3. The name of the author may not be used to endorse or promote
8829 + * products derived from this software without specific prior
8830 + * written permission.
8832 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
8833 + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
8834 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8835 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
8836 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8837 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
8838 + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8839 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
8840 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
8841 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
8842 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8844 + * This file is part of the uIP TCP/IP stack.
8846 + * $Id: uipopt.h,v 1.16.2.5 2003/10/07 13:22:51 adam Exp $
8850 +#ifndef __UIPOPT_H__
8851 +#define __UIPOPT_H__
8853 +/*------------------------------------------------------------------------------*/
8855 + * \defgroup uipopttypedef uIP type definitions
8860 + * The 8-bit unsigned data type.
8862 + * This may have to be tweaked for your particular compiler. "unsigned
8863 + * char" works for most compilers.
8865 +typedef unsigned char u8_t;
8868 + * The 16-bit unsigned data type.
8870 + * This may have to be tweaked for your particular compiler. "unsigned
8871 + * short" works for most compilers.
8873 +typedef unsigned short u16_t;
8876 + * The statistics data type.
8878 + * This datatype determines how high the statistics counters are able
8881 +typedef unsigned short uip_stats_t;
8885 +/*------------------------------------------------------------------------------*/
8888 + * \defgroup uipoptstaticconf Static configuration options
8891 + * These configuration options can be used for setting the IP address
8892 + * settings statically, but only if UIP_FIXEDADDR is set to 1. The
8893 + * configuration options for a specific node includes IP address,
8894 + * netmask and default router as well as the Ethernet address. The
8895 + * netmask, default router and Ethernet address are appliciable only
8896 + * if uIP should be run over Ethernet.
8898 + * All of these should be changed to suit your project.
8902 + * Determines if uIP should use a fixed IP address or not.
8904 + * If uIP should use a fixed IP address, the settings are set in the
8905 + * uipopt.h file. If not, the macros uip_sethostaddr(),
8906 + * uip_setdraddr() and uip_setnetmask() should be used instead.
8908 + * \hideinitializer
8910 +#define UIP_FIXEDADDR 0
8913 + * Ping IP address asignment.
8915 + * uIP uses a "ping" packets for setting its own IP address if this
8916 + * option is set. If so, uIP will start with an empty IP address and
8917 + * the destination IP address of the first incoming "ping" (ICMP echo)
8918 + * packet will be used for setting the hosts IP address.
8920 + * \note This works only if UIP_FIXEDADDR is 0.
8922 + * \hideinitializer
8924 +#define UIP_PINGADDRCONF 0
8926 +#define UIP_IPADDR0 192 /**< The first octet of the IP address of
8927 + this uIP node, if UIP_FIXEDADDR is
8928 + 1. \hideinitializer */
8929 +#define UIP_IPADDR1 168 /**< The second octet of the IP address of
8930 + this uIP node, if UIP_FIXEDADDR is
8931 + 1. \hideinitializer */
8932 +#define UIP_IPADDR2 0 /**< The third octet of the IP address of
8933 + this uIP node, if UIP_FIXEDADDR is
8934 + 1. \hideinitializer */
8935 +#define UIP_IPADDR3 250 /**< The fourth octet of the IP address of
8936 + this uIP node, if UIP_FIXEDADDR is
8937 + 1. \hideinitializer */
8939 +#define UIP_NETMASK0 255 /**< The first octet of the netmask of
8940 + this uIP node, if UIP_FIXEDADDR is
8941 + 1. \hideinitializer */
8942 +#define UIP_NETMASK1 255 /**< The second octet of the netmask of
8943 + this uIP node, if UIP_FIXEDADDR is
8944 + 1. \hideinitializer */
8945 +#define UIP_NETMASK2 255 /**< The third octet of the netmask of
8946 + this uIP node, if UIP_FIXEDADDR is
8947 + 1. \hideinitializer */
8948 +#define UIP_NETMASK3 0 /**< The fourth octet of the netmask of
8949 + this uIP node, if UIP_FIXEDADDR is
8950 + 1. \hideinitializer */
8952 +#define UIP_DRIPADDR0 192 /**< The first octet of the IP address of
8953 + the default router, if UIP_FIXEDADDR is
8954 + 1. \hideinitializer */
8955 +#define UIP_DRIPADDR1 168 /**< The second octet of the IP address of
8956 + the default router, if UIP_FIXEDADDR is
8957 + 1. \hideinitializer */
8958 +#define UIP_DRIPADDR2 0 /**< The third octet of the IP address of
8959 + the default router, if UIP_FIXEDADDR is
8960 + 1. \hideinitializer */
8961 +#define UIP_DRIPADDR3 1 /**< The fourth octet of the IP address of
8962 + the default router, if UIP_FIXEDADDR is
8963 + 1. \hideinitializer */
8966 + * Specifies if the uIP ARP module should be compiled with a fixed
8967 + * Ethernet MAC address or not.
8969 + * If this configuration option is 0, the macro uip_setethaddr() can
8970 + * be used to specify the Ethernet address at run-time.
8972 + * \hideinitializer
8974 +#define UIP_FIXEDETHADDR 0
8976 +#define UIP_ETHADDR0 0x00 /**< The first octet of the Ethernet
8977 + address if UIP_FIXEDETHADDR is
8978 + 1. \hideinitializer */
8979 +#define UIP_ETHADDR1 0xbd /**< The second octet of the Ethernet
8980 + address if UIP_FIXEDETHADDR is
8981 + 1. \hideinitializer */
8982 +#define UIP_ETHADDR2 0x3b /**< The third octet of the Ethernet
8983 + address if UIP_FIXEDETHADDR is
8984 + 1. \hideinitializer */
8985 +#define UIP_ETHADDR3 0x33 /**< The fourth octet of the Ethernet
8986 + address if UIP_FIXEDETHADDR is
8987 + 1. \hideinitializer */
8988 +#define UIP_ETHADDR4 0x05 /**< The fifth octet of the Ethernet
8989 + address if UIP_FIXEDETHADDR is
8990 + 1. \hideinitializer */
8991 +#define UIP_ETHADDR5 0x71 /**< The sixth octet of the Ethernet
8992 + address if UIP_FIXEDETHADDR is
8993 + 1. \hideinitializer */
8996 +/*------------------------------------------------------------------------------*/
8998 + * \defgroup uipoptip IP configuration options
9003 + * The IP TTL (time to live) of IP packets sent by uIP.
9005 + * This should normally not be changed.
9007 +#define UIP_TTL 255
9010 + * Turn on support for IP packet reassembly.
9012 + * uIP supports reassembly of fragmented IP packets. This features
9013 + * requires an additonal amount of RAM to hold the reassembly buffer
9014 + * and the reassembly code size is approximately 700 bytes. The
9015 + * reassembly buffer is of the same size as the uip_buf buffer
9016 + * (configured by UIP_BUFSIZE).
9018 + * \note IP packet reassembly is not heavily tested.
9020 + * \hideinitializer
9022 +#define UIP_REASSEMBLY 0
9025 + * The maximum time an IP fragment should wait in the reassembly
9026 + * buffer before it is dropped.
9029 +#define UIP_REASS_MAXAGE 40
9033 +/*------------------------------------------------------------------------------*/
9035 + * \defgroup uipoptudp UDP configuration options
9038 + * \note The UDP support in uIP is still not entirely complete; there
9039 + * is no support for sending or receiving broadcast or multicast
9040 + * packets, but it works well enough to support a number of vital
9041 + * applications such as DNS queries, though
9045 + * Toggles wether UDP support should be compiled in or not.
9047 + * \hideinitializer
9052 + * Toggles if UDP checksums should be used or not.
9054 + * \note Support for UDP checksums is currently not included in uIP,
9055 + * so this option has no function.
9057 + * \hideinitializer
9059 +#define UIP_UDP_CHECKSUMS 0
9062 + * The maximum amount of concurrent UDP connections.
9064 + * \hideinitializer
9066 +#define UIP_UDP_CONNS 10
9069 + * The name of the function that should be called when UDP datagrams arrive.
9071 + * \hideinitializer
9073 +#define UIP_UDP_APPCALL udp_appcall
9076 +/*------------------------------------------------------------------------------*/
9078 + * \defgroup uipopttcp TCP configuration options
9083 + * Determines if support for opening connections from uIP should be
9086 + * If the applications that are running on top of uIP for this project
9087 + * do not need to open outgoing TCP connections, this configration
9088 + * option can be turned off to reduce the code size of uIP.
9090 + * \hideinitializer
9092 +#define UIP_ACTIVE_OPEN 1
9095 + * The maximum number of simultaneously open TCP connections.
9097 + * Since the TCP connections are statically allocated, turning this
9098 + * configuration knob down results in less RAM used. Each TCP
9099 + * connection requires approximatly 30 bytes of memory.
9101 + * \hideinitializer
9103 +#define UIP_CONNS 10
9106 + * The maximum number of simultaneously listening TCP ports.
9108 + * Each listening TCP port requires 2 bytes of memory.
9110 + * \hideinitializer
9112 +#define UIP_LISTENPORTS 10
9115 + * The size of the advertised receiver's window.
9117 + * Should be set low (i.e., to the size of the uip_buf buffer) is the
9118 + * application is slow to process incoming data, or high (32768 bytes)
9119 + * if the application processes data quickly.
9121 + * \hideinitializer
9123 +#define UIP_RECEIVE_WINDOW 32768
9126 + * Determines if support for TCP urgent data notification should be
9129 + * Urgent data (out-of-band data) is a rarely used TCP feature that
9130 + * very seldom would be required.
9132 + * \hideinitializer
9134 +#define UIP_URGDATA 1
9137 + * The initial retransmission timeout counted in timer pulses.
9139 + * This should not be changed.
9144 + * The maximum number of times a segment should be retransmitted
9145 + * before the connection should be aborted.
9147 + * This should not be changed.
9149 +#define UIP_MAXRTX 8
9152 + * The maximum number of times a SYN segment should be retransmitted
9153 + * before a connection request should be deemed to have been
9156 + * This should not need to be changed.
9158 +#define UIP_MAXSYNRTX 3
9161 + * The TCP maximum segment size.
9163 + * This is should not be to set to more than UIP_BUFSIZE - UIP_LLH_LEN - 40.
9165 +#define UIP_TCP_MSS (UIP_BUFSIZE - UIP_LLH_LEN - 40)
9168 + * How long a connection should stay in the TIME_WAIT state.
9170 + * This configiration option has no real implication, and it should be
9173 +#define UIP_TIME_WAIT_TIMEOUT 120
9177 +/*------------------------------------------------------------------------------*/
9179 + * \defgroup uipoptarp ARP configuration options
9184 + * The size of the ARP table.
9186 + * This option should be set to a larger value if this uIP node will
9187 + * have many connections from the local network.
9189 + * \hideinitializer
9191 +#define UIP_ARPTAB_SIZE 8
9194 + * The maxium age of ARP table entries measured in 10ths of seconds.
9196 + * An UIP_ARP_MAXAGE of 120 corresponds to 20 minutes (BSD
9199 +#define UIP_ARP_MAXAGE 120
9203 +/*------------------------------------------------------------------------------*/
9206 + * \defgroup uipoptgeneral General configuration options
9211 + * The size of the uIP packet buffer.
9213 + * The uIP packet buffer should not be smaller than 60 bytes, and does
9214 + * not need to be larger than 1500 bytes. Lower size results in lower
9215 + * TCP throughput, larger size results in higher TCP throughput.
9217 + * \hideinitializer
9219 +#define UIP_BUFSIZE 1500
9223 + * Determines if statistics support should be compiled in.
9225 + * The statistics is useful for debugging and to show the user.
9227 + * \hideinitializer
9229 +#define UIP_STATISTICS 1
9232 + * Determines if logging of certain events should be compiled in.
9234 + * This is useful mostly for debugging. The function uip_log()
9235 + * must be implemented to suit the architecture of the project, if
9236 + * logging is turned on.
9238 + * \hideinitializer
9240 +#define UIP_LOGGING 0
9243 + * Print out a uIP log message.
9245 + * This function must be implemented by the module that uses uIP, and
9246 + * is called by uIP whenever a log message is generated.
9248 +void uip_log(char *msg);
9251 + * The link level header length.
9253 + * This is the offset into the uip_buf where the IP header can be
9254 + * found. For Ethernet, this should be set to 14. For SLIP, this
9255 + * should be set to 0.
9257 + * \hideinitializer
9259 +#define UIP_LLH_LEN 14
9263 +/*------------------------------------------------------------------------------*/
9265 + * \defgroup uipoptcpu CPU architecture configuration
9268 + * The CPU architecture configuration is where the endianess of the
9269 + * CPU on which uIP is to be run is specified. Most CPUs today are
9270 + * little endian, and the most notable exception are the Motorolas
9271 + * which are big endian. The BYTE_ORDER macro should be changed to
9272 + * reflect the CPU architecture on which uIP is to be run.
9274 +#ifndef LITTLE_ENDIAN
9275 +#define LITTLE_ENDIAN 3412
9276 +#endif /* LITTLE_ENDIAN */
9278 +#define BIG_ENDIAN 1234
9279 +#endif /* BIGE_ENDIAN */
9282 + * The byte order of the CPU architecture on which uIP is to be run.
9284 + * This option can be either BIG_ENDIAN (Motorola byte order) or
9285 + * LITTLE_ENDIAN (Intel byte order).
9287 + * \hideinitializer
9289 +/*#ifndef BYTE_ORDER*/
9290 +#define BYTE_ORDER BIG_ENDIAN
9291 +/*#endif*/ /* BYTE_ORDER */
9294 +/*------------------------------------------------------------------------------*/
9297 + * \defgroup uipoptapp Appication specific configurations
9300 + * An uIP application is implemented using a single application
9301 + * function that is called by uIP whenever a TCP/IP event occurs. The
9302 + * name of this function must be registered with uIP at compile time
9303 + * using the UIP_APPCALL definition.
9305 + * uIP applications can store the application state within the
9306 + * uip_conn structure by specifying the size of the application
9307 + * structure with the UIP_APPSTATE_SIZE macro.
9309 + * The file containing the definitions must be included in the
9312 + * The following example illustrates how this can look.
9315 +void httpd_appcall(void);
9316 +#define UIP_APPCALL httpd_appcall
9318 +struct httpd_state {
9324 +#define UIP_APPSTATE_SIZE (sizeof(struct httpd_state))
9329 + * \var #define UIP_APPCALL
9331 + * The name of the application function that uIP should call in
9332 + * response to TCP/IP events.
9337 + * \var #define UIP_APPSTATE_SIZE
9339 + * The size of the application state that is to be stored in the
9340 + * uip_conn structure.
9344 +/* Include the header file for the application program that should be
9345 + used. If you don't use the example web server, you should change
9350 +#endif /* __UIPOPT_H__ */