source: azure_iot_hub/trunk/ntshell/src/socket_stub.c@ 401

Last change on this file since 401 was 401, checked in by coas-nagasima, 3 years ago

Socketのbind, connect, accept時に失敗するとメモリリークが発生するのを修正

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 52.7 KB
Line 
1/*
2 * TOPPERS PROJECT Home Network Working Group Software
3 *
4 * Copyright (C) 2017-2019 Cores Co., Ltd. Japan
5 *
6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
8 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
9 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
10 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
11 * スコード中に含まれていること.
12 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
13 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
14 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
15 * の無保証規定を掲載すること.
16 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
17 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
18 * と.
19 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
20 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
21 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
22 * 報告すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
27 * 免責すること.
28 *
29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
31 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
33 * の責任を負わない.
34 *
35 * @(#) $Id: socket_stub.c 1982 2019-07-18 00:13:56Z coas-nagasima $
36 */
37#include "shellif.h"
38#include <kernel.h>
39#include <t_syslog.h>
40#include <t_stdlib.h>
41#include <string.h>
42#include <sil.h>
43#include "syssvc/syslog.h"
44#include <tinet_defs.h>
45#include <tinet_config.h>
46#include <net/net.h>
47#include <net/net_endian.h>
48#include <netinet/in.h>
49#include <netinet/in_itron.h>
50#include <tinet_nic_defs.h>
51#include <tinet_cfg.h>
52#include <netinet/in_var.h>
53#include <net/ethernet.h>
54#include <net/if6_var.h>
55#include <net/net.h>
56#include <net/if_var.h>
57#include <netinet/udp_var.h>
58#include <net/net_buf.h>
59#include <netinet/udp.h>
60#include <netinet/udp_var.h>
61#include <netinet/tcp.h>
62#include <netinet/tcp_var.h>
63#include <netapp/resolver.h>
64#include "fdtable.h"
65#include "kernel_cfg.h"
66
67#ifdef _DEBUG
68static const char THIS_FILE[] = __FILE__;
69#endif
70
71#define SOCKET_TIMEOUT 2000000
72#define TCP_SOCKET_BUF_SIZE 512
73
74struct addrinfo {
75 int ai_flags;
76 int ai_family;
77 int ai_socktype;
78 int ai_protocol;
79 socklen_t ai_addrlen;
80 struct sockaddr *ai_addr;
81 char *ai_canonname;
82 struct addrinfo *ai_next;
83};
84
85typedef uint16_t in_port_t;
86typedef uint32_t in_addr_t;
87struct in_addr { in_addr_t s_addr; };
88
89struct sockaddr_in {
90 sa_family_t sin_family;
91 in_port_t sin_port;
92 struct in_addr sin_addr;
93 uint8_t sin_zero[8];
94};
95
96struct in6_addr
97{
98 union {
99 uint8_t __s6_addr[16];
100 uint16_t __s6_addr16[8];
101 uint32_t __s6_addr32[4];
102 } __in6_union;
103};
104//#define s6_addr __in6_union.__s6_addr
105//#define s6_addr16 __in6_union.__s6_addr16
106//#define s6_addr32 __in6_union.__s6_addr32
107
108struct sockaddr_in6
109{
110 sa_family_t sin6_family;
111 in_port_t sin6_port;
112 uint32_t sin6_flowinfo;
113 struct in6_addr sin6_addr;
114 uint32_t sin6_scope_id;
115};
116
117typedef struct socket_t {
118 int family;
119 int type;
120 int protocol;
121 int cepid;
122 int repid;
123 int backlog;
124 unsigned int flags;
125 union {
126 struct sockaddr_in laddr4;
127 struct sockaddr_in6 laddr6;
128 };
129 union {
130 struct sockaddr_in raddr4;
131 struct sockaddr_in6 raddr6;
132 };
133 int buf_size;
134 unsigned char *buf;
135 void *input;
136 int len;
137} socket_t;
138
139#define tcp6_cre_cep tcp_cre_cep
140#define tcp6_del_cep tcp_del_cep
141#define tcp6_del_rep tcp_del_rep
142#define tcp6_sht_cep tcp_sht_cep
143#define tcp6_cls_cep tcp_cls_cep
144#define tcp6_snd_oob tcp_snd_oob
145#define tcp6_snd_dat tcp_snd_dat
146#define tcp6_rcv_oob tcp_rcv_oob
147#define tcp6_rcv_buf tcp_rcv_buf
148#define tcp6_rel_buf tcp_rel_buf
149#define tcp6_get_opt tcp_get_opt
150#define tcp6_set_opt tcp_set_opt
151
152#ifndef SUPPORT_INET6
153
154ER tcp6_cre_rep (ID repid, T_TCP6_CREP *pk_crep) { return E_SYS; }
155ER tcp6_acp_cep (ID cepid, ID repid, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; }
156ER tcp6_con_cep (ID cepid, T_IPV6EP *p_myaddr, T_IPV6EP *p_dstaddr, TMO tmout) { return E_SYS; }
157
158ER udp6_cre_cep (ID cepid, T_UDP6_CCEP *pk_ccep) { return E_SYS; }
159ER udp6_del_cep (ID cepid) { return E_SYS; }
160ER_UINT udp6_snd_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
161ER_UINT udp6_rcv_dat (ID cepid, T_IPV6EP *p_dstaddr, void *data, int_t len, TMO tmout) { return E_SYS; }
162ER udp6_set_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
163ER udp6_get_opt (ID cepid, int_t optname, void *optval, int_t optlen) { return E_SYS; }
164
165const T_IN6_ADDR *in6_get_ifaddr (int_t index) { return NULL; }
166
167#endif
168
169ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk);
170ER socket_tcp6_callback(ID cepid, FN fncd, void *p_parblk);
171ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk);
172ER socket_udp6_callback(ID cepid, FN fncd, void *p_parblk);
173
174static int tcp_fd_close(struct SHELL_FILE *fp);
175static size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
176static size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
177static off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
178static int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
179static bool_t tcp_fd_readable(struct SHELL_FILE *fp);
180static bool_t tcp_fd_writable(struct SHELL_FILE *fp);
181static void tcp_fd_delete(struct SHELL_FILE *fp);
182
183static int udp_fd_close(struct SHELL_FILE *fp);
184static size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
185static size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
186static off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
187static int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
188static bool_t udp_fd_readable(struct SHELL_FILE *fp);
189static bool_t udp_fd_writable(struct SHELL_FILE *fp);
190static void udp_fd_delete(struct SHELL_FILE *fp);
191
192IO_TYPE IO_TYPE_TCP = { tcp_fd_close, tcp_fd_read, tcp_fd_write, tcp_fd_seek, tcp_fd_ioctl, tcp_fd_readable, tcp_fd_writable, tcp_fd_delete };
193IO_TYPE IO_TYPE_UDP = { udp_fd_close, udp_fd_read, udp_fd_write, udp_fd_seek, udp_fd_ioctl, udp_fd_readable, udp_fd_writable, udp_fd_delete };
194
195typedef struct id_table_t {
196 int used;
197 ID id;
198} id_table_t;
199
200id_table_t tcp_repid_table[] = {
201 {0, USR_TCP_REP1}, {0, USR_TCP_REP2}, {0, USR_TCP_REP3}, {0, USR_TCP_REP4}
202};
203#define tcp_repid_table_count (sizeof(tcp_repid_table) / sizeof(tcp_repid_table[0]))
204
205id_table_t tcp_cepid_table[] = {
206 {0, USR_TCP_CEP1}, {0, USR_TCP_CEP2}, {0, USR_TCP_CEP3}, {0, USR_TCP_CEP4},
207#ifndef TOPPERS_GRSAKURA
208 {0, USR_TCP_CEP5}, {0, USR_TCP_CEP6}, {0, USR_TCP_CEP7}, {0, USR_TCP_CEP8}
209#endif
210};
211#define tcp_cepid_table_count (sizeof(tcp_cepid_table) / sizeof(tcp_cepid_table[0]))
212
213#ifdef SUPPORT_INET6
214
215id_table_t tcp6_repid_table[] = {
216 {0, USR_TCP6_REP1}, {0, USR_TCP6_REP2}, {0, USR_TCP6_REP3}, {0, USR_TCP6_REP4}
217};
218#define tcp6_repid_table_count (sizeof(tcp6_repid_table) / sizeof(tcp6_repid_table[0]))
219
220id_table_t tcp6_cepid_table[] = {
221 {0, USR_TCP6_CEP1}, {0, USR_TCP6_CEP2}, {0, USR_TCP6_CEP3}, {0, USR_TCP6_CEP4},
222#ifndef TOPPERS_GRSAKURA
223 {0, USR_TCP6_CEP5}, {0, USR_TCP6_CEP6}, {0, USR_TCP6_CEP7}, {0, USR_TCP6_CEP8}
224#endif
225};
226#define tcp6_cepid_table_count (sizeof(tcp6_cepid_table) / sizeof(tcp6_cepid_table[0]))
227
228#else
229
230#define tcp6_repid_table NULL
231#define tcp6_repid_table_count 0
232
233#define tcp6_cepid_table NULL
234#define tcp6_cepid_table_count 0
235
236#endif
237
238id_table_t udp_cepid_table[] = {
239 {0, USR_UDP_CEP1}, {0, USR_UDP_CEP2}, {0, USR_UDP_CEP3}, {0, USR_UDP_CEP4}
240};
241#define udp_cepid_table_count (sizeof(udp_cepid_table) / sizeof(udp_cepid_table[0]))
242
243#ifdef SUPPORT_INET6
244
245id_table_t udp6_cepid_table[] = {
246 {0, USR_UDP6_CEP1}, {0, USR_UDP6_CEP2}, {0, USR_UDP6_CEP3}, {0, USR_UDP6_CEP4}
247};
248#define udp6_cepid_table_count (sizeof(udp6_cepid_table) / sizeof(udp6_cepid_table[0]))
249
250#else
251
252#define udp6_cepid_table NULL
253#define udp6_cepid_table_count 0
254
255#endif
256
257void addrcpy(void *_dst, const void *_src, int len)
258{
259#if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
260 memcpy(_dst, _src, len);
261#else
262 uint8_t *dst = (uint8_t *)_dst, *src = &((uint8_t *)_src)[len];
263 while (src != _src)
264 *dst++ = *--src;
265#endif
266}
267
268ID new_id(id_table_t *table, int count)
269{
270 for (int i = 0; i < count; i++) {
271 id_table_t *item = &table[i];
272 if (item->used != 0)
273 continue;
274
275 item->used = 1;
276 return item->id;
277}
278
279 return -ENOMEM;
280}
281
282int delete_id(id_table_t *table, int count, ID id)
283{
284 for (int i = 0; i < count; i++) {
285 id_table_t *item = &table[i];
286 if ((item->used == 0) || (item->id != id))
287 continue;
288
289 item->used = 0;
290 return 0;
291 }
292 return -EINVAL;
293}
294
295typedef struct SHELL_FILE SOCKET;
296
297int shell_socket(int family, int type, int protocol)
298{
299 SOCKET *fp;
300 unsigned int flags;
301
302 switch (family) {
303 case AF_INET:
304 case AF_INET6:
305 break;
306 default:
307 return -EAFNOSUPPORT;
308 }
309
310 flags = type & (SOCK_CLOEXEC|SOCK_NONBLOCK);
311 type &= ~flags;
312
313 switch (type) {
314 case SOCK_STREAM:
315 fp = new_fp(&IO_TYPE_TCP, 0, 0);
316 if (fp == NULL)
317 return -ENOMEM;
318
319 fp->exinf = malloc(sizeof(socket_t));
320 memset(fp->exinf, 0, sizeof(socket_t));
321 break;
322 case SOCK_DGRAM:
323 fp = new_fp(&IO_TYPE_UDP, 0, 1);
324 if (fp == NULL)
325 return -ENOMEM;
326
327 fp->exinf = malloc(sizeof(socket_t));
328 memset(fp->exinf, 0, sizeof(socket_t));
329 break;
330 default:
331 return -ENOPROTOOPT;
332 }
333
334 socket_t *socket = (socket_t *)fp->exinf;
335 socket->family = family;
336 socket->type = type;
337 socket->protocol = protocol;
338 socket->flags = flags;
339
340 return fp->fd;
341}
342
343int shell_bind(int fd, const struct sockaddr *addr, socklen_t len)
344{
345 SOCKET *fp = fd_to_fp(fd);
346 if (fp == NULL)
347 return -EBADF;
348 socket_t *socket = (socket_t *)fp->exinf;
349 if (socket->family != addr->sa_family)
350 return -EINVAL;
351
352 ER ret;
353 switch (addr->sa_family) {
354 case AF_INET: {
355 if (len < 8) {
356 return -EINVAL;
357 }
358 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
359 memcpy(&socket->laddr4, addr, len);
360 switch (socket->type) {
361 case SOCK_STREAM: {
362 ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
363 if (cepid < 0)
364 return -ENOMEM;
365
366 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
367 socket->buf = malloc(socket->buf_size);
368#ifdef _DEBUG
369 memset(socket->buf, 0, socket->buf_size);
370#endif
371 T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
372 ret = tcp_cre_cep(cepid, &ccep);
373 if (ret != E_OK) {
374 free(socket->buf);
375 socket->buf = NULL;
376 socket->buf_size = 0;
377 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
378 return -ENOMEM;
379 }
380 fp->handle = cepid;
381 socket->cepid = cepid;
382 break;
383 }
384 case SOCK_DGRAM: {
385 ID cepid = new_id(udp_cepid_table, udp_cepid_table_count);
386 if (cepid < 0)
387 return -ENOMEM;
388
389 T_UDP_CCEP ccep = { 0, {ntohl(addr_in->sin_addr.s_addr), ntohs(addr_in->sin_port)}, (FP)socket_udp_callback };
390 ret = udp_cre_cep(cepid, &ccep);
391 if (ret != E_OK) {
392 delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
393 return -ENOMEM;
394 }
395 fp->handle = cepid;
396 socket->cepid = cepid;
397 break;
398 }
399 default:
400 return -ENOPROTOOPT;
401 }
402 break;
403 }
404 case AF_INET6: {
405 if (len < 20) {
406 return -EINVAL;
407 }
408 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
409 memcpy(&socket->laddr6, addr, len);
410 switch (socket->type) {
411 case SOCK_STREAM: {
412 ID cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
413 if (cepid < 0)
414 return -ENOMEM;
415
416 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
417 socket->buf = malloc(socket->buf_size);
418#ifdef _DEBUG
419 memset(socket->buf, 0, socket->buf_size);
420#endif
421 T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
422 ret = tcp6_cre_cep(cepid, &ccep);
423 if (ret != E_OK) {
424 free(socket->buf);
425 socket->buf = NULL;
426 socket->buf_size = 0;
427 delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
428 return -ENOMEM;
429 }
430 fp->handle = cepid;
431 socket->cepid = cepid;
432 break;
433 }
434 case SOCK_DGRAM: {
435 ID cepid = new_id(udp6_cepid_table, udp6_cepid_table_count);
436 if (cepid < 0)
437 return -ENOMEM;
438
439 T_UDP6_CCEP ccep = { 0, { { ntohl(addr_in6->sin6_addr.__in6_union.__s6_addr) }, ntohs(addr_in6->sin6_port)}, (FP)socket_udp6_callback };
440 ret = udp6_cre_cep(cepid, &ccep);
441 if (ret != E_OK) {
442 delete_id(udp6_cepid_table, udp6_cepid_table_count, cepid);
443 return -ENOMEM;
444 }
445 fp->handle = cepid;
446 socket->cepid = cepid;
447 break;
448 }
449 default:
450 return -ENOPROTOOPT;
451 }
452 break;
453 }
454 default:
455 return -ENOPROTOOPT;
456 }
457
458 return 0;
459}
460
461int shell_listen(int fd, int backlog)
462{
463 SOCKET *fp = fd_to_fp(fd);
464 if (fp == NULL)
465 return -EBADF;
466 socket_t *socket = (socket_t *)fp->exinf;
467 if (socket->type != SOCK_STREAM)
468 return -EINVAL;
469
470 socket->backlog = backlog;
471
472 ER ret;
473 switch (socket->family) {
474 case AF_INET: {
475 ID repid = new_id(tcp_repid_table, tcp_repid_table_count);
476 if (repid < 0)
477 return -ENOMEM;
478
479 struct sockaddr_in *laddr = &socket->laddr4;
480 T_TCP_CREP crep = { 0, {ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port)} };
481 ret = tcp_cre_rep(repid, &crep);
482 if (ret != E_OK) {
483 delete_id(tcp_repid_table, tcp_repid_table_count, repid);
484 return -ENOMEM;
485 }
486 socket->repid = repid;
487 break;
488 }
489 case AF_INET6: {
490 ID repid = new_id(tcp6_repid_table, tcp6_repid_table_count);
491 if (repid < 0)
492 return -ENOMEM;
493
494 struct sockaddr_in6 *laddr = &socket->laddr6;
495 T_TCP6_CREP crep = { 0, { { ntohl(laddr->sin6_addr.__in6_union.__s6_addr) }, ntohs(laddr->sin6_port)} };
496 ret = tcp6_cre_rep(repid, &crep);
497 if (ret != E_OK) {
498 delete_id(tcp6_repid_table, tcp6_repid_table_count, repid);
499 return -ENOMEM;
500 }
501 socket->repid = repid;
502 break;
503 }
504 default:
505 return -ENOPROTOOPT;
506 }
507
508 return 0;
509}
510
511int shell_connect(int fd, const struct sockaddr *addr, socklen_t alen)
512{
513 SOCKET *fp = fd_to_fp(fd);
514 if (fp == NULL)
515 return -EBADF;
516 socket_t *socket = (socket_t *)fp->exinf;
517 if (socket->type != SOCK_STREAM)
518 return -EINVAL;
519
520 ER ret;
521 switch (socket->family) {
522 case AF_INET: {
523 if (alen < sizeof(struct sockaddr_in)) {
524 return -EINVAL;
525 }
526 if (socket->cepid == 0) {
527 ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
528 if (cepid < 0)
529 return -ENOMEM;
530
531 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
532 socket->buf = malloc(socket->buf_size);
533#ifdef _DEBUG
534 memset(socket->buf, 0, socket->buf_size);
535#endif
536 T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
537 ret = tcp_cre_cep(cepid, &ccep);
538 if (ret != E_OK) {
539 free(socket->buf);
540 socket->buf = NULL;
541 socket->buf_size = 0;
542 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
543 return -ENOMEM;
544 }
545 fp->handle = cepid;
546 fp->writable = 1;
547 socket->cepid = cepid;
548 }
549 struct sockaddr_in *laddr = &socket->laddr4;
550 struct sockaddr_in *raddr = &socket->raddr4;
551 memcpy(raddr, addr, sizeof(struct sockaddr_in));
552 T_IPV4EP lep = { ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port) };
553 T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
554 ret = tcp_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
555 if (ret == E_TMOUT) {
556 return -ETIMEDOUT;
557 }
558 else if (ret < 0) {
559 return -EHOSTUNREACH;
560 }
561 break;
562 }
563 case AF_INET6: {
564 if (alen < sizeof(struct sockaddr_in6)) {
565 return -EINVAL;
566 }
567 if (socket->cepid == 0) {
568 ID cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
569 if (cepid < 0)
570 return -ENOMEM;
571
572 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
573 socket->buf = malloc(socket->buf_size);
574#ifdef _DEBUG
575 memset(socket->buf, 0, socket->buf_size);
576#endif
577 T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
578 ret = tcp6_cre_cep(cepid, &ccep);
579 if (ret != E_OK) {
580 free(socket->buf);
581 socket->buf = NULL;
582 socket->buf_size = 0;
583 delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
584 return -ENOMEM;
585 }
586 fp->handle = cepid;
587 fp->writable = 1;
588 socket->cepid = cepid;
589 }
590 struct sockaddr_in6 *laddr = &socket->laddr6;
591 struct sockaddr_in6 *raddr = &socket->raddr6;
592 memcpy(raddr, addr, sizeof(struct sockaddr_in6));
593 T_IPV6EP lep;
594 addrcpy(&lep.ipaddr, &laddr->sin6_addr, 16);
595 lep.portno = ntohs(laddr->sin6_port);
596 T_IPV6EP rep;
597 addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
598 rep.portno = ntohs(raddr->sin6_port);
599 ret = tcp6_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
600 if (ret == E_TMOUT) {
601 return -ETIMEDOUT;
602 }
603 else if (ret < 0) {
604 return -EHOSTUNREACH;
605 }
606 break;
607 }
608 default:
609 return -ENOPROTOOPT;
610 }
611
612 if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
613
614 return 0;
615}
616
617int shell_accept(int fd, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
618{
619 SOCKET *lfp = fd_to_fp(fd);
620 if (lfp == NULL)
621 return -EBADF;
622 if (((socket_t *)lfp->exinf)->type != SOCK_STREAM)
623 return -EINVAL;
624
625 SOCKET *fp = new_fp(&IO_TYPE_TCP, 0, 0);
626 if (fp == NULL)
627 return -ENOMEM;
628
629 fp->exinf = malloc(sizeof(socket_t));
630 memset(fp->exinf, 0, sizeof(socket_t));
631
632 memcpy(fp->exinf, lfp->exinf, offsetof(socket_t, buf_size));
633
634 ER ret;
635 socket_t *socket = (socket_t *)fp->exinf;
636 switch (socket->family) {
637 case AF_INET: {
638 ID cepid;
639 if (socket->cepid == 0) {
640 cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
641 if (cepid < 0)
642 return -ENOMEM;
643
644 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
645 socket->buf = malloc(socket->buf_size);
646#ifdef _DEBUG
647 memset(socket->buf, 0, socket->buf_size);
648#endif
649 T_TCP_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp_callback };
650 ret = tcp_cre_cep(cepid, &ccep);
651 if (ret != E_OK) {
652 free(socket->buf);
653 socket->buf = NULL;
654 socket->buf_size = 0;
655 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
656 return -ENOMEM;
657 }
658 fp->handle = cepid;
659 fp->writable = 1;
660 socket->cepid = cepid;
661 }
662 else {
663 cepid = ((socket_t *)lfp->exinf)->cepid;
664 fp->handle = cepid;
665 fp->writable = 1;
666 lfp->handle = tmax_tcp_cepid + ((socket_t *)lfp->exinf)->repid;
667 ((socket_t *)lfp->exinf)->cepid = 0;
668 ((socket_t *)lfp->exinf)->buf_size = 0;
669 ((socket_t *)lfp->exinf)->buf = 0;
670 }
671 T_IPV4EP rep = { 0, 0 };
672 ret = tcp_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
673 if (ret < 0) {
674 return -ENOMEM;
675 }
676 struct sockaddr_in *raddr = &socket->raddr4;
677 memset(raddr, 0, sizeof(struct sockaddr_in));
678 raddr->sin_family = AF_INET;
679 raddr->sin_port = htons(rep.portno);
680 raddr->sin_addr.s_addr = htonl(rep.ipaddr);
681
682 if (addr != NULL && alen != NULL) {
683 int sz = *alen;
684 if (sz < sizeof(struct sockaddr_in)) {
685 return -EINVAL;
686 }
687 struct sockaddr_in *raddr = &socket->raddr4;
688 if (sz > sizeof(struct sockaddr_in))
689 sz = sizeof(struct sockaddr_in);
690 memcpy(addr, raddr, sz);
691 *alen = sz;
692 }
693 break;
694 }
695 case AF_INET6: {
696 ID cepid;
697 if (socket->cepid == 0) {
698 cepid = new_id(tcp6_cepid_table, tcp6_cepid_table_count);
699 if (cepid < 0)
700 return -ENOMEM;
701
702 socket->buf_size = TCP_SOCKET_BUF_SIZE + TCP_SOCKET_BUF_SIZE;
703 socket->buf = malloc(socket->buf_size);
704#ifdef _DEBUG
705 memset(socket->buf, 0, socket->buf_size);
706#endif
707 T_TCP6_CCEP ccep = { 0, socket->buf, TCP_SOCKET_BUF_SIZE, &socket->buf[TCP_SOCKET_BUF_SIZE], TCP_SOCKET_BUF_SIZE, (FP)socket_tcp6_callback };
708 ret = tcp6_cre_cep(cepid, &ccep);
709 if (ret != E_OK) {
710 free(socket->buf);
711 socket->buf = NULL;
712 socket->buf_size = 0;
713 delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
714 return -ENOMEM;
715 }
716 fp->handle = cepid;
717 fp->writable = 1;
718 socket->cepid = cepid;
719 }
720 else {
721 cepid = ((socket_t *)lfp->exinf)->cepid;
722 fp->handle = cepid;
723 fp->writable = 1;
724 lfp->handle = tmax_tcp6_cepid + ((socket_t *)lfp->exinf)->repid;
725 ((socket_t *)lfp->exinf)->cepid = 0;
726 ((socket_t *)lfp->exinf)->buf_size = 0;
727 ((socket_t *)lfp->exinf)->buf = 0;
728 }
729 T_IPV6EP rep = { { 0 }, 0 };
730 ret = tcp6_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
731 if (ret < 0) {
732 return -ENOMEM;
733 }
734 struct sockaddr_in6 *raddr = &socket->raddr6;
735 memset(raddr, 0, sizeof(struct sockaddr_in6));
736 raddr->sin6_family = AF_INET;
737 raddr->sin6_port = htons(rep.portno);
738 addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
739
740 if (addr != NULL && alen != NULL) {
741 int sz = *alen;
742 if (sz < sizeof(struct sockaddr_in6)) {
743 return -EINVAL;
744 }
745 struct sockaddr_in6 *raddr = &socket->raddr6;
746 if (sz > sizeof(struct sockaddr_in6))
747 sz = sizeof(struct sockaddr_in6);
748 memcpy(addr, raddr, sz);
749 *alen = sz;
750 }
751 break;
752 }
753 default:
754 return -ENOPROTOOPT;
755 }
756
757 if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
758
759 return fp->fd;
760}
761
762ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
763{
764 SOCKET *fp = fd_to_fp(fd);
765 if (fp == NULL) {
766 return -EBADF;
767 }
768
769 int ret = 0;
770 socket_t *socket = (socket_t *)fp->exinf;
771 switch (socket->family) {
772 case AF_INET: {
773 switch (socket->type) {
774 case SOCK_STREAM: {
775 if ((addr != NULL) || (alen != 0)) {
776 return -EISCONN;
777 }
778
779 if (flags & MSG_OOB) {
780 ret = tcp_snd_oob(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
781 if (ret < 0) {
782 return -ECOMM;
783 }
784 }
785 else {
786 int temp = len;
787 for (;;) {
788 ret = tcp_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
789 if (ret < 0) {
790 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
791 }
792 len -= ret;
793 if (len <= 0)
794 break;
795 buf = (const void *)&((uint8_t *)buf)[ret];
796 }
797 ret = temp;
798 }
799 break;
800 }
801 case SOCK_DGRAM: {
802 if ((addr == NULL) || (alen < sizeof(struct sockaddr_in))) {
803 return -EINVAL;
804 }
805 struct sockaddr_in *raddr = &socket->raddr4;
806 memcpy(raddr, addr, sizeof(struct sockaddr_in));
807 T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
808 ret = udp_snd_dat(socket->cepid, &rep, (void *)buf, len,
809 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
810 if (ret < 0) {
811 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
812 }
813 break;
814 }
815 }
816 break;
817 }
818 case AF_INET6: {
819 switch (socket->type) {
820 case SOCK_STREAM: {
821 if ((addr != NULL) || (alen != 0)) {
822 return -EISCONN;
823 }
824
825 if (flags & MSG_OOB) {
826 ret = tcp6_snd_oob(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
827 if (ret < 0) {
828 return -ECOMM;
829 }
830 }
831 else {
832 int temp = len;
833 for (;;) {
834 ret = tcp6_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
835 if (ret < 0) {
836 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
837 }
838 len -= ret;
839 if (len <= 0)
840 break;
841 buf = (const void *)&((uint8_t *)buf)[ret];
842 }
843 ret = temp;
844 }
845 break;
846 }
847 case SOCK_DGRAM: {
848 if ((addr == NULL) || (alen < sizeof(struct sockaddr_in6))) {
849 return -EINVAL;
850 }
851 struct sockaddr_in6 *raddr = &socket->raddr6;
852 memcpy(raddr, addr, sizeof(struct sockaddr_in6));
853 T_IPV6EP rep;
854 addrcpy(&rep.ipaddr, &raddr->sin6_addr, 16);
855 rep.portno = ntohs(raddr->sin6_port);
856 ret = udp6_snd_dat(socket->cepid, &rep, (void *)buf, len,
857 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
858 if (ret < 0) {
859 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
860 }
861 break;
862 }
863 }
864 break;
865 }
866 default:
867 return -ENOPROTOOPT;
868 }
869
870 if (fp->writeevt_w != fp->writeevt_r) fp->writeevt_r++;
871
872 return ret;
873}
874
875ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags)
876{
877 no_implement("sendmsg\n");
878 return -ENOSYS;
879}
880
881ssize_t shell_recvfrom(int fd, void *__restrict buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
882{
883 SOCKET *fp = fd_to_fp(fd);
884 if (fp == NULL) {
885 return -EBADF;
886 }
887
888 int ret = 0;
889 socket_t *socket = (socket_t *)fp->exinf;
890 switch (socket->family) {
891 case AF_INET: {
892 switch (socket->type) {
893 case SOCK_STREAM: {
894 if (flags & MSG_OOB) {
895 ret = tcp_rcv_oob(socket->cepid, buf, len);
896 if (ret < 0) {
897 syslog(LOG_ERROR, "tcp_rcv_oob => %d", ret);
898 return -ECOMM;
899 }
900 }
901 else {
902 int rsz, tmp;
903 if (socket->input == NULL) {
904 ret = wai_sem(SEM_FILEDESC);
905 if (ret < 0) {
906 syslog(LOG_ERROR, "wai_sem => %d", ret);
907 }
908 socket->len = 0;
909 ret = sig_sem(SEM_FILEDESC);
910 if (ret < 0) {
911 syslog(LOG_ERROR, "sig_sem => %d", ret);
912 }
913 ret = tcp_rcv_buf(socket->cepid, &socket->input,
914 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
915 if (ret < 0) {
916 if ((socket->flags & O_NONBLOCK) == 0)
917 syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
918 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
919 }
920 rsz = ret;
921 }
922 else
923 rsz = socket->len;
924 tmp = rsz;
925 if (rsz > len)
926 rsz = len;
927 if (rsz >= 0) {
928 memcpy(buf, socket->input, rsz);
929 ret = wai_sem(SEM_FILEDESC);
930 if (ret < 0) {
931 syslog(LOG_ERROR, "wai_sem => %d", ret);
932 }
933 socket->len = tmp - rsz;
934 ret = sig_sem(SEM_FILEDESC);
935 if (ret < 0) {
936 syslog(LOG_ERROR, "sig_sem => %d", ret);
937 }
938 if (tmp - rsz == 0) {
939 socket->input = NULL;
940 }
941 else
942 socket->input = (void *)&((uint8_t *)socket->input)[rsz];
943 ret = tcp_rel_buf(socket->cepid, rsz);
944 if ((ret != E_OBJ) && (ret < 0)) {
945 syslog(LOG_ERROR, "tcp_rel_buf => %d", ret);
946 //return -ECOMM;
947 }
948 }
949 ret = rsz;
950 }
951 break;
952 }
953 case SOCK_DGRAM: {
954 struct sockaddr_in *raddr = &socket->raddr4;
955 int rsz;
956 ret = wai_sem(SEM_FILEDESC);
957 if (ret < 0) {
958 syslog(LOG_ERROR, "wai_sem => %d", ret);
959 }
960 T_NET_BUF *input = socket->input;
961 if (input == NULL) {
962 ret = sig_sem(SEM_FILEDESC);
963 if (ret < 0) {
964 syslog(LOG_ERROR, "sig_sem => %d", ret);
965 }
966
967 T_IPV4EP rep = { 0, 0 };
968 ret = udp_rcv_dat(socket->cepid, &rep, buf, len,
969 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
970 if (ret < 0) {
971 if ((socket->flags & O_NONBLOCK) == 0)
972 syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
973 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
974 }
975 rsz = ret;
976 if ((addr != NULL) && (alen != NULL)) {
977 ret = wai_sem(SEM_FILEDESC);
978 if (ret < 0) {
979 syslog(LOG_ERROR, "wai_sem => %d", ret);
980 }
981 int sz = *alen;
982 memset(raddr, 0, sizeof(struct sockaddr_in));
983 raddr->sin_family = AF_INET;
984 raddr->sin_port = htons(rep.portno);
985 raddr->sin_addr.s_addr = htonl(rep.ipaddr);
986 if (sz > sizeof(struct sockaddr_in))
987 sz = sizeof(struct sockaddr_in);
988 memcpy(addr, raddr, sz);
989 *alen = sz;
990 ret = sig_sem(SEM_FILEDESC);
991 if (ret < 0) {
992 syslog(LOG_ERROR, "sig_sem => %d", ret);
993 }
994 }
995 }
996 else {
997 rsz = socket->len;
998 void *pbuf = socket->buf;
999 socket->input = NULL;
1000 socket->len = 0;
1001 socket->buf = NULL;
1002 if ((addr != NULL) && (alen != NULL)) {
1003 int sz = *alen;
1004 if (sz > sizeof(struct sockaddr_in))
1005 sz = sizeof(struct sockaddr_in);
1006 memcpy(addr, raddr, sz);
1007 *alen = sz;
1008 }
1009 ret = sig_sem(SEM_FILEDESC);
1010 if (ret < 0) {
1011 syslog(LOG_ERROR, "sig_sem => %d", ret);
1012 }
1013 if (rsz > len)
1014 rsz = len;
1015 memcpy(buf, pbuf, rsz);
1016 ret = rel_net_buf(input);
1017 if (ret < 0) {
1018 syslog(LOG_ERROR, "rel_net_buf => %d", ret);
1019 //return -ECOMM;
1020 }
1021 }
1022 ret = rsz;
1023 }
1024 }
1025 break;
1026 }
1027 case AF_INET6: {
1028 switch (socket->type) {
1029 case SOCK_STREAM: {
1030 if (flags & MSG_OOB) {
1031 ret = tcp6_rcv_oob(socket->cepid, buf, len);
1032 if (ret < 0) {
1033 syslog(LOG_ERROR, "tcp6_rcv_oob => %d", ret);
1034 return -ECOMM;
1035 }
1036 }
1037 else {
1038 int rsz, tmp;
1039 if (socket->input == NULL) {
1040 ret = wai_sem(SEM_FILEDESC);
1041 if (ret < 0) {
1042 syslog(LOG_ERROR, "wai_sem => %d", ret);
1043 }
1044 socket->len = 0;
1045 ret = sig_sem(SEM_FILEDESC);
1046 if (ret < 0) {
1047 syslog(LOG_ERROR, "sig_sem => %d", ret);
1048 }
1049 ret = tcp6_rcv_buf(socket->cepid, &socket->input,
1050 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
1051 if (ret < 0) {
1052 if ((socket->flags & O_NONBLOCK) == 0)
1053 syslog(LOG_ERROR, "tcp6_rcv_buf => %d", ret);
1054 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
1055 }
1056 rsz = ret;
1057 }
1058 else
1059 rsz = socket->len;
1060 tmp = rsz;
1061 if (rsz > len)
1062 rsz = len;
1063 if (rsz >= 0) {
1064 memcpy(buf, socket->input, rsz);
1065 ret = wai_sem(SEM_FILEDESC);
1066 if (ret < 0) {
1067 syslog(LOG_ERROR, "wai_sem => %d", ret);
1068 }
1069 socket->len = tmp - rsz;
1070 ret = sig_sem(SEM_FILEDESC);
1071 if (ret < 0) {
1072 syslog(LOG_ERROR, "sig_sem => %d", ret);
1073 }
1074 if (tmp - rsz == 0) {
1075 socket->input = NULL;
1076 }
1077 else
1078 socket->input = (void *)&((uint8_t *)socket->input)[rsz];
1079 ret = tcp6_rel_buf(socket->cepid, rsz);
1080 if ((ret != E_OBJ) && (ret < 0)) {
1081 syslog(LOG_ERROR, "tcp6_rel_buf => %d", ret);
1082 //return -ECOMM;
1083 }
1084 }
1085 ret = rsz;
1086 }
1087 break;
1088 }
1089 case SOCK_DGRAM: {
1090 struct sockaddr_in6 *raddr = &socket->raddr6;
1091 int rsz;
1092 ret = wai_sem(SEM_FILEDESC);
1093 if (ret < 0) {
1094 syslog(LOG_ERROR, "wai_sem => %d", ret);
1095 }
1096 T_NET_BUF *input = socket->input;
1097 if (input == NULL) {
1098 ret = sig_sem(SEM_FILEDESC);
1099 if (ret < 0) {
1100 syslog(LOG_ERROR, "sig_sem => %d", ret);
1101 }
1102
1103 T_IPV6EP rep = { { 0 }, 0 };
1104 ret = udp6_rcv_dat(socket->cepid, &rep, buf, len,
1105 (socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
1106 if (ret < 0) {
1107 if ((socket->flags & O_NONBLOCK) == 0)
1108 syslog(LOG_ERROR, "udp6_rcv_buf => %d", ret);
1109 return (ret == E_TMOUT) ? -EAGAIN : -ECOMM;
1110 }
1111 rsz = ret;
1112 if ((addr != NULL) && (alen != NULL)) {
1113 ret = wai_sem(SEM_FILEDESC);
1114 if (ret < 0) {
1115 syslog(LOG_ERROR, "wai_sem => %d", ret);
1116 }
1117 int sz = *alen;
1118 memset(raddr, 0, sizeof(struct sockaddr_in6));
1119 raddr->sin6_family = AF_INET;
1120 raddr->sin6_port = htons(rep.portno);
1121 addrcpy(&raddr->sin6_addr, &rep.ipaddr, 16);
1122 if (sz > sizeof(struct sockaddr_in6))
1123 sz = sizeof(struct sockaddr_in6);
1124 memcpy(addr, raddr, sz);
1125 *alen = sz;
1126 ret = sig_sem(SEM_FILEDESC);
1127 if (ret < 0) {
1128 syslog(LOG_ERROR, "sig_sem => %d", ret);
1129 }
1130 }
1131 }
1132 else {
1133 rsz = socket->len;
1134 void *pbuf = socket->buf;
1135 socket->input = NULL;
1136 socket->len = 0;
1137 socket->buf = NULL;
1138 if ((addr != NULL) && (alen != NULL)) {
1139 int sz = *alen;
1140 if (sz > sizeof(struct sockaddr_in6))
1141 sz = sizeof(struct sockaddr_in6);
1142 memcpy(addr, raddr, sz);
1143 *alen = sz;
1144 }
1145 ret = sig_sem(SEM_FILEDESC);
1146 if (ret < 0) {
1147 syslog(LOG_ERROR, "sig_sem => %d", ret);
1148 }
1149 if (rsz > len)
1150 rsz = len;
1151 memcpy(buf, pbuf, rsz);
1152 ret = rel_net_buf(input);
1153 if (ret < 0) {
1154 syslog(LOG_ERROR, "rel_net_buf => %d", ret);
1155 //return -ECOMM;
1156 }
1157 }
1158 ret = rsz;
1159 }
1160 }
1161 break;
1162 }
1163 default:
1164 return -ENOPROTOOPT;
1165 }
1166
1167 if (fp->readevt_w != fp->readevt_r) fp->readevt_r++;
1168
1169 return ret;
1170}
1171
1172ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags)
1173{
1174 no_implement("recvmsg\n");
1175 return -ENOSYS;
1176}
1177
1178int shell_shutdown(int fd, int how)
1179{
1180 SOCKET *fp = fd_to_fp(fd);
1181 if (fp == NULL) {
1182 return -EBADF;
1183 }
1184
1185 ER ret;
1186 socket_t *socket = (socket_t *)fp->exinf;
1187 switch (socket->family) {
1188 case AF_INET: {
1189 switch (socket->type) {
1190 case SOCK_STREAM: {
1191 ret = tcp_sht_cep(socket->cepid);
1192 if (ret < 0) {
1193 return -ECOMM;
1194 }
1195 break;
1196 }
1197 }
1198 break;
1199 }
1200 case AF_INET6: {
1201 switch (socket->type) {
1202 case SOCK_STREAM: {
1203 ret = tcp6_sht_cep(socket->cepid);
1204 if (ret < 0) {
1205 return -ECOMM;
1206 }
1207 break;
1208 }
1209 }
1210 break;
1211 }
1212 default:
1213 return -ENOPROTOOPT;
1214 }
1215
1216 return 0;
1217}
1218
1219int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *__restrict optlen)
1220{
1221 SOCKET *fp = fd_to_fp(fd);
1222 if (fp == NULL) {
1223 return -EBADF;
1224 }
1225
1226 ER ret;
1227 socket_t *socket = (socket_t *)fp->exinf;
1228 switch (socket->family) {
1229 case AF_INET: {
1230 switch (socket->type) {
1231 case SOCK_STREAM: {
1232 switch (level) {
1233 case SOL_SOCKET:
1234 switch (optname) {
1235 case SO_REUSEADDR:
1236 if (socket->flags & SO_REUSEADDR) {
1237 *(bool *)optval = true;
1238 }
1239 else {
1240 *(bool *)optval = false;
1241 }
1242 break;
1243 case SO_KEEPALIVE:
1244 if (socket->flags & SO_KEEPALIVE) {
1245 *(bool *)optval = true;
1246 }
1247 else {
1248 *(bool *)optval = false;
1249 }
1250 break;
1251 case SO_ERROR:
1252 *(int *)optval = 0;
1253 break;
1254 default:
1255 return -EINVAL;
1256 }
1257 break;
1258 case IPPROTO_TCP:
1259 ret = tcp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
1260 if (ret < 0) {
1261 return -EINVAL;
1262 }
1263 *optlen = ret;
1264 break;
1265 default:
1266 return -EINVAL;
1267 }
1268 break;
1269 }
1270 case SOCK_DGRAM: {
1271 switch (level) {
1272 case IPPROTO_UDP:
1273 ret = udp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
1274 if (ret < 0) {
1275 return -EINVAL;
1276 }
1277 *optlen = ret;
1278 break;
1279 default:
1280 return -EINVAL;
1281 }
1282 break;
1283 }
1284 }
1285 break;
1286 }
1287 case AF_INET6: {
1288 switch (socket->type) {
1289 case SOCK_STREAM: {
1290 switch (level) {
1291 case SOL_SOCKET:
1292 switch (optname) {
1293 case SO_REUSEADDR:
1294 if (socket->flags & SO_REUSEADDR) {
1295 *(bool *)optval = true;
1296 }
1297 else {
1298 *(bool *)optval = false;
1299 }
1300 break;
1301 case SO_KEEPALIVE:
1302 if (socket->flags & SO_KEEPALIVE) {
1303 *(bool *)optval = true;
1304 }
1305 else {
1306 *(bool *)optval = false;
1307 }
1308 break;
1309 case SO_ERROR:
1310 *(int *)optval = 0;
1311 break;
1312 default:
1313 return -EINVAL;
1314 }
1315 break;
1316 case IPPROTO_TCP:
1317 ret = tcp6_get_opt(socket->cepid, optname, (void *)optval, *optlen);
1318 if (ret < 0) {
1319 return -EINVAL;
1320 }
1321 *optlen = ret;
1322 break;
1323 default:
1324 return -EINVAL;
1325 }
1326 break;
1327 }
1328 case SOCK_DGRAM: {
1329 switch (level) {
1330 case IPPROTO_UDP:
1331 ret = udp6_get_opt(socket->cepid, optname, (void *)optval, *optlen);
1332 if (ret < 0) {
1333 return -EINVAL;
1334 }
1335 *optlen = ret;
1336 break;
1337 default:
1338 return -EINVAL;
1339 }
1340 break;
1341 }
1342 }
1343 break;
1344 }
1345 default:
1346 return -ENOPROTOOPT;
1347 }
1348
1349 return 0;
1350}
1351
1352int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
1353{
1354 SOCKET *fp = fd_to_fp(fd);
1355 if (fp == NULL) {
1356 return -EBADF;
1357 }
1358
1359 ER ret;
1360 socket_t *socket = (socket_t *)fp->exinf;
1361 switch (socket->family) {
1362 case AF_INET: {
1363 switch (socket->type) {
1364 case SOCK_STREAM: {
1365 switch (level){
1366 case SOL_SOCKET:
1367 switch (optname) {
1368 case SO_REUSEADDR:
1369 if (*(bool *)optval) {
1370 socket->flags |= SO_REUSEADDR;
1371 }
1372 else {
1373 socket->flags &= ~SO_REUSEADDR;
1374 }
1375 break;
1376 case SO_KEEPALIVE:
1377 if (*(bool *)optval) {
1378 socket->flags |= SO_KEEPALIVE;
1379 }
1380 else {
1381 socket->flags &= ~SO_KEEPALIVE;
1382 }
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
1387 break;
1388 case IPPROTO_TCP:
1389 ret = tcp_set_opt(socket->cepid, optname, (void *)optval, optlen);
1390 if (ret < 0) {
1391 return -EINVAL;
1392 }
1393 break;
1394 default:
1395 return -EINVAL;
1396 }
1397 break;
1398 }
1399 case SOCK_DGRAM: {
1400 switch (level){
1401 case IPPROTO_UDP:
1402 ret = udp_set_opt(socket->cepid, optname, (void *)optval, optlen);
1403 if (ret < 0) {
1404 return -EINVAL;
1405 }
1406 break;
1407 default:
1408 return -EINVAL;
1409 }
1410 break;
1411 }
1412 }
1413 break;
1414 }
1415 case AF_INET6: {
1416 switch (socket->type) {
1417 case SOCK_STREAM: {
1418 switch (level){
1419 case SOL_SOCKET:
1420 switch (optname) {
1421 case SO_REUSEADDR:
1422 if (*(bool *)optval) {
1423 socket->flags |= SO_REUSEADDR;
1424 }
1425 else {
1426 socket->flags &= ~SO_REUSEADDR;
1427 }
1428 break;
1429 case SO_KEEPALIVE:
1430 if (*(bool *)optval) {
1431 socket->flags |= SO_KEEPALIVE;
1432 }
1433 else {
1434 socket->flags &= ~SO_KEEPALIVE;
1435 }
1436 break;
1437 default:
1438 return -EINVAL;
1439 }
1440 break;
1441 case IPPROTO_TCP:
1442 ret = tcp6_set_opt(socket->cepid, optname, (void *)optval, optlen);
1443 if (ret < 0) {
1444 return -EINVAL;
1445 }
1446 break;
1447 default:
1448 return -EINVAL;
1449 }
1450 break;
1451 }
1452 case SOCK_DGRAM: {
1453 switch (level){
1454 case IPPROTO_UDP:
1455 ret = udp6_set_opt(socket->cepid, optname, (void *)optval, optlen);
1456 if (ret < 0) {
1457 return -EINVAL;
1458 }
1459 break;
1460 default:
1461 return -EINVAL;
1462 }
1463 break;
1464 }
1465 }
1466 break;
1467 }
1468 default:
1469 return -ENOPROTOOPT;
1470 }
1471
1472 return 0;
1473}
1474
1475int shell_getpeername(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
1476{
1477 SOCKET *fp = fd_to_fp(fd);
1478 if (fp == NULL) {
1479 return -EBADF;
1480 }
1481 if (len == NULL) {
1482 return -EINVAL;
1483 }
1484
1485 socklen_t size = *len;
1486 socket_t *socket = (socket_t *)fp->exinf;
1487 switch (socket->family) {
1488 case AF_INET: {
1489 struct sockaddr_in *raddr = &socket->raddr4;
1490 *len = sizeof(struct sockaddr_in);
1491 if (size > sizeof(struct sockaddr_in))
1492 size = sizeof(struct sockaddr_in);
1493 memcpy(addr, raddr, size);
1494 break;
1495 }
1496 case AF_INET6: {
1497 struct sockaddr_in6 *raddr = &socket->raddr6;
1498 *len = sizeof(struct sockaddr_in6);
1499 if (size > sizeof(struct sockaddr_in6))
1500 size = sizeof(struct sockaddr_in6);
1501 memcpy(addr, raddr, size);
1502 break;
1503 }
1504 default:
1505 return -ENOPROTOOPT;
1506 }
1507
1508 return 0;
1509}
1510
1511int shell_getsockname(int fd, struct sockaddr *restrict addr, socklen_t *restrict len)
1512{
1513 SOCKET *fp = fd_to_fp(fd);
1514 if (fp == NULL) {
1515 return -EBADF;
1516 }
1517 if (len == NULL) {
1518 return -EINVAL;
1519 }
1520
1521 socklen_t size = *len;
1522 socket_t *socket = (socket_t *)fp->exinf;
1523 switch (socket->family) {
1524 case AF_INET: {
1525 const T_IN4_ADDR *laddr4 = in4_get_ifaddr(0);
1526 struct sockaddr_in laddr;
1527 laddr.sin_family = AF_INET;
1528 laddr.sin_addr.s_addr = htonl(*laddr4);
1529 laddr.sin_port = socket->laddr4.sin_port;
1530 memset(&laddr.sin_zero, 0, sizeof(laddr.sin_zero));
1531 *len = sizeof(struct sockaddr_in);
1532 if (size > sizeof(struct sockaddr_in))
1533 size = sizeof(struct sockaddr_in);
1534 memcpy(addr, &laddr, size);
1535 break;
1536 }
1537 case AF_INET6: {
1538 const T_IN6_ADDR *laddr6 = in6_get_ifaddr(0);
1539 struct sockaddr_in6 laddr;
1540 laddr.sin6_family = AF_INET;
1541 addrcpy(&laddr.sin6_addr, laddr6, 16);
1542 laddr.sin6_port = socket->laddr6.sin6_port;
1543 *len = sizeof(struct sockaddr_in6);
1544 if (size > sizeof(struct sockaddr_in6))
1545 size = sizeof(struct sockaddr_in6);
1546 memcpy(addr, &laddr, size);
1547 break;
1548 }
1549 default:
1550 return -ENOPROTOOPT;
1551 }
1552
1553 return 0;
1554}
1555
1556int tcp_fd_close(struct SHELL_FILE *fp)
1557{
1558 ER ret, ret2;
1559
1560 socket_t *socket = (socket_t *)fp->exinf;
1561 switch (socket->family) {
1562 case AF_INET: {
1563 if (socket->cepid != 0) {
1564 ID cepid = socket->cepid;
1565 ret = tcp_sht_cep(cepid);
1566 if (ret < 0) {
1567 //return -1;
1568 }
1569 ret = tcp_cls_cep(cepid, (socket->repid != 0) ? 0 : SOCKET_TIMEOUT);
1570 ret2 = tcp_del_cep(cepid);
1571 //delete_fd_by_id(&IO_TYPE_TCP, cepid);
1572 delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
1573 if ((ret < 0) || (ret2 < 0)) {
1574 return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
1575 }
1576 }
1577 else if (socket->repid != 0) {
1578 ID repid = socket->repid;
1579 ret = tcp_del_rep(repid);
1580 //delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + repid);
1581 delete_id(tcp_repid_table, tcp_repid_table_count, repid);
1582 if (ret < 0) {
1583 return -EINVAL;
1584 }
1585 }
1586 else {
1587 return -EINVAL;
1588 }
1589 break;
1590 }
1591 case AF_INET6: {
1592 if (socket->cepid != 0) {
1593 ID cepid = socket->cepid;
1594 ret = tcp6_sht_cep(cepid);
1595 if (ret < 0) {
1596 //return -1;
1597 }
1598 ret = tcp6_cls_cep(cepid, (socket->repid != 0) ? 0 : SOCKET_TIMEOUT);
1599 ret2 = tcp6_del_cep(cepid);
1600 //delete_fd_by_id(&IO_TYPE_TCP, cepid);
1601 delete_id(tcp6_cepid_table, tcp6_cepid_table_count, cepid);
1602 if ((ret < 0) || (ret2 < 0)) {
1603 return (ret == E_TMOUT) ? -ETIMEDOUT : -EIO;
1604 }
1605 }
1606 else if (socket->repid != 0) {
1607 ID repid = socket->repid;
1608 ret = tcp6_del_rep(repid);
1609 //delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp6_cepid + repid);
1610 delete_id(tcp6_repid_table, tcp6_repid_table_count, repid);
1611 if (ret < 0) {
1612 return -EINVAL;
1613 }
1614 }
1615 else {
1616 return -EINVAL;
1617 }
1618 break;
1619 }
1620 default:
1621 return -ENOPROTOOPT;
1622 }
1623
1624 return 0;
1625}
1626
1627size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
1628{
1629 return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
1630}
1631
1632size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
1633{
1634 return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
1635}
1636
1637off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
1638{
1639 return -EPERM;
1640}
1641
1642int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
1643{
1644 socket_t *socket = (socket_t *)fp->exinf;
1645
1646 switch (req) {
1647 case F_GETFL:
1648 return socket->flags;
1649 case F_SETFL:
1650 socket->flags = (unsigned int)arg;
1651 return 0;
1652 }
1653
1654 return -EINVAL;
1655}
1656
1657bool_t tcp_fd_readable(struct SHELL_FILE *fp)
1658{
1659 ER ret;
1660
1661 socket_t *socket = (socket_t *)fp->exinf;
1662 if (socket->cepid != 0) {
1663 if (socket->len == 0) {
1664 ret = tcp_rcv_buf(socket->cepid, &socket->input, TMO_NBLK);
1665 if ((ret != E_WBLK) && (ret != E_OBJ) && (ret < 0)) {
1666 syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
1667 //return ret;
1668 }
1669 if (ret > 0) {
1670 ret = wai_sem(SEM_FILEDESC);
1671 if (ret < 0) {
1672 syslog(LOG_ERROR, "wai_sem => %d", ret);
1673 }
1674 socket->len += ret;
1675 ret = sig_sem(SEM_FILEDESC);
1676 if (ret < 0) {
1677 syslog(LOG_ERROR, "sig_sem => %d", ret);
1678 }
1679 }
1680 }
1681 else ret = 1;
1682 if (ret > 0) {
1683 return true;
1684 }
1685 }
1686
1687 return false;
1688}
1689
1690bool_t tcp_fd_writable(struct SHELL_FILE *fp)
1691{
1692 //socket_t *socket = (socket_t *)fp->exinf;
1693
1694 return /*fp->writable &&*/ (fp->writeevt_w == fp->writeevt_r);
1695}
1696
1697void tcp_fd_delete(struct SHELL_FILE *fp)
1698{
1699 socket_t *socket = (socket_t *)fp->exinf;
1700 free(socket->buf);
1701 socket->buf = NULL;
1702 free(fp->exinf);
1703 fp->exinf = NULL;
1704}
1705
1706ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk)
1707{
1708 struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_TCP, cepid);
1709 FLGPTN flgptn = 0;
1710 ER ret;
1711 int len;
1712
1713 if (fp == NULL)
1714 return E_PAR;
1715
1716 socket_t *socket = (socket_t *)fp->exinf;
1717 int fd = fp->fd;
1718 FD_SET(fd, (fd_set *)&flgptn);
1719
1720 switch (fncd) {
1721 case TFN_TCP_RCV_BUF:
1722 len = *(int *)p_parblk;
1723 if ((len <= 0) || (fp->exinf == NULL))
1724 return E_OK;
1725
1726 ret = wai_sem(SEM_FILEDESC);
1727 if (ret < 0) {
1728 syslog(LOG_ERROR, "wai_sem => %d", ret);
1729 }
1730 socket->len += len;
1731 ret = sig_sem(SEM_FILEDESC);
1732 if (ret < 0) {
1733 syslog(LOG_ERROR, "sig_sem => %d", ret);
1734 }
1735
1736 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
1737
1738 set_flg(FLG_SELECT_WAIT, flgptn);
1739 return E_OK;
1740
1741 case TFN_TCP_RCV_DAT:
1742 len = *(int *)p_parblk;
1743 if ((len <= 0) || (fp->exinf == NULL))
1744 return E_OK;
1745
1746 ret = wai_sem(SEM_FILEDESC);
1747 if (ret < 0) {
1748 syslog(LOG_ERROR, "wai_sem => %d", ret);
1749 }
1750 socket->len += len;
1751 ret = sig_sem(SEM_FILEDESC);
1752 if (ret < 0) {
1753 syslog(LOG_ERROR, "sig_sem => %d", ret);
1754 }
1755
1756 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
1757
1758 set_flg(FLG_SELECT_WAIT, flgptn);
1759 return E_OK;
1760
1761 case TFN_TCP_SND_DAT:
1762 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
1763
1764 set_flg(FLG_SELECT_WAIT, flgptn);
1765 return E_OK;
1766
1767 case TFN_TCP_CAN_CEP:
1768 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
1769
1770 set_flg(FLG_SELECT_WAIT, flgptn);
1771 return E_OK;
1772
1773 case TFN_TCP_DEL_REP:
1774 delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + cepid);
1775 return E_OK;
1776
1777 case TFN_TCP_DEL_CEP:
1778 delete_fd_by_id(&IO_TYPE_TCP, cepid);
1779 return E_OK;
1780
1781 default:
1782 return E_OK;
1783 }
1784}
1785
1786ER socket_tcp6_callback(ID cepid, FN fncd, void *p_parblk)
1787{
1788 struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_TCP, cepid);
1789 FLGPTN flgptn = 0;
1790 ER ret;
1791 int len;
1792
1793 if (fp == NULL)
1794 return E_PAR;
1795
1796 socket_t *socket = (socket_t *)fp->exinf;
1797 int fd = fp->fd;
1798 FD_SET(fd, (fd_set *)&flgptn);
1799
1800 switch (fncd) {
1801 case TFN_TCP_RCV_BUF:
1802 len = *(int *)p_parblk;
1803 if ((len <= 0) || (fp->exinf == NULL))
1804 return E_OK;
1805
1806 ret = wai_sem(SEM_FILEDESC);
1807 if (ret < 0) {
1808 syslog(LOG_ERROR, "wai_sem => %d", ret);
1809 }
1810 socket->len += len;
1811 ret = sig_sem(SEM_FILEDESC);
1812 if (ret < 0) {
1813 syslog(LOG_ERROR, "sig_sem => %d", ret);
1814 }
1815
1816 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
1817
1818 set_flg(FLG_SELECT_WAIT, flgptn);
1819 return E_OK;
1820
1821 case TFN_TCP_RCV_DAT:
1822 len = *(int *)p_parblk;
1823 if ((len <= 0) || (fp->exinf == NULL))
1824 return E_OK;
1825
1826 ret = wai_sem(SEM_FILEDESC);
1827 if (ret < 0) {
1828 syslog(LOG_ERROR, "wai_sem => %d", ret);
1829 }
1830 socket->len += len;
1831 ret = sig_sem(SEM_FILEDESC);
1832 if (ret < 0) {
1833 syslog(LOG_ERROR, "sig_sem => %d", ret);
1834 }
1835
1836 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
1837
1838 set_flg(FLG_SELECT_WAIT, flgptn);
1839 return E_OK;
1840
1841 case TFN_TCP_SND_DAT:
1842 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
1843
1844 set_flg(FLG_SELECT_WAIT, flgptn);
1845 return E_OK;
1846
1847 case TFN_TCP_CAN_CEP:
1848 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
1849
1850 set_flg(FLG_SELECT_WAIT, flgptn);
1851 return E_OK;
1852
1853 case TFN_TCP_DEL_REP:
1854 delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + cepid);
1855 return E_OK;
1856
1857 case TFN_TCP_DEL_CEP:
1858 delete_fd_by_id(&IO_TYPE_TCP, cepid);
1859 return E_OK;
1860
1861 default:
1862 return E_OK;
1863 }
1864}
1865
1866int udp_fd_close(struct SHELL_FILE *fp)
1867{
1868 ER ret;
1869 ID cepid;
1870
1871 socket_t *socket = (socket_t *)fp->exinf;
1872 switch (socket->family) {
1873 case AF_INET: {
1874 cepid = socket->cepid;
1875 ret = udp_del_cep(cepid);
1876 //delete_fd_by_id(&IO_TYPE_UDP, cepid);
1877 delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
1878 if (ret < 0) {
1879 return -EINVAL;
1880 }
1881 break;
1882 }
1883 case AF_INET6: {
1884 cepid = socket->cepid;
1885 ret = udp6_del_cep(cepid);
1886 //delete_fd_by_id(&IO_TYPE_UDP, cepid);
1887 delete_id(udp6_cepid_table, udp6_cepid_table_count, cepid);
1888 if (ret < 0) {
1889 return -EINVAL;
1890 }
1891 break;
1892 }
1893 default:
1894 return -ENOPROTOOPT;
1895 }
1896
1897 return 0;
1898}
1899
1900size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
1901{
1902 return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
1903}
1904
1905size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
1906{
1907 return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
1908}
1909
1910off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
1911{
1912 return -EPERM;
1913}
1914
1915int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
1916{
1917 return -EINVAL;
1918}
1919
1920bool_t udp_fd_readable(struct SHELL_FILE *fp)
1921{
1922 socket_t *socket = (socket_t *)fp->exinf;
1923 if (socket->cepid != 0) {
1924 if (socket->input != NULL) {
1925 return true;
1926 }
1927 }
1928
1929 return false;
1930}
1931
1932bool_t udp_fd_writable(struct SHELL_FILE *fp)
1933{
1934 //socket_t *socket = (socket_t *)fp->exinf;
1935
1936 return fp->writable && (fp->writeevt_w == fp->writeevt_r);
1937}
1938
1939void udp_fd_delete(struct SHELL_FILE *fp)
1940{
1941 //socket_t *socket = (socket_t *)fp->exinf;
1942 //free(socket->buf);
1943 //socket->buf = NULL;
1944 free(fp->exinf);
1945 fp->exinf = NULL;
1946}
1947
1948ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk)
1949{
1950 struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_UDP, cepid);
1951 FLGPTN flgptn = 0;
1952 int len;
1953
1954 if (fp == NULL)
1955 return E_PAR;
1956
1957 int fd = fp->fd;
1958 FD_SET(fd, (fd_set *)&flgptn);
1959
1960 switch (fncd) {
1961 case TEV_UDP_RCV_DAT:
1962 {
1963 T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
1964 len = udppara->len;
1965 if ((len <= 0) || (fp->exinf == NULL))
1966 return E_OK;
1967
1968 ER ret = wai_sem(SEM_FILEDESC);
1969 if (ret < 0) {
1970 syslog(LOG_ERROR, "wai_sem => %d", ret);
1971 }
1972 socket_t *socket = (socket_t *)fp->exinf;
1973 socket->len = len;
1974 if (socket->input != NULL) {
1975 ret = rel_net_buf(socket->input);
1976 if (ret < 0) {
1977 syslog(LOG_ERROR, "rel_net_buf => %d", ret);
1978 }
1979 }
1980 socket->input = udppara->input;
1981 socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
1982 memset(&socket->raddr4, 0, sizeof(struct sockaddr_in));
1983 socket->raddr4.sin_family = AF_INET;
1984 socket->raddr4.sin_port = htons(udppara->rep4.portno);
1985 socket->raddr4.sin_addr.s_addr = htonl(udppara->rep4.ipaddr);
1986 udppara->input->flags |= NB_FLG_NOREL_IFOUT;
1987 ret = sig_sem(SEM_FILEDESC);
1988 if (ret < 0) {
1989 syslog(LOG_ERROR, "sig_sem => %d", ret);
1990 }
1991
1992 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
1993
1994 set_flg(FLG_SELECT_WAIT, flgptn);
1995 return E_OK;
1996 }
1997 case TFN_UDP_CRE_CEP:
1998 return E_OK;
1999
2000 case TFN_UDP_RCV_DAT:
2001 len = *(int *)p_parblk;
2002 if ((len <= 0) || (fp->exinf == NULL))
2003 return E_OK;
2004
2005 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
2006
2007 set_flg(FLG_SELECT_WAIT, flgptn);
2008 return E_OK;
2009
2010 case TFN_UDP_SND_DAT:
2011 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
2012
2013 set_flg(FLG_SELECT_WAIT, flgptn);
2014 return E_OK;
2015
2016 case TFN_UDP_CAN_CEP:
2017 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
2018
2019 set_flg(FLG_SELECT_WAIT, flgptn);
2020 return E_OK;
2021
2022 case TFN_UDP_DEL_CEP:
2023 delete_fd_by_id(&IO_TYPE_UDP, cepid);
2024 return E_OK;
2025
2026 default:
2027 return E_OK;
2028 }
2029}
2030
2031ER socket_udp6_callback(ID cepid, FN fncd, void *p_parblk)
2032{
2033 struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_UDP, cepid);
2034 FLGPTN flgptn = 0;
2035 int len;
2036
2037 if (fp == NULL)
2038 return E_PAR;
2039
2040 int fd = fp->fd;
2041 FD_SET(fd, (fd_set *)&flgptn);
2042
2043 switch (fncd) {
2044 case TEV_UDP_RCV_DAT:
2045 {
2046 T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
2047 len = udppara->len;
2048 if ((len <= 0) || (fp->exinf == NULL))
2049 return E_OK;
2050
2051 ER ret = wai_sem(SEM_FILEDESC);
2052 if (ret < 0) {
2053 syslog(LOG_ERROR, "wai_sem => %d", ret);
2054 }
2055 socket_t *socket = (socket_t *)fp->exinf;
2056 socket->len = len;
2057 if (socket->input != NULL) {
2058 ret = rel_net_buf(socket->input);
2059 if (ret < 0) {
2060 syslog(LOG_ERROR, "rel_net_buf => %d", ret);
2061 }
2062 }
2063 socket->input = udppara->input;
2064 socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
2065 memset(&socket->raddr6, 0, sizeof(struct sockaddr_in6));
2066 socket->raddr6.sin6_family = AF_INET;
2067 socket->raddr6.sin6_port = htons(udppara->rep6.portno);
2068 addrcpy(&socket->raddr6.sin6_addr, &udppara->rep6.ipaddr, 16);
2069 udppara->input->flags |= NB_FLG_NOREL_IFOUT;
2070 ret = sig_sem(SEM_FILEDESC);
2071 if (ret < 0) {
2072 syslog(LOG_ERROR, "sig_sem => %d", ret);
2073 }
2074
2075 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
2076
2077 set_flg(FLG_SELECT_WAIT, flgptn);
2078 return E_OK;
2079 }
2080 case TFN_UDP_CRE_CEP:
2081 return E_OK;
2082
2083 case TFN_UDP_RCV_DAT:
2084 len = *(int *)p_parblk;
2085 if ((len <= 0) || (fp->exinf == NULL))
2086 return E_OK;
2087
2088 if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
2089
2090 set_flg(FLG_SELECT_WAIT, flgptn);
2091 return E_OK;
2092
2093 case TFN_UDP_SND_DAT:
2094 if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
2095
2096 set_flg(FLG_SELECT_WAIT, flgptn);
2097 return E_OK;
2098
2099 case TFN_UDP_CAN_CEP:
2100 if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
2101
2102 set_flg(FLG_SELECT_WAIT, flgptn);
2103 return E_OK;
2104
2105 case TFN_UDP_DEL_CEP:
2106 delete_fd_by_id(&IO_TYPE_UDP, cepid);
2107 return E_OK;
2108
2109 default:
2110 return E_OK;
2111 }
2112}
2113
2114#ifndef TCP_CFG_EXTENTIONS
2115ER tcp_cre_rep(ID repid, T_TCP_CREP *pk_crep)
2116{
2117 syslog(LOG_ERROR, "tcp_cre_rep not implement");
2118 shell_abort();
2119 return E_SYS;
2120}
2121
2122ER tcp_cre_cep(ID cepid, T_TCP_CCEP *pk_ccep)
2123{
2124 syslog(LOG_ERROR, "tcp_cre_cep not implement");
2125 shell_abort();
2126 return E_SYS;
2127}
2128#endif
2129
2130#ifndef UDP_CFG_EXTENTIONS
2131ER udp_cre_cep(ID cepid, T_UDP_CCEP *pk_ccep)
2132{
2133 syslog(LOG_ERROR, "udp_cre_cep not implement");
2134 shell_abort();
2135 return E_SYS;
2136}
2137#endif
2138
2139#ifndef TCP_CFG_EXTENTIONS
2140ER_UINT tcp_snd_oob(ID cepid, void *data, int_t len, TMO tmout)
2141{
2142 syslog(LOG_ERROR, "tcp_snd_oob not implement");
2143 shell_abort();
2144 return E_SYS;
2145}
2146
2147ER_UINT tcp_rcv_oob(ID cepid, void *data, int_t len)
2148{
2149 syslog(LOG_ERROR, "tcp_rcv_oob not implement");
2150 shell_abort();
2151 return E_SYS;
2152}
2153
2154ER tcp_set_opt(ID cepid, int_t optname, void *optval, int_t optlen)
2155{
2156 syslog(LOG_ERROR, "tcp_set_opt not implement");
2157 shell_abort();
2158 return E_SYS;
2159}
2160
2161ER tcp_get_opt(ID cepid, int_t optname, void *optval, int_t optlen)
2162{
2163 syslog(LOG_ERROR, "tcp_get_opt not implement");
2164 shell_abort();
2165 return E_SYS;
2166}
2167#endif
2168
2169#ifndef UDP_CFG_EXTENTIONS
2170ER udp_get_opt(ID cepid, int_t optname, void *optval, int_t optlen)
2171{
2172 syslog(LOG_ERROR, "udp_get_opt not implement");
2173 shell_abort();
2174 return E_SYS;
2175}
2176
2177ER udp_set_opt(ID cepid, int_t optname, void *optval, int_t optlen)
2178{
2179 syslog(LOG_ERROR, "udp_set_opt not implement");
2180 shell_abort();
2181 return E_SYS;
2182}
2183#endif
2184
2185// musl-1.1.18/network/lookup.h
2186struct address {
2187 int family;
2188 unsigned scopeid;
2189 uint8_t addr[16];
2190 int sortkey;
2191};
2192
2193#define MAXNS 3
2194
2195struct resolvconf {
2196 struct address ns[MAXNS];
2197 unsigned nns, attempts, ndots;
2198 unsigned timeout;
2199};
2200
2201// musl-1.1.18/network/resolvconf.c
2202int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz)
2203{
2204 int nns = 0;
2205
2206 conf->ndots = 1;
2207 conf->timeout = 5;
2208 conf->attempts = 2;
2209 if (search) *search = 0;
2210
2211#if defined(SUPPORT_INET4)
2212 T_IN4_ADDR in4_addr;
2213 conf->ns[nns].family = AF_INET;
2214 conf->ns[nns].scopeid = 0;
2215 dns_in4_get_addr(&in4_addr);
2216 *(uint32_t *)conf->ns[nns].addr = ntohl(in4_addr);
2217 nns++;
2218#endif
2219
2220#if defined(SUPPORT_INET6)
2221 conf->ns[nns].family = AF_INET6;
2222 conf->ns[nns].scopeid = 0;
2223 dns_in6_get_addr((T_IN6_ADDR *)conf->ns[nns].addr);
2224 nns++;
2225#endif
2226 conf->nns = nns;
2227
2228 return 0;
2229}
Note: See TracBrowser for help on using the repository browser.