// INCLUDES #include #include "connlist.h" #include "ftp.h" // DEFINITIONS #define ARRAY_SIZE 512 // STRUCTURES typedef struct { char *username; char *password; char *account; char *host; SOCKET sock; } CONN_T; struct CONNLIST { int num; int size; CONN_T **conns; }; // FUNCTION PROTOTYPES CONN_T *Conn_Create(LPSTR, LPSTR, LPSTR, LPSTR); void Conn_Destroy(CONN_T *); // FUNCTION DEFINITIONS /* ConnList_Create */ CONNLIST *ConnList_Create() { CONNLIST *list = malloc( sizeof(CONNLIST) ); assert(list); list->num = 0; list->size = ARRAY_SIZE; list->conns = malloc( sizeof(CONN_T *) * list->size ); assert(list->conns); return list; } /* ConnList_Destroy */ void ConnList_Destroy(CONNLIST *list) { int i; if (list) { for ( i = 0 ; i < list->num ; i++ ) Conn_Destroy(list->conns[i]); list->num = 0; list->size = 0; free(list->conns); free(list); } } /* ConnList_Initialize */ void ConnList_Initialize(CONNLIST *list) { int i; for ( i = 0 ; i < list->num ; i++ ) Conn_Destroy(list->conns[i]); list->num = 0; list->size = ARRAY_SIZE; list->conns = realloc(list->conns, sizeof(CONN_T *) * list->size); } /* ConnList_Add */ int ConnList_Add(CONNLIST *list, LPSTR mountline) { LPSTR ptr; char username[50], password[50], account[50], host[100], temp[256]; // convert _at_ to @ while ( (ptr = strstr(mountline, "_at_")) != NULL ) { *ptr = '@'; ptr++; *ptr = '\0'; ptr += 3; strcpy(temp, ptr); strcat(mountline, temp); printf("current string: %s\n", mountline); } ptr = strchr(mountline, ':'); if ( ptr == NULL ) return -1; *ptr = '\0'; strcpy(host, mountline); mountline = ptr + 1; ptr = strchr(mountline, ':'); if ( ptr == NULL ) return -1; *ptr = '\0'; strcpy(username, mountline); mountline = ptr + 1; ptr = strchr(mountline, ':'); if ( ptr != NULL ) { *ptr = '\0'; strcpy(password, mountline); mountline = ptr + 1; strcpy(account, mountline); } else { strcpy(password, mountline); strcpy(account, ""); } if ( list->num == list->size ) { list->size *= 2; list->conns = realloc(list->conns, sizeof(CONN_T *) * list->size); } list->conns[list->num] = Conn_Create(username, password, account, host); assert(list->conns[list->num]); list->num++; return list->num - 1; } /* ConnList_GetNumConn */ int ConnList_GetNumConn(CONNLIST *list) { return list->num; } /* ConnList_Connect */ SOCKET ConnList_Connect(CONNLIST *list, int index) { if ( list->conns[index]->sock > 0 ) DoClose(list->conns[index]->sock); list->conns[index]->sock = DoConnect(list->conns[index]->host, list->conns[index]->username, list->conns[index]->password, list->conns[index]->account); return list->conns[index]->sock; } /* ConnList_GetUsername */ LPSTR ConnList_GetUsername(CONNLIST *list, int index) { return list->conns[index]->username; } /* ConnList_GetPassword */ LPSTR ConnList_GetPassword(CONNLIST *list, int index) { return list->conns[index]->password; } /* ConnList_GetAccount */ LPSTR ConnList_GetAccount(CONNLIST *list, int index) { return list->conns[index]->account; } /* ConnList_GetHost */ LPSTR ConnList_GetHost(CONNLIST *list, int index) { return list->conns[index]->host; } /* ConnList_GetSocket */ SOCKET ConnList_GetSocket(CONNLIST *list, int index) { return list->conns[index]->sock; } /* Conn_Create() */ CONN_T *Conn_Create(LPSTR username, LPSTR password, LPSTR account, LPSTR host) { CONN_T *conn = malloc(sizeof(CONN_T)); assert(conn); conn->username = malloc(sizeof(char) * (strlen(username) + 1)); conn->password = malloc(sizeof(char) * (strlen(password) + 1)); conn->account = malloc(sizeof(char) * (strlen(account) + 1)); conn->host = malloc(sizeof(char) * (strlen(host) + 1)); assert(conn->username && conn->password && conn->account && conn->host); strcpy(conn->username, username); strcpy(conn->password, password); strcpy(conn->account, account); strcpy(conn->host, host); conn->sock = INVALID_SOCKET; return conn; } /* Conn_Destroy */ void Conn_Destroy(CONN_T *conn) { if (conn) { conn->sock = 0; free(conn->username); free(conn->password); free(conn->account); free(conn->host); free(conn); } }