C/C++ with Web API Example

/******************************************* 
main.cpp : EZserver API Samples
Author: EZhometech
Initial Date: 02/07/2016
Modified Date: 02/07/2016
http://www.ezhometech.com
sales@ezhometech.com
Copyright: EZhometech Inc. 
********************************************/
#include <winsock.h>
#if defined(_MFC_API)
#include <afxwin.h>
#else
#include <windows.h>
#endif
#include <process.h>

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/stat.h>

#define MAX_PENDING_CONNECTS 1
#define TCP 19
#define ANYPORT 0

#define TOKEN_STRING_SIZE 50
//char g_szEZserverIP[]="192.168.0.10";
char g_szEZserverIP[]="192.168.0.6";
char g_szHTTP_Streaming_Port[]="8000";
char g_szHTTP_API_Port[]="17000";
char g_szToken[TOKEN_STRING_SIZE]="";
char g_szUserName[]="test";
char g_szPassword[]="1234";
char g_szRatingsPassword[]="2345";

static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static void dprintf(char *format, ...)
{
    va_list args;
    char buffer[1024*5];

    va_start(args,format);

    strcpy(buffer + vsprintf(buffer,format,args), "");
    OutputDebugString(buffer);

}
unsigned char * base64_encode(const unsigned char *src, size_t len, size_t *out_len)
{
    unsigned char *out, *pos;
    const unsigned char *end, *in;
    size_t olen;
    int line_len;

    olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
    olen += olen / 72; /* line feeds */
    olen++; /* nul termination */
    out = (unsigned char *)malloc(olen);
    if (out == NULL)
    return NULL;

    end = src + len;
    in = src;
    pos = out;
    line_len = 0;
    while (end - in >= 3) {
        *pos++ = base64_table[in[0] >> 2];
        *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
        *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
        *pos++ = base64_table[in[2] & 0x3f];
        in += 3;
        line_len += 4;
        if (line_len >= 72) {
            *pos++ = '\n';
            line_len = 0;
        }
    }

    if (end - in) {
        *pos++ = base64_table[in[0] >> 2];
        if (end - in == 1) {
            *pos++ = base64_table[(in[0] & 0x03) << 4];
            *pos++ = '=';
        } else {
            *pos++ = base64_table[((in[0] & 0x03) << 4) |
            (in[1] >> 4)];
            *pos++ = base64_table[(in[1] & 0x0f) << 2];
        }
        *pos++ = '=';
        line_len += 4;
    }

    if (line_len)
    *pos++ = '\n';

    *pos = '\0';
    if (out_len)
    *out_len = pos - out;
    return out;
}

int EZAPI_TCPOpen(int descrp,const unsigned char *szIPAddr,int lport,int nMaxListenNo)
{
    int sock_id,i,j;
    struct sockaddr_in remote;
    char ip_name[30];
    char ch;
    const unsigned char *pszPort;
    int len=0;
    int val=1;
    int r=0;

    sock_id=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sock_id == INVALID_SOCKET)

    {
        return(-1);
    }
    len=strlen((const char*)szIPAddr);
    j=0;

    for(i=0;i<len;i++)
    {
        if(szIPAddr[i] == ':')
        {
                pszPort = szIPAddr+i+1; //e,g szIPAddr="192.168.168.1:80" => pszPort = "80"
                while (isdigit( ch=szIPAddr[i++]) )
                        j = 10*j + (ch - '0');
                  break;
        }
        ip_name[i]=szIPAddr[i];
    }
    ip_name[i]='\0';

    remote.sin_family=AF_INET;
    remote.sin_addr.s_addr=inet_addr(ip_name);
    if(lport == ANYPORT)
        lport = j;
    remote.sin_port=htons(lport);
    if(setsockopt(sock_id,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val))!=0) 
    {
        return(-1);
    }
    if(connect(sock_id,(struct sockaddr *)&remote,sizeof(remote)) != 0)
    {
        closesocket( sock_id );
        return(-1);
    }
    return(sock_id);

}
int EZAPI_TCPClose(int descrp)
{
    shutdown (descrp, 0x02);
    closesocket(descrp);
    return(1);
}
int User_Login(char * szToken, char * szEZserverIP, char * szHTTPPort, char * szUserID, char * szPassword)

    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];
    char szUserID_PASS[128];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;
    unsigned char * pRet_BASE64_String;
    unsigned int nOut_Len;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        return 0;
     } 
    sprintf(szUserID_PASS,"%s:%s",szUserID, szPassword);
    pRet_BASE64_String=base64_encode((unsigned char *)&szUserID_PASS,
    strlen(szUserID_PASS),
    &nOut_Len);
    if (pRet_BASE64_String>0)
    {
        if (nOut_Len>0)
        {
            *(pRet_BASE64_String+nOut_Len)=0;
            sprintf(szSendData,"GET /token/createtokenbased64?encrpty=%s HTTP/1.1\r\n\r\n",pRet_BASE64_String); //
            send(nSocket, szSendData, strlen (szSendData),0);
        }
        free(pRet_BASE64_String);
    } else
    {
        return 0;
    }
    p=szReceiveData;
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
            iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
            if (iReturn <0)
            {
                if (nSocket) EZAPI_TCPClose(nSocket);

                return 0;
            } else if (iReturn==0)
            {
                break;
            }
            nTotal_Length+=iReturn;
            p+=iReturn;
        } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);
    p=szReceiveData;
    r=strstr(p,"token=");
    if (r>0)
    {
        q=strstr(r,"\r\n");
        if (q>0)
        {
            *q=0;
            if (r!=0)
            {
                    strcpy(szToken,r+strlen("token="));
                    return 1;
            } else
            {
                    return 0;
            }
        } else
        {
                return 0;

        }
        }else
        {
                return 0;
        }
        return 0;
}
int User_Logout(char * szToken, char * szEZserverIP, char * szHTTPPort)


    char *p;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
            printf("0");
            return 0;
    } 
    sprintf(szSendData,"GET /token/destroytoken?token=%s HTTP/1.1\r\n\r\n",szToken);
    send(nSocket, szSendData, strlen (szSendData),0);
    p=szReceiveData;
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
            iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
            if (iReturn <0)
            {
                    if (nSocket) EZAPI_TCPClose(nSocket);

                    return 0;
            } else if (iReturn==0)
            {
                break;
            }
            nTotal_Length+=iReturn;
            p+=iReturn;
        } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);

    return 1;
}
int Play_Channel(char * szToken, char * szEZserverIP, char * szStreamingHTTPPort, int nCHNo)


    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szStreamingHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        printf("0");
        return 0;
    } 
    sprintf(szSendData,"GET /%d.ch?token=%s HTTP/1.1\r\n\r\n", nCHNo, szToken);
    send(nSocket, szSendData, strlen (szSendData),0);
    dprintf("Start to Play Channel [%d]\n", nCHNo);
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
                iReturn = recv (nSocket, szReceiveData, sizeof (szReceiveData), 0);
                if (iReturn <0)
                {
                    if (nSocket) EZAPI_TCPClose(nSocket);

                    return 0;
                } else if (iReturn==0)
                {
                        break;
                }
                nTotal_Length+=iReturn;
                dprintf("Channel Video Size=[%d]\n",nTotal_Length);
                if (nTotal_Length> 65536)
                {
                    break;
                }
        } 
    }
    if (nSocket) EZAPI_TCPClose(nSocket);
    dprintf("End to Play Channel [%d] size[%d]\n",nCHNo, nTotal_Length);

}
int Play_Movie(char * szToken, char * szEZserverIP, char * szStreamingHTTPPort, char * szMovieName)


    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szStreamingHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        printf("0");
        return 0;
    } 
    sprintf(szSendData,"GET /%s?token=%s HTTP/1.1\r\n\r\n", szMovieName, szToken);
    send(nSocket, szSendData, strlen (szSendData),0);
    dprintf("Start to play Movie [%s]\n", szMovieName);
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
            iReturn = recv (nSocket, szReceiveData, sizeof (szReceiveData), 0);
            if (iReturn <0)
            {
                if (nSocket) EZAPI_TCPClose(nSocket);

                return 0;
            } else if (iReturn==0)
            {
                break;
            }
            nTotal_Length+=iReturn;
            dprintf("Movie Video Size=[%d]\n",nTotal_Length);
            if (nTotal_Length> 65536)
            {
                    break;
            }
         } 
    }
    if (nSocket) EZAPI_TCPClose(nSocket);
    dprintf("End to play Movie [%s] size[%d]\n",szMovieName, nTotal_Length);

}
int Check_User_PPV(char * szToken, char * szEZserverIP, char * szHTTPPort, char *pszMovieName, char * pszMovieType)


    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        printf("0");
        return 0;
    } 
    sprintf(szSendData,"GET /server/check_user_ppv?token=%s\r\nmovie_name=%s\r\nmovie_tran_type=%s HTTP/1.1\r\n\r\n",
    szToken,pszMovieName,pszMovieType);
    send(nSocket, szSendData, strlen (szSendData),0);
    p=szReceiveData;
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
            iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
            if (iReturn <0)
            {
                if (nSocket) EZAPI_TCPClose(nSocket);

                return 0;
            } else if (iReturn==0)
            {
                    break;
            }
            nTotal_Length+=iReturn;
            p+=iReturn;
        } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);

    p=szReceiveData;
    r=strstr(p,"\r\n\r\n");
    if (r>0)
    {
            q=strstr(r,"\r\n");
            if (q>0)
            {
                    *q=0;
                    if (r!=0)
                    {
                            iReturn=atoi(r+strlen("\r\n\r\n"));
                            return iReturn;
                    } else
                    {
                            return 0;
                    }
                    } else
                    {
                            return 0;

                    }
                    }else
                    {
                            return 0;
                    }
                    return 0;

}
int Charge_User_PPV(char * szToken, char * szEZserverIP, char * szHTTPPort, char *pszMovieName, char * pszMovieType)


    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
            printf("0");
            return 0;
    } 
    sprintf(szSendData,"GET /server/charge_user_ppv?token=%s\r\nmovie_name=%s\r\nmovie_tran_type=%s HTTP/1.1\r\n\r\n",
    szToken,pszMovieName,pszMovieType);
    send(nSocket, szSendData, strlen (szSendData),0);
    p=szReceiveData;
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
                iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
                if (iReturn <0)
                {
                    if (nSocket) EZAPI_TCPClose(nSocket);

                    return 0;
                } else if (iReturn==0)
                {
                        break;
                }
                nTotal_Length+=iReturn;
                p+=iReturn;
          } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);
    p=szReceiveData;
    r=strstr(p,"\r\n\r\n");
    if (r>0)
    {
        q=strstr(r,"\r\n");
        if (q>0)
        {
                *q=0;
                if (r!=0)
                {
                    iReturn=atoi(r+strlen("\r\n\r\n"));
                    return iReturn;
                } else
                {
                        return 0;
                }
         } else
         {
              return 0;

          }
    }else
    {
            return 0;
    }
    return 0;

}
int Get_Movie_Ratings(char * szToken, char * szEZserverIP, char * szHTTPPort, char *pszMovieName)


    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        printf("0");
        return 0;
    } 
    sprintf(szSendData,"GET /server/get_movie_ratings?token=%s\r\nmovie_name=%s HTTP/1.1\r\n\r\n",
    szToken,pszMovieName);
    send(nSocket, szSendData, strlen (szSendData),0);
    p=szReceiveData;
    while (1)
    {
        FD_ZERO( &readfds );
        FD_SET( nSocket, &readfds );
        timeTimeout.tv_sec = 1; 
        timeTimeout.tv_usec = 0;
        ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
        if( ret > 0 )
        { 
                iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
                if (iReturn <0)
                {
                    if (nSocket) EZAPI_TCPClose(nSocket);

                    return 0;
                } else if (iReturn==0)
                {
                        break;
                }
                nTotal_Length+=iReturn;
                p+=iReturn;
          } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);
    p=szReceiveData;
    r=strstr(p,"\r\n\r\n");
    if (r>0)
    {
            q=strstr(r,"\r\n");
            if (q>0)
            {
                *q=0;
                if (r!=0)
                {
                        iReturn=atoi(r+strlen("\r\n\r\n"));
                        return iReturn;
                } else
                {
                        return 0;
                }
            } else
            {
                return 0;

            }
     }else
    {
            return 0;
     }
    return 0;

}
int Check_User_Ratings_Password(char * szToken, char * szEZserverIP, char * szHTTPPort, char * szUserID, char * szPassword)

    char *p,*q,*r;
    int nSocket;
    char szSendData[1024]; 
    char szReceiveData[1024];
    char szUserID_PASS[128];

    int iReturn=0;
    int val=0;
    timeval timeTimeout;
    fd_set readfds;
    int nTimeOutCount=0;
    int ret=0;
    int nTotal_Length=0;
    unsigned char * pRet_BASE64_String;
    unsigned int nOut_Len;

    nSocket=EZAPI_TCPOpen(TCP,(const unsigned char *)szEZserverIP,atoi(szHTTPPort),MAX_PENDING_CONNECTS);
    if (nSocket<=0) 
    {
        return 0;
    } 
    sprintf(szUserID_PASS,"%s:%s",szUserID, szPassword);
    pRet_BASE64_String=base64_encode((unsigned char *)&szUserID_PASS,
    strlen(szUserID_PASS),
    &nOut_Len);
    if (pRet_BASE64_String>0)
    {
        if (nOut_Len>0)
        {
            *(pRet_BASE64_String+nOut_Len)=0;
            sprintf(szSendData,"GET /server/check_user_ratings_password?token=%s\r\nencrpty=%s HTTP/1.1\r\n\r\n",szToken, pRet_BASE64_String); //
            send(nSocket, szSendData, strlen (szSendData),0);
        }
        free(pRet_BASE64_String);
    } else
    {
        return 0;
    }
    p=szReceiveData;
    while (1)
    {
            FD_ZERO( &readfds );
            FD_SET( nSocket, &readfds );
            timeTimeout.tv_sec = 1; 
            timeTimeout.tv_usec = 0;
            ret = select( nSocket+1, &readfds, NULL, NULL, &timeTimeout );
            if( ret > 0 )
            { 
                    iReturn = recv (nSocket, p, sizeof (szReceiveData), 0);
                    if (iReturn <0)
                    {
                        if (nSocket) EZAPI_TCPClose(nSocket);

                        return 0;
                    } else if (iReturn==0)
                    {
                            break;
                    }
                    nTotal_Length+=iReturn;
                    p+=iReturn;
            } 
    }
    szReceiveData[nTotal_Length]=0;
    if (nSocket) EZAPI_TCPClose(nSocket);
    p=szReceiveData;
    r=strstr(p,"\r\n\r\n");
    if (r>0)
    {
            q=strstr(r,"\r\n");
            if (q>0)
            {
                *q=0;
                if (r!=0)
                {
                        iReturn=atoi(r+strlen("\r\n\r\n"));
                        return iReturn;
                } else
                {
                    return 0;
            }
            } else
            {
                    return 0;

            }
    }else
    {
            return 0;
    }
    return 0;
}
int main(int argc, char* argv[])
{

    int iret=0;
    int bPlayMovie=0;
    char szMovieTransType[5]="";
    char szMovieName[]="MOVIE1";
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested=MAKEWORD(1,1);
    WSAStartup(wVersionRequested,&wsaData);

    dprintf("EZserver API Samples: Start\n");

    // Login EZserver to get a token for next APIs use.
    User_Login((char *) &g_szToken,g_szEZserverIP,g_szHTTP_API_Port,g_szUserName,g_szPassword);
    dprintf("Get Token=[%s]\n",g_szToken);

    // Play CH1 for 65536 bytes
    Play_Channel(g_szToken,g_szEZserverIP,g_szHTTP_Streaming_Port,1);

    // check_user_ppv : Rent a Movie
    strcpy(szMovieTransType,"rent");
    // Buyy a Movie
    //strcpy(szMovieTransType,"buy");
    iret=Check_User_PPV(g_szToken,g_szEZserverIP,g_szHTTP_API_Port,szMovieName,szMovieTransType);
    if (iret==1)
    {
            dprintf("Movie1 is available for watching\n");
            bPlayMovie=1;
    }else if (iret==2)
    {
         iret=Charge_User_PPV(g_szToken,g_szEZserverIP,g_szHTTP_API_Port,szMovieName,szMovieTransType);
        if (iret==1)
        {
                dprintf("Movie1 is charged for watching\n");
                bPlayMovie=1;
        }else if (iret==2)
        {
                dprintf("User is free mode for watching\n");
                bPlayMovie=1;
        }else
        {
                dprintf("test user does not have enough point\n");
                bPlayMovie=0;
        }
   }else if (iret==0)
    {
       dprintf("test user does not have enough point\n");
       bPlayMovie=0;
   }

    // Check Movie Protection
    if (bPlayMovie==1)
    {
        iret=Get_Movie_Ratings(g_szToken,g_szEZserverIP,g_szHTTP_API_Port,szMovieName);
         if (iret==1)
        {
            // Check User Rating Password
            iret=Check_User_Ratings_Password(g_szToken,g_szEZserverIP,g_szHTTP_API_Port,g_szUserName,g_szRatingsPassword);
            if (iret==1)
            {
                    bPlayMovie=1;
            }else
            {
                bPlayMovie=0;
                dprintf("user [%s] ratings password error\n", g_szUserName);
            }
        }
    }

    if (bPlayMovie==1)
    {
        // Play MOVIE1 for 65536 bytes
        Play_Movie(g_szToken,g_szEZserverIP,g_szHTTP_Streaming_Port,"MOVIE1");
    }


     // Logut EZserver to delete the token
    User_Logout(g_szToken,g_szEZserverIP,g_szHTTP_API_Port);

    dprintf("EZserver API Samples: End\n");

    WSACleanup( );
    return 1;
}


Copyright © EZhometech, Inc.