1 | /***********************************************************/ |
---|
2 | /* |
---|
3 | * |
---|
4 | * The telnet DAEMON |
---|
5 | * |
---|
6 | * Author: 17,may 2001 |
---|
7 | * |
---|
8 | * WORK: fernando.ruiz@ctv.es |
---|
9 | * HOME: correo@fernando-ruiz.com |
---|
10 | * |
---|
11 | * After start the net you can start this daemon. |
---|
12 | * It uses the previously inited pseudo-terminales (pty.c) |
---|
13 | * getting a new terminal with getpty(). This function |
---|
14 | * gives a terminal name passing a opened socket like parameter. |
---|
15 | * |
---|
16 | * With register_telnetd() you add a new command in the shell to start |
---|
17 | * this daemon interactively. (Login in /dev/console of course) |
---|
18 | * |
---|
19 | * Sorry but OOB is not still implemented. (This is the first version) |
---|
20 | */ |
---|
21 | #ifdef HAVE_CONFIG_H |
---|
22 | #include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/error.h> |
---|
27 | #include <rtems/pty.h> |
---|
28 | #include <rtems/shell.h> |
---|
29 | #include <rtems/telnetd.h> |
---|
30 | #include <sys/socket.h> |
---|
31 | #include <netinet/in.h> |
---|
32 | #include <arpa/inet.h> |
---|
33 | #include <unistd.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <string.h> /* memset */ |
---|
36 | #include <stdio.h> |
---|
37 | /***********************************************************/ |
---|
38 | rtems_id telnetd_task_id =0; |
---|
39 | uint32_t telnetd_stack_size =16384; |
---|
40 | rtems_task_priority telnetd_task_priority=100; |
---|
41 | /***********************************************************/ |
---|
42 | rtems_task rtems_task_telnetd(rtems_task_argument task_argument) { |
---|
43 | int des_socket, |
---|
44 | acp_socket; |
---|
45 | struct sockaddr_in srv; |
---|
46 | char * devname; |
---|
47 | int i=1; |
---|
48 | int size_adr; |
---|
49 | if ((des_socket=socket(PF_INET,SOCK_STREAM,0))<0) { |
---|
50 | perror("telnetd:socket"); |
---|
51 | rtems_task_delete(RTEMS_SELF); |
---|
52 | }; |
---|
53 | setsockopt(des_socket,SOL_SOCKET,0,&i,sizeof(i)); |
---|
54 | memset(&srv,0,sizeof(srv)); |
---|
55 | srv.sin_family=AF_INET; |
---|
56 | srv.sin_port=htons(23); |
---|
57 | size_adr=sizeof(srv); |
---|
58 | if ((bind(des_socket,(struct sockaddr *)&srv,size_adr))<0) { |
---|
59 | perror("telnetd:bind"); |
---|
60 | close(des_socket); |
---|
61 | rtems_task_delete(RTEMS_SELF); |
---|
62 | }; |
---|
63 | if ((listen(des_socket,5))<0) { |
---|
64 | perror("telnetd:listen"); |
---|
65 | close(des_socket); |
---|
66 | rtems_task_delete(RTEMS_SELF); |
---|
67 | }; |
---|
68 | do { |
---|
69 | acp_socket=accept(des_socket,(struct sockaddr*)&srv,&size_adr); |
---|
70 | if (acp_socket<0) { |
---|
71 | perror("telnetd:accept"); |
---|
72 | break; |
---|
73 | }; |
---|
74 | if ((devname = get_pty(acp_socket)) ) { |
---|
75 | shell_init(&devname[5], |
---|
76 | telnetd_stack_size, |
---|
77 | telnetd_task_priority, |
---|
78 | devname,B9600|CS8,FALSE); |
---|
79 | } else { |
---|
80 | close(acp_socket); |
---|
81 | }; |
---|
82 | } while(1); |
---|
83 | close(des_socket); |
---|
84 | rtems_task_delete(RTEMS_SELF); |
---|
85 | } |
---|
86 | /***********************************************************/ |
---|
87 | int rtems_initialize_telnetd(void) { |
---|
88 | void register_icmds(void); |
---|
89 | rtems_status_code sc; |
---|
90 | |
---|
91 | register_icmds(); /* stats for tcp/ip */ |
---|
92 | |
---|
93 | if (telnetd_task_id ) return RTEMS_RESOURCE_IN_USE; |
---|
94 | if (telnetd_stack_size<=0 ) telnetd_stack_size =16384; |
---|
95 | if (telnetd_task_priority<=2) telnetd_task_priority=100; |
---|
96 | sc=rtems_task_create(new_rtems_name("tlnd"), |
---|
97 | 100,RTEMS_MINIMUM_STACK_SIZE, |
---|
98 | RTEMS_DEFAULT_MODES, |
---|
99 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
100 | &telnetd_task_id); |
---|
101 | if (sc!=RTEMS_SUCCESSFUL) { |
---|
102 | rtems_error(sc,"creating task telnetd"); |
---|
103 | return (int)sc; |
---|
104 | }; |
---|
105 | sc=rtems_task_start(telnetd_task_id, |
---|
106 | rtems_task_telnetd, |
---|
107 | (rtems_task_argument)NULL); |
---|
108 | if (sc!=RTEMS_SUCCESSFUL) { |
---|
109 | rtems_error(sc,"starting task telnetd"); |
---|
110 | }; |
---|
111 | return (int)sc; |
---|
112 | } |
---|
113 | /***********************************************************/ |
---|
114 | int main_telnetd(int argc,char * argv[]) { |
---|
115 | rtems_status_code sc; |
---|
116 | if (telnetd_task_id) { |
---|
117 | printf("ERROR:telnetd already started\n"); |
---|
118 | return 1; |
---|
119 | }; |
---|
120 | if (argc>1) telnetd_stack_size =str2int(argv[1]); |
---|
121 | if (argc>2) telnetd_task_priority=str2int(argv[2]); |
---|
122 | sc=rtems_initialize_telnetd(); |
---|
123 | if (sc!=RTEMS_SUCCESSFUL) return sc; |
---|
124 | printf("rtems_telnetd() started with stacksize=%u,priority=%d\n", |
---|
125 | telnetd_stack_size,telnetd_task_priority); |
---|
126 | return 0; |
---|
127 | } |
---|
128 | /***********************************************************/ |
---|
129 | int register_telnetd(void) { |
---|
130 | shell_add_cmd("telnetd","telnet","telnetd [stacksize [tsk_priority]]",main_telnetd); |
---|
131 | return 0; |
---|
132 | } |
---|
133 | /***********************************************************/ |
---|