📄 sockets.c
字号:
int result; if(buf == NULL) { /* * Fake an invalid address system error. */ errno = EINVAL; WARNING_MESSAGE("socket_receive called with NULL buf"); return(-1); } do { do { result = read(fd, receive_pointer, bytes_to_receive); } while(result == -1 && errno == EINTR); if(result != -1) { receive_pointer += result; bytes_received += result; bytes_to_receive -= result; } /* * Why did I have this here??? * * if(result == -1 && errno == EINTR) * fprintf(stderr, "receive_remote_data EINTR\n"); */ } while(bytes_received < nbytes && result > 0); /* * And why was this part of the while condition??? * || (result == -1 && errno == EPIPE)); */ if(result <= 0) { if (Debug(sockets)) { sprintf(message, "Error trying to receive %d bytes, received %d, result was %d", nbytes, bytes_received, result); if(result == -1) WARNING_PERROR(message); else WARNING_MESSAGE(message); } return(result); } return(bytes_received);}/*------------------------------------------------------------------------ * socket_nb_receive() * * Tries to receive a buffer full of stuff from a file descriptor * * RETURNS: * negative if error, 0 if EOF, positive if success. * * AND, in bytes_received, the number of bytes read on the file. *________________________________________________________________________*/int socket_nb_receive( int fd, /* File descriptor */ void *buf, /* Data buffer to use */ int nbytes, /* # of bytes to get */ int *bytes_received) /* # of bytes read */{ int bytes_to_receive = nbytes; char *receive_pointer = buf; int result; if (bytes_received == (int *)0) { /* Null pointer! Error! */ errno = EINVAL; WARNING_PERROR("socket_nb_receive called with bad bytes_recvd ptr."); return(-1); } (*bytes_received) = 0; if(buf == NULL) { /* * Fake an invalid address system error. */ errno = EINVAL; WARNING_PERROR("socket_nb_receive called with NULL buf"); return(-1); } do { do { result = read(fd, receive_pointer, bytes_to_receive); } while(result == -1 && errno == EINTR); if(result > 0) { receive_pointer += result; (*bytes_received) += result; bytes_to_receive -= result; } if (result == 0) return 0; } while((*bytes_received) < nbytes && result > 0); if(result < 0 && errno != EWOULDBLOCK) { sprintf(message, "Error trying to receive %d bytes, received %d, result was %d", nbytes, (*bytes_received), result); if(result == -1) WARNING_PERROR(message); else WARNING_MESSAGE(message); return(result); } return(1);}/*------------------------------------------------------------------------ * socket_peek() * * Tries to receive a buffer full of stuff from a client. * Does not actually take the data off the socket. * * RETURNS: * -1 if error * number of bytes received if no error *________________________________________________________________________*/int socket_peek( int fd, /* Socket to peek at */ void *buf, /* Data buffer to use */ int nbytes) /* # of bytes to get */{ int result; if(buf == NULL) { /* * Fake an invalid address system error. */ errno = EINVAL; WARNING_PERROR("socket_peek called with NULL buf"); return(-1); } do { result = recv(fd, buf, nbytes, MSG_PEEK); } while (result == -1 && errno == EINTR); if(result == -1) { sprintf(message, "socket_peek(): socket connection error, fd = %d ", fd); WARNING_PERROR(message); } return(result);}/*------------------------------------------------------------------------ * socket_test() * * Tries to see if the socket is still open * Must have a SIGPIPE handler installed for this to work. * * RETURNS: * 1 if socket is open * -1 if error *________________________________________________________________________*/int socket_test( int fd) /* Socket to test */{ int result; char dummy; do { result = recv(fd, &dummy, 1, MSG_PEEK); } while(result == -1 && errno == EINTR); if(result > 0 || result == -1 && errno == EWOULDBLOCK) return(1); return(-1);}/*------------------------------------------------------------------------ * socket_count() * * Tries to return the number of bytes waiting at the socket. * * RETURNS: * -1 if error * number of bytes ready if no error *________________________________________________________________________*/int socket_count(int fd) /* Socket to count bytes on */{ int result; int nbytes; result = ioctl(fd, FIONREAD, &nbytes); if(result == -1) { sprintf(message, "socket_count(): ioctl FIONREAD error, fd = %d ", fd); WARNING_PERROR(message); return(-1); } return(nbytes);}/*------------------------------------------------------------------------ * socket_send() * * Tries to send a buffer full of stuff to a file descriptor * If a SIGPIPE error occurs and there is no handler for it, * the program exits without warning!!! * * RETURNS: * -1 if error * number of bytes sent if no error *________________________________________________________________________*/int socket_send( int fd, /* File descriptor */ void *buf, /* Data buffer to use */ int nbytes) /* # of bytes to get */{ int bytes_to_send = nbytes; int bytes_sent = 0; char *send_pointer = buf; int result; int chunk_size; if(buf == NULL) { /* * Fake an invalid address system error. */ errno = EINVAL; WARNING_PERROR("socket_send called with NULL buf"); return(-1); } do { if(bytes_to_send > 4096) chunk_size = 4096; else chunk_size = bytes_to_send; do { result = write(fd, send_pointer, chunk_size); } while(result == -1 && errno == EINTR); if(result != -1) { send_pointer += result; bytes_sent += result; bytes_to_send -= result; } } while(bytes_sent < nbytes && result > 0 ); if(result <= 0) { sprintf(message, "Error trying to send %d bytes, sent %d, result was %d", nbytes, bytes_sent, result); if(result == -1) WARNING_PERROR(message); else WARNING_MESSAGE(message); return(result); } return(bytes_sent);}/*------------------------------------------------------------------------ * socket_nb_send() * * Tries to send a buffer full of stuff to a file descriptor * If a SIGPIPE error occurs and there is no handler for it, * the program exits without warning!!! * * Non-blocking version of socket_send * * RETURNS: * -1 if error * number of bytes sent if no error *________________________________________________________________________*/int socket_nb_send( int fd, /* File descriptor */ void *buf, /* Data buffer to use */ int nbytes) /* # of bytes to get */{ int bytes_to_send = nbytes; int bytes_sent = 0; char *send_pointer = buf; int result; int chunk_size; if(buf == NULL) { /* * Fake an invalid address system error. */ errno = EINVAL; WARNING_PERROR("socket_send called with NULL buf"); return(-1); } do { if(bytes_to_send > 4096) chunk_size = 4096; else chunk_size = bytes_to_send; do { result = write(fd, send_pointer, chunk_size); } while(result == -1 && errno == EINTR); if(result != -1) { send_pointer += result; bytes_sent += result; bytes_to_send -= result; if(result < chunk_size) { return(bytes_sent); } } if(result < 0 && errno == EWOULDBLOCK) { return(bytes_sent); } } while(bytes_sent < nbytes && result > 0 ); if(result <= 0) { sprintf(message, "Error trying to send %d bytes, sent %d, result was %d", nbytes, bytes_sent, result); if(result == -1) WARNING_PERROR(message); else WARNING_MESSAGE(message); return(result); } return(bytes_sent);}/* ------------------------------------------------------------------------ * * socket_control() * * Set or clear the control flags available via the ioctl SETFL/GETFL * calls * * RETURNS: -1 on error * * ------------------------------------------------------------------------ */int socket_control( int fd, /* Socket to control */ int whichFlag, /* Which flag to set/clear */ int what) /* True = set, False = clear */{ int result; int oldFlags; oldFlags = fcntl(fd, F_GETFL, 0); if(oldFlags < 0) { sprintf(message, "Error getting fcntl flags for fd %d", fd); WARNING_PERROR(message); return(oldFlags); } if(what == True) { oldFlags |= whichFlag; } else { oldFlags &= ~whichFlag; } result = fcntl(fd, F_SETFL, oldFlags); if(result < 0) { sprintf(message, "Error setting fcntl flags for fd %d", fd); WARNING_PERROR(message); return(result); } return(oldFlags);}char *ConnectedInternetAddress(int fd){ struct sockaddr_in name; int namelen = sizeof(name); char hostname[256]; int result; result = getpeername(fd, (struct sockaddr *) &name, &namelen); if(result < 0) { sprintf(msgBuf, "Error getting internet address of socket %d", fd); WARNING_PERROR(msgBuf); return((char *) 0); } if(name.sin_family == AF_INET) { return(inet_ntoa(name.sin_addr)); } else {#if !defined(READY_FOR_PRIME_TIME) return("127.0.0.1");#else result = gethostname(hostname, sizeof(hostname)); if(result < 0) { WARNING_PERROR("Error getting host name of local host"); return((char *) 0); } if ((host_entry = gethostbyname (hostname)) == NULL) { sprintf(message, "Couldn't get host entry for %s", hostname); WARNING_PERROR(message); return ((char *) 0); } /* Check the host entry for validity */ if (host_entry->h_addrtype != AF_INET) { sprintf(message, "Got unexpected address type %d in host entry.", host_entry->h_addrtype); WARNING_PERROR(message); return ((char *) 0); } if(host_entry->h_addr_list && *(host_entry->h_addr_list)) { return(inet_ntoa(host_entry->h_addr_list[0].sin_addr)) }#endif }}char *ConnectedHostname(int fd){ struct sockaddr_in name; struct hostent *host_entry; int namelen = sizeof(name); int result; result = getpeername(fd, (struct sockaddr *) &name, &namelen); if(result < 0) { sprintf(msgBuf, "Error getting internet address of socket %d", fd); WARNING_PERROR(msgBuf); return((char *) 0); } if(name.sin_family == AF_INET) { host_entry = gethostbyaddr((char *) &name.sin_addr, sizeof(struct in_addr), AF_INET); if(host_entry) { return(host_entry->h_name); } else { sprintf(msgBuf, "Error getting internet name of socket %d", fd); WARNING_MESSAGE(msgBuf); return((char *) 0); } } else { return("localhost"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -