📄 smsd.c
字号:
if (control_data->data_file != NULL) { unlink(control_data->data_file); free(control_data->data_file); control_data->data_file = NULL; } if (control_data->id != NULL) { free(control_data->id); control_data->id = NULL; }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void gateway(int new_fd){ char buf[MAX_STRING_LEN], host_name[512], ip_address[512]; int state, data_fd; SMS_CONTROL_DATA *control_data; control_data = init_control_data(); if (get_client_information(new_fd, host_name, ip_address) == 0) { lprintf(LOG_STANDARD, "Connection from %s [%s]\n", host_name, ip_address); } hprintf(new_fd, "400 SMS Gateway Ready\r\n"); while ((state != EXIT_STATE) && (state != ERROR_STATE) && (hgets(buf, MAX_STRING_LEN, new_fd) != NULL)) { state = NULL_STATE; if (strncasecmp(buf, "MAIL", 4) == 0) { control_data->mail = get_data(buf); if (control_data->mail == NULL) { state = ERROR_STATE; } } else if (strncasecmp(buf, "HOST", 4) == 0) { control_data->host = get_data(buf); if (control_data->host == NULL) { state = ERROR_STATE; } } else if (strncasecmp(buf, "DEST", 4) == 0) { control_data->destination = get_data(buf); if (control_data->destination == NULL) { state = ERROR_STATE; } } else if (strncasecmp(buf, "DATA", 4) == 0) { control_data->alen = get_data(buf); if (control_data->alen == NULL) { state = ERROR_STATE; } else { control_data->len = atoi(control_data->alen); /* WARNING - use strtol and strict */ /* checks */ /* Read 'len' bytes of data from fd */ /* and copy to data_file */ /* Open up the data_file - */ /* ensure we have a control file and */ /* all locks are setup */ if (gen_controlfile(control_data) == -1) { state = ERROR_STATE; } else { data_fd = open(control_data->data_file, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (data_fd < 0) { lprintf(LOG_ERROR, "opening data_file %s\n", control_data->data_file); exit(-1); } if (copy_data(data_fd, new_fd, control_data->len) != 0) { lprintf(LOG_ERROR, "copying data\n"); exit(-1); } close(data_fd); read(new_fd, &buf[0], 1); if (buf[0] == '\n') { } else if (buf[0] == '\r') { read(new_fd, &buf[0], 1); if (buf[0] == '\n') { } else { state = ERROR_STATE; } } else { state = ERROR_STATE; } } } } else if (strncasecmp(buf, "USER", 4) == 0) { control_data->user = get_data(buf); if (control_data->user == NULL) { state = ERROR_STATE; } } else if (strncasecmp(buf, "SEND", 4) == 0) { if (is_complete(control_data)) { write_control_data(control_data); } else { state = ERROR_STATE; } } else if (strncasecmp(buf, "SENJ", 4) == 0) { if (is_complete(control_data)) { write_control_data(control_data); } else { state = ERROR_STATE; } } else if (strncasecmp(buf, "SENQ", 4) == 0) { if (is_complete(control_data)) { write_control_data(control_data); } else { state = ERROR_STATE; } } else if (strncasecmp(buf, "SENA", 4) == 0) { if (is_complete(control_data)) { write_control_data(control_data); } else { state = ERROR_STATE; } } else if (strncasecmp(buf, "STAJ", 4) == 0) { } else if (strncasecmp(buf, "STAQ", 4) == 0) { } else if (strncasecmp(buf, "STAT", 4) == 0) { } else if (strncasecmp(buf, "INFO", 4) == 0) { if (((buf[4] == '\r') && (buf[5] == '\n')) || (buf[4] == '\n')) { hprintf(new_fd, "231- SMS Server info:\r\n"); hprintf(new_fd, " Version: 0.1\r\n"); display_current(new_fd, control_data); hprintf(new_fd, "231 End of Info.\r\n"); continue; } else { } } else if (strncasecmp(buf, "QUIT", 4) == 0) { hprintf(new_fd, "220 Goodbye.\r\n"); state = EXIT_STATE; continue; } else if (strncasecmp(buf, "HELP", 4) == 0) { if (((buf[4] == '\r') && (buf[5] == '\n')) || (buf[4] == '\n')) { hprintf(new_fd, "230- The following commands are recognized\r\n"); hprintf(new_fd, " USER DATA MAIL HOST DEST\r\n"); hprintf(new_fd, " SEND SENJ SENQ SENA \r\n"); hprintf(new_fd, " STAT STAJ STAQ \r\n"); hprintf(new_fd, " QUIT INFO HELP \r\n"); hprintf(new_fd, "230 End of Help.\r\n"); continue; } else { } } else { state = UNKNOWN_STATE; } if (state == ERROR_STATE) { hprintf(new_fd, "510 Error Connection Terminated.\r\n"); } else if (state == UNKNOWN_STATE) { hprintf(new_fd, "511 Error Command not understood.\r\n"); } else { hprintf(new_fd, "210 Ok.\r\n"); } } if (state == ERROR_STATE) { cleanup_files(control_data); } free(control_data);}/* -------------------------------------------------------------------- *//* Read 'len' bytes of data from src_fd and copy to 'dst_fd' *//* Return Values *//* *//* 0 Success *//* -1 Failure *//* -------------------------------------------------------------------- */int copy_data(int dst_fd, int src_fd, long dlen){ int len, sent; char *ptr; char *mbuf; mbuf = (char *)malloc(sizeof(char) * 1024); if (mbuf == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(-1); } { len = read(src_fd, mbuf, ((dlen<1024)?(dlen):(1024))); if (len < 0) { if (errno != EINTR) { lprintf(LOG_ERROR, "read\n"); return(-1); } } } while (len < 0); while(len) { dlen -= len; ptr = mbuf; sent = 0; while (len - sent) { len -= sent; ptr += sent; { sent = write(dst_fd, ptr, len); if (sent < 0) { if (errno != EINTR) { lprintf(LOG_ERROR, "write"); return(-1); } } } while (len < 0); if (sent == 0) { lprintf(LOG_ERROR, "write\n"); return(-1); } } if (dlen <= 0) { break; } { len = read(src_fd, mbuf, ((dlen<1024)?(dlen):(1024))); if (len < 0) { if (errno != EINTR) { lprintf(LOG_ERROR, "read"); return(-1); } } } while (len < 0); } free(mbuf); return 0;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void usage(char *file){ lprintf(LOG_STANDARD, "Usage: %s [-l loglevel] [-p port]\n", file);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int main(int argc, char *argv[]){ int c, port; char *ptr;struct sockaddr sa_client; /* ---------------------------- */ set_logfile(LOGFILE); set_loglevel(LOGLEVEL); set_consolelog(TRUE); /* ---------------------------- */ port = SMS_PORT; while ((c = getopt (argc, argv, "p:l:")) != -1) { switch (c) { case 'p': port = (int)strtol(optarg, &ptr, 10); break; case 'l': set_loglevel((int)strtol(optarg, &ptr, 10)); if (ptr == optarg) { lprintf(LOG_ERROR, "Option l requires an argument\n"); usage(argv[0]); exit(-1); } break; case '?': lprintf(LOG_ERROR, "Unknown option `-%c'\n", optopt); usage(argv[0]); exit(-1); default: abort (); } } /* ---------------------------- */ read_resource_file("smsd_config", resource_list, 1); exit(0); /* ---------------------------- */ if ((argc - optind) != 0) { usage(argv[0]); exit(-1); } /* ---------------------------- */ c = sizeof(sa_client); if (getpeername(fileno(stdin), &sa_client, &c) < 0) { /* getpeername() fails if fd isn't a */ /* socket. If this is the case we can */ /* assume that we aren't running */ /* from inetd and should startup and */ /* as and run as a daemon ourselves. */ lprintf(LOG_STANDARD, "Starting SMSD Standalone Server deamon...\n"); if (server_main(port, gateway) != 0) { lprintf(LOG_STANDARD, "Failed to start SMSD Standalone Server deamon\n"); exit(-1); } } else { set_consolelog(FALSE); lprintf(LOG_STANDARD, "Starting SMSD Server as an INETD service\n"); gateway(fileno(stdin)); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -