/** Free all the arg buffers in the argv, and the argv array itself. * */ void freeArgs(char **argv) { UINT32 i=0; while (argv[i] != NULL) { CMSMEM_FREE_BUF_AND_NULL_PTR(argv[i]); i++; } CMSMEM_FREE_BUF_AND_NULL_PTR(argv); }
CmsRet oal_Net_getGMACPortIfNameList(char **GMACPortIfNameList) { #ifdef CMS_BRCM_GMAC #ifdef DESKTOP_LINUX *GMACPortIfNameList = cmsMem_alloc(512, 0); strcpy(*GMACPortIfNameList, "eth1,eth3"); #else SINT32 skfd; struct ifreq ifr; if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { cmsLog_error("Error openning socket for getting the GMAC enet port list"); return CMSRET_INTERNAL_ERROR; } /* Get the name -> if_index mapping for ethswctl */ strcpy(ifr.ifr_name, "bcmsw"); if (ioctl(skfd, SIOCGIFINDEX, &ifr) < 0) { close(skfd); cmsLog_debug("bcmsw interface does not exist. Error: %d", errno); return CMSRET_INTERNAL_ERROR; } /* Allocate dynamic memory to hold max interface names (eth0,eth1,..eth10<cr>)*/ if ((*GMACPortIfNameList = cmsMem_alloc(((MAX_GMAC_ETH_PORT * (IFNAMSIZ+1)) + 2), ALLOC_ZEROIZE)) == NULL) { cmsLog_error("Fail to alloc mem in getting the GMAC enet port list"); close(skfd); return CMSRET_RESOURCE_EXCEEDED; } memset((void *) &ifr, sizeof(ifr), 0); ifr.ifr_data = *GMACPortIfNameList; if (ioctl(skfd, SIOCGGMACPORT, &ifr) < 0) { cmsLog_error("ioct error in getting the GMAC enet port list. Error: %d", errno); close(skfd); CMSMEM_FREE_BUF_AND_NULL_PTR(*GMACPortIfNameList); return CMSRET_INTERNAL_ERROR; } close(skfd); cmsLog_debug("GMACPortIfNameList=%s, strlen=%d", *GMACPortIfNameList, strlen(*GMACPortIfNameList)); #endif /* DESKTOP_LINUX */ #endif /* CMS_BRCM_GMAC */ return CMSRET_SUCCESS; }
void oalMsg_cleanup(void **msgHandle) { CmsMsgHandle *handle = (CmsMsgHandle *) *msgHandle; if (handle->commFd != CMS_INVALID_FD) { close(handle->commFd); } CMSMEM_FREE_BUF_AND_NULL_PTR((*msgHandle)); return; }
static int myRead(char *outBuf, int inLen) { int readLen = 0; static int xmlCfgLen = 0; static int offset = 0; static CmsMsgHeader *responseMsg=NULL; CmsMsgHeader requestMsg = EMPTY_MSG_HEADER; char *cfgStart; CmsRet ret; if (responseMsg == NULL) { cmsLog_debug("first time, get config file from smd"); /* * This is the first time that we were called. * Send a message to smd to request a copy of the config file. */ requestMsg.src = EID_TFTP; requestMsg.dst = EID_SMD; requestMsg.type = CMS_MSG_GET_CONFIG_FILE; requestMsg.flags_request = 1; if ((ret = cmsMsg_send(msgHandle, &requestMsg)) != CMSRET_SUCCESS) { cmsLog_error("could not send GET_CONFIG_FILE msg to smd."); return -1; } if ((ret = cmsMsg_receive(msgHandle, &responseMsg)) != CMSRET_SUCCESS) { cmsLog_error("could not receive GET_CONFIG_FILE msg from smd."); CMSMEM_FREE_BUF_AND_NULL_PTR(responseMsg); return -1; } xmlCfgLen = (int) responseMsg->dataLength; cmsLog_debug("got config buffer len=%u", xmlCfgLen); } /* config data starts immediately after the header */ cfgStart = (char *) (responseMsg + 1); if (xmlCfgLen <= inLen) readLen = xmlCfgLen; else readLen = inLen; memcpy(outBuf, (cfgStart + offset), readLen); xmlCfgLen -= readLen; offset += readLen; glbUploadSize += readLen; if (xmlCfgLen == 0) { /* done copying all the config data out, free the message */ CMSMEM_FREE_BUF_AND_NULL_PTR(responseMsg); offset = 0; cmsLog_debug("send out entire config buf, free msg"); } return readLen; }
/** Return a pointer to the beginning of the requested flash buffer. * * On DESKTOP_LINUX, this just opens the fake flash file, allocates a buffer, * read contents of fake flash file into buffer, and returns pointer to the * buffer. Persistent flash data is the only thing in the flash file (for now). * If fake flash file is not present, create one and fill it with zeros. */ char *fake_getSharedBlks(int start_block, int num_blocks) { UINT32 bufLen; char *buf=NULL; char path[BUFLEN_1024]; struct stat statbuf; int rc, fd; CmsRet ret; cmsLog_debug("reading block %d through %d", start_block, start_block+num_blocks); if (start_block != 0) { cmsLog_error("cannot handle non-zero start block yet."); return NULL; } if (num_blocks > FAKE_NUM_PSP_FLASH_BLOCKS) { cmsLog_error("requested more blocks than PSP flash blocks, not handled."); return NULL; } /* first allocate the buffer we will need for the read */ bufLen = FAKE_FLASH_BLOCK_SIZE * FAKE_NUM_PSP_FLASH_BLOCKS; if ((buf = cmsMem_alloc(bufLen, ALLOC_ZEROIZE)) == NULL) { cmsLog_error("malloc of %d bytes failed", bufLen); return NULL; } /* form path to the flash file */ if ((ret = cmsUtl_getBaseDir(path, sizeof(path))) != CMSRET_SUCCESS) { cmsLog_error("getBaseDir failed, abort func"); cmsMem_free(buf); return NULL; } else { UINT32 offset; offset = strlen(path); snprintf(&(path[offset]), sizeof(path)-offset, "/%s", FAKE_FLASH_PSP_FILENAME); } cmsLog_debug("checking for flash file at %s", path); if ((rc = stat(path, &statbuf)) < 0) { cmsLog_debug("creating fake flash file and initialize to zeros"); fd = open(path, O_CREAT|O_RDWR, 0644); if (fd < 0) { cmsLog_error("create of flash file %s failed, errno=%d", path, errno); cmsMem_free(buf); return NULL; } /* fill rest of file with zeros */ rc = write(fd, buf, bufLen); cmsLog_debug("filler write returned %d", rc); close(fd); } /* * at this point, we know there is a flash file, so just open it and read it. * Don't bother with offsets for now. Just assume PSP is at the beginning * of the flash. */ fd = open(path, O_RDWR); rc = read(fd, buf, num_blocks * FAKE_FLASH_BLOCK_SIZE); if (rc != num_blocks * FAKE_FLASH_BLOCK_SIZE) { cmsLog_error("unexpected rc %d from read, expected %d", rc, num_blocks * FAKE_FLASH_BLOCK_SIZE); CMSMEM_FREE_BUF_AND_NULL_PTR(buf); } close(fd); return buf; }
CmsRet cmsUtl_parseUrl(const char *url, UrlProto *proto, char **addr, UINT16 *port, char **path) { int n = 0; char *p = NULL; char protocol[BUFLEN_16]; char host[BUFLEN_1024]; char uri[BUFLEN_1024]; if (url == NULL) { cmsLog_debug("url is NULL"); return CMSRET_INVALID_ARGUMENTS; } *port = 0; protocol[0] = host[0] = uri[0] = '\0'; /* proto */ p = (char *) url; if ((p = strchr(url, ':')) == NULL) { return CMSRET_INVALID_ARGUMENTS; } n = p - url; strncpy(protocol, url, n); protocol[n] = '\0'; if (!strcmp(protocol, "http")) { *proto = URL_PROTO_HTTP; } else if (!strcmp(protocol, "https")) { *proto = URL_PROTO_HTTPS; } else if (!strcmp(protocol, "ftp")) { *proto = URL_PROTO_FTP; } else if (!strcmp(protocol, "tftp")) { *proto = URL_PROTO_TFTP; } else { cmsLog_error("unrecognized proto in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } /* skip "://" */ if (*p++ != ':') return CMSRET_INVALID_ARGUMENTS; if (*p++ != '/') return CMSRET_INVALID_ARGUMENTS; if (*p++ != '/') return CMSRET_INVALID_ARGUMENTS; /* host */ { char *pHost = host; while (*p && *p != ':' && *p != '/') { *pHost++ = *p++; } *pHost = '\0'; } if (strlen(host) != 0) { *addr = cmsMem_strdup(host); } else { cmsLog_error("unrecognized host in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } /* end */ if (*p == '\0') { *path = cmsMem_strdup("/"); return CMSRET_SUCCESS; } /* port */ if (*p == ':') { char buf[BUFLEN_16]; char *pBuf = buf; p++; while (isdigit(*p)) { *pBuf++ = *p++; } *pBuf = '\0'; if (strlen(buf) == 0) { CMSMEM_FREE_BUF_AND_NULL_PTR(*addr); cmsLog_error("unrecognized port in URL %s", url); return CMSRET_INVALID_ARGUMENTS; } *port = atoi(buf); } /* path */ if (*p == '/') { char *pUri = uri; while ((*pUri++ = *p++)); *path = cmsMem_strdup(uri); } else { *path = cmsMem_strdup("/"); } return CMSRET_SUCCESS; }