error_t smtpSendMail(const SmtpAuthInfo *authInfo, const SmtpMail *mail) { error_t error; uint_t i; uint_t replyCode; IpAddr serverIpAddr; SmtpClientContext *context; //Check parameters if(!authInfo || !mail) return ERROR_INVALID_PARAMETER; //Make sure the server name is valid if(!authInfo->serverName) return ERROR_INVALID_PARAMETER; //Debug message TRACE_INFO("Sending a mail to %s port %" PRIu16 "...\r\n", authInfo->serverName, authInfo->serverPort); //The specified SMTP server can be either an IP or a host name error = getHostByName(authInfo->interface, authInfo->serverName, &serverIpAddr, 0); //Unable to resolve server name? if(error) return ERROR_NAME_RESOLUTION_FAILED; //Allocate a memory buffer to hold the SMTP client context context = osAllocMem(sizeof(SmtpClientContext)); //Failed to allocate memory? if(!context) return ERROR_OUT_OF_MEMORY; //Open a TCP socket context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); //Failed to open socket? if(!context->socket) { //Free previously allocated resources osFreeMem(context); //Report an error return ERROR_OPEN_FAILED; } #if (SMTP_TLS_SUPPORT == ENABLED) //Do not use SSL/TLS for the moment context->tlsContext = NULL; #endif //Start of exception handling block do { //Bind the socket to a particular network interface? if(authInfo->interface) { //Associate the socket with the relevant interface error = socketBindToInterface(context->socket, authInfo->interface); //Any error to report? if(error) break; } //Set timeout for blocking operations error = socketSetTimeout(context->socket, SMTP_DEFAULT_TIMEOUT); //Any error to report? if(error) break; //Connect to the SMTP server error = socketConnect(context->socket, &serverIpAddr, authInfo->serverPort); //Connection to server failed? if(error) break; #if (SMTP_TLS_SUPPORT == ENABLED) //Open a secure SSL/TLS session? if(authInfo->useTls) { //Initialize TLS context context->tlsContext = tlsInit(); //Initialization failed? if(!context->tlsContext) { //Unable to allocate memory error = ERROR_OUT_OF_MEMORY; //Stop immediately break; } //Bind TLS to the relevant socket error = tlsSetSocket(context->tlsContext, context->socket); //Any error to report? if(error) break; //Select client operation mode error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT); //Any error to report? if(error) break; //Set the PRNG algorithm to be used error = tlsSetPrng(context->tlsContext, authInfo->prngAlgo, authInfo->prngContext); //Any error to report? if(error) break; //Perform TLS handshake error = tlsConnect(context->tlsContext); //Failed to established a TLS session? if(error) break; } #endif //Wait for the connection greeting reply error = smtpSendCommand(context, NULL, &replyCode, NULL); //Any communication error to report? if(error) break; //Check whether the greeting message was properly received if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } //Clear security features context->authLoginSupported = FALSE; context->authPlainSupported = FALSE; context->authCramMd5Supported = FALSE; context->startTlsSupported = FALSE; //Send EHLO command and parse server response error = smtpSendCommand(context, "EHLO [127.0.0.1]\r\n", &replyCode, smtpEhloReplyCallback); //Any communication error to report? if(error) break; //Check SMTP response code if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } #if (SMTP_TLS_SUPPORT == ENABLED) //Check whether the STARTTLS command is supported if(context->startTlsSupported && !context->tlsContext) { //Send STARTTLS command error = smtpSendCommand(context, "STARTTLS\r\n", &replyCode, NULL); //Any communication error to report? if(error) break; //Check SMTP response code if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } //Initialize TLS context context->tlsContext = tlsInit(); //Initialization failed? if(!context->tlsContext) { //Unable to allocate memory error = ERROR_OUT_OF_MEMORY; //Stop immediately break; } //Bind TLS to the relevant socket error = tlsSetSocket(context->tlsContext, context->socket); //Any error to report? if(error) break; //Select client operation mode error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT); //Any error to report? if(error) break; //Set the PRNG algorithm to be used error = tlsSetPrng(context->tlsContext, authInfo->prngAlgo, authInfo->prngContext); //Any error to report? if(error) break; //Perform TLS handshake error = tlsConnect(context->tlsContext); //Failed to established a TLS session? if(error) break; //Clear security features context->authLoginSupported = FALSE; context->authPlainSupported = FALSE; context->authCramMd5Supported = FALSE; //Send EHLO command and parse server response error = smtpSendCommand(context, "EHLO [127.0.0.1]\r\n", &replyCode, smtpEhloReplyCallback); //Any communication error to report? if(error) break; //Check SMTP response code if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } } #endif //Authentication requires a valid user name and password if(authInfo->userName && authInfo->password) { #if (SMTP_LOGIN_AUTH_SUPPORT == ENABLED) //LOGIN authentication mechanism supported? if(context->authLoginSupported) { //Perform LOGIN authentication error = smtpSendAuthLogin(context, authInfo); //Authentication failed? if(error) break; } else #endif #if (SMTP_PLAIN_AUTH_SUPPORT == ENABLED) //PLAIN authentication mechanism supported? if(context->authPlainSupported) { //Perform PLAIN authentication error = smtpSendAuthPlain(context, authInfo); //Authentication failed? if(error) break; } else #endif #if (SMTP_CRAM_MD5_AUTH_SUPPORT == ENABLED) //CRAM-MD5 authentication mechanism supported? if(context->authCramMd5Supported) { //Perform CRAM-MD5 authentication error = smtpSendAuthCramMd5(context, authInfo); //Authentication failed? if(error) break; } else #endif //No authentication mechanism supported? { //Skip authentication step } } //Format the MAIL FROM command (a null return path must be accepted) if(mail->from.addr) sprintf(context->buffer, "MAIL FROM:<%s>\r\n", mail->from.addr); else strcpy(context->buffer, "MAIL FROM:<>\r\n"); //Send the command to the server error = smtpSendCommand(context, context->buffer, &replyCode, NULL); //Any communication error to report? if(error) break; //Check SMTP response code if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } //Format the RCPT TO command for(i = 0; i < mail->recipientCount; i++) { //Skip recipient addresses that are not valid if(!mail->recipients[i].addr) continue; //Format the RCPT TO command sprintf(context->buffer, "RCPT TO:<%s>\r\n", mail->recipients[i].addr); //Send the command to the server error = smtpSendCommand(context, context->buffer, &replyCode, NULL); //Any communication error to report? if(error) break; //Check SMTP response code if(!SMTP_REPLY_CODE_2YZ(replyCode)) { //An unexpected response was received... error = ERROR_UNEXPECTED_RESPONSE; //Stop immediately break; } } //Propagate exception if necessary if(error) break; //Send message body error = smtpSendData(context, mail); //Any error to report? if(error) break; //End of exception handling block } while(0); //Check status code if(error == NO_ERROR || error == ERROR_UNEXPECTED_RESPONSE || error == ERROR_AUTHENTICATION_FAILED) { //Properly disconnect from the SMTP server smtpSendCommand(context, "QUIT\r\n", &replyCode, NULL); } #if (SMTP_TLS_SUPPORT == ENABLED) //Gracefully close SSL/TLS session if(context->tlsContext != NULL) tlsFree(context->tlsContext); #endif //Close socket socketClose(context->socket); //Clean up previously allocated resources osFreeMem(context); //Return status code return error; }
Socket(const char *host, int port) { sock = socketOpen(host, port); }
static int initWebs(int demo) { struct hostent *hp; struct in_addr intaddr; char host[128], dir[128], webdir[128]; char *cp; char_t wbuf[128]; /* * Initialize the socket subsystem */ socketOpen(); #ifdef USER_MANAGEMENT_SUPPORT /* * Initialize the User Management database */ umOpen(); umRestore(T("umconfig.txt")); #endif /* * Define the local Ip address, host name, default home page and the * root web directory. */ if (gethostname(host, sizeof(host)) < 0) { error(E_L, E_LOG, T("Can't get hostname")); //Modify by :LuiShiLi return -1; //Date :2011.09.28 } if ((hp = gethostbyname(host)) == NULL) { error(E_L, E_LOG, T("Can't get host address")); return -1; } memcpy((char *) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length); //intaddr.s_addr = inet_addr("10.0.0.9"); /* * Set ../web as the root web. Modify this to suit your needs * A "-demo" option to the command line will set a webdemo root */ getcwd(dir, sizeof(dir)); //取得当前的工作目录:/home/forlinuxsmb/../ if ((cp = strrchr(dir, '/'))) //寻找出'/'最后出现的位置 { *cp = '\0'; } #ifdef DEBUG //Modify by :LuiShiLi printf("Working directory is :%s\n",dir); //Date :2011.09.28 #endif if (demo) { sprintf(webdir, "%s/%s", dir, demoWeb); } else { sprintf(webdir, "%s/%s", dir, rootWeb); } #ifdef DEBUG printf("Web directory is :%s\n",webdir); //Modify by :LuiShiLi #endif //Date :2011.09.28 /* * Configure the web server options before opening the web server */ websSetDefaultDir(webdir); cp = inet_ntoa(intaddr); ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf))); websSetIpaddr(wbuf); ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf))); websSetHost(wbuf); /* * Configure the web server options before opening the web server */ websSetDefaultPage(T("default.asp")); #ifdef DEBUG printf("T\(\"default.asp\"\) is :%s\n",T("default.asp")); #endif websSetPassword(password); /* * Open the web server on the given port. If that port is taken, try * the next sequential port for up to "retries" attempts. */ websOpenServer(port, retries); /* * First create the URL handlers. Note: handlers are called in sorted order * with the longest path handler examined first. Here we define the security * handler, forms handler and the default web page handler. */ websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0); websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0); websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST); /* * Now define two test procedures. Replace these with your application * relevant ASP script procedures and form functions. */ websAspDefine(T("aspTest"), aspTest); //关联ASP标签和C函数名字 websFormDefine(T("formTest"), formTest); //关联GoForms标签和C函数名字 /* * Create the Form handlers for the User Management pages */ #ifdef USER_MANAGEMENT_SUPPORT formDefineUserMgmt(); #endif /* * Create a handler for the default home page */ websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); return 0; }
error_t udpEchoStart(void) { error_t error; EchoServiceContext *context; #if (NET_STATIC_OS_RESOURCES == DISABLED) OsTask *task; #endif //Debug message TRACE_INFO("Starting UDP echo service...\r\n"); //Allocate a memory block to hold the context context = osAllocMem(sizeof(EchoServiceContext)); //Failed to allocate memory? if(!context) return ERROR_OUT_OF_MEMORY; //Start of exception handling block do { //Open a UDP socket context->socket = socketOpen(SOCKET_TYPE_DGRAM, SOCKET_IP_PROTO_UDP); //Failed to open socket? if(!context->socket) { //Report an error error = ERROR_OPEN_FAILED; //Exit immediately break; } //The server listens for incoming datagrams on port 7 error = socketBind(context->socket, &IP_ADDR_ANY, ECHO_PORT); //Unable to bind the socket to the desired port? if(error) break; #if (NET_STATIC_OS_RESOURCES == ENABLED) //Create a task to handle incoming datagrams osCreateStaticTask(&udpEchoTaskStruct, "UDP Echo", udpEchoTask, context, udpEchoTaskStack, ECHO_SERVICE_STACK_SIZE, ECHO_SERVICE_PRIORITY); #else //Create a task to handle incoming datagrams task = osCreateTask("UDP Echo", udpEchoTask, context, ECHO_SERVICE_STACK_SIZE, ECHO_SERVICE_PRIORITY); //Unable to create the task? if(task == OS_INVALID_HANDLE) { //Report an error to the calling function error = ERROR_OUT_OF_RESOURCES; break; } #endif //End of exception handling block } while(0); //Any error to report? if(error) { //Clean up side effects... socketClose(context->socket); osFreeMem(context); } //Return status code return error; }
error_t ping(NetInterface *interface, const IpAddr *ipAddr, time_t timeout, time_t *rtt) { error_t error; uint_t i; size_t length; uint16_t identifier; uint16_t sequenceNumber; time_t startTime; time_t roundTripTime; Socket *socket; IcmpEchoMessage *message; //Debug message TRACE_INFO("Pinging %s with 64 bytes of data...\r\n", ipAddrToString(ipAddr, NULL)); //Length of the complete ICMP message including header and data length = sizeof(IcmpEchoMessage) + PING_DATA_SIZE; //Allocate memory buffer to hold an ICMP message message = osMemAlloc(length); //Failed to allocate memory? if(!message) return ERROR_OUT_OF_MEMORY; //Identifier field is used to help matching requests and replies identifier = rand(); //Sequence Number field is increment each time an Echo Request is sent sequenceNumber = osAtomicInc16(&pingSequenceNumber); //Format ICMP Echo Request message message->type = ICMP_TYPE_ECHO_REQUEST; message->code = 0; message->checksum = 0; message->identifier = identifier; message->sequenceNumber = sequenceNumber; //Copy data for(i = 0; i < PING_DATA_SIZE; i++) message->data[i] = i; #if (IPV4_SUPPORT == ENABLED) //Target address is an IPv4 address? if(ipAddr->length == sizeof(Ipv4Addr)) { Ipv4Addr srcIpAddr; //Select the source IPv4 address and the relevant network //interface to use when pinging the specified host error = ipv4SelectSourceAddr(&interface, ipAddr->ipv4Addr, &srcIpAddr); //Any error to report? if(error) { //Free previously allocated memory osMemFree(message); //Return the corresponding error code return error; } //ICMP Echo Request message message->type = ICMP_TYPE_ECHO_REQUEST; //Message checksum calculation message->checksum = ipCalcChecksum(message, length); //Open a raw socket socket = socketOpen(SOCKET_TYPE_RAW, SOCKET_PROTOCOL_ICMP); } else #endif #if (IPV6_SUPPORT == ENABLED) //Target address is an IPv6 address? if(ipAddr->length == sizeof(Ipv6Addr)) { Ipv6PseudoHeader pseudoHeader; //Select the source IPv6 address and the relevant network //interface to use when pinging the specified host error = ipv6SelectSourceAddr(&interface, &ipAddr->ipv6Addr, &pseudoHeader.srcAddr); //Any error to report? if(error) { //Free previously allocated memory osMemFree(message); //Return the corresponding error code return error; } //ICMPv6 Echo Request message message->type = ICMPV6_TYPE_ECHO_REQUEST; //Format IPv6 pseudo header pseudoHeader.destAddr = ipAddr->ipv6Addr; pseudoHeader.length = htonl(length); pseudoHeader.reserved = 0; pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER; //Message checksum calculation message->checksum = ipCalcUpperLayerChecksum( &pseudoHeader, sizeof(Ipv6PseudoHeader), message, length); //Open a raw socket socket = socketOpen(SOCKET_TYPE_RAW, SOCKET_PROTOCOL_ICMPV6); } else #endif //Target address is not valid? { //Free previously allocated memory osMemFree(message); //Report an error return ERROR_INVALID_ADDRESS; } //Failed to open socket? if(!socket) { //Free previously allocated memory osMemFree(message); //Report an error return ERROR_OPEN_FAILED; } //Associate the newly created socket with the relevant interface error = socketBindToInterface(socket, interface); //Unable to bind the socket to the desired interface? if(error) { //Free previously allocated memory osMemFree(message); //Close socket socketClose(socket); //Return status code return error; } //Connect the socket to the target host error = socketConnect(socket, ipAddr, 0); //Any error to report? if(error) { //Free previously allocated memory osMemFree(message); //Close socket socketClose(socket); //Return status code return error; } //Send Echo Request message error = socketSend(socket, message, length, NULL, 0); //Failed to send message ? if(error) { //Free previously allocated memory osMemFree(message); //Close socket socketClose(socket); //Return status code return error; } //Save the time at which the request was sent startTime = osGetTickCount(); //Timeout value exceeded? while((osGetTickCount() - startTime) < timeout) { //Adjust receive timeout error = socketSetTimeout(socket, timeout); //Any error to report? if(error) break; //Wait for an incoming ICMP message error = socketReceive(socket, message, sizeof(IcmpEchoMessage) + PING_DATA_SIZE, &length, 0); //Any error to report? if(error) break; //Check message length if(length != (sizeof(IcmpEchoMessage) + PING_DATA_SIZE)) continue; //Verify message type if(ipAddr->length == sizeof(Ipv4Addr) && message->type != ICMP_TYPE_ECHO_REPLY) continue; if(ipAddr->length == sizeof(Ipv6Addr) && message->type != ICMPV6_TYPE_ECHO_REPLY) continue; //Response identifier matches request identifier? if(message->identifier != identifier) continue; //Make sure the sequence number is correct if(message->sequenceNumber != sequenceNumber) continue; //Loop through data field for(i = 0; i < PING_DATA_SIZE; i++) { //Compare received data against expected data if(message->data[i] != i) break; } //Valid Echo Reply message received? if(i == PING_DATA_SIZE) { //Calculate round-trip time roundTripTime = osGetTickCount() - startTime; //Debug message TRACE_INFO("Echo received (round-trip time = %ums)...\r\n", roundTripTime); //Free previously allocated memory osMemFree(message); //Close socket socketClose(socket); //Return round-trip time if(rtt) *rtt = roundTripTime; //No error to report return NO_ERROR; } } //Debug message TRACE_INFO("No echo received!\r\n"); //Free previously allocated memory osMemFree(message); //Close socket socketClose(socket); //No Echo Reply received from host... return ERROR_NO_RESPONSE; }
static int initWebs(void) { struct in_addr intaddr; #ifdef GA_HOSTNAME_SUPPORT struct hostent *hp; char host[128]; #else const char *lan_ip = nvram_bufget(RT2860_NVRAM, "lan_ipaddr"); #endif char webdir[128]; char *cp; char_t wbuf[128]; /* * Initialize the socket subsystem */ socketOpen(); #ifdef USER_MANAGEMENT_SUPPORT /* * Initialize the User Management database */ char *admu = (char *) nvram_bufget(RT2860_NVRAM, "Login"); char *admp = (char *) nvram_bufget(RT2860_NVRAM, "Password"); umOpen(); //umRestore(T("umconfig.txt")); //winfred: instead of using umconfig.txt, we create 'the one' adm defined in nvram umAddGroup(T("adm"), 0x07, AM_DIGEST, FALSE, FALSE); if (admu && strcmp(admu, "") && admp && strcmp(admp, "")) { umAddUser(admu, admp, T("adm"), FALSE, FALSE); umAddAccessLimit(T("/"), AM_DIGEST, FALSE, T("adm")); } else error(E_L, E_LOG, T("gohead.c: Warning: empty administrator account or password")); #endif #ifdef GA_HOSTNAME_SUPPORT /* * Define the local Ip address, host name, default home page and the * root web directory. */ if (gethostname(host, sizeof(host)) < 0) { error(E_L, E_LOG, T("gohead.c: Can't get hostname")); return -1; } if ((hp = gethostbyname(host)) == NULL) { error(E_L, E_LOG, T("gohead.c: Can't get host address")); return -1; } memcpy((char *) &intaddr, (char *) hp->h_addr_list[0], (size_t) hp->h_length); #else /* * get ip address from nvram configuration (we executed initInternet) */ if (NULL == lan_ip) { error(E_L, E_LOG, T("initWebs: cannot find lan_ip in NVRAM")); return -1; } intaddr.s_addr = inet_addr(lan_ip); if (intaddr.s_addr == INADDR_NONE) { error(E_L, E_LOG, T("initWebs: failed to convert %s to binary ip data"), lan_ip); return -1; } #endif /* * Set rootWeb as the root web. Modify this to suit your needs */ sprintf(webdir, "%s", rootWeb); /* * Configure the web server options before opening the web server */ websSetDefaultDir(webdir); cp = inet_ntoa(intaddr); ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf))); websSetIpaddr(wbuf); #ifdef GA_HOSTNAME_SUPPORT ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf))); #else //use ip address (already in wbuf) as host #endif websSetHost(wbuf); /* * Configure the web server options before opening the web server */ websSetDefaultPage(T("default.asp")); websSetPassword(password); /* * Open the web server on the given port. If that port is taken, try * the next sequential port for up to "retries" attempts. */ websOpenServer(port, retries); /* * First create the URL handlers. Note: handlers are called in sorted order * with the longest path handler examined first. Here we define the security * handler, forms handler and the default web page handler. */ websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0); websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0); websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST); /* * Define our functions */ formDefineUtilities(); formDefineInternet(); #if defined CONFIG_RALINKAPP_SWQOS formDefineQoS(); #endif #if defined CONFIG_USB formDefineUSB(); #endif #if defined CONFIG_RALINKAPP_MPLAYER formDefineMedia(); #endif formDefineWireless(); #if defined (RTDEV_SUPPORT) formDefineInic(); #elif defined (CONFIG_RT2561_AP) || defined (CONFIG_RT2561_AP_MODULE) formDefineLegacy(); #endif #if defined CONFIG_RT2860V2_STA || defined CONFIG_RT2860V2_STA_MODULE formDefineStation(); #endif formDefineFirewall(); formDefineManagement(); /* * Create the Form handlers for the User Management pages */ #ifdef USER_MANAGEMENT_SUPPORT //formDefineUserMgmt(); winfred: we do it ourselves #endif /* * Create a handler for the default home page */ websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); return 0; }
error_t ftpConnect(FtpClientContext *context, NetInterface *interface, IpAddr *serverAddr, uint16_t serverPort, uint_t flags) { error_t error; uint_t replyCode; //Invalid context? if(context == NULL) return ERROR_INVALID_PARAMETER; //Clear context memset(context, 0, sizeof(FtpClientContext)); //Underlying network interface context->interface = interface; //Save the IP address of the FTP server context->serverAddr = *serverAddr; //Use passive mode? if(flags & FTP_PASSIVE_MODE) context->passiveMode = TRUE; //Open control socket context->controlSocket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); //Failed to open socket? if(!context->controlSocket) return ERROR_OPEN_FAILED; //Start of exception handling block do { //Bind the socket to a particular network interface? if(context->interface != NULL) { //Associate the socket with the relevant interface error = socketBindToInterface(context->controlSocket, context->interface); //Any error to report? if(error) break; } //Set timeout for blocking operations error = socketSetTimeout(context->controlSocket, FTP_CLIENT_DEFAULT_TIMEOUT); //Any error to report? if(error) break; //Specify the size of the send buffer error = socketSetTxBufferSize(context->controlSocket, FTP_CLIENT_SOCKET_MIN_TX_BUFFER_SIZE); //Any error to report? if(error) break; //Specify the size of the receive buffer error = socketSetRxBufferSize(context->controlSocket, FTP_CLIENT_SOCKET_MIN_RX_BUFFER_SIZE); //Any error to report? if(error) break; //Connect to the FTP server error = socketConnect(context->controlSocket, serverAddr, serverPort); //Connection to server failed? if(error) break; //Wait for the connection greeting reply error = ftpSendCommand(context, NULL, &replyCode); //Any communication error to report? if(error) break; //Check FTP response code if(!FTP_REPLY_CODE_2YZ(replyCode)) error = ERROR_UNEXPECTED_RESPONSE; //End of exception handling block } while(0); //Any error to report? if(error) { //Clean up side effects socketClose(context->controlSocket); context->controlSocket = NULL; } //Return status code return error; }
error_t httpClientTest(void) { error_t error; size_t length; IpAddr ipAddr; Socket *socket; static char_t buffer[256]; //Debug message TRACE_INFO("\r\n\r\nResolving server name...\r\n"); //Resolve HTTP server name error = getHostByName(NULL, CLIENT_SERVER_NAME, &ipAddr, 1, NULL, 0); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to resolve server name!\r\n"); //Exit immedialtely return error; } //Create a new socket to handle the request socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_PROTOCOL_TCP); //Any error to report? if(!socket) { //Debug message TRACE_INFO("Failed to open socket!\r\n"); //Exit immedialtely return error; } //Start of exception handling block do { //Debug message TRACE_INFO("Connecting to HTTP server %s\r\n", ipAddrToString(&ipAddr, NULL)); //Connect to the HTTP server error = socketConnect(socket, &ipAddr, CLIENT_SERVER_PORT); //Any error to report? if(error) break; //Debug message TRACE_INFO("Successful connection\r\n"); //Format HTTP request length = sprintf(buffer, "GET %s HTTP/1.0\r\nHost: %s:%u\r\n\r\n", CLIENT_REQUEST_URI, CLIENT_SERVER_NAME, CLIENT_SERVER_PORT); //Debug message TRACE_INFO("\r\nHTTP request:\r\n%s", buffer); //Send HTTP request error = socketSend(socket, buffer, length, NULL, 0); //Any error to report? if(error) break; //Debug message TRACE_INFO("HTTP response header:\r\n"); //Parse HTTP response header while(1) { //Read the header line by line error = socketReceive(socket, buffer, sizeof(buffer) - 1, &length, SOCKET_FLAG_BREAK_CRLF); //End of stream? if(error) break; //Properly terminate the string with a NULL character buffer[length] = '\0'; //Dump current data TRACE_INFO("%s", buffer); //The end of the header has been reached? if(!strcmp(buffer, "\r\n")) break; } //Debug message TRACE_INFO("HTTP response body:\r\n"); //Parse HTTP response body while(1) { //Read response body error = socketReceive(socket, buffer, sizeof(buffer) - 1, &length, 0); //End of stream? if(error) break; //Properly terminate the string with a NULL character buffer[length] = '\0'; //Dump current data TRACE_INFO("%s", buffer); } //End of exception handling block } while(0); //Close the connection socketClose(socket); //Debug message TRACE_INFO("\r\nConnection closed...\r\n"); //Return status code return error; }
error_t sslClientTest(void) { error_t error; size_t length; IpAddr ipAddr; static char_t buffer[256]; //Underlying socket Socket *socket = NULL; //SSL/TLS context TlsContext *tlsContext = NULL; //Debug message TRACE_INFO("Resolving server name...\r\n"); //Resolve SSL server name error = getHostByName(NULL, APP_SERVER_NAME, &ipAddr, 0); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to resolve server name!\r\n"); //Exit immediately return error; } //Create a new socket to handle the request socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); //Any error to report? if(!socket) { //Debug message TRACE_INFO("Failed to open socket!\r\n"); //Exit immediately return ERROR_OPEN_FAILED; } //Start of exception handling block do { //Debug message TRACE_INFO("Connecting to SSL server %s\r\n", ipAddrToString(&ipAddr, NULL)); //Connect to the SSL server error = socketConnect(socket, &ipAddr, APP_SERVER_PORT); //Any error to report? if(error) break; //Initialize SSL/TLS context tlsContext = tlsInit(); //Initialization failed? if(!tlsContext) { //Report an error error = ERROR_OUT_OF_MEMORY; //Exit immediately break; } //Bind TLS to the relevant socket error = tlsSetSocket(tlsContext, socket); //Any error to report? if(error) break; //Select client operation mode error = tlsSetConnectionEnd(tlsContext, TLS_CONNECTION_END_CLIENT); //Any error to report? if(error) break; //Set the PRNG algorithm to be used error = tlsSetPrng(tlsContext, YARROW_PRNG_ALGO, &yarrowContext); //Any error to report? if(error) break; #if (APP_SET_CIPHER_SUITES == ENABLED) //Preferred cipher suite list error = tlsSetCipherSuites(tlsContext, cipherSuites, arraysize(cipherSuites)); //Any error to report? if(error) break; #endif #if (APP_SET_SERVER_NAME == ENABLED) //Set the fully qualified domain name of the server error = tlsSetServerName(tlsContext, APP_SERVER_NAME); //Any error to report? if(error) break; #endif #if (APP_SET_TRUSTED_CA_LIST == ENABLED) //Import the list of trusted CA certificates error = tlsSetTrustedCaList(tlsContext, trustedCaList, trustedCaListLength); //Any error to report? if(error) break; #endif #if (APP_SET_CLIENT_CERT == ENABLED) //Import the client's certificate error = tlsAddCertificate(tlsContext, clientCert, clientCertLength, clientPrivateKey, clientPrivateKeyLength); //Any error to report? if(error) break; #endif //Establish a secure session error = tlsConnect(tlsContext); //TLS handshake failure? if(error) break; //Format HTTP request sprintf(buffer, "GET %s HTTP/1.0\r\nHost: %s:%u\r\n\r\n", APP_REQUEST_URI, APP_SERVER_NAME, APP_SERVER_PORT); //Debug message TRACE_INFO("\r\n"); TRACE_INFO("HTTP request:\r\n%s", buffer); //Send the request error = tlsWrite(tlsContext, buffer, strlen(buffer), 0); //Any error to report? if(error) break; //Debug message TRACE_INFO("HTTP response:\r\n"); //Read the whole response while(1) { //Read data error = tlsRead(tlsContext, buffer, sizeof(buffer) - 1, &length, 0); //End of stream? if(error) break; //Properly terminate the string with a NULL character buffer[length] = '\0'; //Debug message TRACE_INFO("%s", buffer); } //Successfull processing error = NO_ERROR; //End of exception handling block } while(0); //Any error to report? if(error) { //Debug message TRACE_INFO("Failed to communicate with SSL server!\r\n"); } //Terminate TLS session tlsFree(tlsContext); //Close socket socketClose(socket); //Debug message TRACE_INFO("Connection closed...\r\n"); //Return status code return error; }
static int initWebs() { struct hostent *hp; struct in_addr intaddr; char *cp; char host[64], dir[128]; char_t dir_t[128]; char_t wbuf[256]; /* * Initialize the socket subsystem */ socketOpen(); /* * Initialize the User Management database */ #ifdef USER_MANAGEMENT_SUPPORT umOpen(); umRestore(T("umconfig.txt")); #endif /* * Define the local Ip address, host name, default home page and the * root web directory. */ if (gethostname(host, sizeof(host)) < 0) { error(E_L, E_LOG, T("Can't get hostname")); return -1; } if ((hp = gethostbyname(host)) == NULL) { error(E_L, E_LOG, T("Can't get host address")); return -1; } memcpy((void *) &intaddr, (void *) hp->h_addr_list[0], (size_t) hp->h_length); /* * Set ../web as the root web. Modify this to suit your needs */ getcwd(dir, sizeof(dir)); for (cp = dir; *cp; cp++) { if (*cp == '\\') *cp = '/'; } if (cp = strrchr(dir, '/')) { *cp = '\0'; } ascToUni(dir_t, dir, sizeof(dir_t)); gsprintf(wbuf, T("%s/%s"), dir_t, rootWeb); websSetDefaultDir(wbuf); cp = inet_ntoa(intaddr); ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf))); websSetIpaddr(wbuf); ascToUni(wbuf, hp->h_name, min(strlen(hp->h_name) + 1, sizeof(wbuf))); websSetHost(wbuf); /* * Configure the web server options before opening the web server */ websSetDefaultPage(T("default.asp")); websSetPassword(password); /* * Open the web server on the given port. If that port is taken, try * the next sequential port for up to "retries" attempts. */ websOpenServer(port, retries); /* * First create the URL handlers. Note: handlers are called in sorted order * with the longest path handler examined first. Here we define the security * handler, forms handler and the default web page handler. */ websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0); websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0); websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST); /* * Now define two test procedures. Replace these with your application * relevant ASP script procedures and form functions. */ websAspDefine(T("aspTest"), aspTest); websFormDefine(T("formTest"), formTest); /* * Create the Form handlers for the User Management pages */ #ifdef USER_MANAGEMENT_SUPPORT formDefineUserMgmt(); #endif /* * Create a handler for the default home page */ websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); /* * Set the socket service timeout to the default */ sockServiceTime = SOCK_DFT_SVC_TIME; return 0; }
error_t dhcpv6RelayStart(Dhcpv6RelayCtx *context, const Dhcpv6RelaySettings *settings) { error_t error; uint_t i; OsTask *task; //Debug message TRACE_INFO("Starting DHCPv6 relay agent...\r\n"); //Ensure the parameters are valid if(!context || !settings) return ERROR_INVALID_PARAMETER; //The pointer to the network-facing interface shall be valid if(!settings->serverInterface) return ERROR_INVALID_INTERFACE; //Check the number of client-facing interfaces if(!settings->clientInterfaceCount) return ERROR_INVALID_PARAMETER; if(settings->clientInterfaceCount >= DHCPV6_RELAY_MAX_CLIENT_IF) return ERROR_INVALID_PARAMETER; //Loop through the client-facing interfaces for(i = 0; i < settings->clientInterfaceCount; i++) { //A valid pointer is required for each interface if(!settings->clientInterface[i]) return ERROR_INVALID_INTERFACE; } //Check the address to be used when forwarding messages to the server if(ipv6CompAddr(&settings->serverAddress, &IPV6_UNSPECIFIED_ADDR)) return ERROR_INVALID_ADDRESS; //Clear the DHCPv6 relay agent context memset(context, 0, sizeof(Dhcpv6RelayCtx)); //Save the network-facing interface context->serverInterface = settings->serverInterface; //Save the number of client-facing interfaces context->clientInterfaceCount = settings->clientInterfaceCount; //Save all the client-facing interfaces for(i = 0; i < context->clientInterfaceCount; i++) context->clientInterface[i] = settings->clientInterface[i]; //Save the address to be used when relaying client messages to the server context->serverAddress = settings->serverAddress; //Join the All_DHCP_Relay_Agents_and_Servers multicast group //for each client-facing interface error = dhcpv6RelayJoinMulticastGroup(context); //Any error to report? if(error) return error; //Start of exception handling block do { //Open a UDP socket to handle the network-facing interface context->serverSocket = socketOpen(SOCKET_TYPE_DGRAM, SOCKET_PROTOCOL_UDP); //Failed to open socket? if(!context->serverSocket) { //Report an error error = ERROR_OPEN_FAILED; //Stop processing break; } //Explicitly associate the socket with the relevant interface error = socketBindToInterface(context->serverSocket, context->serverInterface); //Unable to bind the socket to the desired interface? if(error) break; //Relay agents listen for DHCPv6 messages on UDP port 547 error = socketBind(context->serverSocket, &IP_ADDR_ANY, DHCPV6_SERVER_PORT); //Unable to bind the socket to the desired port? if(error) break; //Only accept datagrams with source port number 547 error = socketConnect(context->serverSocket, &IP_ADDR_ANY, DHCPV6_SERVER_PORT); //Any error to report? if(error) break; //If the relay agent relays messages to the All_DHCP_Servers address //or other multicast addresses, it sets the Hop Limit field to 32 //Loop through the client-facing interfaces for(i = 0; i < context->clientInterfaceCount; i++) { //Open a UDP socket to handle the current interface context->clientSocket[i] = socketOpen(SOCKET_TYPE_DGRAM, SOCKET_PROTOCOL_UDP); //Failed to open socket? if(!context->clientSocket[i]) { //Report an error error = ERROR_OPEN_FAILED; //Stop processing break; } //Explicitly associate the socket with the relevant interface error = socketBindToInterface(context->clientSocket[i], context->clientInterface[i]); //Unable to bind the socket to the desired interface? if(error) break; //Relay agents listen for DHCPv6 messages on UDP port 547 error = socketBind(context->clientSocket[i], &IP_ADDR_ANY, DHCPV6_SERVER_PORT); //Unable to bind the socket to the desired port? if(error) break; //Only accept datagrams with source port number 546 error = socketConnect(context->clientSocket[i], &IP_ADDR_ANY, DHCPV6_CLIENT_PORT); //Any error to report? if(error) break; } //Propagate exception if necessary... if(error) break; //Create event objects context->event = osEventCreate(FALSE, FALSE); context->ackEvent = osEventCreate(FALSE, FALSE); //Out of resources? if(context->event == OS_INVALID_HANDLE || context->ackEvent == OS_INVALID_HANDLE) { //Report an error error = ERROR_OUT_OF_RESOURCES; //Stop processing break; } //The DHCPv6 relay agent is now running context->running = TRUE; //Start the DHCPv6 relay agent service task = osTaskCreate("DHCPv6 Relay", dhcpv6RelayTask, context, DHCPV6_RELAY_STACK_SIZE, DHCPV6_RELAY_PRIORITY); //Unable to create the task? if(task == OS_INVALID_HANDLE) error = ERROR_OUT_OF_RESOURCES; //End of exception handling block } while(0); //Did we encounter an error? if(error) { //Close the socket associated with the network-facing interface socketClose(context->serverSocket); //Close the socket associated with each client-facing interface for(i = 0; i < context->clientInterfaceCount; i++) socketClose(context->clientSocket[i]); //Leave the All_DHCP_Relay_Agents_and_Servers multicast group //for each client-facing interface dhcpv6RelayLeaveMulticastGroup(context); //Close event objects osEventClose(context->event); osEventClose(context->ackEvent); } //Return status code return error; }
static int initWebs() { struct in_addr intaddr; char *pString; char host[64], webdir[128]; char_t wbuf[128]; /* * Initialize the socket and sym subsystems */ socketOpen(); symSubOpen(); #ifdef USER_MANAGEMENT_SUPPORT /* * Initialize the User Management database */ umOpen(); umRestore(T("umconfig.txt")); #endif /* * Define the local Ip address, host name, default home page and the * root web directory. */ if (gethostname(host, sizeof(host)) < 0) { error(E_L, E_LOG, T("Can't get hostname")); return -1; } intaddr.s_addr = (unsigned long) hostGetByName(host); /* * Set ../web as the root web. Modify this to suit your needs */ sprintf(webdir, "%s/%s", ROOT_DIR, rootWeb); /* * Configure the web server options before opening the web server */ websSetDefaultDir(webdir); pString = inet_ntoa(intaddr); ascToUni(wbuf, pString, min(strlen(pString) + 1, sizeof(wbuf))); free(pString); websSetIpaddr(wbuf); ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf))); websSetHost(wbuf); /* * Configure the web server options before opening the web server */ websSetDefaultPage(T("default.asp")); websSetPassword(password); /* * Open the web server on the given port. If that port is taken, try * the next sequential port for up to "retries" attempts. */ websOpenServer(port, retries); /* * First create the URL handlers. Note: handlers are called in sorted order * with the longest path handler examined first. Here we define the security * handler, forms handler and the default web page handler. */ websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0); websUrlHandlerDefine(T("/cgi-bin"), NULL, 0, websCgiHandler, 0); websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST); /* * Now define two test procedures. Replace these with your application * relevant ASP script procedures and form functions. */ websAspDefine(T("aspTest"), aspTest); websFormDefine(T("formTest"), formTest); /* * Create the Form handlers for the User Management pages */ #ifdef USER_MANAGEMENT_SUPPORT formDefineUserMgmt(); #endif /* * Create a handler for the default home page */ websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); /* * Provide signal for clean up on termination. */ signal(SIGTERM, websTermSigHandler); signal(SIGKILL, websTermSigHandler); return 0; }
error_t icecastClientConnect(IcecastClientContext *context) { error_t error; size_t length; IpAddr serverIpAddr; //Icecast request template const char_t requestTemplate[] = "GET /%s HTTP/1.1\r\n" "Host: %s\r\n" "User-agent: UserAgent\r\n" "Icy-MetaData: 1\r\n" "Connection: close\r\n" "\r\n"; //The specified Icecast server can be either an IP or a host name error = getHostByName(context->settings.interface, context->settings.serverName, &serverIpAddr, 1, NULL, 0); //Unable to resolve server name? if(error) return error; //Open a TCP socket context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_PROTOCOL_TCP); //Failed to open socket? if(!context->socket) return ERROR_OUT_OF_RESOURCES; //Start of exception handling block do { //Adjust receive timeout error = socketSetTimeout(context->socket, ICECAST_CLIENT_TIMEOUT); //Any error to report? if(error) return error; //Connect to the specified Icecast server error = socketConnect(context->socket, &serverIpAddr, context->settings.serverPort); //Connection with server failed? if(error) return error; //Format Icecast request length = sprintf(context->buffer, requestTemplate, context->settings.resource, context->settings.serverName); //Debug message TRACE_DEBUG(context->buffer); //Send Icecast request error = socketSend(context->socket, context->buffer, length, NULL, SOCKET_FLAG_WAIT_ACK); //Failed to send the request? if(error) return error; //Parse response header while(1) { char_t *separator; char_t *property; char_t *value; //Read a line from the response header error = socketReceive(context->socket, context->buffer, ICECAST_CLIENT_METADATA_MAX_SIZE, &length, SOCKET_FLAG_BREAK_CRLF); //Failed to read data? if(error) break; //Properly terminate the string with a NULL character context->buffer[length] = '\0'; //The end of the header has been reached? if(!strcmp(context->buffer, "\r\n")) break; //Check whether a separator is present separator = strchr(context->buffer, ':'); //Separator found? if(separator) { //Split the line *separator = '\0'; //Get property name and value property = strTrimWhitespace(context->buffer); value = strTrimWhitespace(separator + 1); //Debug message TRACE_INFO("<%s>=<%s>\r\n", property, value); //Icy-Metaint property found? if(!strcasecmp(property, "Icy-Metaint")) context->blockSize = atoi(value); } } //End of exception handling block } while(0); //Check whether an error occurred if(error) { //Clean up side effects socketClose(context->socket); } //Return status code return error; }
static int initWebs() { char host[128]; char *cp; char_t wbuf[128]; /* * Initialize networking. */ init_all_network_interfaces(); /* * Initialize the socket subsystem */ socketOpen(); /* * Configure the web server options before opening the web server */ websSetDefaultDir("/"); cp = inet_ntoa(eth0_bootp_data.bp_yiaddr); ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf))); websSetIpaddr(wbuf); ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf))); websSetHost(wbuf); /* * Configure the web server options before opening the web server */ websSetDefaultPage(T("default.asp")); websSetPassword(password); /* * Open the web server on the given port. If that port is taken, try * the next sequential port for up to "retries" attempts. */ websOpenServer(port, retries); /* * First create the URL handlers. Note: handlers are called in sorted order * with the longest path handler examined first. Here we define the security * handler, forms handler and the default web page handler. */ websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0); websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, WEBS_HANDLER_LAST); /* * Now define two test procedures. Replace these with your application * relevant ASP script procedures and form functions. */ websAspDefine(T("aspTest"), aspTest); websFormDefine(T("formTest"), formTest); /* * Create a handler for the default home page */ websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); return 0; }
error_t ftpOpenFile(FtpClientContext *context, const char_t *path, uint_t flags) { error_t error; uint_t replyCode; IpAddr ipAddr; uint16_t port; //Invalid context? if(context == NULL) return ERROR_INVALID_PARAMETER; //Open data socket context->dataSocket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP); //Failed to open socket? if(!context->dataSocket) return ERROR_OPEN_FAILED; //Start of exception handling block do { //Bind the socket to a particular network interface? if(context->interface != NULL) { //Associate the socket with the relevant interface error = socketBindToInterface(context->dataSocket, context->interface); //Any error to report? if(error) break; } //Set timeout for blocking operations error = socketSetTimeout(context->dataSocket, FTP_CLIENT_DEFAULT_TIMEOUT); //Any error to report? if(error) break; //Check data transfer direction if(flags & (FTP_FOR_WRITING | FTP_FOR_APPENDING)) { //Maximize transmission throughput by using a large buffer error = socketSetTxBufferSize(context->dataSocket, FTP_CLIENT_SOCKET_MAX_TX_BUFFER_SIZE); //Any error to report? if(error) break; //Use a small buffer for the reception path error = socketSetRxBufferSize(context->dataSocket, FTP_CLIENT_SOCKET_MIN_RX_BUFFER_SIZE); //Any error to report? if(error) break; } else { //Use a small buffer for the transmission path error = socketSetTxBufferSize(context->dataSocket, FTP_CLIENT_SOCKET_MIN_TX_BUFFER_SIZE); //Any error to report? if(error) break; //Maximize reception throughput by using a large buffer error = socketSetRxBufferSize(context->dataSocket, FTP_CLIENT_SOCKET_MAX_RX_BUFFER_SIZE); //Any error to report? if(error) break; } //Set representation type if(flags & FTP_TEXT_TYPE) { //Use ASCII type error = ftpSetType(context, 'A'); //Any error to report? if(error) break; } else { //Use image type error = ftpSetType(context, 'I'); //Any error to report? if(error) break; } //Check transfer mode if(!context->passiveMode) { //Place the data socket in the listening state error = socketListen(context->dataSocket, 1); //Any error to report? if(error) break; //Retrieve local IP address error = socketGetLocalAddr(context->controlSocket, &ipAddr, NULL); //Any error to report? if(error) break; //Retrieve local port number error = socketGetLocalAddr(context->dataSocket, NULL, &port); //Any error to report? if(error) break; //Set the port to be used in data connection error = ftpSetPort(context, &ipAddr, port); //Any error to report? if(error) break; } else { //Enter passive mode error = ftpSetPassiveMode(context, &port); //Any error to report? if(error) break; //Establish data connection error = socketConnect(context->dataSocket, &context->serverAddr, port); //Connection to server failed? if(error) break; } //Format the command if(flags & FTP_FOR_WRITING) sprintf(context->buffer, "STOR %s\r\n", path); else if(flags & FTP_FOR_APPENDING) sprintf(context->buffer, "APPE %s\r\n", path); else sprintf(context->buffer, "RETR %s\r\n", path); //Send the command to the server error = ftpSendCommand(context, context->buffer, &replyCode); //Any error to report? if(error) break; //Check FTP response code if(!FTP_REPLY_CODE_1YZ(replyCode)) { //Report an error error = ERROR_UNEXPECTED_RESPONSE; break; } //Check transfer mode if(!context->passiveMode) { //Wait for the server to connect back to the client's data port Socket *socket = socketAccept(context->dataSocket, NULL, NULL); //No connection request? if(!socket) { //Report an error error = ERROR_FAILURE; break; } //Close the listening socket socketClose(context->dataSocket); //Save socket handle context->dataSocket = socket; //Set timeout for blocking operations error = socketSetTimeout(context->dataSocket, FTP_CLIENT_DEFAULT_TIMEOUT); //Any error to report? if(error) break; } //End of exception handling block } while(0); //Any error to report? if(error) { //Clean up side effects socketClose(context->dataSocket); context->dataSocket = NULL; } //Return status code return error; }
int serverMain(void) { int rc, signal; pthread_t thread_listen, thread_answer; running = 1; signal = SIGTERM; rc = EX_SOFTWARE; if (!sqlite3_threadsafe()) { fprintf(stderr, "thread-safe sqlite3 required\n"); goto error0; } if (pthreadInit()) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error0; } if ((service_addr = socketAddressNew("0.0.0.0", port)) == NULL) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error1; } if ((service = socketOpen(service_addr, 0)) == NULL) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (socketSetReuse(service, 1)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (socketBind(service, &service->address)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error2; } if (serverSignalsInit(&signals)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error3; } if (queueInit(&queries_unused)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error4; } if (queueInit(&queries_waiting)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error5; } if (sqlite3_open(database_path, &db) != SQLITE_OK) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error6; } if (create_database(db)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error7; } if (sqlite3_prepare_v2(db, SQL_SELECT_ONE, -1, &db_select_one, NULL) != SQLITE_OK) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error7; } if (0 < debug) syslog(LOG_DEBUG, "sql=\"%s\"", sqlite3_sql(db_select_one)); if (pthread_create(&thread_answer, NULL, answer_thread, NULL)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error8; } if (pthread_create(&thread_listen, NULL, listen_thread, NULL)) { fprintf(stderr, "%s(%d): %s\n", __FILE__, __LINE__, strerror(errno)); goto error9; } syslog(LOG_INFO, "ready"); signal = serverSignalsLoop(&signals); syslog(LOG_INFO, "signal %d, terminating process", signal); running = 0; rc = EXIT_SUCCESS; (void) pthread_cancel(thread_answer); (void) pthread_join(thread_answer, NULL); error9: (void) pthread_cancel(thread_listen); (void) pthread_join(thread_listen, NULL); error8: sqlite3_finalize(db_select_one); error7: sqlite3_close(db); error6: queueFini(&queries_waiting); error5: queueFini(&queries_unused); error4: serverSignalsFini(&signals); error3: socketClose(service); error2: free(service_addr); error1: pthreadFini(); error0: syslog(LOG_INFO, "signal %d, terminated", signal); return rc; }
bool mainLoop(int argc, char **argv) { const char *port = PORT; int backlog = BACKLOG; int bufferSize = BUFFER_SIZE; bool interactive = INTERACTIVE; args_param_t args_param_list[] = { {"-p", &port, argsString }, {"--port", &port, argsString }, {"-b", &backlog, argsInteger }, {"--backlog", &backlog, argsInteger }, {"-B", &bufferSize, argsInteger }, {"--buffer", &bufferSize, argsInteger }, {"-i", &interactive, argsBoolTrue }, {"--interactive", &interactive, argsBoolTrue }, {"-d", &interactive, argsBoolFalse }, {"--daemon", &interactive, argsBoolFalse }, ARGS_DONE }; argsProcess(argc, argv, args_param_list); int socket = -1; fd_set socketSet; bool success = true; success = success && socketOpen(port, backlog, &socket); success = success && socketSetInitialize(socket, &socketSet, interactive); if (success) { infof("Message buffer size set to %d.", bufferSize); } // main loop int maxSocket = socket; success = success && protocolInit(socket, &socketSet, maxSocket, bufferSize); bool done = !success; // skip loop on error while (!done) { fd_set readSocketSet; FD_COPY(&socketSet, &readSocketSet); success = success && socketSelect(maxSocket, &readSocketSet); for (int s = 0; !done && s <= maxSocket; s++) { if (FD_ISSET(s, &readSocketSet)) { if (s == socket) { socketConnectionNew(s, &maxSocket, &socketSet, bufferSize); // failure is not terminal } else if (STDIN_FILENO == s) { // system control done = localControl(s, &socketSet, maxSocket, bufferSize); } else { // existing connection protocolUpdate(s, &socketSet, maxSocket, bufferSize); // failure is not terminal } } } done = done || !success; } protocolCleanup(socket, &socketSet, maxSocket, bufferSize); // always close(socket); return done; }