#include "nntp.h" /* *-------------------------------------------------------------------------------- */ int nntp_line_get (int chan,char *bufp,int bufl) { int sz; int pos; pos = 0; do { if ( 0 >= (sz = recv(chan,bufp+pos,1, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_line_get:%s",chan,strerror(errno)); return -1; } pos += sz; if ( (pos >= 2) && *(bufp+pos-2) == '\r' && *(bufp+pos-1) == '\n' ) break; } while (pos < bufl); pos -= 2; *(bufp+pos) = '\0'; NNTP_LOG(LOGD,"[Ch:%d]nntp_line_get:%d bytes",chan,pos); return pos; } /* *-------------------------------------------------------------------------------- */ int nntp_cmd_get (int chan,char *bufp,int bufl) { int sz; int pos; char *cp; pos = 0; do { if ( 0 >= (sz = recv(chan,bufp+pos,bufl-pos, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_cmd_get:%s",chan,strerror(errno)); return -1; } if ( !sz ) continue; pos += sz; *(bufp+pos) = '\0'; if ( (pos >= 2) && (cp = strstr(bufp,"\r\n")) ) break; } while (pos < bufl); if ( cp ) *(cp) = '\0'; else return -1; NNTP_LOG(LOGD,"[Ch:%d]nntp_cmd_get:%d bytes",chan,cp-bufp); return cp-bufp; } /* *-------------------------------------------------------------------------------- */ int nntp_txt_get (int chan,char *bufp,int bufl) { int sz,pos,flag; char *cp; pos = flag = 0; while (1) { do { if ( 0 >= (sz = recv(chan,bufp+pos,bufl-pos, 0)) ) { NNTP_LOG(LOGE,"[Ch:%d]nntp_txt_get:%s",chan,strerror(errno)); return -1; } pos += sz; *(bufp+pos) = '\0'; if ( (pos >= 5) && (cp = strstr(bufp,"\r\n.\r\n")) ) return ( flag == 0?cp-bufp+5:-1 ); } while (pos < bufl); /* For getting rest of big messages. */ memmove(bufp,bufp+(pos/2),1+(pos/2)); pos /= 2; flag++; } return 0; } /* *-------------------------------------------------------------------------------- */ void nntp_sockshut (int sock) { shutdown(sock,0); close(sock); } /* *-------------------------------------------------------------------------------- */ int net_connect ( WorkerContext *Wctxp, char *IPs, int port, long *IPn ) { int sock_1; struct sockaddr_in sock2_name; struct hostent HostEnt; struct hostent *pHostEnt; if ( NULL == (pHostEnt = gethostbyname (IPs)) ) { NNTP_LOGT(Wctxp,LOGW,"'gethostbyname' %s.",strerror(errno)); if ( NULL == (pHostEnt = gethostbyaddr (IPs,strlen(IPs),AF_INET)) ) { NNTP_LOGT(Wctxp,LOGE,"'gethostbyaddr' %s.",strerror(errno)); return -1; } } HostEnt = *pHostEnt; sock2_name.sin_port = htons (port); sock2_name.sin_family = HostEnt.h_addrtype; sock2_name.sin_addr = *((struct in_addr *) HostEnt.h_addr); if ( 0 > (sock_1 = socket (AF_INET, SOCK_STREAM, 0)) ) { NNTP_LOGT(Wctxp,LOGF,"'socket' %s.",strerror(errno)); return -1; } if ( 0 > connect(sock_1,(struct sockaddr *) &sock2_name,sizeof (sock2_name)) ) { NNTP_LOGT(Wctxp,LOGE,"'connect' %s.",strerror(errno)); nntp_sockshut (sock_1); return -1; } *IPn = sock2_name.sin_addr.s_addr; return sock_1; } /* *-------------------------------------------------------------------------------- */ char *ip_to_a ( long IP, char *buf ) { char *cp; struct in_addr ia; ia.s_addr = IP; pthread_lock_global_np (); cp = inet_ntoa (ia); strcpy (buf,cp); pthread_unlock_global_np (); return buf; }