📄 libmpdclient.c
字号:
/* libmpdclient (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu) This project's homepage is: http://www.musicpd.org Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Music Player Daemon nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include "libmpdclient.h"#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <stdio.h>#include <sys/param.h>#include <string.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#ifndef MPD_NO_IPV6#ifdef AF_INET6#define MPD_HAVE_IPV6#endif#endif#ifndef MSG_DONTWAIT#define MSG_DONTWAIT 0#endif#define COMMAND_LIST 1#define COMMAND_LIST_OK 2#ifdef MPD_HAVE_IPV6 int mpd_ipv6Supported() { int s; s = socket(AF_INET6,SOCK_STREAM,0); if(s == -1) return 0; close(s); return 1; } #endif char * mpd_sanitizeArg(const char * arg) { size_t i; int count=0; char * ret; for(i=0;i<strlen(arg);i++) { if(arg[i]=='"' || arg[i]=='\\') count++; } ret = malloc(strlen(arg)+count+1); count = 0; for(i=0;i<strlen(arg)+1;i++) { if(arg[i]=='"' || arg[i]=='\\') { ret[i+count] = '\\'; count++; } ret[i+count] = arg[i]; } return ret;}mpd_ReturnElement * mpd_newReturnElement(const char * name, const char * value){ mpd_ReturnElement * ret = malloc(sizeof(mpd_ReturnElement)); ret->name = strdup(name); ret->value = strdup(value); return ret;}void mpd_freeReturnElement(mpd_ReturnElement * re) { free(re->name); free(re->value); free(re);}void mpd_setConnectionTimeout(mpd_Connection * connection, float timeout) { connection->timeout.tv_sec = (int)timeout; connection->timeout.tv_usec = (int)(timeout*1e6 - connection->timeout.tv_sec*1000000+0.5);}mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) { int err; struct hostent * he; struct sockaddr * dest;#ifdef HAVE_SOCKLEN_T socklen_t destlen;#else int destlen;#endif struct sockaddr_in sin; char * rt; char * output; mpd_Connection * connection = malloc(sizeof(mpd_Connection)); struct timeval tv; fd_set fds;#ifdef MPD_HAVE_IPV6 struct sockaddr_in6 sin6;#endif strcpy(connection->buffer,""); connection->buflen = 0; connection->bufstart = 0; strcpy(connection->errorStr,""); connection->error = 0; connection->doneProcessing = 0; connection->commandList = 0; connection->listOks = 0; connection->doneListOk = 0; connection->returnElement = NULL; if(!(he=gethostbyname(host))) { snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "host \"%s\" not found",host); connection->error = MPD_ERROR_UNKHOST; return connection; } memset(&sin,0,sizeof(struct sockaddr_in)); /*dest.sin_family = he->h_addrtype;*/ sin.sin_family = AF_INET; sin.sin_port = htons(port);#ifdef MPD_HAVE_IPV6 memset(&sin6,0,sizeof(struct sockaddr_in6)); sin6.sin6_family = AF_INET6; sin6.sin6_port = htons(port);#endif switch(he->h_addrtype) { case AF_INET: memcpy((char *)&sin.sin_addr.s_addr,(char *)he->h_addr, he->h_length); dest = (struct sockaddr *)&sin; destlen = sizeof(struct sockaddr_in); break;#ifdef MPD_HAVE_IPV6 case AF_INET6: if(!mpd_ipv6Supported()) { strcpy(connection->errorStr,"no IPv6 suuport but a " "IPv6 address found\n"); connection->error = MPD_ERROR_SYSTEM; return connection; } memcpy((char *)&sin6.sin6_addr.s6_addr,(char *)he->h_addr, he->h_length); dest = (struct sockaddr *)&sin6; destlen = sizeof(struct sockaddr_in6); break;#endif default: strcpy(connection->errorStr,"address type is not IPv4 or " "IPv6\n"); connection->error = MPD_ERROR_SYSTEM; return connection; break; } if((connection->sock = socket(dest->sa_family,SOCK_STREAM,0))<0) { strcpy(connection->errorStr,"problems creating socket"); connection->error = MPD_ERROR_SYSTEM; return connection; } mpd_setConnectionTimeout(connection,timeout); /* connect stuff */ { int flags = fcntl(connection->sock, F_GETFL, 0); fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK); if(connect(connection->sock,dest,destlen)<0 && errno!=EINPROGRESS) { snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "problems connecting to \"%s\" on port" " %i",host,port); connection->error = MPD_ERROR_CONNPORT; return connection; } } while(!(rt = strstr(connection->buffer,"\n"))) { tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; FD_ZERO(&fds); FD_SET(connection->sock,&fds); if((err = select(connection->sock+1,&fds,NULL,NULL,&tv)) == 1) { int readed; readed = recv(connection->sock, &(connection->buffer[connection->buflen]), MPD_BUFFER_MAX_LENGTH-connection->buflen,0); if(readed<=0) { snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "problems getting a response from" " \"%s\" on port %i : %s",host, port, strerror(errno)); connection->error = MPD_ERROR_NORESPONSE; return connection; } connection->buflen+=readed; connection->buffer[connection->buflen] = '\0'; tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; } else if(err<0) { switch(errno) { case EINTR: continue; default: snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "problems connecting to \"%s\" on port" " %i",host,port); connection->error = MPD_ERROR_CONNPORT; return connection; } } else { snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "timeout in attempting to get a response from" " \"%s\" on port %i",host,port); connection->error = MPD_ERROR_NORESPONSE; return connection; } } *rt = '\0'; output = strdup(connection->buffer); strcpy(connection->buffer,rt+1); connection->buflen = strlen(connection->buffer); if(strncmp(output,MPD_WELCOME_MESSAGE,strlen(MPD_WELCOME_MESSAGE))) { free(output); snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "mpd not running on port %i on host \"%s\"", port,host); connection->error = MPD_ERROR_NOTMPD; return connection; } { char * test; char * version[3]; char * tmp = &output[strlen(MPD_WELCOME_MESSAGE)]; char * search = "."; int i; for(i=0;i<3;i++) { char * tok; if(i==3) search = " "; version[i] = strtok_r(tmp,search,&tok); if(!version[i]) { free(output); snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "error parsing version number at " "\"%s\"", &output[strlen(MPD_WELCOME_MESSAGE)]); connection->error = MPD_ERROR_NOTMPD; return connection; } connection->version[i] = strtol(version[i],&test,10); if(version[i]==test || *test!='\0') { free(output); snprintf(connection->errorStr, MPD_BUFFER_MAX_LENGTH, "error parsing version number at " "\"%s\"", &output[strlen(MPD_WELCOME_MESSAGE)]); connection->error = MPD_ERROR_NOTMPD; return connection; } tmp = NULL; } } free(output); connection->doneProcessing = 1; return connection;}void mpd_clearError(mpd_Connection * connection) { connection->error = 0; connection->errorStr[0] = '\0';}void mpd_closeConnection(mpd_Connection * connection) { close(connection->sock); if(connection->returnElement) free(connection->returnElement); free(connection);}void mpd_executeCommand(mpd_Connection * connection, char * command) { int ret; struct timeval tv; fd_set fds; char * commandPtr = command; int commandLen = strlen(command); if(!connection->doneProcessing && !connection->commandList) { strcpy(connection->errorStr,"not done processing current command"); connection->error = 1; return; } mpd_clearError(connection); FD_ZERO(&fds); FD_SET(connection->sock,&fds); tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; while((ret = select(connection->sock+1,NULL,&fds,NULL,&tv)==1) || (ret==-1 && errno==EINTR)) { ret = send(connection->sock,commandPtr,commandLen,#ifdef WIN32 ioctlsocket(connection->sock, commandLen, commandPtr));#endif#ifndef WIN32 MSG_DONTWAIT);#endif if(ret<=0) { if(ret==EAGAIN || ret==EINTR) continue; snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "problems giving command \"%s\"",command); connection->error = MPD_ERROR_SENDING; return; } else { commandPtr+=ret; commandLen-=ret; } if(commandLen<=0) break; } if(commandLen>0) { perror(""); snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH, "timeout sending command \"%s\"",command); connection->error = MPD_ERROR_TIMEOUT; return; } if(!connection->commandList) connection->doneProcessing = 0; else if(connection->commandList == COMMAND_LIST_OK) { connection->listOks++; }}void mpd_getNextReturnElement(mpd_Connection * connection) { char * output = NULL; char * rt = NULL; char * name = NULL; char * value = NULL; fd_set fds; struct timeval tv; char * tok = NULL; int readed; char * bufferCheck = NULL; int err; if(connection->returnElement) mpd_freeReturnElement(connection->returnElement); connection->returnElement = NULL; if(connection->doneProcessing || (connection->listOks && connection->doneListOk)) { strcpy(connection->errorStr,"already done processing current command"); connection->error = 1; return; } bufferCheck = connection->buffer+connection->bufstart; while(connection->bufstart>=connection->buflen || !(rt = strchr(bufferCheck,'\n'))) { if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) { memmove(connection->buffer, connection->buffer+ connection->bufstart, connection->buflen- connection->bufstart+1); connection->buflen-=connection->bufstart; connection->bufstart = 0; } if(connection->buflen>=MPD_BUFFER_MAX_LENGTH) { strcpy(connection->errorStr,"buffer overrun"); connection->error = MPD_ERROR_BUFFEROVERRUN; connection->doneProcessing = 1; connection->doneListOk = 0; return; } bufferCheck = connection->buffer+connection->buflen; tv.tv_sec = connection->timeout.tv_sec; tv.tv_usec = connection->timeout.tv_usec; FD_ZERO(&fds); FD_SET(connection->sock,&fds); if((err = select(connection->sock+1,&fds,NULL,NULL,&tv) == 1)) { readed = recv(connection->sock, connection->buffer+connection->buflen, MPD_BUFFER_MAX_LENGTH-connection->buflen,#ifdef WIN32 ioctlsocket(connection->sock, commandLen, commandPtr));#endif#ifndef WIN32 MSG_DONTWAIT);#endif if(readed<0 && (errno==EAGAIN || errno==EINTR)) { continue; } if(readed<=0) { strcpy(connection->errorStr,"connection" " closed"); connection->error = MPD_ERROR_CONNCLOSED; connection->doneProcessing = 1; connection->doneListOk = 0; return; } connection->buflen+=readed; connection->buffer[connection->buflen] = '\0'; } else if(err<0 && errno==EINTR) continue; else { strcpy(connection->errorStr,"connection timeout"); connection->error = MPD_ERROR_TIMEOUT; connection->doneProcessing = 1; connection->doneListOk = 0; return; } } *rt = '\0'; output = connection->buffer+connection->bufstart; connection->bufstart = rt - connection->buffer + 1; if(strcmp(output,"OK")==0) { if(connection->listOks > 0) { strcpy(connection->errorStr, "expected more list_OK's"); connection->error = 1; } connection->listOks = 0; connection->doneProcessing = 1; connection->doneListOk = 0; return; } if(strcmp(output, "list_OK") == 0) { if(!connection->listOks) { strcpy(connection->errorStr, "got an unexpected list_OK"); connection->error = 1; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -