Update of /cvsroot/netrek/server/Vanilla/tools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13292/tools Modified Files: players.c Log Message: describe a local server as on this computer Index: players.c =================================================================== RCS file: /cvsroot/netrek/server/Vanilla/tools/players.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- players.c 22 Apr 2006 02:16:47 -0000 1.5 +++ players.c 6 May 2006 12:23:30 -0000 1.6 @@ -4,6 +4,8 @@ #include <errno.h> #include <sys/types.h> #include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include "defs.h" #include "struct.h" #include "data.h" @@ -185,16 +187,16 @@ } /* server comment */ -static char *comment() { +static char *comment_get() { #define MAXPATH 256 char name[MAXPATH]; snprintf(name, MAXPATH, "%s/%s", SYSCONFDIR, "comment"); FILE *file = fopen(name, "r"); - if (file == NULL) return ""; + if (file == NULL) return NULL; static char text[80]; char *res = fgets(text, 80, file); fclose(file); - if (res == NULL) return ""; + if (res == NULL) return NULL; res[strlen(res)-1] = '\0'; return res; } @@ -216,10 +218,10 @@ static void udp() { char buf[2]; - struct sockaddr addr; + struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int sock = 0; - int stat = recvfrom(sock, buf, 1, MSG_TRUNC, &addr, &addrlen); + int stat = recvfrom(sock, buf, 1, MSG_TRUNC, (struct sockaddr *) &addr, &addrlen); if (stat < 0 && errno == ENOTSOCK) fprintf(stderr, "players: must be called by netrekd with UDP file descriptor setup\n"); if (stat < 0) { perror("players: recvfrom"); return; } @@ -231,24 +233,35 @@ /* compose a reply packet */ char packet[128]; + /* default the comment */ + char *comment = comment_get(); + if (comment == NULL) { + char *ip = inet_ntoa(addr.sin_addr); + if (!strcmp(ip,"127.0.0.1")) { + comment = "server on this computer"; + } else { + comment = "a nearby netrek server"; + } + } + /* s,type,comment,ports,port,players,queue[,port,players,queue] */ /* if server isn't running, send simple reply */ if (!openmem(-1)) { - sprintf(packet, "s,B,%s,1,2592,%d,%d\n", comment(), 0, 0); + sprintf(packet, "s,B,%s,1,2592,%d,%d\n", comment, 0, 0); } else { if (status->gameup & GU_INROBOT) { int q1 = QU_HOME; int q2 = QU_AWAY; sprintf(packet, "s,B,%s,2,4566,%d,%d,4577,%d,%d\n", - comment(), pc(q1), ql(q1), pc(q2), ql(q2)); + comment, pc(q1), ql(q1), pc(q2), ql(q2)); } else { int q = QU_PICKUP; - sprintf(packet, "s,B,%s,1,2592,%d,%d\n", comment(), pc(q), ql(q)); + sprintf(packet, "s,B,%s,1,2592,%d,%d\n", comment, pc(q), ql(q)); } } /* send the reply */ - stat = sendto(sock, packet, strlen(packet), 0, &addr, addrlen); + stat = sendto(sock, packet, strlen(packet), 0, (struct sockaddr *) &addr, addrlen); if (stat < 0) { perror("players: sendto"); return; } }