|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T c
Length: 3363 (0xd23) Types: TextFile Names: »connect.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987 └─⟦this⟧ »EUUGD18/General/Kriegspiel/connect.c«
/* connect.c */ /* BUG: Should close sock after accepting. */ #include "externs.h" #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <pwd.h> #include <ctype.h> /* typedef unsigned short uid_t; */ /* Needed on 4.2 */ connectport (opponent, port) char *opponent; int port; { struct sockaddr_in addr; struct hostent *hishost, *myhost; struct passwd *mypasswd, *getpwuid(); char *hishostname, *myhostname, *myname, *index(), *getpw(), *malloc(); uid_t getuid(); int i; if (hishostname = index (opponent, '@')) { hishostname [0] = '\0'; /* separate user and host */ hishostname++; } else { hishostname = malloc (MAXBUFF); if (gethostname (hishostname, MAXBUFF) < 0) error ("gethostname in connectport"); } myhostname = malloc (MAXBUFF); if (gethostname (myhostname, MAXBUFF) < 0) error ("gethostname in connectport"); if ((myhost = (struct hostent *) gethostbyname(myhostname)) == (struct hostent *) NIL) error ("gethostname in connectport"); strcpy (myhostname, myhost -> h_name); lowerstring (myhostname); if ((hishost = (struct hostent *) gethostbyname(hishostname)) == (struct hostent *) NIL) error ("gethostname in connectport"); lowerstring (hishost -> h_name); mypasswd = getpwuid ((int) getuid()); myname = mypasswd -> pw_name; if (iamserver == UNSET) if (i = strcmp (myname, opponent)) iamserver = (i < 0); else if (i = strcmp (myhostname, hishost -> h_name)) iamserver = (i < 0); if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) error ("socket in connectport"); bzero ((char *) &addr, sizeof (addr)); if (port == 0 && iamserver) { hashport (myhostname, &port); hashport (hishost -> h_name, &port); hashport (myname, &port); hashport (opponent, &port); } if (port == 0 && !iamserver) { hashport (hishost -> h_name, &port); hashport (myhostname, &port); hashport (opponent, &port); hashport (myname, &port); } port = port % 50021 + 10000; /* 50021 is prime */ addr.sin_family = AF_INET; addr.sin_port = htons ((u_short) port); if (iamserver) { if (bind (sock, /*(char *)*/ &addr, sizeof (addr)) < 0) if ((errno == EADDRINUSE || errno == ENOTCONN) && iamserver == UNSET) { /* hope other player is server */ iamserver = FALSE; close (sock); if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) error ("socket in connectport"); } else error ("bind in connectport"); if (iamserver && listen (sock, 1) < 0) error ("listen in connectport"); while (iamserver && (sock = accept (sock, (struct sockaddr_in *) NULL, (int *) NULL)) < 0) if (errno != EINTR && errno != EBADF) error ("accept in connectport"); } if (!iamserver) { bcopy (hishost->h_addr, (char *) &addr.sin_addr, hishost->h_length); while (connect (sock, /*(char *)*/ &addr, sizeof (addr)) < 0) if ( errno == EINTR || errno == ECONNREFUSED) { close (sock); if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) error ("socket in connectport"); } else error ("connect in connectport"); } } hashport (s, port) char *s; int *port; { while (*s) *port = ((*port < 0) | (*port << 1)) + *s++; *port &= ~0x80000000; /* make *port nonnegative */ } lowerstring (s) char *s; { while (*s) { if (isupper (*s)) *s = tolower (*s); s++; } }