added a new header file to encapsulate compiler-specific stuff.
[newos.git] / apps / nettest / main.c
blob06abd6fbccb37b6ec0d08045671d681f80a7de00
1 /*
2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <sys/syscalls.h>
6 #include <newos/errors.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <socket/socket.h>
13 static int fd;
14 static sem_id exit_sem;
16 int nettest1(void);
17 int nettest2(void);
18 int nettest3(void);
19 int nettest4(void);
20 int nettest5(void);
21 int nettest6(void);
23 static int read_thread(void *args)
25 char buf[1024];
26 ssize_t len;
27 int i;
29 for(;;) {
30 len = socket_read(fd, buf, sizeof(buf));
31 if(len < 0) {
32 printf("\nsocket_read returns %ld\n", (long)len);
33 break;
36 for(i=0; i<len; i++)
37 printf("%c", buf[i]);
40 _kern_sem_release(exit_sem, 1);
42 return 0;
45 static int write_thread(void *args)
47 char c;
48 ssize_t len;
50 for(;;) {
51 c = getchar();
52 len = socket_write(fd, &c, sizeof(c));
53 if(len < 0) {
54 printf("\nsocket_write returns %ld\n", len);
55 break;
59 _kern_sem_release(exit_sem, 1);
61 return 0;
64 int nettest1(void)
66 int err;
67 sockaddr addr;
69 fd = socket_create(SOCK_PROTO_TCP, 0);
70 printf("created socket, fd %d\n", fd);
71 if(fd < 0)
72 return 0;
74 memset(&addr, 0, sizeof(addr));
75 addr.addr.len = 4;
76 addr.addr.type = ADDR_TYPE_IP;
77 // addr.port = 19;
78 // NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
79 // addr.port = 23;
80 // NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(192,168,0,3);
81 addr.port = 6667;
82 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(209,131,227,242);
84 err = socket_connect(fd, &addr);
85 printf("socket_connect returns %d\n", err);
86 if(err < 0)
87 return err;
89 exit_sem = _kern_sem_create(0, "exit");
91 _kern_thread_resume_thread(_kern_thread_create_thread("read_thread", &read_thread, NULL));
92 _kern_thread_resume_thread(_kern_thread_create_thread("write_thread", &write_thread, NULL));
94 for(;;)
95 _kern_sem_acquire(exit_sem, 1);
97 return 0;
100 int nettest2(void)
102 int err;
103 sockaddr addr;
105 fd = socket_create(SOCK_PROTO_TCP, 0);
106 printf("created socket, fd %d\n", fd);
107 if(fd < 0)
108 return 0;
110 memset(&addr, 0, sizeof(addr));
111 addr.addr.len = 4;
112 addr.addr.type = ADDR_TYPE_IP;
113 addr.port = 9;
114 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
116 err = socket_connect(fd, &addr);
117 printf("socket_connect returns %d\n", err);
118 if(err < 0)
119 return err;
121 for(;;) {
122 char buf[4096];
123 socket_write(fd, buf, sizeof(buf));
125 return 0;
128 int nettest3(void)
130 int err;
131 sockaddr addr;
133 fd = socket_create(SOCK_PROTO_TCP, 0);
134 printf("created socket, fd %d\n", fd);
135 if(fd < 0)
136 return 0;
138 memset(&addr, 0, sizeof(addr));
139 addr.addr.len = 4;
140 addr.addr.type = ADDR_TYPE_IP;
141 addr.port = 1900;
142 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
144 err = socket_connect(fd, &addr);
145 printf("socket_connect returns %d\n", err);
146 if(err < 0)
147 return err;
149 for(;;) {
150 char buf[4096];
151 ssize_t len;
153 len = socket_read(fd, buf, sizeof(buf));
154 printf("socket_read returns %ld\n", len);
155 if(len <= 0)
156 break;
159 err = socket_close(fd);
160 printf("socket_close returns %d\n", err);
162 return 0;
165 int nettest4(void)
167 int err;
168 sockaddr addr;
170 fd = socket_create(SOCK_PROTO_TCP, 0);
171 printf("created socket, fd %d\n", fd);
172 if(fd < 0)
173 return 0;
175 memset(&addr, 0, sizeof(addr));
176 addr.addr.len = 4;
177 addr.addr.type = ADDR_TYPE_IP;
178 addr.port = 1900;
179 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
181 err = socket_connect(fd, &addr);
182 printf("socket_connect returns %d\n", err);
183 if(err < 0)
184 return err;
186 usleep(5000000);
188 err = socket_close(fd);
189 printf("socket_close returns %d\n", err);
191 return 0;
194 int nettest5(void)
196 int err;
197 sockaddr addr;
198 int new_fd;
200 fd = socket_create(SOCK_PROTO_TCP, 0);
201 printf("created socket, fd %d\n", fd);
202 if(fd < 0)
203 return 0;
205 memset(&addr, 0, sizeof(addr));
206 addr.addr.len = 4;
207 addr.addr.type = ADDR_TYPE_IP;
208 addr.port = 1900;
209 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(0,0,0,0);
211 err = socket_bind(fd, &addr);
212 printf("socket_bind returns %d\n", err);
213 if(err < 0)
214 return 0;
216 err = socket_listen(fd);
217 printf("socket_listen returns %d\n", err);
218 if(err < 0)
219 return 0;
221 new_fd = socket_accept(fd, &addr);
222 printf("socket_accept returns %d\n", new_fd);
223 if(new_fd < 0)
224 return 0;
226 err = socket_write(new_fd, "hello world!\n", strlen("hello world!\n"));
227 printf("socket_write returns %d\n", err);
229 printf("sleeping for 5 seconds\n");
230 usleep(5000000);
232 printf("closing fd %d\n", new_fd);
233 socket_close(new_fd);
234 printf("closing fd %d\n", fd);
235 socket_close(fd);
237 return 0;
240 int nettest6(void)
242 int err;
243 sockaddr addr;
244 int new_fd;
246 fd = socket_create(SOCK_PROTO_TCP, 0);
247 printf("created socket, fd %d\n", fd);
248 if(fd < 0)
249 return 0;
251 memset(&addr, 0, sizeof(addr));
252 addr.addr.len = 4;
253 addr.addr.type = ADDR_TYPE_IP;
254 addr.port = 1900;
255 NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(0,0,0,0);
257 err = socket_bind(fd, &addr);
258 printf("socket_bind returns %d\n", err);
259 if(err < 0)
260 return 0;
262 err = socket_listen(fd);
263 printf("socket_listen returns %d\n", err);
264 if(err < 0)
265 return 0;
267 for(;;) {
268 int saved_stdin, saved_stdout, saved_stderr;
269 char *argv;
271 new_fd = socket_accept(fd, &addr);
272 printf("socket_accept returns %d\n", new_fd);
273 if(new_fd < 0)
274 continue;
276 saved_stdin = dup(0);
277 saved_stdout = dup(1);
278 saved_stderr = dup(2);
280 dup2(new_fd, 0);
281 dup2(new_fd, 1);
282 dup2(new_fd, 2);
283 close(new_fd);
285 // XXX launch
286 argv = "/boot/bin/shell";
287 err = _kern_proc_create_proc("/boot/bin/shell", "shell", &argv, 1, 5, 0);
289 dup2(saved_stdin, 0);
290 dup2(saved_stdout, 1);
291 dup2(saved_stderr, 2);
292 close(saved_stdin);
293 close(saved_stdout);
294 close(saved_stderr);
295 printf("_kern_proc_create_proc returns %d\n", err);
299 int main(int argc, char **argv)
301 // nettest1();
302 // nettest2();
303 // nettest3();
304 // nettest4();
305 // nettest5();
306 nettest6();
308 return 0;