Annotation of parser3/src/lib/smtp/comms.C, revision 1.14
1.1 paf 1: /** @file
2: Parser: SMTP sender impl.
3:
1.14 ! moko 4: Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
! 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 paf 6: */
7:
1.9 moko 8: #include "smtp.h"
1.1 paf 9:
1.14 ! moko 10: volatile const char * IDENT_COMMS_C="$Id: comms.C,v 1.13 2020/12/15 17:10:33 moko Exp $";
1.1 paf 11:
12: // ---------------------------------------------------------------------------
13: int SMTP::
14: IsAddressARawIpaddress(const char* string)
15: {
16: while( *string )
17: {
1.6 paf 18: if( !isdigit((unsigned char)*string) )
1.1 paf 19: {
20: return 0;
21: }
22: string++;
23: }
24: return 1;
25: }
26:
27: //---------------------------------------------------------------------------
1.3 paf 28: ///@bug getservbyname is not reenterant
1.1 paf 29: int SMTP::
30: ResolveService(const char* service, u_short *our_port)
31: {
1.3 paf 32: struct servent *serventry = NULL;
1.1 paf 33:
34: if( IsAddressARawIpaddress(service) )
35: {
36: char * tail;
37:
38: *our_port = (u_short)strtol(service, &tail, 10);
39:
40: if( tail == service )
41: {
42: return WSAEPROTONOSUPPORT;
43: }
44: else
45: {
46: *our_port = htons(*our_port);
47: }
48: }
49: else
50: {
1.3 paf 51: serventry = getservbyname(service, "tcp");
1.1 paf 52:
53: if( serventry )
54: *our_port = serventry->s_port;
55: else
56: {
1.10 moko 57: #ifdef _MSC_VER
1.3 paf 58: int retval = WSAGetLastError();
1.1 paf 59: if( (retval == WSANO_DATA) || (retval == WSANO_RECOVERY) )
60: {
61: return WSAEPROTONOSUPPORT;
62: }
63: else
64: {
65: return (retval - 5000);
66: }
1.3 paf 67: #else
68: return WSAEPROTONOSUPPORT;
69: #endif
1.1 paf 70: }
71: }
72:
73: return 0;
74: }
75:
76: //---------------------------------------------------------------------------
1.3 paf 77: /// @bug gethostbyname is not reenterant
1.1 paf 78: int SMTP::
79: ResolveHostname(const char* hostname, struct sockaddr_in *sa_in)
80: {
1.3 paf 81: struct hostent *hostentry = NULL;
1.1 paf 82: unsigned long ip_address;
83:
84: if( (ip_address = inet_addr(hostname)) != INADDR_NONE )
85: {
86: sa_in->sin_addr.s_addr = ip_address;
87: }
88: else
89: {
90: if( (hostentry = gethostbyname(hostname)) == NULL )
91: {
92: return WSAHOST_NOT_FOUND;
93: }
94:
1.3 paf 95: sa_in->sin_addr.s_addr = *(long *)hostentry->h_addr;
1.1 paf 96: }
97:
98: return 0;
99: }
100:
101: //---------------------------------------------------------------------------
102: int SMTP::
103: GetAndSetTheSocket(SOCKET *the_socket)
104: {
105: if( INVALID_SOCKET == (*the_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*был 0, вложенно не работал*/)) )
106: {
107: return WSAESOCKTNOSUPPORT;
108: }
109:
1.5 paf 110: // To enable SO_DONTLINGER (that is, disable SO_LINGER)
111: // l_onoff should be set to zero and setsockopt should be called
112: linger dont_linger={0,0};
113: setsockopt(*the_socket, SOL_SOCKET, SO_LINGER, (const char *)&dont_linger, sizeof(dont_linger));
1.1 paf 114:
115: return 0;
116: }
117:
118: //---------------------------------------------------------------------------
119: int SMTP::
120: GetConnection(SOCKET the_socket, struct sockaddr_in *sa_in)
121: {
122:
1.3 paf 123: if( connect(the_socket,
1.1 paf 124: (struct sockaddr *)sa_in,
1.4 paf 125: sizeof(struct sockaddr_in))<0
1.1 paf 126: )
127: {
1.3 paf 128: int retval = 0;
1.1 paf 129: switch( (retval = WSAGetLastError()) )
130: {
131: case WSAEWOULDBLOCK:
132: break;
133:
134: case WSAECONNREFUSED:
135: return WSAECONNREFUSED;
136:
137: default:
138: //wsprintf(message, "unexpected error %d from winsock\n", retval);
139: //ShowError(message);
140: return WSAHOST_NOT_FOUND;
141: }
142: }
143:
144: return 0;
145: }
146:
147: //---------------------------------------------------------------------------
148: void SMTP::
149: MiscSocketSetup(SOCKET soc, fd_set *fds, struct timeval *timeout)
150: {
1.10 moko 151: #ifdef _MSC_VER
1.1 paf 152: unsigned long ioctl_blocking = 1;
153: ioctlsocket(soc, FIONBIO, &ioctl_blocking);
1.3 paf 154: #endif
1.1 paf 155:
156: FD_ZERO(fds);
157: FD_SET(soc, fds);
158:
159: timeout->tv_sec = 30;
160: timeout->tv_usec = 0;
161: }
162:
163: //---------------------------------------------------------------------------
E-mail: