[c5c522c] | 1 | #include <unistd.h> |
---|
| 2 | #include <stddef.h> |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <errno.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <sys/socket.h> |
---|
| 7 | #include <sys/un.h> |
---|
| 8 | #include <sys/time.h> |
---|
| 9 | #include <sys/types.h> |
---|
| 10 | #include <netinet/in.h> |
---|
| 11 | #include <netdb.h> |
---|
| 12 | #include <sys/socket.h> |
---|
| 13 | #include <arpa/inet.h> |
---|
| 14 | #include "sockets.h" |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /************************************************** |
---|
| 18 | LCDproc sockets code... |
---|
| 19 | |
---|
| 20 | This is messy, and needs to be finished. |
---|
| 21 | **************************************************/ |
---|
| 22 | |
---|
| 23 | fd_set active_fd_set, read_fd_set; |
---|
| 24 | int sock; |
---|
| 25 | |
---|
| 26 | // Length of longest transmission allowed at once... |
---|
| 27 | #define MAXMSG 8192 |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | int init_sockaddr (sockaddr_in *name, |
---|
| 32 | const char *hostname, |
---|
| 33 | unsigned short int port) |
---|
| 34 | { |
---|
| 35 | struct hostent *hostinfo; |
---|
| 36 | |
---|
| 37 | name->sin_family = AF_INET; |
---|
| 38 | name->sin_port = htons (port); |
---|
| 39 | hostinfo = gethostbyname (hostname); |
---|
| 40 | if (hostinfo == NULL) |
---|
| 41 | { |
---|
| 42 | fprintf (stderr,"Unknown host %s.\n", hostname); |
---|
| 43 | return -1; |
---|
| 44 | } |
---|
| 45 | name->sin_addr = *(struct in_addr *) hostinfo->h_addr; |
---|
| 46 | |
---|
| 47 | return 0; |
---|
| 48 | |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | #if 0 |
---|
| 53 | // Creates a socket as a file... |
---|
| 54 | int CreateNamedSocket(char *filename) |
---|
| 55 | { |
---|
| 56 | struct sockaddr_un name; |
---|
| 57 | size_t size; |
---|
| 58 | |
---|
| 59 | sock=socket(PF_FILE, SOCK_STREAM, 0); |
---|
| 60 | if(sock < 0) return -1; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | name.sun_family = AF_FILE; |
---|
| 64 | strcpy (name.sun_path, filename); |
---|
| 65 | |
---|
| 66 | /* The size of the address is |
---|
| 67 | the offset of the start of the filename, |
---|
| 68 | plus its length, |
---|
| 69 | plus one for the terminating null byte. */ |
---|
| 70 | size = (offsetof (struct sockaddr_un, sun_path) |
---|
| 71 | + strlen (name.sun_path) + 1); |
---|
| 72 | |
---|
| 73 | if (bind (sock, (struct sockaddr *) &name, size) < 0) |
---|
| 74 | return -1; |
---|
| 75 | |
---|
| 76 | return sock; |
---|
| 77 | } |
---|
| 78 | #endif |
---|
| 79 | |
---|
| 80 | // Creates a socket in internet space |
---|
| 81 | int CreateInetSocket(unsigned short int port) |
---|
| 82 | { |
---|
| 83 | struct sockaddr_in name; |
---|
| 84 | |
---|
| 85 | /* Create the socket. */ |
---|
| 86 | //fprintf(stderr,"Creating Inet Socket\n"); |
---|
| 87 | sock = socket (PF_INET, SOCK_STREAM, 0); |
---|
| 88 | if (sock < 0) |
---|
| 89 | return -1; |
---|
| 90 | |
---|
| 91 | /* Give the socket a name. */ |
---|
| 92 | //fprintf(stderr,"Binding Inet Socket\n"); |
---|
| 93 | name.sin_family = AF_INET; |
---|
| 94 | name.sin_port = htons (port); |
---|
| 95 | name.sin_addr.s_addr = htonl (INADDR_ANY); |
---|
| 96 | if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
| 99 | return sock; |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | // Checks the LCDproc port for a response... |
---|
| 104 | int PingLCDport() |
---|
| 105 | { |
---|
| 106 | struct sockaddr_in servername; |
---|
| 107 | int err=0; |
---|
| 108 | char ping[16] = {0xFE, 0x01, 0}; |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | //fprintf(stderr,"Creating socket (client)\n"); |
---|
| 112 | sock = socket(PF_INET, SOCK_STREAM, 0); |
---|
| 113 | if(sock < 0) |
---|
| 114 | { |
---|
| 115 | fprintf(stderr,"Error creating socket (client)\n"); |
---|
| 116 | return sock; |
---|
| 117 | } |
---|
| 118 | //else fprintf(stderr,"Created socket (%i) (client)\n", sock); |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | init_sockaddr(&servername, "localhost", LCDPORT); |
---|
| 122 | |
---|
| 123 | err = connect (sock, |
---|
| 124 | (struct sockaddr *) &servername, |
---|
| 125 | sizeof (servername)); |
---|
| 126 | if(err<0) |
---|
| 127 | { |
---|
| 128 | //fprintf (stderr,"connect failed (client)\n"); |
---|
| 129 | shutdown(sock, 2); |
---|
| 130 | return 0; // Normal exit if server doesn't exist... |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | err = write (sock, ping, strlen(ping) + 1); |
---|
| 134 | if (err < 0) |
---|
| 135 | { |
---|
| 136 | fprintf (stderr,"socket write error (client)"); |
---|
| 137 | shutdown(sock, 2); |
---|
| 138 | return err; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | shutdown(sock, 2); |
---|
| 142 | |
---|
| 143 | return 1; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | int ConnectAsClient() |
---|
| 147 | { |
---|
| 148 | return 0; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | int StartSocketServer() |
---|
| 152 | { |
---|
| 153 | |
---|
| 154 | /* Create the socket and set it up to accept connections. */ |
---|
| 155 | sock = CreateInetSocket (LCDPORT); |
---|
| 156 | if(sock < 0) |
---|
| 157 | { |
---|
| 158 | fprintf(stderr,"Error creating socket (server)\n"); |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | if (listen (sock, 1) < 0) |
---|
| 164 | { |
---|
| 165 | fprintf (stderr,"Listen error (server)\n"); |
---|
| 166 | return -1; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /* Initialize the set of active sockets. */ |
---|
| 170 | FD_ZERO (&active_fd_set); |
---|
| 171 | FD_SET (sock, &active_fd_set); |
---|
| 172 | |
---|
| 173 | return sock; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | int PollSockets() |
---|
| 178 | { |
---|
| 179 | int i; |
---|
| 180 | struct sockaddr_in clientname; |
---|
| 181 | size_t size; |
---|
| 182 | struct timeval t; |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | t.tv_sec = 0; |
---|
| 186 | t.tv_usec = 0; |
---|
| 187 | |
---|
| 188 | /* Block until input arrives on one or more active sockets. */ |
---|
| 189 | read_fd_set = active_fd_set; |
---|
| 190 | |
---|
| 191 | if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, &t) < 0) |
---|
| 192 | { |
---|
| 193 | fprintf (stderr,"Select error (server)\n"); |
---|
| 194 | return -1; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /* Service all the sockets with input pending. */ |
---|
| 198 | for (i = 0; i < FD_SETSIZE; ++i) |
---|
| 199 | if (FD_ISSET (i, &read_fd_set)) |
---|
| 200 | { |
---|
| 201 | if (i == sock) |
---|
| 202 | { |
---|
| 203 | /* Connection request on original socket. */ |
---|
| 204 | int new; |
---|
| 205 | size = sizeof (clientname); |
---|
| 206 | new = accept (sock, |
---|
| 207 | (struct sockaddr *) &clientname, |
---|
| 208 | &size); |
---|
| 209 | if (new < 0) |
---|
| 210 | { |
---|
| 211 | fprintf (stderr,"Accept error (server)\n"); |
---|
| 212 | return -1; |
---|
| 213 | } |
---|
| 214 | fprintf (stderr,"Server: connect from host %s, port %hd.\n", |
---|
| 215 | inet_ntoa (clientname.sin_addr), |
---|
| 216 | ntohs (clientname.sin_port)); |
---|
| 217 | FD_SET (new, &active_fd_set); |
---|
| 218 | } |
---|
| 219 | else |
---|
| 220 | { |
---|
| 221 | /* Data arriving on an already-connected socket. */ |
---|
| 222 | if (read_from_client (i) < 0) |
---|
| 223 | { |
---|
| 224 | close (i); |
---|
| 225 | FD_CLR (i, &active_fd_set); |
---|
| 226 | fprintf(stderr,"Closed connection %i\n", i); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | return 0; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | int read_from_client (int filedes) |
---|
| 235 | { |
---|
| 236 | char buffer[MAXMSG]; |
---|
| 237 | int nbytes; |
---|
| 238 | |
---|
| 239 | nbytes = read (filedes, buffer, MAXMSG); |
---|
| 240 | buffer[nbytes] = 0; |
---|
| 241 | buffer[nbytes+1] = 0; |
---|
| 242 | |
---|
| 243 | if (nbytes < 0) // Read error |
---|
| 244 | { |
---|
| 245 | fprintf (stderr,"Read error (server)\n"); |
---|
| 246 | return -1; |
---|
| 247 | } |
---|
| 248 | else if (nbytes == 0) // EOF |
---|
| 249 | return -1; |
---|
| 250 | else // Data Read |
---|
| 251 | { |
---|
| 252 | fprintf (stderr,"Server: got message: `%s'\n", buffer); |
---|
| 253 | return 0; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | int CloseAllConnections() |
---|
| 258 | { |
---|
| 259 | int i; |
---|
| 260 | |
---|
| 261 | /* Service all the sockets with input pending. */ |
---|
| 262 | for (i = 0; i < FD_SETSIZE; ++i) |
---|
| 263 | if (FD_ISSET (i, &read_fd_set)) |
---|
| 264 | { |
---|
| 265 | /* Data arriving on an already-connected socket. */ |
---|
| 266 | close (i); |
---|
| 267 | FD_CLR (i, &active_fd_set); |
---|
| 268 | fprintf(stderr,"Closed connection %i\n", i); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | return 0; |
---|
| 272 | |
---|
| 273 | } |
---|