home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / spiele / FreeCiv / src / freeciv-1.7.0 / server / meta.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  2.6 KB  |  103 lines

  1. /*
  2.  * This bit sends "I'm here" packages to the metaserver.
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include <meta.h>
  14. #include <packets.h>
  15.  
  16. #ifndef INADDR_NONE
  17. #define INADDR_NONE     0xffffffff
  18. #endif
  19.  
  20. extern int errno;
  21. static int            sockfd,n,in_size;
  22. static struct sockaddr_in    cli_addr,serv_addr;
  23.  
  24. void send_to_metaserver(char *desc, char *info)
  25. {
  26.   unsigned char buffer[MAX_PACKET_SIZE], *cptr;
  27.   
  28.   if(sockfd!=0) {
  29.     cptr=put_int16(buffer+2,  PACKET_UDP_PCKT);
  30.     cptr=put_string(cptr, desc);
  31.     cptr=put_string(cptr, info);
  32.     put_int16(buffer, cptr-buffer);
  33.     
  34.     n=sendto(sockfd, buffer, cptr-buffer,0, 
  35.          (struct sockaddr *) &serv_addr, sizeof(serv_addr) );
  36.   }
  37. }
  38.  
  39. void server_close_udp(void)
  40. {
  41.   close(sockfd);
  42. }
  43.  
  44. void server_open_udp(void)
  45. {
  46.   char servername[]=METASERVER_ADDR;
  47.   struct hostent *hp;
  48.   u_int bin;
  49.       
  50.   /*
  51.    * Fill in the structure "serv_addr" with the address of the
  52.    * server that we want to connect with, checks if server address 
  53.    * is valid, both decimal-dotted and name.
  54.    */
  55.   in_size = sizeof(inet_addr(servername));
  56.   bin = inet_addr(servername);
  57.   if ((hp = gethostbyaddr((char *) &bin,in_size,AF_INET)) ==NULL)
  58.     {
  59.       if((hp = gethostbyname(servername)) ==NULL) {
  60.     perror("Metaserver: address error");
  61.     printf("Not reporting to the metaserver in this game\n");
  62.     printf("Use option --nometa to always enforce this\n> ");
  63.     fflush(stdout);
  64.     return;
  65.       }
  66.     }
  67.   memset(&serv_addr, 0, sizeof(serv_addr));
  68.   serv_addr.sin_family      = AF_INET;
  69.   serv_addr.sin_port        = htons(METASERVER_PORT);
  70.   memcpy(&serv_addr.sin_addr, hp->h_addr, hp->h_length); 
  71.   
  72.   /*
  73.    * Open a UDP socket (an Internet datagram socket).
  74.    */
  75.   
  76.   if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  77.     perror("metaserver: can't open datagram socket");
  78.     printf("Not reporting to the metaserver in this game\n");
  79.     printf("Use option --nometa to always enforce this\n> ");
  80.     fflush(stdout);
  81.     return;
  82.   }
  83.   /*
  84.    * Bind any local address for us.
  85.    */
  86.   
  87.   memset( &cli_addr, 0, sizeof(cli_addr));    /* zero out */
  88.   cli_addr.sin_family      = AF_INET;
  89.   cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  90.   cli_addr.sin_port        = htons(0);
  91.   if(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
  92.     perror("metaserver: can't bind local address");
  93.     printf("Not reporting to the metaserver in this game\n");
  94.     printf("Use option --nometa to always enforce this\n> ");
  95.     fflush(stdout);
  96.     return;
  97.   }
  98.  
  99. }
  100.  
  101.  
  102.  
  103.