/* WARNING: Removing unreachable block (ram,0x0040dc48) */
/* WARNING: Removing unreachable block (ram,0x0040dc74) */
int dhcp6_set_options(int param_1,void *param_2,int param_3,uint *param_4)
{
int iVar1;
uint uVar2;
void *pvVar3;
uint uVar4;
undefined4 uVar5;
void *__dest;
uint uVar6;
char *pcVar7;
uint uVar8;
uint uVar9;
uint *puVar10;
int *piVar11;
size_t sVar12;
ushort local_40 [2];
void *local_3c;
int local_38;
uint local_34;
int local_30;
local_38 = 0;
local_3c = param_2;
if ((*param_4 == 0) ||
(iVar1 = FUN_0040b9dc(1,*param_4 & 0xffff,param_4[1],&local_3c,param_3,&local_38), iVar1 == 0))
{
if ((param_4[2] != 0) &&
(iVar1 = FUN_0040b9dc(2,param_4[2] & 0xffff,param_4[3],&local_3c,param_3,&local_38),
iVar1 != 0)) {
return -1;
}
for (piVar11 = (int *)param_4[0xc]; piVar11 != (int *)0x0; piVar11 = (int *)*piVar11) {
sVar12 = FUN_0040b550(0,0,piVar11) //possible issue identified - semgrep
if ((int)sVar12 < 0) goto LAB_0040d9cc //possible issue identified - semgrep
if (param_3 - (int)local_3c < (int)sVar12) goto LAB_0040db64;
pvVar3 = malloc(sVar12) //possible issue identified - semgrep
if (pvVar3 == (void *)0x0) {
pcVar7 = "memory allocation failed for IA_NA options";
goto LAB_0040db84;
}
iVar1 = FUN_0040b550(pvVar3,(int)pvVar3 + sVar12,piVar11) //possible issue identified - semgrep
if (iVar1 < 0) {
dprintf(3,"dhcp6_set_options","failed to construct an IA_NA option");
goto LAB_0040dbb8;
}
memcpy(local_3c,pvVar3,sVar12) //possible issue identified - semgrep
free(pvVar3);
local_38 = local_38 + sVar12 //possible issue identified - semgrep
local_3c = (void *)((int)local_3c + sVar12);
}
if ((param_4[4] != 0) && (iVar1 = FUN_0040b9dc(0xe,0,0,&local_3c,param_3,&local_38), iVar1 != 0)
) {
return -1;
}
local_30 = 0;
if (param_4[5] != 0xffffffff) {
local_40[0] = CONCAT11(local_40[0]._1_1_,(char)param_4[5]);
iVar1 = FUN_0040b9dc(7,1,local_40,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
}
uVar2 = param_4[7] & 0xffff;
if (param_4[7] != 0xffffffff) {
local_40[0] = (ushort)(uVar2 << 8) | (ushort)(uVar2 >> 8);
iVar1 = FUN_0040b9dc(8,2,local_40,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
}
puVar10 = (uint *)param_4[0x10];
if (puVar10 != (uint *)0x0) {
do {
local_40[0] = (ushort)puVar10[3] << 8 | (ushort)puVar10[3] >> 8;
iVar1 = FUN_0040b9dc(0xd,2,local_40,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
puVar10 = (uint *)*puVar10;
} while (puVar10 != (uint *)0x0);
}
puVar10 = (uint *)param_4[0xe];
iVar1 = 0;
if (puVar10 != (uint *)0x0) {
do {
puVar10 = (uint *)*puVar10;
iVar1 = iVar1 + 1;
} while (puVar10 != (uint *)0x0);
sVar12 = iVar1 * 2 //possible issue identified - semgrep
pvVar3 = malloc(sVar12) //possible issue identified - semgrep
if (pvVar3 == (void *)0x0) {
pcVar7 = "memory allocation failed for options";
goto LAB_0040e1b8;
}
puVar10 = (uint *)param_4[0xe];
if (puVar10 == (uint *)0x0) {
uVar2 = 0;
}
else {
uVar2 = 0;
do {
uVar4 = puVar10[3];
if ((uVar4 == 0x20) && (param_1 != 0xb)) {
uVar5 = dhcp6msgstr(param_1);
dprintf(7,"dhcp6_set_options","refresh time option is not requested for %s",uVar5);
uVar4 = puVar10[3];
}
puVar10 = (uint *)*puVar10;
*(ushort *)((int)pvVar3 + uVar2) =
(ushort)((uVar4 & 0xffff) << 8) | (ushort)((uVar4 & 0xffff) >> 8);
uVar2 = uVar2 + 2;
} while (puVar10 != (uint *)0x0);
}
if (local_30 != 0) {
__dest = malloc(sVar12 + 2) //possible issue identified - semgrep
if (__dest == (void *)0x0) {
dprintf(3,"dhcp6_set_options","memory allocation failed for options");
goto LAB_0040dbb8;
}
memcpy(__dest,pvVar3,sVar12) //possible issue identified - semgrep
puVar10 = (uint *)param_4[0xe];
iVar1 = 0;
if (puVar10 != (uint *)0x0) {
do {
puVar10 = (uint *)*puVar10;
iVar1 = iVar1 + 1;
} while (puVar10 != (uint *)0x0);
puVar10 = (uint *)(iVar1 * 2);
}
*(undefined2 *)((int)__dest + (int)puVar10) = 0x1400;
uVar2 = uVar2 + 2;
pvVar3 = __dest;
}
if ((0 < (int)uVar2) &&
(iVar1 = FUN_0040b9dc(6,uVar2 & 0xffff,pvVar3,&local_3c,param_3,&local_38), iVar1 != 0)) {
LAB_0040dbb8:
free(pvVar3);
return -1;
}
free(pvVar3);
}
iVar1 = FUN_0040d4a0(0x15,param_4 + 0x14,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x16,param_4 + 0x12,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040d4a0(0x40,param_4 + 0x18,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x62,param_4 + 0x16,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x17,param_4 + 0x1a,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040d4a0(0x18,param_4 + 0x1c,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x1b,param_4 + 0x22,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x1c,param_4 + 0x26,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040d4a0(0x1d,param_4 + 0x24,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040d4a0(0x1e,param_4 + 0x28,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x1f,param_4 + 0x1e,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040d4a0(0x21,param_4 + 0x2c,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
iVar1 = FUN_0040c718(0x22,param_4 + 0x2a,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
for (piVar11 = (int *)param_4[10]; piVar11 != (int *)0x0; piVar11 = (int *)*piVar11) {
sVar12 = FUN_0040b550(0,0,piVar11) //possible issue identified - semgrep
if ((int)sVar12 < 0) goto LAB_0040d9cc //possible issue identified - semgrep
if (param_3 - (int)local_3c < (int)sVar12) goto LAB_0040db64;
pvVar3 = malloc(sVar12) //possible issue identified - semgrep
if (pvVar3 == (void *)0x0) {
pcVar7 = "memory allocation failed for IA_PD options";
LAB_0040db84:
dprintf(5,"dhcp6_set_options",pcVar7);
return -1;
}
iVar1 = FUN_0040b550(pvVar3,(int)pvVar3 + sVar12,piVar11) //possible issue identified - semgrep
if (iVar1 < 0) {
dprintf(3,"dhcp6_set_options","failed to construct an IA_PD option");
goto LAB_0040dbb8;
}
memcpy(local_3c,pvVar3,sVar12) //possible issue identified - semgrep
free(pvVar3);
local_38 = local_38 + sVar12 //possible issue identified - semgrep
local_3c = (void *)((int)local_3c + sVar12);
}
if ((param_4[0x2e] != 0) &&
(iVar1 = FUN_0040b9dc(9,param_4[0x2e] & 0xffff,param_4[0x2f],&local_3c,param_3,&local_38),
iVar1 != 0)) {
return -1;
}
if ((param_4[0x31] != 0) &&
(iVar1 = FUN_0040b9dc(0x12,(short)param_4[0x30],param_4[0x31],&local_3c,param_3,&local_38),
iVar1 != 0)) {
return -1;
}
uVar2 = param_4[8];
if ((uVar2 != 0xffffffff) || (param_4[9] != 0xffffffff)) {
local_34 = uVar2 << 0x18 | uVar2 >> 0x18 | uVar2 >> 8 & 0xff00 | (uVar2 & 0xff00) << 8;
iVar1 = FUN_0040b9dc(0x20,4,&local_34,&local_3c,param_3,&local_38);
if (iVar1 != 0) {
return -1;
}
}
if (param_4[0x33] == 0xffffffff) {
return local_38;
}
if ((param_4[0x32] & 1) == 0) {
if (param_4[0x33] != 2) {
pcVar7 = "unexpected authentication protocol";
LAB_0040e1b8:
dprintf(3,"dhcp6_set_options",pcVar7);
return -1;
}
sVar12 = param_4[0x39] + 0x23 //possible issue identified - semgrep
}
else {
sVar12 = 0xf //possible issue identified - semgrep
}
pvVar3 = malloc(sVar12) //possible issue identified - semgrep
if (pvVar3 == (void *)0x0) {
dprintf(4,"dhcp6_set_options","failed to allocate memory for authentication information");
return -1;
}
memset(pvVar3,0,sVar12);
uVar4 = param_4[0x32];
uVar8 = param_4[0x37];
*(uint *)((int)pvVar3 + 7) = param_4[0x36];
uVar9 = param_4[0x33];
uVar2 = param_4[0x34];
uVar6 = param_4[0x35];
*(uint *)((int)pvVar3 + 0xb) = uVar8;
*(char *)((int)pvVar3 + 5) = (char)uVar2;
*(char *)((int)pvVar3 + 6) = (char)uVar6;
*(char *)((int)pvVar3 + 4) = (char)uVar9;
if ((uVar4 & 1) == 0) {
if (uVar9 != 2) {
dprintf(3,"dhcp6_set_options","unexpected authentication protocol");
free(pvVar3);
return -1;
}
memcpy((void *)((int)pvVar3 + 0xf),(void *)param_4[0x3a],param_4[0x39]) //possible issue identified - semgrep
uVar4 = param_4[0x38];
local_34 = uVar4 << 0x18 | uVar4 >> 0x18 | uVar4 >> 8 & 0xff00 | (uVar4 & 0xff00) << 8;
uVar2 = (int)local_3c + sVar12 + (-0x10 - (int)param_2);
param_4[0x3b] = uVar2;
*(uint *)((int)pvVar3 + 0xf + param_4[0x39]) = local_34;
dprintf(7,"dhcp6_set_options","key ID %x, offset %d",uVar4,uVar2);
}
iVar1 = FUN_0040b9dc(0xb,sVar12 - 4 & 0xffff,(int)pvVar3 + 4,&local_3c,param_3,&local_38) //possible issue identified - semgrep
if (iVar1 == 0) {
free(pvVar3);
return local_38;
}
}
return -1;
LAB_0040d9cc:
pcVar7 = "failed to count option length";
goto LAB_0040d9dc;
LAB_0040db64:
pcVar7 = "short buffer";
LAB_0040d9dc:
dprintf(6,"dhcp6_set_options",pcVar7);
return -1;
}