Exemplo n.º 1
0
static void
docmd_group(char *cmdstring, server_cb_inf *inf)
{
	char *ptr;
	char *sql_cmd;
	
	ptr = get_slinearg(cmdstring, 1);
	
	if (!ptr) {
		ToSend(no_group_given, strlen(no_group_given), inf);
		return; /* let him try again */
	}
	
	if ((sql_cmd = (char *)calloc(strlen(cmdstring) + 0x7f, sizeof(char))) == NULL) {
		DO_SYSL("GROUP: not enough memory.")
		free(ptr);
		kill_thread(inf);
		/*NOTREACHED*/
	}
	snprintf(sql_cmd, strlen(cmdstring) + 0x7f, "select * from newsgroups where name='%s';", ptr);
	/* the cb func sets found_group to '1' if the group is found */
	inf->servinf->found_group = 0;
	sqlite3_secexec(inf, sql_cmd, docmd_group_cb, inf);
	free(ptr);
	free(sql_cmd);
	/* if the group was not found (=the cb function was not called) ... */
	if (inf->servinf->found_group == 0) {
		/* send "no such group" */
		ToSend(nosuchgroup, strlen(nosuchgroup), inf);
	}
	
	return;
}
Exemplo n.º 2
0
static void
docmd_list_sql(server_cb_inf *inf)
{
	char cmd[]= "select * from Newsgroups;";

	ToSend(list, strlen(list), inf);
#ifdef DEBUG
	printf("executing cmd: %s\n", cmd);
#endif
	sqlite3_secexec(inf, cmd, docmd_list_cb, inf);
	
	ToSend(period_end, strlen(period_end), inf);
}
Exemplo n.º 3
0
static void
docmd_date(server_cb_inf *inf)
{
	char datestr[50]={'\0'};
	time_t ltime;
	
	ltime = time(NULL);
	strftime(datestr, sizeof(datestr), "111 %Y%m%d%H%M%S\r\n", localtime(&ltime));
	ToSend(datestr, strlen(datestr), inf);
}
Exemplo n.º 4
0
static int
docmd_article_cb_cb2(void *infp, int argc, char **argv, char **col)
{
	server_cb_inf *inf = (server_cb_inf *) infp;

	/* add header to the send buffer */
	switch (inf->cmdtype) {
	case CMDTYP_ARTICLE:
		ToSend(argv[0], strlen(argv[0]), inf);
		ToSend("\r\n", 2, inf); /* insert empty line */
		break;
	case CMDTYP_HEAD:
		ToSend(argv[0], strlen(argv[0]), inf);
		break;
	}
	/* add missing .\r\n\r\n on HEAD cmd */
	if (inf->cmdtype == CMDTYP_HEAD)
		ToSend(period_end, strlen(period_end), inf);
	
	return 0;
}
Exemplo n.º 5
0
/* format: <group> <high> <low> <pflag> */
static int
docmd_list_cb(void *infp, int argc, char **argv, char **ColName)
{
	server_cb_inf *inf = (server_cb_inf *) infp;
	char addbuf[1024] = { '\0' };
	/* the first message is '1' if we already have an article, else it will be zero */
	int already_posted = atoi(argv[3]);
	
	snprintf(addbuf, 1023, "%s %s %i %s\r\n", argv[1], argv[3], (already_posted ? 1 : 0), argv[2]);
	ToSend(addbuf, strlen(addbuf), inf);
	return 0;
}
Exemplo n.º 6
0
/* this was a good idea but acctually it does nothing more than executing sqlite3_exec. I do
 * all the checks by hand, what is better because it is needed only two times in this whole
 * file! Btw. This is good because I can better add the database abstraction layer later!
 */
void
sqlite3_secexec(server_cb_inf *inf, char *cmd, int (*cb)(void *, int, char **, char **), void *arg)
{
	if (sqlite3_exec(inf->servinf->db, cmd, cb, arg,
			 &inf->servinf->sqlite_err_msg) != SQLITE_OK) {
		fprintf(stderr, "SQLite3 error: %s\n", inf->servinf->sqlite_err_msg);
		sqlite3_free(inf->servinf->sqlite_err_msg);
		ToSend(progerr503, strlen(progerr503), inf);
		DO_SYSL("sqlite3_SECexec(): Unable to exec on database.")
		kill_thread(inf);
		/*NOTREACHED*/
	}
}
Exemplo n.º 7
0
static int
docmd_xover_cb(void *infp, int argc, char **argv, char **col)
{
	server_cb_inf *inf = (server_cb_inf *) infp;
	int articlenum, lines;
	int postlen;
	int len;
	char *ref, *xref, *bodylen;
	char *add_to_response;
	char tbuf[40] = { '\0' };
	
	articlenum = atoi(argv[0]);
	lines = atoi(argv[5]);
	
	/* Calculate the posting size (byte count) */
	bodylen = CDP_return_linevalue(argv[6], "X-WendzelNNTPdBodysize:");
	if (bodylen) {
		postlen = atoi(bodylen);
		free(bodylen);
	}
	postlen += strlen(argv[6]);
	
	ref = CDP_return_linevalue(argv[6], "References:");
	xref = CDP_return_linevalue(argv[6], "Xref:");
	
	len = 0xff + strlen(argv[1]) + strlen(argv[2]) + 39 /* timestamp len */ + strlen(argv[4]);
	if (ref)
		len += strlen(ref);
	if (xref)
		len += strlen(xref);
	
	nntp_localtime_to_str(tbuf, atol(argv[3]));
	
	CALLOC_Thread(add_to_response, (char *), len, sizeof(char))
	snprintf(add_to_response, len - 1,
		"%i\t%s\t%s\t%s\t%s\t%s\t%i\t%i\t%s\r\n",
		articlenum, argv[1], argv[2], tbuf, argv[4], (ref ? ref : " "), postlen, lines, (xref ? xref : " "));
	ToSend(add_to_response, strlen(add_to_response), inf);
	
	if (ref)
		free(ref);
	if (xref)
		free(xref);
	free(add_to_response);
	return 0;
}
Exemplo n.º 8
0
static void
docmd_authinfo_user(char *recvbuf, server_cb_inf *inf)
{
	char need_more_inf[]="381 More authentication information required.\r\n";
	char *user;
	
	inf->servinf->auth_is_there=0;
	
	if (!(user = get_slinearg(recvbuf, 2))) {
		/* error msg comes from get_slinearg() */
		kill_thread(inf);
		/*NOTREACHED*/
	}
	
	if (inf->servinf->cur_auth_user)
		free(inf->servinf->cur_auth_user);
	
	inf->servinf->cur_auth_user = user;
	ToSend(need_more_inf, strlen(need_more_inf), inf);
}
Exemplo n.º 9
0
static int
docmd_group_cb(void *infp, int argc, char **argv, char **col)
{
	server_cb_inf *inf = (server_cb_inf *) infp;
	int estimated, first, last;
	char *buf;

	last = atoi(argv[3]);
	if (last)
		first = 1;	/* if there's already a posting: the 1st posting has the num 1 */
	else
		first = 0;	/* else, we display a zero */
	estimated = last;
	
	if ((buf = (char *) calloc(strlen(argv[1]) + 0xff, sizeof(char))) == NULL) {
		DO_SYSL("GROUP_cb: not enough memory.")
		kill_thread(inf);
		/*NOTREACHED*/
	}
	snprintf(buf, strlen(argv[1]) + 0xff - 1, "211 %i %i %i %s group selected\r\n",
		estimated, first, last, argv[1]);
	ToSend(buf, strlen(buf), inf);
	free(buf);
	
	if (inf->servinf->selected_group)
		free(inf->servinf->selected_group);
	
	if ((inf->servinf->selected_group = (char *) calloc(strlen(argv[1]) + 1, sizeof(char))) == NULL) {
		DO_SYSL("GROUP_cb: not enough memory.")
		kill_thread(inf);
		/*NOTREACHED*/
	}
	strncpy(inf->servinf->selected_group, argv[1], strlen(argv[1]));
	
	/* unset the selected article */
	inf->servinf->selected_article = NULL;
	/* we found the group */
	inf->servinf->found_group = 1;
	
	return 0;
}
Exemplo n.º 10
0
static int
docmd_xhdr_cb(void *infp, int argc, char **argv, char **col)
{
	server_cb_inf *inf = (server_cb_inf *) infp;
	int len;
	int articlenum;
	char *add_to_response;
	static char none[] = "(none)";
	char *value;
	char tbuf[40] = { '\0' };
	server_cb_inf *linf;
	
	linf = (server_cb_inf *) infp;
	
	articlenum = atoi(argv[0]);
	
	/* If the requested header-part is supported, we get its value in argv[1],
	 * if not, we get NULL since we did not request a send parameter in the
	 * SQL query.
	 */
	if (argv[1] != NULL)
		value = argv[1];
	else
		value = none;
	
	/* catch date values we need to convert! */
	if (linf->speccmd == SPECCMD_DATE) {
		nntp_localtime_to_str(tbuf, atol(argv[1]));
		value = tbuf;
	}
	
	len = 0xfff + 1 + strlen(value) + 2 + 1;
	
	CALLOC_Thread(add_to_response, (char *), len, sizeof(char))
	snprintf(add_to_response, len - 1, "%i %s\r\n", articlenum, value);
	ToSend(add_to_response, strlen(add_to_response), inf);
	
	free(add_to_response);
	return 0;
}
Exemplo n.º 11
0
static void
docmd_article(char *cmdstring, server_cb_inf *inf)
{
#define ARTCLTYP_MESSAGEID	0x01
#define ARTCLTYP_NUMBER		0x02
#define ARTCLTYP_CURRENT	0x03  /* > ARTICLE\r\n -> return the currently selected article */
	int type=0;
	char *param;
	int i;
	int found=0;
	char *sql_cmd = NULL;
	int len;
	
	/* the cb func will set this to '1' if we found an article */
	inf->servinf->found_article = 0;
	
	if (cmdstring[0]=='A' || cmdstring[0]=='a') inf->cmdtype = CMDTYP_ARTICLE;
	else if(cmdstring[0]=='H' || cmdstring[0]=='h') inf->cmdtype = CMDTYP_HEAD;
	else if(cmdstring[0]=='B' || cmdstring[0]=='b') inf->cmdtype = CMDTYP_BODY;
	else if(cmdstring[0]=='S' || cmdstring[0]=='s') inf->cmdtype = CMDTYP_STAT;
	
	/* if(no newsgroup is selected) -> return a 412 error */
	if(inf->servinf->selected_group == NULL) {
		ToSend(nogroupselected, strlen(nogroupselected), inf);
		return;
	}

	/* point to the parameter-substring of the cmd-string */
	param = get_slinearg(cmdstring, 1);
	if (param == NULL) {
		type = ARTCLTYP_CURRENT;
	} else {
		/* okay, not the case. but shall we use the message-id or the news-id? */
		if (param[0] == '<') {
			type = ARTCLTYP_MESSAGEID;
		} else if (param[0] >= '0' && param[0] <= '9') {
			type = ARTCLTYP_NUMBER;
		} else {
			ToSend(noarticleselected, strlen(noarticleselected), inf);
			return;
		}
	}

	/* create first response line (code + info + message like "230 3 38 head follows") */
	switch(type)
	{
	case ARTCLTYP_MESSAGEID:
			// get the message-id-string
			for (i = 0; i < (int)strlen(param); i++) {
				if (param[i] == '>') {
					found = 1;
					break;
				}
			}
			if (!found) {
				ToSend(noarticleselected, strlen(noarticleselected), inf);
				return;
			}
			param[i+1] = '\0';
			
			/* select * from ngposts where ng='%s' and messageid='%s'; */
			len = 0x7f + strlen(inf->servinf->selected_group) + strlen(param);
			if ((sql_cmd = (char *) calloc(len + 1, sizeof(char))) == NULL) {
				DO_SYSL("ARTICLE(andCo): not enough memory.")
				kill_thread(inf);
				/*NOTREACHED*/
			}
			snprintf(sql_cmd, len, "select * from ngposts where ng='%s' and msgid='%s'",
				inf->servinf->selected_group, param);
		break;
	
	case ARTCLTYP_NUMBER:
			/* select * from ngposts where ng='%s' and messageid='%s'; */
			len = 0x7f + strlen(inf->servinf->selected_group) + strlen(param);
			if ((sql_cmd = (char *) calloc(len + 1, sizeof(char))) == NULL) {
				DO_SYSL("ARTICLE(andCo): not enough memory.")
				kill_thread(inf);
				/*NOTREACHED*/
			}
			snprintf(sql_cmd, len, "select * from ngposts where ng='%s' and postnum='%s'",
				inf->servinf->selected_group, param);
		break;
						
	case ARTCLTYP_CURRENT:
			// erstmal testen, ob nen article selected ist
			if (inf->servinf->selected_article == NULL) {
				ToSend(noarticleselected, strlen(noarticleselected), inf);
				return;
			}
			/* select * from ngposts where ng='%s' and messageid='%s'; */
			len = 0x7f + strlen(inf->servinf->selected_group) + strlen(inf->servinf->selected_article);
			if ((sql_cmd = (char *) calloc(len + 1, sizeof(char))) == NULL) {
				DO_SYSL("ARTICLE(andCo): not enough memory.")
				kill_thread(inf);
				/*NOTREACHED*/
			}
			snprintf(sql_cmd, len, "select * from ngposts where ng='%s' and postnum='%s'",
				inf->servinf->selected_group, inf->servinf->selected_article);
		break;
	}

	/* try to find the article and add header+body if found/needed */
	sqlite3_secexec(inf, sql_cmd, docmd_article_cb, inf);
	free(sql_cmd);
	if (param) /* can be NULL due to ARTCLTYP_CURRENT case */
		free(param);
	
	/* if this is still zero, no cb function was called -> no article was found */
	if (inf->servinf->found_article == 0) {
		ToSend(nosucharticle, strlen(nosucharticle), inf);
	}
}
Exemplo n.º 12
0
static int
docmd_article_cb(void *infp, int argc, char **argv, char **col)
{
	server_cb_inf *inf = (server_cb_inf *) infp;
	char *msgid;
	char *id;
	char *sendbuffer;
	char *sql_cmd = NULL;
	int len, len_sql;
	
	if (argc < 3) {
		DO_SYSL("ARTICLE(andCo)_cb: argc < 3!")
		kill_thread(infp);
	}
	msgid = argv[0];
	id = argv[2];
	
	len = 0xff + strlen(msgid) + strlen(id);
	if ((sendbuffer = (char *) calloc(len + 1, sizeof(char))) == NULL) {
		DO_SYSL("ARTICLE(andCo)_cb: not enough memory.")
		kill_thread(infp);
	}

	/* set the selected article */
	if (inf->servinf->selected_article)
		free(inf->servinf->selected_article);
	
	inf->servinf->selected_article = (char *) calloc(strlen(id) + 1, sizeof(char));
	strncpy(inf->servinf->selected_article, id, strlen(id));

	/* compose return message */
//postings (msgid string primary key, date string, author string, newsgroups string, subject string, lines string, header varchar(16000), body varchar(250000));
	len_sql = 0x7f + strlen(msgid);
	if ((sql_cmd = (char *) calloc(len + 1, sizeof(char))) == NULL) {
		DO_SYSL("ARTICLE(andCo)_cb: not enough memory.")
		kill_thread(infp);
	}
	
	switch (inf->cmdtype) {
	case CMDTYP_ARTICLE:
		snprintf(sendbuffer, len, "220 %s %s article retrieved - head and body follow\r\n", id, msgid);
		snprintf(sql_cmd, len_sql, "select header from postings where msgid='%s';", msgid);
		break;
	case CMDTYP_HEAD:
		snprintf(sendbuffer, len, "221 %s %s article retrieved - head follows\r\n", id, msgid);
		snprintf(sql_cmd, len_sql,  "select header from postings where msgid='%s';", msgid);
		break;
	case CMDTYP_BODY:
		snprintf(sendbuffer, len, "222 %s %s article retrieved - body follows\r\n", id, msgid);
		break;
	case CMDTYP_STAT:
		snprintf(sendbuffer, len, "223 %s %s article retrieved - request text separately\r\n", id, msgid);
		break;
	}
	ToSend(sendbuffer, strlen(sendbuffer), inf);
	free(sendbuffer);
	
	/* send the header, if needed */
	if (inf->cmdtype == CMDTYP_ARTICLE || inf->cmdtype == CMDTYP_HEAD) {
		sqlite3_secexec(inf, sql_cmd, docmd_article_cb_cb2, inf);
	}
	if (sql_cmd)
		free(sql_cmd);
	
	/* send the body, if needed */
	if (inf->cmdtype == CMDTYP_ARTICLE || inf->cmdtype == CMDTYP_BODY) {
		char *msgbody = filebackend_retrbody(msgid);
		if (msgbody != NULL) {
			ToSend(msgbody, strlen(msgbody), inf);
			free(msgbody);
		}
	}
	
	inf->servinf->found_article = 1;
	
	return 0;
}
Exemplo n.º 13
0
static void
docmd_xover(char *cmdstring, server_cb_inf *inf)
{
	u_int32_t min, max;
	u_int32_t REALmax;
	int len;
	char *ptr;
	char *sql_cmd;
	
	/* if (no newsgroup is selected) -> return a 412 error */
	if (inf->servinf->selected_group == NULL) {
		ToSend(nogroupselected, strlen(nogroupselected), inf);
		return;
	}
	
	/* get the REALmax value (max-num in the newsgroups table) */	
	REALmax = DB_get_high_value(inf->servinf->selected_group, inf);
	
	/* get the min + max values */
	ptr=get_slinearg(cmdstring, 1);
	if(ptr==NULL) { /* no value is given by the client => use current article */
		if (inf->servinf->selected_article == NULL) {
			ToSend(noarticleselected, strlen(noarticleselected), inf);
			return;
		}
		min = (u_int32_t) atoi(inf->servinf->selected_article);
		max = min;
	} else { /* at least a min-value is given */
		char *ptr_orig = ptr;
		
		min = atoi(ptr);
		max = min; /* this is correct. if we find a '-', we will change it in a moment */
		/* check for the '-' character */
		while(ptr[0]!='\0' && max!=REALmax) {
			if(ptr[0]=='-') { /* all articles from min to the end */
				max=REALmax;
			}
			ptr++;
		}
		if(max==REALmax) { /* look if there is an end-value for max */
			if(ptr[0]!='\0') {
				max=atoi(ptr);
				/* if the client sent us bullshit then max could be zero ... */
				if (!max) {
					/* ... in this case, we set it back to REALmax ;-) */
					max = REALmax;
				}
			}
		}
		
		free(ptr_orig);
	}
	
	onxxdebugm("%s%i%s%i%s%i%s",
	           "min: ", min, " max: ", max, " REALmax: ", REALmax, "\n");

	/* if no articles are in the range -> return a 420 error */
	if (min > REALmax || max > REALmax || min > max) {
		ToSend(noarticleselected, strlen(noarticleselected), inf);
		return;
	}

	/* now send the list */
	ToSend(xover, strlen(xover), inf);
	
	/* now let the DB do the scheissjob  ;-) */
	len = 0xfff + strlen(inf->servinf->selected_group);
	CALLOC_Thread(sql_cmd, (char *), len, sizeof(char))
	snprintf(sql_cmd, len - 1,
		"select n.postnum, p.subject, p.author, p.date, n.msgid, p.lines, p.header"
		" from ngposts n,postings p"
		" where ng='%s' and (postnum between %i and %i) and n.msgid = p.msgid;",
		inf->servinf->selected_group, min, max);
		
	sqlite3_secexec(inf, sql_cmd, docmd_xover_cb, inf);

	free(sql_cmd);
	ToSend(period_end, strlen(period_end), inf);
}
Exemplo n.º 14
0
static void
docmd_authinfo_pass(char *recvbuf, server_cb_inf *inf)
{
	char need_more_inf[]="381 More authentication information required.\r\n";
	char auth_accept[]="281 Authentication accepted.\r\n";
	char auth_reject[]="482 Authentication rejected.\r\n";	
	char *pass;
	int len;
	char *sql_cmd;
	char *log_str = NULL;
	
	inf->servinf->auth_is_there=0;

	if(inf->servinf->cur_auth_user) {
		if (!(pass = get_slinearg(recvbuf, 2))) {
			/* error msg comes from get_slinearg() */
			kill_thread(inf);
			/*NOTREACHED*/
		}
		if (inf->servinf->cur_auth_pass)
			free(inf->servinf->cur_auth_pass);
		inf->servinf->cur_auth_pass = pass;
		
		/* now check if combination of user+pass is valid */
		len = strlen(inf->servinf->cur_auth_user)+strlen(inf->servinf->cur_auth_pass)+0xff;
		if (!(sql_cmd = (char *)calloc(len, sizeof(char)))) {
			kill_thread(inf);
			/*NOTREACHED*/
		}
		snprintf(sql_cmd, len - 1, "select * from users where name='%s' and password='******';",
			inf->servinf->cur_auth_user, inf->servinf->cur_auth_pass);
		sqlite3_secexec(inf, sql_cmd, docmd_authinfo_pass_cb, inf);
		free(sql_cmd);
		
		if(inf->servinf->auth_is_there==0) {
			ToSend(auth_reject, strlen(auth_reject), inf);
			log_str = str_concat("Authentication REJECTED for user ",
						inf->servinf->cur_auth_user,
						" from IP ", inf->sockinf->ip, NULL);
						
		} else {
			ToSend(auth_accept, strlen(auth_accept), inf);
			log_str = str_concat("Authentication accepted for user ",
						inf->servinf->cur_auth_user,
						" from IP ", inf->sockinf->ip, NULL);
		}
		if (log_str) {
			DO_SYSL(log_str)
			free(log_str);
		}
			
		/* delete the username to prevent password brute-force attacks */
		free(inf->servinf->cur_auth_user); /* do this for security reasons! */
		inf->servinf->cur_auth_user = NULL;
		free(inf->servinf->cur_auth_pass);
		inf->servinf->cur_auth_pass = NULL;
	} else {
		/* the client first has to send a username */
		ToSend(need_more_inf, strlen(need_more_inf), inf);
	}
}
Exemplo n.º 15
0
static void
docmd_xhdr(char *cmdstring, server_cb_inf *inf)
{
	short req_message_id = 0;
	u_int32_t min, max;
	u_int32_t REALmax;
	int len;
	char *ptr;
	char *sql_cmd;
	char *hdr_type_str;
	
	/* if (no newsgroup is selected) -> return a 412 error */
	if (inf->servinf->selected_group == NULL) {
		ToSend(nogroupselected, strlen(nogroupselected), inf);
		return;
	}
	
	/* get the REALmax value (max-num in the newsgroups table) */	
	REALmax = DB_get_high_value(inf->servinf->selected_group, inf);
	
	/* get the command */
	ptr = get_slinearg(cmdstring, 1);
	if (!ptr) {
		DO_SYSL("unable to parse command line")
		ToSend(noarticleselected, strlen(noarticleselected), inf);
		return;
	}
	if (strncasecmp(ptr, "from", 4) == 0) {
		hdr_type_str = ",p.author";
	} else if (strncasecmp(ptr, "date", 4) == 0) {
		hdr_type_str = ",p.date";
		/* we must convert the time() returned value stored in the db to a
		 * real standard conform string */
		inf->speccmd = SPECCMD_DATE;
	} else if (strncasecmp(ptr, "newsgroups", 10) == 0) {
		hdr_type_str = ",p.newsgroups";
	} else if (strncasecmp(ptr, "subject", 7) == 0) {
		hdr_type_str = ",p.subject";
	} else if (strncasecmp(ptr, "lines", 5) == 0) {
		hdr_type_str = ",p.lines";
	} else {
		/* not supported */
		hdr_type_str = " "; /* nothing */
	}
	free(ptr);
	
	/* get the min + max values */
	ptr = get_slinearg(cmdstring, 2);
	if(ptr == NULL) { /* no value is given by the client => use current article */
		if (inf->servinf->selected_article == NULL) {
			ToSend(noarticleselected, strlen(noarticleselected), inf);
			return;
		}
		min = (u_int32_t) atoi(inf->servinf->selected_article);
		max = min;
	} else { /* at least a min-value is given */
		/* now check if the client requests either a message numner (num[-[to]]) or a message id (<message-id>) */
		if (ptr[0] == '<') { /* message ID */
			req_message_id = 1;
		} else {
			char *range_ptr = ptr;
			
			req_message_id = 0;
			min = atoi(range_ptr);
			max = min; /* this is correct. if we find a '-', we will change it in a moment */
			/* check for the '-' character */
			while(range_ptr[0]!='\r' && max!=REALmax) {
				if(range_ptr[0]=='-') { /* all articles from min to the end */
					max=REALmax;
				}
				range_ptr++;
			}
			if(max==REALmax) { /* look if there is an end-value for max */
				if(range_ptr[0]!='\r') {
					max=atoi(range_ptr);
					/* if the client sent us bullshit then max could be zero ... */
					if (!max) {
						/* ... in this case, we set it back to REALmax ;-) */
						max = REALmax;
					}
				}
			}
		}
	}
	/*onxxdebugm("%s%i%s%i%s%i%s",
	           "min: ", min, " max: ", max, " REALmax: ", REALmax, "\n");*/
	// check if the requested message ID exists
	if (req_message_id == 1) {
		if (DB_chk_if_msgid_exists(inf->servinf->selected_group, ptr, inf) == 0) {
			ToSend(noarticleselected, strlen(noarticleselected), inf);
			return;
		}
	}
	/* if no articles are in the range -> return a 420 error */
	else if (min > REALmax || max > REALmax || min > max) {
		ToSend(noarticleselected, strlen(noarticleselected), inf);
		return;
	}
	
	/* now send the list */
	ToSend(xhdr, strlen(xhdr), inf);
	 
	/* now let the DB do the scheissjob ;-) */
	len = 0xfff + strlen(inf->servinf->selected_group) + strlen(hdr_type_str);
	if (ptr != NULL)
		len += strlen(ptr);
	CALLOC_Thread(sql_cmd, (char *), len, sizeof(char))
	
	if (req_message_id) {
		snprintf(sql_cmd, len - 1,
			"select n.postnum %s"
			" from ngposts n,postings p"
			" where ng='%s' and p.msgid = '%s' and n.msgid = p.msgid;",
			hdr_type_str, inf->servinf->selected_group, ptr);
	} else {
		snprintf(sql_cmd, len - 1,
			"select n.postnum %s"
			" from ngposts n,postings p"
			" where ng='%s' and (postnum between %i and %i) and n.msgid = p.msgid;",
			hdr_type_str, inf->servinf->selected_group, min, max);
	}
	sqlite3_secexec(inf, sql_cmd, docmd_xhdr_cb, inf);

	free(sql_cmd);
	if (ptr)
		free(ptr);
	ToSend(period_end, strlen(period_end), inf);
}
Exemplo n.º 16
0
void Tcp()
{
	unsigned static retry=0;
	if(GsmCheckMsg()){
	GsmClearBufffer();
	switch (Gsm.Event)
	{
		case T_CsqReceived:
		if(Gsm.Rssi<=2)
		{
		Gsm.State=CsqLow;	
		}
		else if(Gsm.Rssi<99)
		{
			Gsm.State=CsqHigh;	
		}
		else if(Gsm.Rssi==99)
		{
			Gsm.State=CsqNoSignal;
		}
		
		
		break; //CsqReceived
		
		case IP_INITIAL:
		Gsm.State=T_CCSTT;
		Gsm.Event				= 	EvtInit;
		break;//IP_INITIAL
		
		case IP_GPRSACT:
		Gsm.State=T_CCIFSR;
		Gsm.Event				= 	EvtInit;
		break;//IP_GPRSACT
			
		case IP_START:
		Gsm.State=T_CCIICR;
		Gsm.Event				= 	EvtInit;
		break;
	
		case ALREADY_CONNECTED:
		nop();
		Gsm.Event				= 	EvtInit;
		Gsm.State=TcpConnected;
		break;//ALREADY_CONNECTED
	
	
		case TCP_SEND_OK:
		Gsm.Event				= 	EvtInit;
		Gsm.State=Con_SENDOK;
		break;//TCP_SEND_OK
		case PDP_DEACT:
		Gsm.Event		= 	EvtInit;
		Gsm.State		=		T_CCIPSHUT	;
		break;//PDP_DEACT
			
		case TCP_CLOSED:
		Gsm.State=T_CCIPSHUT;
		Gsm.Event				= 	EvtInit;
		break;//TCP_CLOSED
		case TCP_CONNECTING:
		Gsm.State=T_CCIPSTATUS;
		break;//TCP_CONNECTING
		case CONNECT_OK:
		Gsm.Event				= 	EvtInit;
		Gsm.State=TcpConnected;
		break;//CONNECT_OK
	
		case TCP_SHUT_OK:
			Gsm.Event				= 	EvtInit;
		Gsm.State=T_CCIPSHUT;
		break;//TCP_SHUT_OK
	
		case	EvtGsmRing:
		break;//EvtGsmRing:
		case 	EvtGsmMsg:
		break;//EvtGsmMsg:
		
		case Tcp_CREG_oK:
		Gsm.State				=	T_CCIPSTATUS;
		Gsm.Event				= 	EvtInit;
		break;//Tcp_CREG_oK

		case MsgOk:
		Gsm.State				=	Gsm.NextState;
		Gsm.Event				= 	EvtInit;
		GsmClearBufffer();
		break;//MsgOk
		case MsgError:
		break;//MsgError,

	}}
	
		switch (Gsm.State)
				{
				/*case Con_ATE0:
					#ifdef DebugOn
	  			    GsmDisplayUpdate("ATE0    ",1)	;	
					#endif
				    drv_sim900_send_cmd(6,"ATE0\r\n");

					GsmConfig(Con_Running,T_CCREG,Con_ATE0,MsgSend,	MsgOk,Sec(1),Gsm.MaxDelay,TimerStart);
					if(Gsm.Retry>1)
					{
					Gsm.State				=	Gsm.NextState;	
					retry=0;
					}
				
				break;	*/
				case CsqLow:
				case CsqHigh:
					sprintf(Gsm.Gsm_Sms,"CSQ = %d",Gsm.Rssi);// same with csq precedded GsmDisplayUpdate(Gsm._Csq,2);
					Gsm.State				=	Gsm.NextState;
					Gsm.Event				= 	EvtInit;
				break;//csqdetected
				
				case CheckCSQ:
					#ifdef DebugOn
				   	GsmDisplayUpdate("CSQ        ",1)	;	
					#endif	
					drv_sim900_send_cmd(6,"AT+CSQ\r\n");
					GsmConfig(Con_Running,T_CCREG,CheckCSQ,MsgSend,	T_CsqReceived,Sec(2),Sec(10),TimerStart);
					if(Gsm.Retry>5)
					{
					Gsm.State				=	T_CCIPSHUT;	
					drv_sim900_send_cmd(4,"AT\r\n");//test	
					Gsm.Retry=0;
					}
				break;
				case T_CCREG:
							#ifdef DebugOn
							   GsmDisplayUpdate("CCREG    ",1)	;
							#endif
					drv_sim900_send_cmd(get_size(CREG),CREG);
					GsmConfig(Con_Running,T_CCIPSTATUS, T_CCREG,MsgSend,	Tcp_CREG_oK,Sec(1),Gsm.MaxDelay,TimerStart);
					if(Gsm.Retry>5)
					{
					Gsm.State				=	T_CCIPSHUT;	
					Gsm.Retry=0;
					}
					break;//T_CCREG

				case T_CCIPSTATUS://CREG OK
					#ifdef DebugOn
						  // GsmDisplayUpdate("CCIPSTATUS    ",1)	;
					#endif
					GsmClearBufffer();
					drv_sim900_send_cmd(14,CIPSTATUS); //ok state Ip intial
				
					GsmConfig(Con_Running,T_CCIPSTATUS, T_CCIPSTATUS,MsgSend,	TCP_MSG,Sec(1),Gsm.MaxDelay,TimerStart);
				break;//T_CCIPSTATUS

					case T_CCSTT:
					#ifdef DebugOn
						   GsmDisplayUpdate("CSTT     ",1)	;
					#endif
					  GsmClearBufffer();
					  GsmConfig(Con_Running,T_CCIPSTATUS, T_CCSTT,MsgSend,	MsgOk,Sec(1),Gsm.MaxDelay,TimerStart);
			          drv_sim900_send_cmd(get_size(CSTT),CSTT);
			          drv_sim900_send_cmd(1,"\"");
					  drv_sim900_send_cmd(get_size("INTERNET"),"INTERNET");
			          drv_sim900_send_cmd(1,"\"");
			          drv_sim900_send_cmd(6,",\"\",\"\""); // ,"",""
			          drv_sim900_send_cmd(2,"\r\n");
					break;//T_CCIPSTATUS,
					case 	T_CIPCLOSE:
					#ifdef DebugOn
						   GsmDisplayUpdate("cipclose     ",1)	;
					#endif
					GsmClearBufffer();
					drv_sim900_send_cmd(get_size(CIPCLOSE),CIPCLOSE);
					break;//	CIPCLOSE,

					case T_CCIPCSGP:
					break;//T_CCIPCSGP

					case T_CCIICR:
					#ifdef DebugOn
						   GsmDisplayUpdate("CIICR     ",1)	;
					#endif
					  GsmClearBufffer();
  					  GsmConfig(Con_Running,T_CLPORT, T_CCIICR,MsgSend,	MsgOk,Sec(1),Gsm.MaxDelay,TimerStart);					  
					  drv_sim900_send_cmd(get_size(CIICR),CIICR);

					break;//T_CCSTT

					case T_CLPORT :
					#ifdef DebugOn
						   GsmDisplayUpdate("CLPORT    ",1)	;
					#endif
							GsmClearBufffer();
 							GsmConfig(Con_Running,T_CCIFSR, T_CLPORT,MsgSend,MsgOk,Sec(1),Gsm.MaxDelay,TimerStart);
							drv_sim900_send_cmd(get_size(CLPORT),CLPORT);
			             	drv_sim900_send_cmd(1,"\"");
			              	drv_sim900_send_cmd(get_size("60"),"60");
			              	drv_sim900_send_cmd(3,"\"\r\n");


				break;//T_CLPORT

					case T_CCIPSTART:
					#ifdef DebugOn
						   GsmDisplayUpdate("CCIPSTART    ",1)	;
					#endif
					GsmClearBufffer();
 					GsmConfig(Con_Running,T_CCIPSTATUS, T_CCIPSTART,MsgSend,	TCP_MSG,Sec(1),Gsm.MaxDelay,TimerStart);					
			        drv_sim900_send_cmd(get_size(CIPSTART),CIPSTART);
			        drv_sim900_send_cmd(1,"\"");
			        drv_sim900_send_cmd(get_size(Gsm.Ip_ADD),Gsm.Ip_ADD);   //"64.27.25.155\",\"10020\"\r\n"
			        drv_sim900_send_cmd(3,"\",\"");
			        drv_sim900_send_cmd(get_size(Gsm.PortNo),Gsm.PortNo);
			        drv_sim900_send_cmd(3,"\"\r\n");
					break;//T_CCIFSR

					case T_CCIFSR:
					#ifdef DebugOn
						   GsmDisplayUpdate("CCIFSR      ",1)	;
					#endif
					GsmClearBufffer();
			       	drv_sim900_send_cmd(get_size(CIFSR),CIFSR);
					GsmConfig(Con_Running,T_CCIPSTART, T_CCIFSR,MsgSend,	TCP_MSG,Sec(1),Gsm.MaxDelay,TimerStart);										
					break;//T_CCIPSTART,


					case T_CCIPSHUT:
							#ifdef DebugOn
						   GsmDisplayUpdate("CIPSHUT   ",1)	;
					#endif
							GsmClearBufffer();
							 drv_sim900_send_cmd(12,"AT+CIPSHUT\r\n");
							 GsmConfig(Con_Running,CheckCSQ, CheckCSQ,MsgSend,TCP_SHUT_OK,Sec(1),Gsm.MaxDelay,TimerStart);	
					break;//T_CCIPSHUT

				case TcpConnected:
				#ifdef DebugOn
			    GsmDisplayUpdate("TCP Connected",2)	;	
				#endif
				if(TCP_Delay.Function==FnEnter)
				{
					TCP_Delay.Delay=Gsm.L_Tick+ TCP_DELAY_Interval;//10sec
					TCP_Delay.Function=FnExit;
					ToSend(RegularUpdate);	
					GsmConfig(T_CCIPSTATUS,T_CCIPSTATUS, T_CCIPSTATUS,MsgSend,	TCP_SEND_OK,Sec(1),Sec(10),TimerStart);								
				}
				else
				{
						Gsm.State=T_CCIPSTATUS;	
						if(Gsm.L_Tick>TCP_Delay.Delay)
						{
						TCP_Delay.Function=FnEnter;	
						GsmConfig(T_CCIPSHUT,T_CCIPSTATUS, T_CCIPSTATUS,MsgSend,	SERVER_LISTENING,Sec(1),Gsm.MaxDelay,TimerStart);																
						}				
				}
			
				break;//TcpConnected

				case Con_SENDOK:
				#ifdef DebugOn
			    GsmDisplayUpdate("Send ok",1)	;
				GsmConfig(T_CCIPSTATUS,T_CCIPSTATUS, T_CCIPSTATUS,MsgSend,	SERVER_LISTENING,Sec(1),Gsm.MaxDelay,TimerStart);										
				#endif
				break;//Con_SENDOK;
				
				case Con_Running:
				nop();
				break;//Con_Running
				}
	


}
Exemplo n.º 17
0
 void sendPacket(const T* packet) {
     sendPacket(ToSend(packet, HostAddress::AnyAddress, 0));
 }