1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <rtems.h> |
---|
6 | #include <rtems/error.h> |
---|
7 | #include <rtems/shell.h> |
---|
8 | #include <rtems/telnetd.h> |
---|
9 | #include <sys/socket.h> |
---|
10 | #include <netinet/in.h> |
---|
11 | #include <arpa/inet.h> |
---|
12 | #include <unistd.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <stdio.h> |
---|
15 | /***********************************************************/ |
---|
16 | rtems_id telnetd_task_id =0; |
---|
17 | rtems_unsigned32 telnetd_stack_size =16384; |
---|
18 | rtems_task_priority telnetd_task_priority=100; |
---|
19 | /***********************************************************/ |
---|
20 | rtems_task rtems_task_telnetd(rtems_task_argument task_argument) { |
---|
21 | int des_socket, |
---|
22 | acp_socket; |
---|
23 | struct sockaddr_in srv; |
---|
24 | char * devname; |
---|
25 | int i=1; |
---|
26 | int size_adr; |
---|
27 | if ((des_socket=socket(PF_INET,SOCK_STREAM,0))<0) { |
---|
28 | perror("socket"); |
---|
29 | rtems_task_delete(RTEMS_SELF); |
---|
30 | }; |
---|
31 | setsockopt(des_socket,SOL_SOCKET,0,&i,sizeof(i)); |
---|
32 | memset(&srv,0,sizeof(srv)); |
---|
33 | srv.sin_family=AF_INET; |
---|
34 | srv.sin_port=htons(23); |
---|
35 | size_adr=sizeof(srv); |
---|
36 | if ((bind(des_socket,(struct sockaddr *)&srv,size_adr))<0) { |
---|
37 | perror("bind"); |
---|
38 | close(des_socket); |
---|
39 | rtems_task_delete(RTEMS_SELF); |
---|
40 | }; |
---|
41 | if ((listen(des_socket,5))<0) { |
---|
42 | perror("listen"); |
---|
43 | close(des_socket); |
---|
44 | rtems_task_delete(RTEMS_SELF); |
---|
45 | }; |
---|
46 | do { |
---|
47 | acp_socket=accept(des_socket,(struct sockaddr*)&srv,&size_adr); |
---|
48 | if (acp_socket<0) { |
---|
49 | perror("accept"); |
---|
50 | break; |
---|
51 | }; |
---|
52 | if (devname = get_pty(acp_socket) ) { |
---|
53 | shell_init(&devname[5], |
---|
54 | telnetd_stack_size, |
---|
55 | telnetd_task_priority, |
---|
56 | devname,B9600|CS8,FALSE); |
---|
57 | } else { |
---|
58 | close(acp_socket); |
---|
59 | }; |
---|
60 | } while(1); |
---|
61 | close(des_socket); |
---|
62 | rtems_task_delete(RTEMS_SELF); |
---|
63 | } |
---|
64 | /***********************************************************/ |
---|
65 | int rtems_initialize_telnetd(void) { |
---|
66 | rtems_status_code sc; |
---|
67 | if (telnetd_task_id ) return RTEMS_RESOURCE_IN_USE; |
---|
68 | if (telnetd_stack_size<=0 ) telnetd_stack_size =16384; |
---|
69 | if (telnetd_task_priority<=2) telnetd_task_priority=100; |
---|
70 | sc=rtems_task_create(new_rtems_name("TLND"), |
---|
71 | 100,RTEMS_MINIMUM_STACK_SIZE, |
---|
72 | RTEMS_DEFAULT_MODES, |
---|
73 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
74 | &telnetd_task_id); |
---|
75 | if (sc!=RTEMS_SUCCESSFUL) { |
---|
76 | rtems_error(sc,"creating task telnetd"); |
---|
77 | return (int)sc; |
---|
78 | }; |
---|
79 | sc=rtems_task_start(telnetd_task_id, |
---|
80 | rtems_task_telnetd, |
---|
81 | (rtems_task_argument)NULL); |
---|
82 | if (sc!=RTEMS_SUCCESSFUL) { |
---|
83 | rtems_error(sc,"starting task telnetd"); |
---|
84 | }; |
---|
85 | return (int)sc; |
---|
86 | } |
---|
87 | /***********************************************************/ |
---|
88 | int main_telnetd(int argc,char * argv[]) { |
---|
89 | rtems_status_code sc; |
---|
90 | if (telnetd_task_id) { |
---|
91 | printf("ERROR:telnetd already started\n"); |
---|
92 | return 1; |
---|
93 | }; |
---|
94 | if (argc>1) telnetd_stack_size =str2int(argv[1]); |
---|
95 | if (argc>2) telnetd_task_priority=str2int(argv[2]); |
---|
96 | sc=rtems_initialize_telnetd(); |
---|
97 | if (sc!=RTEMS_SUCCESSFUL) return sc; |
---|
98 | printf("rtems_telnetd() started with stacksize=%u,priority=%d\n", |
---|
99 | telnetd_stack_size,telnetd_task_priority); |
---|
100 | return 0; |
---|
101 | } |
---|
102 | /***********************************************************/ |
---|
103 | int register_telnetd(void) { |
---|
104 | shell_add_cmd("telnetd","telnet","telnetd [stacksize [tsk_priority]]",main_telnetd); |
---|
105 | return 0; |
---|
106 | } |
---|
107 | /***********************************************************/ |
---|