1 | /* Copyright Milan Technology, 1991 1992 */ |
---|
2 | |
---|
3 | /* @(#)udpstat.c 2.2 10/16/92 */ |
---|
4 | |
---|
5 | #include "udp.h" |
---|
6 | #include <stdio.h> |
---|
7 | |
---|
8 | #include <sys/types.h> |
---|
9 | |
---|
10 | /* #include <sys/socket.h> */ |
---|
11 | #include <sys/errno.h> |
---|
12 | #ifndef HPOLD |
---|
13 | #include <netinet/in.h> |
---|
14 | #endif |
---|
15 | #include <signal.h> |
---|
16 | #include "dp.h" |
---|
17 | |
---|
18 | void print_status_info(); |
---|
19 | int get_udpstatus(); |
---|
20 | void print_active_connection(); |
---|
21 | |
---|
22 | /* UDP Status packet */ |
---|
23 | |
---|
24 | udp_status_packet udp_stat; |
---|
25 | |
---|
26 | #ifdef ANSI |
---|
27 | int |
---|
28 | get_printer_status(char *printer, char *error_string, int port) |
---|
29 | #else |
---|
30 | int |
---|
31 | get_printer_status(printer, error_string, port) |
---|
32 | char *printer; |
---|
33 | char *error_string; |
---|
34 | int port; |
---|
35 | #endif |
---|
36 | { |
---|
37 | int fastport; |
---|
38 | struct sockaddr_in fp_sock, my_sock; |
---|
39 | unsigned long temp; |
---|
40 | struct hostent *hp; |
---|
41 | int status; |
---|
42 | |
---|
43 | bzero((char *) &fp_sock, sizeof(fp_sock)); |
---|
44 | fp_sock.sin_family = AF_INET; |
---|
45 | |
---|
46 | if (!(hp = gethostbyname(printer))) { |
---|
47 | return(0); |
---|
48 | } |
---|
49 | temp = *(unsigned long *) hp->h_addr; |
---|
50 | fp_sock.sin_addr.s_addr = temp; |
---|
51 | fp_sock.sin_port = htons(35); |
---|
52 | if ((fastport = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
---|
53 | perror("error on socket\n"); |
---|
54 | return(0); |
---|
55 | } |
---|
56 | /* |
---|
57 | * need to be able to receive status so bind the socket |
---|
58 | */ |
---|
59 | bzero((char *) &my_sock, sizeof(my_sock)); |
---|
60 | my_sock.sin_family = AF_INET; |
---|
61 | my_sock.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
62 | my_sock.sin_port = htons(0); |
---|
63 | if (bind(fastport, (struct sockaddr *) & my_sock, sizeof(my_sock)) < 0) { |
---|
64 | perror("can't bind:"); |
---|
65 | return(0); |
---|
66 | } |
---|
67 | status = get_udpstatus(fastport, (struct sockaddr *) & fp_sock, sizeof(fp_sock), |
---|
68 | error_string, port, printer); |
---|
69 | close(fastport); |
---|
70 | return(status); |
---|
71 | |
---|
72 | } |
---|
73 | #ifdef ANSI |
---|
74 | int |
---|
75 | get_udpstatus(int fastport, struct sockaddr * server, int server_length, |
---|
76 | char *error_string, int port, char *fastport_name) |
---|
77 | #else |
---|
78 | int |
---|
79 | get_udpstatus(fastport, server, server_length, error_string, port, fastport_name) |
---|
80 | int fastport; |
---|
81 | struct sockaddr *server; |
---|
82 | int server_length; |
---|
83 | char *error_string; |
---|
84 | int port; |
---|
85 | char *fastport_name; |
---|
86 | #endif |
---|
87 | { |
---|
88 | char command[10]; |
---|
89 | |
---|
90 | int nbytes, sel_val; |
---|
91 | fd_set readfds; |
---|
92 | struct timeval timeout; |
---|
93 | struct sockaddr_in rec_address; |
---|
94 | int reclen; |
---|
95 | timeout.tv_sec = 3; |
---|
96 | timeout.tv_usec = 0; |
---|
97 | |
---|
98 | command[0] = MILAN_OP; /* a milan query */ |
---|
99 | command[1] = GET_STATUS; /* action required */ |
---|
100 | |
---|
101 | if ((nbytes = sendto(fastport, command, sizeof(command), 0, server, server_length)) != |
---|
102 | sizeof(command)) { |
---|
103 | perror("Cannot send to server:"); |
---|
104 | exit(1); |
---|
105 | } |
---|
106 | while (1) { |
---|
107 | FD_ZERO(&readfds); |
---|
108 | FD_SET(fastport, &readfds); |
---|
109 | if ((sel_val = select(fastport + 1, &readfds, 0, 0, &timeout)) < 0) { |
---|
110 | sprintf(error_string, "no response from server \n"); |
---|
111 | return(0); |
---|
112 | } |
---|
113 | if (!sel_val) { |
---|
114 | sprintf(error_string, "no response from server %s for %s port\n", fastport_name, port == 2001 ? "Serial" : "Parallel"); |
---|
115 | return(0);; |
---|
116 | } |
---|
117 | if (FD_ISSET(fastport, &readfds)) |
---|
118 | break; |
---|
119 | } |
---|
120 | |
---|
121 | reclen = sizeof(rec_address); |
---|
122 | if ((nbytes = recvfrom(fastport, (char *)&udp_stat, |
---|
123 | sizeof(udp_stat), 0, |
---|
124 | (struct sockaddr *)&rec_address, &reclen)) < |
---|
125 | 0) { |
---|
126 | sprintf(error_string, "udp recvfrom returned error"); |
---|
127 | return(0); |
---|
128 | } |
---|
129 | print_status_info(&udp_stat, error_string, port); |
---|
130 | return(1); |
---|
131 | } |
---|
132 | #ifdef ANSII |
---|
133 | void |
---|
134 | print_status_info(udp_status_packet * stat_buff, char *error_string, int port) |
---|
135 | #else |
---|
136 | void |
---|
137 | print_status_info(stat_buff, error_string, port) |
---|
138 | udp_status_packet *stat_buff; |
---|
139 | char *error_string; |
---|
140 | int port; |
---|
141 | #endif |
---|
142 | { |
---|
143 | char temp_string[100]; |
---|
144 | struct hostent *hp; |
---|
145 | unsigned long host_addr; |
---|
146 | if (port == PARALLEL) { |
---|
147 | if (stat_buff->parallel_addr) { |
---|
148 | #ifndef SCO |
---|
149 | host_addr = ntohl(stat_buff->parallel_addr); |
---|
150 | #else |
---|
151 | host_addr = stat_buff->parallel_addr; |
---|
152 | #endif |
---|
153 | print_active_connection(&host_addr, "parallel ", error_string); |
---|
154 | sprintf(temp_string, "%s\n", stat_buff->message); |
---|
155 | strcat(error_string, temp_string); |
---|
156 | sprintf(temp_string, "Total parallel bytes sent: %d\n\n", ntohl(stat_buff->parallel_bytes)); |
---|
157 | strcat(error_string, temp_string); |
---|
158 | } else { |
---|
159 | strcat(error_string, "No active parallel connection\n"); |
---|
160 | sprintf(temp_string, "%s\n", stat_buff->message); |
---|
161 | strcat(error_string, temp_string); |
---|
162 | sprintf(temp_string, "Total parallel bytes sent: %d\n\n", ntohl(stat_buff->parallel_bytes)); |
---|
163 | strcat(error_string, temp_string); |
---|
164 | } |
---|
165 | } |
---|
166 | if (port == SERIAL) { |
---|
167 | if (stat_buff->serial_addr) { |
---|
168 | #ifndef SCO |
---|
169 | host_addr = ntohl(stat_buff->serial_addr); |
---|
170 | #else |
---|
171 | host_addr = stat_buff->serial_addr; |
---|
172 | #endif |
---|
173 | print_active_connection(&host_addr, "serial ", error_string); |
---|
174 | sprintf(temp_string, "Total serial bytes sent: %d\n\n", ntohl(stat_buff->serial_bytes)); |
---|
175 | strcat(error_string, temp_string); |
---|
176 | } else { |
---|
177 | strcat(error_string, "No active serial connection\n"); |
---|
178 | sprintf(temp_string, "Total serial bytes sent: %d\n\n", ntohl(stat_buff->serial_bytes)); |
---|
179 | strcat(error_string, temp_string); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | #ifdef ANSI |
---|
185 | void |
---|
186 | print_active_connection(unsigned long *host_address, char *kind, char *error_string) |
---|
187 | #else |
---|
188 | void |
---|
189 | print_active_connection(host_address, kind, error_string) |
---|
190 | unsigned long *host_address; |
---|
191 | char *kind; |
---|
192 | char *error_string; |
---|
193 | #endif |
---|
194 | { |
---|
195 | struct hostent *hp; |
---|
196 | char temp_string[100]; |
---|
197 | #ifdef __STDC__ |
---|
198 | if (hp = gethostbyaddr((const char *)host_address, 4, AF_INET)) { |
---|
199 | #else |
---|
200 | if (hp = gethostbyaddr((char *)host_address, 4, AF_INET)) { |
---|
201 | #endif |
---|
202 | unsigned long newaddr; |
---|
203 | newaddr = *(unsigned long *)hp->h_addr; |
---|
204 | sprintf(temp_string, "\nActive %s data session from %s (%s)\n", kind, hp->h_name, |
---|
205 | inet_ntoa((struct in_addr *) (&newaddr))); |
---|
206 | } else { |
---|
207 | sprintf(error_string, "\nActive %s data session from %s\n", kind, |
---|
208 | inet_ntoa((struct in_addr *) (host_address))); |
---|
209 | return; |
---|
210 | } |
---|
211 | strcat(error_string, temp_string); |
---|
212 | } |
---|