⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sms_serv.c

📁 手机短消息的服务器端和客户端的源代码 是应用于LINUX/UNIX开发环境的
💻 C
📖 第 1 页 / 共 2 页
字号:
    if ((gsmitem = getnextgsmdev ()) != NULL) {      if (gsmdevcpy (&gsmdevices[i], gsmitem) == -1)        syserr ("sms_serv: error copying GSM instance");      gsmdevices[i].free = TRUE;      gsmdevices[i].owner = 0;    }    else {      syserr ("sms_serv: error while reading config. file");    }  }                                      /* for (i = 0... */  if (i != ngsmdevs) {    syslog ((FACILITY | LOG_INFO), "internal error: expected %d, found %d definition(s)",           ngsmdevs, i);    syserr ("sms_serv: internal error, not all GSM modules accounted for");  }      syslog ((FACILITY | LOG_INFO), "found %d GSM module definition(s)", ngsmdevs);  /*================================Real start of program */  /* get the hostname I'm running on */  localhost = (char *) malloc ((MINIBUFF + 1) * sizeof (char));  if (gethostname (localhost, MINIBUFF) == -1)    syserr ("sms_serv: can't get host name");  /* get the port number we should listen to */  if ((sent = getservbyname ("sms", "tcp")) == NULL)    syserr ("sms_serv: can't get service port info");#ifdef INCL_DEBUG_CODEprintf ("found port <%d> for service sms\n", ntohs (sent->s_port));#endif      /* now fill in the socket address structure */  s_addr.sin_family = AF_INET;  s_addr.sin_port = sent->s_port;  s_addr.sin_addr.s_addr = htonl (INADDR_ANY);  /* INADDR_ANY allows connection to any valid IP address for this     host, including the loopback address. */    /* let's create the socket */  if ((ssfd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP)) == -1)    syserr ("sms_serv: can't create socket");    /* set socket options (INA)*/  optval = 1;  if (setsockopt (ssfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval))) {       syserr ("sms_serv: can't set socket options to reuse");  }  /* load access control list */  acl_list_init (&gsm_acl);  if (! read_acl (&gsm_acl)) {    syserr ("sms_serv: can't read access control list");  }  /* end INA */  /* now bind the socket to the server address */  if (bind (ssfd, (struct sockaddr *) &s_addr, sizeof (s_addr)) == -1)    syserr ("sms_serv: can't bind socket");      /* create the client queue on the socket */  if (listen (ssfd, MAXCLIENTCONN) == -1)    syserr ("sms_serv: can't listen to that many connections");  /* set the flags to make the socket "non-blocking" */  if ((sockflags = fcntl (ssfd, F_GETFL, 0)) == -1)    syserr ("sms_serv: can't get socket descriptor attributes");  if (fcntl (ssfd, F_SETFL, (O_NONBLOCK | sockflags)) == -1)    syserr ("sms_serv: can't set socket descriptor attributes");  /* initialize "last checked" time */  lastchked = time (NULL);  starttime = lastchked;    syslog ((FACILITY | LOG_INFO), "server now ready to answer requests.");  /*---------------------------Start real processing loop */  while (TRUE) {    /* wait for a client to connect */    if ((csfd = accept (ssfd, (struct sockaddr *) &c_addr, &c_addr_len)) == -1) {      /* no connection - check the reason of the "error" */      switch (errno) {        case EWOULDBLOCK:	  /* no error - time to check the mailbox ? */	  if (difftime (time (NULL), lastchked) >= MBCHKINTERVAL) {	    syslog ((FACILITY | LOG_INFO), "initiating mailbox check.");	    /* now fork a child to handle mailbox check */	    pid = fork ();	    switch (pid) {	      case (-1):                         /* error */		syserr ("sms_serv: can't fork");	        break;	      case (0):	        /* ###########< I'm the child >########## */	        /*----Initialize child-specific variables */		/*------------------------------Now do it */		mbcheck_wrapper ();		/*----------------------------Child exits */		exit (0);	        break;           /* Just to be consistent */	      default:	        /* ##########< I'm the father >########## */		/* re-initialize timer */	        lastchked = time (NULL);	        break;	    }                             /* switch (pid) */	  }                            /* if (difftime... */	  break;	default:	  /* there was a "real" error */          syserr ("sms_serv: can't accept incoming connection");	  break;      }                                 /* switch (errno) */    }    else { /* we have a connection - handle it */      /* first check it against the acl (INA) */      if (! check_acl (&c_addr.sin_addr, gsm_acl)) {        syslog ((FACILITY | LOG_WARNING), "illegal connect from [%s]", inet_ntoa (c_addr.sin_addr));        /* close the socket */        if (close (csfd) == -1)          syserr ("sms_serv: can't close client socket");        continue;      }      /* for logging purposes */      syslog ((FACILITY | LOG_NOTICE), "connect from [%s]", inet_ntoa (c_addr.sin_addr));          /* Now fork a child to handle each individ. request & go on listening */      pid = fork ();      switch (pid) {        case (-1):                               /* error */          syserr ("sms_serv: can't fork");	  break;	        case (0):	  /* ###############< I'm the child >############ */	  /*----------Initialize child-specific variables */	  /* allocate space for the symbols */	  symbols.smsc = (char *) malloc ((MAXPHNUMLEN + 1) * sizeof (char));	  symbols.destgsm = (char *) malloc ((MAXPHNUMLEN + 1) * sizeof (char));	  symbols.message = (char *) malloc ((MAXMSGLEN + 1) * sizeof (char));	  symbols.user = (char *) malloc ((MAXUIDLEN + 1) * sizeof (char));	  /* set those symbols to their default values */	  strcpy (symbols.smsc, DEFAULTSMSC);	  symbols.destgsm[0] = '\0';	  symbols.message[0] = '\0';	  symbols.user[0] = '\0';	  /* scratch space for communication with the client */	  buffer = (char *) malloc ((BUFFSIZE + 1) * sizeof (char));	  buffer[0] = '\0';  	  /*------------------------------Server announce */	  sprintf (buffer, "SMS-Link for Linux, ver %s (%s) on %s\r\n", SMS_SERV_VERSION,          	 SMS_SERV_DATE, localhost);	  tellsock (csfd, buffer);	  sprintf (buffer, "(c) Les Ateliers du Heron, 1998 for Scitex Europe, S.A.\r\n"	          "(c) Internet Access AG, 1999\r\n\r\n");	  tellsock (csfd, buffer);	  /* enter the parser module - handle dialogue with client */	  while (1) {            /* display prompt */            sprintf (buffer, PROMPT);            tellsock (csfd, buffer);	    /* get client data */	    buffer[0] = '\0';	    nread = read (csfd, buffer, BUFFSIZE);	    if (nread == -1)	      syserr ("sms_serv: error while reading from socket");	    buffer[nread] = '\0';     /* may not be needed, but can't harm */            /* prepare variables to be used by flex in the new YYINPUT */	    myinputptr = buffer;	    myinputlim = buffer + strlen (buffer);	    /* parse client data */	    parser ();	  }                                  /* while (1) */	  /*--------------------------Close client socket */	  if (close (csfd) == -1)	    syserr ("sms_serv: can't close client socket");	  /*---------------------------------Child exists */	  exit (0);	  break; /* just to be consistent */              default:  	  /* #############< I'm the father >############# */	  /*--------------------------Close client socket */	  if (close (csfd) == -1)	    syserr ("sms_serv: can't close client socket");	  break;	      }                                   /* switch (pid) */    }                           /* if accept() .. else .. */    /* give the CPU some rest */    sleep (1);  }                                          /* while (1) */  /*------------------------------------------Conclusions */  /* close server socket */  if (close (ssfd) == -1)    syserr ("sms_serv: can't close server socket");      /* let's clean what's allocated on the heap */  free (buffer);  free (destgsm);  free (message);  free (localhost);  /*------------------------------------------End program */  exit (0);}                                              /* main () *//*========================================================== * EOF : sms_serv.c *===================*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -