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

📄 server.y

📁 手机短消息的服务器端和客户端的源代码 是应用于LINUX/UNIX开发环境的
💻 Y
📖 第 1 页 / 共 3 页
字号:
    retries = MAXATTEMPTS;    while ((strstr (scratch, "READY") == NULL) && retries--) {      /* not ready yet -> asking for PIN or PUK ? */      if (strstr (scratch, "SIM PIN")) {        /* send PIN */	sprintf (scratch, "at+cpin=%s\r", gsm->PIN);	tell_gsm (fd, scratch);	memset (scratch, 0, (BIGBUFF + 1));	if (get_gsm_answer (fd, scratch, BIGBUFF, 15)) {          tellsock (csfd, scratch);	  /* check for "OK" */	  if (strstr (scratch, "OK") == NULL) {	    mdm_unlock (mdmopendevice);	    hangup (fd);	    free (scratch);            syslog ((FACILITY | LOG_ERR), "can't send PIN to GSM or wrong PIN.");	    mdmperror ("sms_serv: can't send PIN to GSM or wrong PIN");	    return (-1);	  }	}	else {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);          syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	  mdmperror ("sms_serv: GSM not responding");	  return (-1);	}      }      else if (strstr (scratch, "SIM PUK")) {        /* send PUK - set new random PIN */	srand (getpid ());	newpin = rand ();	syslog ((FACILITY | LOG_WARNING), "I'll try to set new PIN for </dev/%s>.", gsm->device);	sprintf (scratch, "at+cpin=\"%s\",\"%04d\"\r", gsm->PUK, newpin);	tell_gsm (fd, scratch);	memset (scratch, 0, (BIGBUFF + 1));	if (get_gsm_answer (fd, scratch, BIGBUFF, 20)) {          tellsock (csfd, scratch);	  /* check for "OK" */	  if (strstr (scratch, "OK") == NULL) {	    mdm_unlock (mdmopendevice);	    hangup (fd);	    free (scratch);            syslog ((FACILITY | LOG_ERR), "can't send PUK to GSM or wrong PUK.");	    mdmperror ("sms_serv: can't send PUK to GSM or wrong PUK");	    return (-1);	  }	}	else {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);          syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	  mdmperror ("sms_serv: GSM not responding");	  return (-1);	}	/* store new PIN in conf. file (GSMDEVFILE) */	/* log it */	syslog ((FACILITY | LOG_WARNING), "new PIN for </dev/%s> set to [%04d].", gsm->device, newpin);      }      else {                                     /* ERROR */	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "unable to get SIM status info.");	mdmperror ("sms_serv: unable to get SIM status info");	return (-1);      }                 /* if "PIN" elseif "PUK" else ... */      /* query the status again */      sprintf (scratch, "at+cpin?\r");      tell_gsm (fd, scratch);      memset (scratch, 0, (BIGBUFF + 1));      if (!get_gsm_answer (fd, scratch, BIGBUFF, 1)) {	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	mdmperror ("sms_serv: GSM not responding");	return (-1);      }      tellsock (csfd, scratch);    }                                 /* while (!"READY") */  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*------------set "no notify for incoming SMS messages" */  sprintf (scratch, "at+cnmi=0,0,0,0,0\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {    tellsock (csfd, scratch);    /* check for "OK" */    if (strstr (scratch, "OK") == NULL) {      mdm_unlock (mdmopendevice);      hangup (fd);      free (scratch);      syslog ((FACILITY | LOG_ERR), "error after sending AT+CNMI command.");      mdmperror ("sms_serv: error after sending AT+CNMI command");      return (-1);    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*--------------Check stored SCA against requested SMSC */  /* Which SMSC do we require ? */  if (strcmp (symbols.smsc, DEFAULTSMSC) == 0) {    /* user kept default value - let's use device-level default */    strcpy (reqsca, gsm->defsca);  }  else {    /* user requested specific SMSC - let's use it */    strcpy (reqsca, symbols.smsc);  }  sprintf (scratch, "at+csca?\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {    tellsock (csfd, scratch);    /* compare with requested SMSC */    if (strstr (scratch, reqsca) == NULL) {      /* let's set it then */      sprintf (scratch, "at+csca=\"%s\"\r", reqsca);      tell_gsm (fd, scratch);      memset (scratch, 0, (BIGBUFF + 1));      if (get_gsm_answer (fd, scratch, BIGBUFF, 2)) {        tellsock (csfd, scratch);	/* check for "OK" */	if (strstr (scratch, "OK") == NULL) {	  mdm_unlock (mdmopendevice);	  hangup (fd);	  free (scratch);          syslog ((FACILITY | LOG_ERR), "error after sending AT+CSCA= command.");	  mdmperror ("sms_serv: error after sending AT+CSCA= command");	  return (-1);	}      }      else {	mdm_unlock (mdmopendevice);	hangup (fd);	free (scratch);        syslog ((FACILITY | LOG_ERR), "GSM module not responding.");	mdmperror ("sms_serv: GSM not responding");	return (-1);      }    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*---------------------Am I registered on the network ? */  sprintf (scratch, "at+creg?\r");  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 1)) {    tellsock (csfd, scratch);    /* check for "0,1" status */    if (strstr (scratch, "+CREG: 0,1") == NULL) {      mdm_unlock (mdmopendevice);      hangup (fd);      free (scratch);      syslog ((FACILITY | LOG_ERR), "not registered on the network - can't send.");      mdmperror ("sms_serv: not registered on the network - can't send");      return (-1);    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }    /*------------------------Now actually send SMS message */  sprintf (scratch, "at+cmgs=\"%s\"\r%s%s", symbols.destgsm, symbols.message, CTRL_Z);  tell_gsm (fd, scratch);  memset (scratch, 0, (BIGBUFF + 1));  if (get_gsm_answer (fd, scratch, BIGBUFF, 4)) {    tellsock (csfd, scratch);    /* check for "OK" */    if (strstr (scratch, "OK") == NULL) {      mdm_unlock (mdmopendevice);      hangup (fd);      free (scratch);      syslog ((FACILITY | LOG_ERR), "error when sending SMS message.");      mdmperror ("sms_serv: error when sending SMS message");      return (-1);    }  }  else {    mdm_unlock (mdmopendevice);    hangup (fd);    free (scratch);    syslog ((FACILITY | LOG_ERR), "GSM module not responding.");    mdmperror ("sms_serv: GSM not responding");    return (-1);  }  /* we have the answer - now extract the message ID from it */  if ((ptr = strstr (scratch, "+CMGS:")) == NULL) {  }  ptr += 6;                        /* jumps to next space */  cmsgid = strtok (ptr, " \t\n\r");  msgid = atoi (cmsgid);      /*----------------------------------Close communication */  mdm_unlock (mdmopendevice);  hangup (fd);  free (scratch);  return (msgid);}                                          /* send_sms () *//*========================================================*/void sms_send_wrapper (){  extern int global_sem;  extern int shmem_sem;  extern struct gsms_def *gsmdevices;  int nwaitsecs = 0;  int last_errno;  int retval;  /* int instance; */  char device[MAXDEVLEN];  struct gsms_def gsm;  int msgid;  struct sigaction sa_c;    sprintf (buffer, "Waiting for a free GSM instance...\r\n");  tellsock (csfd, buffer);  sprintf (buffer, ".");    while (((retval = sem_decreq (global_sem)) == -1) &&        (errno == EAGAIN) &&	(nwaitsecs < M_TIMEOUT)) {    sleep (W_STEP);    nwaitsecs += W_STEP;    tellsock (csfd, buffer);               /* display dot */  }                                        /* while (...) */  last_errno = errno;  sprintf (buffer, "\r\n");  tellsock (csfd, buffer);    if (retval == -1) {            /* failed to get a modem */    switch (last_errno) {      case EAGAIN: {        syslog ((FACILITY | LOG_WARNING), "timeout expired (all GSMs busy).");        sprintf (buffer, "timeout expired (all GSMs busy) - try again later\r\n");        tellsock (csfd, buffer);        break;      }      default: {        syserr ("sms_serv: can't decrease global semaphore");	break;      }    }                              /* switch (last_errno) */  }  else {                 /* ---- Begin Crit. Sect. #1 --- */    /* at least 1 GSM module is free - find it */    if (sem_wait (shmem_sem) == -1)      syserr ("sms_serv: failed to wait on shared mem. semaphore");    /* ---- Begin Crit. Sect. #2 ---- */    /* Signal Handling */    sa_c.sa_handler = unlock_gsm;    if (sigfillset (&sa_c.sa_mask) == -1)      syserr ("sms_serv: can't fill signal set");    sa_c.sa_flags = 0;    /* catch SIGTERM to handle child's death while GSM's locked */    if (sigaction (SIGTERM, &sa_c, NULL) == -1)      syserr ("sms_serv: can't catch SIGTERM");    /* catch SIGPIPE to handle client disconnect while GSM's locked */    if (sigaction (SIGPIPE, &sa_c, NULL) == -1)      syserr ("sms_serv: can't catch SIGPIPE");    /* search free instance */    instance = 0;    while (instance < ngsmdevs) {      if (gsmdevices[instance].free) {        /* let's process this one */	break;      }      instance++;    }                               /* while (instance...) */    if (instance >= ngsmdevs)      fatal ("sms_serv: can't get a free GSM - internal error");        /* got it - now lock it (device lockfile handled by libmodem) */    instance_is_set = TRUE;    gsmdevices[instance].free = FALSE;    gsmdevices[instance].owner = getpid ();        /* copy to "local" space, to avoid the shmem balagan */    if (gsmdevcpy (&gsm, &gsmdevices[instance]) == -1)      syserr ("sms_serv: error copying GSM instance");        /* leave crit. sect. */    if (sem_signal (shmem_sem) == -1)      syserr ("sms_serv: can't signal shared mem. semaphore");    /* ---- End Crit. Sect. #2 ---- */    /* log it */    syslog ((FACILITY | LOG_NOTICE), "using device </dev/%s>.", gsm.device);    sprintf (buffer, "got </dev/%s>, now sending...\r\n", gsm.device);    tellsock (csfd, buffer);        /* ---->>> Actual SEND <<<---- */    if ((msgid = send_sms (&gsm)) != -1) {      syslog ((FACILITY | LOG_NOTICE), "message sent OK, message ID is <%d>.", msgid);      sprintf (buffer, "message sent OK, message ID is <%d>\r\n", msgid);      tellsock (csfd, buffer);    }    else { /* some error occured */      syslog ((FACILITY | LOG_WARNING), "ERROR: failed to send message.");      sprintf (buffer, "ERROR: failed to send message - try again later\r\n");      tellsock (csfd, buffer);    }        /* now free the modem again */    if (sem_wait (shmem_sem) == -1)      syserr ("sms_serv: failed to wait on shared mem. semaphore");    /* ---- Begin Crit. Sect. #3 ---- */    gsmdevices[instance].free = TRUE;    gsmdevices[instance].owner = 0;    instance_is_set = FALSE;    /* leave crit. sect. */    if (sem_signal (shmem_sem) == -1)      syserr ("sms_serv: can't signal shared mem. semaphore");    /* ---- End Crit. Sect. #3 ---- */    /* leave main crit. sect. */    if (sem_signal (global_sem) == -1)      syserr ("sms_serv: can't signal global semaphore");  }                        /* ---- End Crit. Sect. #1 --- */}                                  /* sms_send_wrapper () *//*========================================================*/void unlock_gsm (){  extern int shmem_sem;  extern struct gsms_def *gsmdevices;  syslog ((FACILITY | LOG_WARNING), "Socket connection unexpectedly lost.");  /* set the allocated GSM module back to 'free' status */  if (instance_is_set) {    if (sem_wait (shmem_sem) == -1)      syserr ("sms_serv: failed to wait on shared mem. semaphore");    /* ---- Begin Crit. Sect. #5 ---- */    if (!gsmdevices[instance].free) {      gsmdevices[instance].free = TRUE;      gsmdevices[instance].owner = 0;      syslog ((FACILITY | LOG_WARNING), "GSM instance </dev/%s> has been unlocked.",             gsmdevices[instance].device);    }    /* leave crit. sect. */    if (sem_signal (shmem_sem) == -1)      syserr ("sms_serv: can't signal shared mem. semaphore");    /* ---- End Crit. Sect. #5 ---- */  }  /* free what's need to be freed and exit */  free (symbols.smsc);  free (symbols.destgsm);  free (symbols.message);  free (buffer);  /*------------------------------------------------------*/  exit (0);}                                        /* unlock_gsm () *//*========================================================*/void conclude (){  syslog ((FACILITY | LOG_NOTICE), "Connection closed by client.");  /* free what's need to be freed and exit */  free (symbols.smsc);  free (symbols.destgsm);  free (symbols.message);  free (buffer);  /*------------------------------------------------------*/  exit (0);}                                          /* conclude () *//*========================================================*//*========================================================== * EOF : server.y *===================*/

⌨️ 快捷键说明

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