Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / core / net / ips.c
blob9d355d025412409a0200cf09c63d08ac8d705731
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <rs232.h>
23 #include <net/socket.h>
24 #include <mutex.h>
27 #define IPS_PROTO_CONNECT "/#ips#c"
28 #define IPS_PROTO_CONNECTED "/#ips#k"
29 #define IPS_PROTO_RECV "/#ips#r"
30 #define IPS_PROTO_EOF "/#ips#e"
31 #define IPS_PROTO_CLOSE "/#ips#q"
32 #define IPS_PROTO_SOCKET "/#ips#s"
33 #define IPS_PROTO_BIND "/#ips#b"
34 #define IPS_PROTO_LISTEN "/#ips#l"
35 #define IPS_PROTO_ACCEPT "/#ips#a"
36 #define IPS_PROTO_FCNTL "/#ips#f"
38 /* Mutex for socket function */
39 MUTEX_CREATE (mutex_ips);
41 /** Internet Protocol over Serial - IPS **
42 * For easy connection to network
45 int ips_socket ()
47 int len = 0;
48 int fd = 0;
50 char cmd[] = IPS_PROTO_SOCKET;
51 ips_send (fd, cmd, 7);
53 mutex_lock (&mutex_ips);
55 char num[5];
56 num[0] = rs232_read ();
57 num[1] = rs232_read ();
58 num[2] = rs232_read ();
59 num[3] = rs232_read ();
61 fd = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
63 // printf ("ips_socket (): %d\n", fd);
65 mutex_unlock (&mutex_ips);
67 return fd;
70 int ips_connect (int fd, sockaddr_in *addr)
72 int len = 0;
74 char data[40];
75 char ip[32];
77 if (!net_proto_ip_convert2 (addr->sin_addr, ip))
78 return 0;
80 sprintf (data, IPS_PROTO_CONNECT " %s %d", ip, addr->sin_port);
82 ips_send (fd, data, strlen (data));
84 mutex_lock (&mutex_ips);
86 /* receive return value */
87 char num[5];
88 num[0] = rs232_read ();
89 num[1] = rs232_read ();
90 num[2] = rs232_read ();
91 num[3] = rs232_read ();
93 len = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
95 mutex_unlock (&mutex_ips);
97 return len;
100 int ips_send (int fd, char *msg, size_t size)
102 int len = 0;
104 mutex_lock (&mutex_ips);
106 rs232_write ((unsigned char) size);
107 rs232_write ((unsigned char) (size >> 8));
108 rs232_write ((unsigned char) (size >> 16));
109 rs232_write ((unsigned char) (size >> 24));
111 rs232_write ((unsigned char) fd);
112 rs232_write ((unsigned char) (fd >> 8));
113 rs232_write ((unsigned char) (fd >> 16));
114 rs232_write ((unsigned char) (fd >> 24));
116 while (len != size) {
117 rs232_write (msg[len]);
118 len ++;
121 // printf ("send (%d): %d, '%s', %d\n", len, fd, msg, size);
123 mutex_unlock (&mutex_ips);
125 return len;
128 int ips_recv (int fd, char *msg, size_t size)
130 int len = 0;
132 char data[40];
134 sprintf (data, IPS_PROTO_RECV " %d", size);
135 ips_send (fd, data, strlen (data));
137 mutex_lock (&mutex_ips);
139 char num[5];
140 num[0] = rs232_read ();
141 num[1] = rs232_read ();
142 num[2] = rs232_read ();
143 num[3] = rs232_read ();
145 len = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
147 if (len < 1) {
148 mutex_unlock (&mutex_ips);
149 return len;
152 if (len > size) {
153 mutex_unlock (&mutex_ips);
154 return -2;
157 int l = 0;
158 while (l != len) {
159 msg[l] = rs232_read ();
160 l ++;
163 mutex_unlock (&mutex_ips);
165 return len;
168 int ips_bind (int fd, sockaddr_in *addr)
170 int ret = 0;
172 char data[40];
174 sprintf (data, IPS_PROTO_BIND " 0 %d", /*addr->sin_addr,*/ addr->sin_port);
175 ips_send (fd, data, strlen (data));
177 mutex_lock (&mutex_ips);
179 char num[5];
180 num[0] = rs232_read ();
181 num[1] = rs232_read ();
182 num[2] = rs232_read ();
183 num[3] = rs232_read ();
185 ret = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
187 mutex_unlock (&mutex_ips);
189 return ret;
192 int ips_listen (int fd, int backlog)
194 int ret = 0;
196 char data[40];
198 sprintf (data, IPS_PROTO_LISTEN " %d", backlog);
199 ips_send (fd, data, strlen (data));
201 mutex_lock (&mutex_ips);
203 char num[5];
204 num[0] = rs232_read ();
205 num[1] = rs232_read ();
206 num[2] = rs232_read ();
207 num[3] = rs232_read ();
209 ret = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
211 mutex_unlock (&mutex_ips);
213 return ret;
216 int ips_accept (int fd, sockaddr_in *addr, socklen_t *addrlen)
218 int ret = 0;
220 char cmd[] = IPS_PROTO_ACCEPT;
221 ips_send (fd, cmd, 7);
223 mutex_lock (&mutex_ips);
225 char num[5];
226 num[0] = rs232_read ();
227 num[1] = rs232_read ();
228 num[2] = rs232_read ();
229 num[3] = rs232_read ();
231 ret = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
233 mutex_unlock (&mutex_ips);
235 return ret;
238 int ips_close (int fd)
240 // mutex_lock (&mutex_ips);
242 char cmd[] = IPS_PROTO_CLOSE;
243 ips_send (fd, cmd, 7);
245 // printf ("ips_close (%d)\n", fd);
247 // mutex_unlock (&mutex_ips);
249 return 1;
252 int ips_fcntl (int fd, int cmd, long arg)
254 int ret = 0;
256 char data[40];
258 sprintf (data, IPS_PROTO_FCNTL " %d %ld", cmd, arg);
259 ips_send (fd, data, strlen (data));
261 mutex_lock (&mutex_ips);
263 char num[5];
264 num[0] = rs232_read ();
265 num[1] = rs232_read ();
266 num[2] = rs232_read ();
267 num[3] = rs232_read ();
269 ret = num[0] | (num[1] << 8) | (num[2] << 16) | (num[3] << 24);
271 mutex_unlock (&mutex_ips);
273 return ret;