2 * WINGs server.c: example how to create a network server using WMConnection
4 * Copyright (c) 2001 Dan Pascu
13 #include <WINGs/WINGs.h>
17 #define MAXCMD_SIZE 512
20 char *SEConnectionShouldBeRemovedNotification
= "SEConnectionShouldBeRemovedNotification";
25 static void didReceiveInput(ConnectionDelegate
*self
, WMConnection
*cPtr
);
27 static void connectionDidDie(ConnectionDelegate
*self
, WMConnection
*cPtr
);
29 static void connectionDidTimeout(ConnectionDelegate
*self
, WMConnection
*cPtr
);
32 extern char *SEConnectionShouldBeRemovedNotification
;
34 static WMUserDefaults
*timeDB
= NULL
;
35 static char *ServerAddress
= NULL
;
36 static char *ServerPort
= NULL
;
37 static WMArray
*allowedHostList
= NULL
;
38 static WMArray
*clientConnections
= NULL
;
39 static WMConnection
*serverPtr
= NULL
;
43 static ConnectionDelegate socketDelegate
= {
44 NULL
, /* client data */
45 NULL
, /* didCatchException */
46 connectionDidDie
, /* didDie */
47 NULL
, /* didInitialize */
48 didReceiveInput
, /* didReceiveInput */
49 connectionDidTimeout
/* didTimeout */
55 wAbort(Bool foo
) /*FOLD00*/
62 printHelp(char *progname
) /*FOLD00*/
64 printf(_("usage: %s [options]\n\n"), progname
);
65 puts(_(" --help print this message"));
66 puts(_(" --listen [address:]port only listen on the specified address/port"));
67 puts(_(" --allow host1[,host2...] only allow connections from listed hosts\n"));
68 puts(_(" By default server listens on all interfaces and port 34567, unless"
69 " something\nelse is specified with the --listen option. If address is"
70 " omitted or the keyword\n'Any' is used, it will listen on all interfaces else"
71 " only on the specified one.\n\nFor example --listen localhost: will"
72 " listen on the default port 34567, but only\non connections comming"
73 " in through the loopback interface.\n\n Also by default the server"
74 " listens to incoming connections from any host,\nunless a list of"
75 " hosts is given with the --allow option, in which case it will\nreject"
76 " connections not comming from those hosts.\nThe list of hosts is comma"
77 " separated and should NOT contain ANY spaces."));
82 enqueueConnectionForRemoval(WMConnection
*cPtr
)
84 WMNotification
*notif
;
86 /*don't release notif here. it will be released by queue after processing */
87 notif
= WMCreateNotification(SEConnectionShouldBeRemovedNotification
,
89 WMEnqueueNotification(WMGetDefaultNotificationQueue(), notif
, WMPostASAP
);
94 sendMessage(WMConnection
*cPtr
, char *message
)
99 if (WMGetConnectionState(cPtr
)!=WCConnected
)
102 aData
= WMCreateDataWithBytes(message
, strlen(message
));
103 res
= WMSendConnectionData(cPtr
, aData
);
104 WMReleaseData(aData
);
111 enqueueMessage(WMConnection
*cPtr
, char *message
)
116 if (WMGetConnectionState(cPtr
)!=WCConnected
)
119 aData
= WMCreateDataWithBytes(message
, strlen(message
));
120 res
= WMEnqueueConnectionData(cPtr
, aData
);
121 WMReleaseData(aData
);
127 static unsigned char*
128 findDelimiter(unsigned char *data
, unsigned const char *endPtr
)
130 wassertrv(data
< endPtr
, NULL
);
132 while (data
<endPtr
&& *data
!='\n' && *data
!='\r' && *data
!=';' && *data
!='\0')
143 getAvailableMessages(WMConnection
*cPtr
)
145 char *ptr
, *crtPos
, *buffer
;
146 const char *bytes
, *endPtr
;
147 WMData
*aData
, *receivedData
, *holdData
;
152 receivedData
= WMGetConnectionAvailableData(cPtr
);
155 if ((length
=WMGetDataLength(receivedData
))==0) {
156 WMReleaseData(receivedData
);
160 holdData
= (WMData
*)WMGetConnectionClientData(cPtr
);
162 WMAppendData(holdData
, receivedData
);
163 WMReleaseData(receivedData
);
164 WMSetConnectionClientData(cPtr
, NULL
);
167 aData
= receivedData
;
170 length
= WMGetDataLength(aData
);
171 bytes
= (char*)WMDataBytes(aData
);
172 endPtr
= bytes
+ length
;
174 messages
= WMCreateArrayWithDestructor(1, wfree
);
175 crtPos
= (char*)bytes
;
176 while (crtPos
<endPtr
&& (ptr
= findDelimiter(crtPos
, endPtr
))!=NULL
) {
177 range
.position
= (crtPos
- bytes
);
178 range
.count
= (ptr
- crtPos
);
179 if (range
.count
> MAXCMD_SIZE
) {
180 /* Hmmm... The message is too long. Possibly that someone is
181 * flooding us, or there is a dumb client which do not know
182 * who is talking to. */
183 sendMessage(cPtr
, "Command too long\n\r");
184 WMFreeArray(messages
);
185 WMReleaseData(aData
);
186 WMCloseConnection(cPtr
);
187 enqueueConnectionForRemoval(cPtr
);
190 buffer
= wmalloc(range
.count
+1);
191 WMGetDataBytesWithRange(aData
, buffer
, range
);
192 buffer
[range
.count
] = '\0';
193 WMAddToArray(messages
, buffer
);
195 while (crtPos
<endPtr
&& (*crtPos
=='\n' || *crtPos
=='\r' ||
196 *crtPos
=='\t' || *crtPos
=='\0' ||
197 *crtPos
==';' || *crtPos
==' ')) {
203 range
.position
= (crtPos
- bytes
);
204 range
.count
= (endPtr
- crtPos
);
205 if (range
.count
> MAXCMD_SIZE
) {
206 /* Flooooooding!!!! */
207 sendMessage(cPtr
, "Message too long\n\r");
208 WMFreeArray(messages
);
209 WMReleaseData(aData
);
210 WMCloseConnection(cPtr
);
211 enqueueConnectionForRemoval(cPtr
);
214 holdData
= WMGetSubdataWithRange(aData
, range
);
215 WMSetConnectionClientData(cPtr
, holdData
);
217 WMReleaseData(aData
);
219 if (WMGetArrayItemCount(messages
)==0) {
220 WMFreeArray(messages
);
229 complainAboutBadArgs(WMConnection
*cPtr
, char *cmdName
, char *badArgs
) /*FOLD00*/
231 char *buf
= wmalloc(strlen(cmdName
) + strlen(badArgs
) + 100);
233 sprintf(buf
, _("Invalid parameters '%s' for command %s. Use HELP for"
234 " a list of commands.\n"), badArgs
, cmdName
);
235 sendMessage(cPtr
, buf
);
241 sendUpdateMessage(WMConnection
*cPtr
, char *id
, int time
) /*FOLD00*/
243 char *buf
= wmalloc(strlen(id
) + 100);
245 sprintf(buf
, "%s has %i minutes left\n", id
, time
);
246 sendMessage(cPtr
, buf
);
252 showId(WMConnection
*cPtr
)
254 sendMessage(cPtr
, "Server example based on WMConnection\n");
259 showHelp(WMConnection
*cPtr
) /*FOLD00*/
261 char *buf
= wmalloc(strlen(WMGetApplicationName()) + 16);
263 sprintf(buf
, _("%s commands:\n\n"), WMGetApplicationName());
265 enqueueMessage(cPtr
, _("\n"));
266 enqueueMessage(cPtr
, buf
);
267 enqueueMessage(cPtr
, _("GET <id>\t- return time left (in minutes) "
268 "for user with id <id>\n"));
269 enqueueMessage(cPtr
, _("SET <id> <time>\t- set time limit to <time> "
270 "minutes for user with id <id>\n"));
271 enqueueMessage(cPtr
, _("ADD <id> <time>\t- add <time> minutes "
272 "for user with id <id>\n"));
273 enqueueMessage(cPtr
, _("SUB <id> <time>\t- subtract <time> minutes "
274 "for user with id <id>\n"));
275 enqueueMessage(cPtr
, _("REMOVE <id>\t- remove time limitations for "
276 "user with id <id>\n"));
277 enqueueMessage(cPtr
, _("LIST\t\t- list all users and their "
278 "corresponding time limit\n"));
279 enqueueMessage(cPtr
, _("ID\t\t- returns the Time Manager "
280 "identification string\n"));
281 enqueueMessage(cPtr
, _("EXIT\t\t- exits session\n"));
282 enqueueMessage(cPtr
, _("QUIT\t\t- exits session\n"));
283 enqueueMessage(cPtr
, _("HELP\t\t- show this message\n\n"));
284 /* Just flush the queue we made before */
285 WMFlushConnection(cPtr
);
291 listUsers(WMConnection
*cPtr
)
297 userList
= WMGetUDAllKeys(timeDB
);
299 for (i
=0; i
<PLGetNumberOfElements(userList
); i
++) {
300 id
= PLGetString(PLGetArrayElement(userList
, i
));
301 time
= WMGetUDIntegerForKey(timeDB
, id
);
302 sendUpdateMessage(cPtr
, id
, time
);
310 setTimeForUser(WMConnection
*cPtr
, char *cmdArgs
) /*FOLD00*/
315 id
= wmalloc(strlen(cmdArgs
));
316 if (sscanf(cmdArgs
, "%s %d", id
, &time
)!=2) {
317 complainAboutBadArgs(cPtr
, "SET", cmdArgs
);
324 WMSetUDIntegerForKey(timeDB
, time
, id
);
326 for (i
=0; i
<WMGetArrayItemCount(clientConnections
); i
++) {
327 cPtr
= WMGetFromArray(clientConnections
, i
);
328 sendUpdateMessage(cPtr
, id
, time
);
335 addTimeToUser(WMConnection
*cPtr
, char *cmdArgs
) /*FOLD00*/
338 int i
, time
, newTime
;
340 id
= wmalloc(strlen(cmdArgs
));
341 if (sscanf(cmdArgs
, "%s %d", id
, &time
)!=2) {
342 complainAboutBadArgs(cPtr
, "ADD", cmdArgs
);
347 newTime
= WMGetUDIntegerForKey(timeDB
, id
) + time
;
351 WMSetUDIntegerForKey(timeDB
, newTime
, id
);
353 for (i
=0; i
<WMGetArrayItemCount(clientConnections
); i
++) {
354 cPtr
= WMGetFromArray(clientConnections
, i
);
355 sendUpdateMessage(cPtr
, id
, newTime
);
362 subTimeFromUser(WMConnection
*cPtr
, char *cmdArgs
) /*FOLD00*/
365 int i
, time
, newTime
;
367 id
= wmalloc(strlen(cmdArgs
));
368 if (sscanf(cmdArgs
, "%s %d", id
, &time
)!=2) {
369 complainAboutBadArgs(cPtr
, "SUB", cmdArgs
);
374 newTime
= WMGetUDIntegerForKey(timeDB
, id
) - time
;
378 WMSetUDIntegerForKey(timeDB
, newTime
, id
);
380 for (i
=0; i
<WMGetArrayItemCount(clientConnections
); i
++) {
381 cPtr
= WMGetFromArray(clientConnections
, i
);
382 sendUpdateMessage(cPtr
, id
, newTime
);
389 removeTimeForUser(WMConnection
*cPtr
, char *cmdArgs
) /*FOLD00*/
394 if (cmdArgs
[0]=='\0') {
395 sendMessage(cPtr
, _("Missing parameter for command REMOVE."
396 " Use HELP for a list of commands.\n"));
401 while (*ptr
&& *ptr
!=' ' && *ptr
!='\t')
405 WMRemoveUDObjectForKey(timeDB
, cmdArgs
);
407 for (i
=0; i
<WMGetArrayItemCount(clientConnections
); i
++) {
408 cPtr
= WMGetFromArray(clientConnections
, i
);
409 sendUpdateMessage(cPtr
, cmdArgs
, -1);
415 getTimeForUser(WMConnection
*cPtr
, char *cmdArgs
) /*FOLD00*/
420 if (cmdArgs
[0]=='\0') {
421 sendMessage(cPtr
, _("Missing parameter for command GET."
422 " Use HELP for a list of commands.\n"));
427 while (*ptr
&& *ptr
!=' ' && *ptr
!='\t')
431 if (WMGetUDObjectForKey(timeDB
, cmdArgs
)!=NULL
)
432 time
= WMGetUDIntegerForKey(timeDB
, cmdArgs
);
436 sendUpdateMessage(cPtr
, cmdArgs
, time
);
441 handleConnection(WMConnection
*cPtr
)
443 char *command
, *ptr
, *cmdArgs
, *buffer
;
447 commands
= getAvailableMessages(cPtr
);
451 for (i
=0; i
<WMGetArrayItemCount(commands
); i
++) {
452 command
= WMGetFromArray(commands
, i
);
453 while (*command
&& (*command
==' ' || *command
=='\t'))
456 while(*ptr
&& *ptr
!=' ' && *ptr
!='\t')
462 while (*ptr
&& (*ptr
==' ' || *ptr
=='\t'))
467 fprintf(stderr
, "Command: '%s', args: '%s'\n", command
, cmdArgs
);
469 if (strcasecmp(command
, "quit")==0 || strcasecmp(command
, "exit")==0) {
470 sendMessage(cPtr
, "Bye\n");
471 WMCloseConnection(cPtr
);
472 enqueueConnectionForRemoval(cPtr
);
473 WMFreeArray(commands
);
475 } else if (strcasecmp(command
, "id")==0) {
477 } else if (strcasecmp(command
, "help")==0) {
479 } else if (strcasecmp(command
, "list")==0) {
481 } else if (strcasecmp(command
, "set")==0) {
482 setTimeForUser(cPtr
, cmdArgs
);
483 } else if (strcasecmp(command
, "add")==0) {
484 addTimeToUser(cPtr
, cmdArgs
);
485 } else if (strcasecmp(command
, "sub")==0) {
486 subTimeFromUser(cPtr
, cmdArgs
);
487 } else if (strcasecmp(command
, "remove")==0) {
488 removeTimeForUser(cPtr
, cmdArgs
);
489 } else if (strcasecmp(command
, "get")==0) {
490 getTimeForUser(cPtr
, cmdArgs
);
492 buffer
= wmalloc(strlen(command
) + 100);
493 sprintf(buffer
, _("Unknown command '%s'. Try HELP for"
494 " a list of commands.\n"), command
);
495 sendMessage(cPtr
, buffer
);
500 WMFreeArray(commands
);
505 isAllowedToConnect(WMConnection
*cPtr
)
510 if (allowedHostList
== NULL
)
511 return True
; /* No list. Allow all by default */
513 hPtr
= WMGetHostWithAddress(WMGetConnectionAddress(cPtr
));
514 for (i
=0; i
<WMGetArrayItemCount(allowedHostList
); i
++) {
515 if (WMIsHostEqualToHost(hPtr
, WMGetFromArray(allowedHostList
, i
))) {
528 didReceiveInput(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
530 if (cPtr
== serverPtr
) {
531 WMConnection
*newPtr
= WMAcceptConnection(cPtr
);
534 if (isAllowedToConnect(newPtr
)) {
535 WMSetConnectionDelegate(newPtr
, &socketDelegate
);
536 WMSetConnectionSendTimeout(newPtr
, 120);
537 WMAddToArray(clientConnections
, newPtr
);
539 sendMessage(newPtr
, "Sorry, you are not allowed to connect.\n");
540 WMDestroyConnection(newPtr
);
544 /* Data arriving on an already-connected socket */
545 handleConnection(cPtr
);
551 connectionDidTimeout(ConnectionDelegate
*self
, WMConnection
*cPtr
) /*FOLD00*/
555 if (cPtr
== serverPtr
) {
556 wfatal(_("The server listening socket did timeout. Exiting."));
560 hPtr
= WMGetHostWithAddress(WMGetConnectionAddress(cPtr
));
561 wwarning(_("Connection with %s did timeout. Closing connection."),
562 WMGetHostName(hPtr
));
565 enqueueConnectionForRemoval(cPtr
);
570 connectionDidDie(ConnectionDelegate
*self
, WMConnection
*cPtr
)
572 if (cPtr
== serverPtr
) {
573 /* trouble. server listening port itself died!!! */
574 wfatal(_("The server listening socket died. Exiting."));
578 enqueueConnectionForRemoval(cPtr
);
583 removeConnection(void *observer
, WMNotification
*notification
)
585 WMConnection
*cPtr
= (WMConnection
*)WMGetNotificationObject(notification
);
588 WMRemoveFromArray(clientConnections
, cPtr
);
589 if ((data
= (WMData
*)WMGetConnectionClientData(cPtr
))!=NULL
)
591 WMDestroyConnection(cPtr
);
597 isDifferent(char *str1
, char *str2
) /*FOLD00*/
599 if ((!str1
&& !str2
) || (str1
&& str2
&& strcmp(str1
, str2
)==0))
608 main(int argc
, char **argv
) /*FOLD00*/
614 WMInitializeApplication("server", &argc
, argv
);
617 for (i
=1; i
<argc
; i
++) {
618 if (strcmp(argv
[i
], "--help")==0) {
621 } else if (strcmp(argv
[i
], "--listen")==0) {
624 if ((p
= strchr(argv
[++i
], ':')) != NULL
) {
626 ServerAddress
= wstrdup(argv
[i
]);
627 ServerPort
= wstrdup(p
+1);
629 if (ServerAddress
[0] == 0) {
630 wfree(ServerAddress
);
631 ServerAddress
= NULL
;
633 if (ServerPort
[0] == 0) {
635 ServerPort
= "34567";
637 } else if (argv
[i
][0]!=0) {
638 ServerPort
= argv
[i
];
640 } else if (strcmp(argv
[i
], "--allow")==0) {
648 if ((p
= strchr(ptr
, ',')) != NULL
) {
652 hPtr
= WMGetHostWithName(ptr
);
654 if (!allowedHostList
)
655 allowedHostList
= WMCreateArray(4);
656 WMAddToArray(allowedHostList
, hPtr
);
658 wwarning(_("Unknown host '%s'. Ignored."), ptr
);
670 printf(_("%s: invalid argument '%s'\n"), argv
[0], argv
[i
]);
671 printf(_("Try '%s --help' for more information\n"), argv
[0]);
677 timeDB
= WMGetDefaultsFromPath("./UserTime.plist");
679 clientConnections
= WMCreateArray(4);
681 /* A NULL ServerAddress means to listen on any address the host has.
682 * Else if ServerAddress points to a specific address (like "localhost",
683 * "host.domain.com" or "192.168.1.1"), then it will only listen on that
684 * interface and ignore incoming connections on the others. */
685 if (ServerAddress
&& strcasecmp(ServerAddress
, "Any")==0)
686 ServerAddress
= NULL
;
687 if (ServerPort
==NULL
)
688 ServerPort
= "34567";
690 printf("Server will listen on '%s:%s'\n", ServerAddress
?ServerAddress
:"Any",
692 printf("This server will allow connections from:");
693 if (allowedHostList
) {
697 for (i
=0; i
<WMGetArrayItemCount(allowedHostList
); i
++) {
698 hName
= WMGetHostName(WMGetFromArray(allowedHostList
, i
));
699 printf("%s'%s'", i
==0?" ":", ", hName
);
703 printf(" any host.\n");
706 serverPtr
= WMCreateConnectionAsServerAtAddress(ServerAddress
, ServerPort
,
710 wfatal("could not create server on `%s:%s`. Exiting.",
711 ServerAddress
? ServerAddress
: "localhost", ServerPort
);
715 WMSetConnectionDelegate(serverPtr
, &socketDelegate
);
717 WMAddNotificationObserver(removeConnection
, NULL
,
718 SEConnectionShouldBeRemovedNotification
, NULL
);
721 /* The ASAP notification queue is called at the end of WHandleEvents()
722 * There's where died connections we get while running through
723 * WHandleEvents() get removed. */