| 1 | /* |
|---|
| 2 | FILE |
|---|
| 3 | $Id$ |
|---|
| 4 | PURPOSE |
|---|
| 5 | Test verifying which server is reachable before mysql_real_connect; |
|---|
| 6 | AUTHOR |
|---|
| 7 | (C) 2010 Gary Wallis for Unixservice, LLC. |
|---|
| 8 | NOTES |
|---|
| 9 | mysql_real_connect() handles the local PF_UNIX just fine, so we can ignore pre |
|---|
| 10 | testing in those cases. |
|---|
| 11 | |
|---|
| 12 | Once this code is tested it can replace all current unxsVZ ConnectDB() type functions. |
|---|
| 13 | It was not needed before for mysqlproxy w/lua failover based installs. |
|---|
| 14 | |
|---|
| 15 | Note that local scoket via NULL DBIP1 overrides remote DBIP0 IP. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #include "mysqlrad.h" |
|---|
| 19 | #include "local.h" |
|---|
| 20 | #include <sys/socket.h> |
|---|
| 21 | #include <arpa/inet.h> |
|---|
| 22 | #include <netinet/in.h> |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <sys/select.h> |
|---|
| 25 | #include <errno.h> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | MYSQL gMysql; |
|---|
| 29 | |
|---|
| 30 | //This is an important setting that depends on your network setup |
|---|
| 31 | #define SELECT_TIMEOUT_USEC 100 |
|---|
| 32 | |
|---|
| 33 | int main() |
|---|
| 34 | { |
|---|
| 35 | //Handle quick cases first |
|---|
| 36 | //Port is irrelevant here. Make it clear. |
|---|
| 37 | mysql_init(&gMysql); |
|---|
| 38 | if(DBIP0==NULL) |
|---|
| 39 | { |
|---|
| 40 | if (mysql_real_connect(&gMysql,DBIP0,DBLOGIN,DBPASSWD,DBNAME,0,DBSOCKET,0)) |
|---|
| 41 | { |
|---|
| 42 | printf("Connected to local socket DBIP0==NULL\n"); |
|---|
| 43 | mysql_close(&gMysql); |
|---|
| 44 | exit(0); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | if(DBIP1==NULL) |
|---|
| 48 | { |
|---|
| 49 | if (mysql_real_connect(&gMysql,DBIP1,DBLOGIN,DBPASSWD,DBNAME,0,DBSOCKET,0)) |
|---|
| 50 | { |
|---|
| 51 | printf("Connected to local socket DBIP1==NULL\n"); |
|---|
| 52 | mysql_close(&gMysql); |
|---|
| 53 | exit(0); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | //Now we can use AF_INET/IPPROTO_TCP cases (TCP connections via IP number) |
|---|
| 58 | char *cPort="3306";//(*1) |
|---|
| 59 | int iSock,iConRes; |
|---|
| 60 | long lFcntlArg; |
|---|
| 61 | struct sockaddr_in sockaddr_inMySQLServer; |
|---|
| 62 | fd_set myset; |
|---|
| 63 | struct timeval tv; |
|---|
| 64 | int valopt; |
|---|
| 65 | socklen_t lon; |
|---|
| 66 | |
|---|
| 67 | //Default port should really be gathered from a different source |
|---|
| 68 | //but for now we use the known MySQL server CentOS default port (*1). |
|---|
| 69 | if(DBPORT!=0) |
|---|
| 70 | sprintf(cPort,"%u",DBPORT); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if(DBIP0!=NULL) |
|---|
| 74 | { |
|---|
| 75 | if((iSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0) |
|---|
| 76 | { |
|---|
| 77 | printf("Could not create socket\n"); |
|---|
| 78 | exit(1); |
|---|
| 79 | } |
|---|
| 80 | // Set non-blocking |
|---|
| 81 | lFcntlArg=fcntl(iSock,F_GETFL,NULL); |
|---|
| 82 | lFcntlArg|=O_NONBLOCK; |
|---|
| 83 | fcntl(iSock,F_SETFL,lFcntlArg); |
|---|
| 84 | |
|---|
| 85 | //DBIP0 has priority if we can create a connection we |
|---|
| 86 | //move forward immediately. |
|---|
| 87 | memset(&sockaddr_inMySQLServer,0,sizeof(sockaddr_inMySQLServer)); |
|---|
| 88 | sockaddr_inMySQLServer.sin_family=AF_INET; |
|---|
| 89 | sockaddr_inMySQLServer.sin_addr.s_addr=inet_addr(DBIP0); |
|---|
| 90 | sockaddr_inMySQLServer.sin_port=htons(atoi(cPort)); |
|---|
| 91 | iConRes=connect(iSock,(struct sockaddr *)&sockaddr_inMySQLServer,sizeof(sockaddr_inMySQLServer)); |
|---|
| 92 | if(iConRes<0) |
|---|
| 93 | { |
|---|
| 94 | if(errno==EINPROGRESS) |
|---|
| 95 | { |
|---|
| 96 | tv.tv_sec=0; |
|---|
| 97 | tv.tv_usec=SELECT_TIMEOUT_USEC; |
|---|
| 98 | FD_ZERO(&myset); |
|---|
| 99 | FD_SET(iSock,&myset); |
|---|
| 100 | if(select(iSock+1,NULL,&myset,NULL,&tv)>0) |
|---|
| 101 | { |
|---|
| 102 | lon=sizeof(int); |
|---|
| 103 | getsockopt(iSock,SOL_SOCKET,SO_ERROR,(void*)(&valopt),&lon); |
|---|
| 104 | if(valopt) |
|---|
| 105 | { |
|---|
| 106 | fprintf(stderr, "Error in connection() %d - %s\n",valopt,strerror(valopt)); |
|---|
| 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | //Valid fast connection |
|---|
| 111 | close(iSock);//Don't need anymore. |
|---|
| 112 | mysql_init(&gMysql); |
|---|
| 113 | if(mysql_real_connect(&gMysql,DBIP0,DBLOGIN,DBPASSWD, |
|---|
| 114 | DBNAME,DBPORT,DBSOCKET,0)) |
|---|
| 115 | { |
|---|
| 116 | printf("Connected to %s:%s\n",(char *)DBIP0,cPort); |
|---|
| 117 | mysql_close(&gMysql); |
|---|
| 118 | exit(0); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | printf("DBIP0 else if select()\n"); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | { |
|---|
| 129 | printf("DBIP0 else if errno==EINPROGRESS\n"); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | close(iSock);//Don't need anymore. |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | if(DBIP1!=NULL) |
|---|
| 136 | { |
|---|
| 137 | if((iSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0) |
|---|
| 138 | { |
|---|
| 139 | printf("Could not create socket\n"); |
|---|
| 140 | exit(1); |
|---|
| 141 | } |
|---|
| 142 | // Set non-blocking |
|---|
| 143 | lFcntlArg=fcntl(iSock,F_GETFL,NULL); |
|---|
| 144 | lFcntlArg|=O_NONBLOCK; |
|---|
| 145 | fcntl(iSock,F_SETFL,lFcntlArg); |
|---|
| 146 | |
|---|
| 147 | //Fallback to DBIP1 |
|---|
| 148 | memset(&sockaddr_inMySQLServer,0,sizeof(sockaddr_inMySQLServer)); |
|---|
| 149 | sockaddr_inMySQLServer.sin_family=AF_INET; |
|---|
| 150 | sockaddr_inMySQLServer.sin_addr.s_addr=inet_addr(DBIP1); |
|---|
| 151 | sockaddr_inMySQLServer.sin_port=htons(atoi(cPort)); |
|---|
| 152 | iConRes=connect(iSock,(struct sockaddr *)&sockaddr_inMySQLServer,sizeof(sockaddr_inMySQLServer)); |
|---|
| 153 | if(iConRes<0) |
|---|
| 154 | { |
|---|
| 155 | if(errno==EINPROGRESS) |
|---|
| 156 | { |
|---|
| 157 | tv.tv_sec=0; |
|---|
| 158 | tv.tv_usec=SELECT_TIMEOUT_USEC; |
|---|
| 159 | FD_ZERO(&myset); |
|---|
| 160 | FD_SET(iSock,&myset); |
|---|
| 161 | if(select(iSock+1,NULL,&myset,NULL,&tv)>0) |
|---|
| 162 | { |
|---|
| 163 | lon=sizeof(int); |
|---|
| 164 | getsockopt(iSock,SOL_SOCKET,SO_ERROR,(void*)(&valopt),&lon); |
|---|
| 165 | if(valopt) |
|---|
| 166 | { |
|---|
| 167 | printf("Error in connection() %d - %s\n",valopt,strerror(valopt)); |
|---|
| 168 | } |
|---|
| 169 | else |
|---|
| 170 | { |
|---|
| 171 | //Valid fast connection |
|---|
| 172 | close(iSock);//Don't need anymore. |
|---|
| 173 | mysql_init(&gMysql); |
|---|
| 174 | if(mysql_real_connect(&gMysql,DBIP1,DBLOGIN,DBPASSWD, |
|---|
| 175 | DBNAME,DBPORT,DBSOCKET,0)) |
|---|
| 176 | { |
|---|
| 177 | printf("Connected to %s:%s\n",(char *)DBIP1,cPort); |
|---|
| 178 | mysql_close(&gMysql); |
|---|
| 179 | exit(0); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | else |
|---|
| 184 | { |
|---|
| 185 | printf("DBIP1 else if select()\n"); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| 190 | printf("DBIP1 else if errno==EINPROGRESS\n"); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | close(iSock);//Don't need anymore. |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | //Failure exit 4 cases |
|---|
| 197 | if(DBIP1!=NULL && DBIP0!=NULL) |
|---|
| 198 | printf("Could not connect to %s:%s or %s:%s\n",(char *)DBIP0,cPort,(char *)DBIP1,cPort); |
|---|
| 199 | else if(DBIP1==NULL && DBIP0==NULL) |
|---|
| 200 | printf("Could not connect. Tried to use local socket\n"); |
|---|
| 201 | else if(DBIP0!=NULL && DBIP1==NULL) |
|---|
| 202 | printf("Could not connect to %s:%s or local socket (DBIP1)\n",(char *)DBIP0,cPort); |
|---|
| 203 | else if(DBIP0==NULL && DBIP1!=NULL) |
|---|
| 204 | printf("Could not connect to %s:%s or local socket (DBIP0)\n",(char *)DBIP1,cPort); |
|---|
| 205 | else if(1) |
|---|
| 206 | printf("Could not connect unexpected case\n"); |
|---|
| 207 | |
|---|
| 208 | exit(1); |
|---|
| 209 | |
|---|
| 210 | }//main() |
|---|