Added support for the FTP standalone client to the c64 target.
[contiki-2.x.git] / tools / codeprop.c
bloba3bf9da33c4ad981ca4ab9b726c04c79ee1f9bcb
1 /*
2 * Copyright (c) 2005, Swedish Institute of Computer Science
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the Contiki operating system.
31 * @(#)$Id: codeprop.c,v 1.5 2010/10/19 18:29:05 adamdunkels Exp $
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/mman.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43 #include <netinet/in.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 /* Should be included from codeprop.h, but the include paths in the makefiles
52 isn't set up for that. */
53 #define HDR_SIZE 4
55 int
56 main(int argc, char **argv) {
57 struct sockaddr_in sa;
58 int s, port, fd;
59 char *ip_addr;
60 int total = 0;
62 if(argc != 3) {
63 printf("usage: %s ipaddress filename\n", argv[0]);
64 exit(1);
66 ip_addr = argv[1];
67 port = 6510;
69 /* Create socket. */
70 if((s = socket(AF_INET,SOCK_STREAM,0)) < 0){
71 perror("Can't create socket");
72 exit(1);
75 /* Set the destination address of the socket. */
76 bzero((char *) &sa, sizeof(sa));
77 sa.sin_family = AF_INET;
78 sa.sin_addr.s_addr = inet_addr(ip_addr);
79 sa.sin_port = uip_htons(port);
81 /* Connect the socket to the remote host. */
82 if(connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
83 perror("connect failed");
84 exit(1);
86 socklen_t slen = 576 - 40;
87 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &slen, sizeof(slen)) < 0) {
88 perror("setsockopt failed");
89 exit(1);
92 if((fd = open(argv[2], O_RDONLY)) < 0) {
93 perror("Could not open file");
94 exit(1);
98 while(1) {
99 char buf[64000];
100 int len;
102 len = read(fd, &buf[HDR_SIZE], sizeof(buf) - HDR_SIZE);
103 if(len == 0) {
104 printf("File successfully sent (%d bytes)\n", total);
105 len = read(s, buf, sizeof(buf));
106 buf[len] = 0;
107 printf("Reply: %s", buf);
108 if(buf[0] != 'o' || buf[1] != 'k') {
109 /* Cut and pasted from core/loader/elfloader.h */
110 printf("OK 0\n"
111 "BAD_HEADER 1\n"
112 "NO_SYMTAB 2\n"
113 "NO_STRTAB 3\n"
114 "NO_TEXT 4\n"
115 "UNDEFINED 5\n"
116 "UNKNOWN_SEGMENT 6\n"
117 "NO_STARTPOINT 7\n"
118 "TEXT_TO_LARGE 8\n"
119 "DATA_TO_LARGE 9\n"
120 "UNKNOWN_RELOC 10\n"
121 "MULTIPLY_DEFINED 11\n");
123 exit(0);
125 total += len;
126 buf[0] = len >> 8;
127 buf[1] = len & 0xff;
128 if(write(s, buf, len + HDR_SIZE) == -1) {
129 perror("network send failed");
130 exit(1);
134 return 0;