int _tmain(int argc, NLchar **argv) #endif { NLaddress addr1; NLchar string[NL_MAX_STRING_LENGTH]; if(nlInit() == NL_FALSE) printErrorExit(); _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION)); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); /* init the string improperly with NO null termination */ memset(string, 'a', sizeof(string)); _tprintf(TEXT("Test nlStringToAddr with unterminated string\n")); if(nlStringToAddr(string, &addr1) == NL_FALSE) printError(); _tprintf(TEXT("Test nlGetAddrFromName with unterminated string\n")); if(nlGetAddrFromName(string, &addr1) == NL_FALSE) printError(); _tprintf(TEXT("Test nlGetAddrFromNameAsync with unterminated string\n")); nlGetAddrFromNameAsync(string, &addr1); printError(); _tprintf(TEXT("Done\n")); nlShutdown(); return 0; }
///////////////////// // Initializes network bool InitNetworkSystem() { curl_global_init(CURL_GLOBAL_ALL); nlSystemUseChangeLock.startWriteAccess(); bNetworkInited = false; if(!nlInit()) { SystemError("nlInit failed"); nlSystemUseChangeLock.endWriteAccess(); return false; } if(!nlSelectNetwork(NL_IP)) { SystemError("could not select IP-based network"); nlSystemUseChangeLock.endWriteAccess(); return false; } bNetworkInited = true; dnsCache = new ThreadVar<dnsCacheT>(); #ifndef WIN32 //sigignore(SIGPIPE); signal(SIGPIPE, sigpipe_handler); #endif nlSystemUseChangeLock.endWriteAccess(); return true; }
int main(int argc, char **argv) { NLsocket sock; NLaddress addr, *alladdr; NLbyte string[NL_MAX_STRING_LENGTH]; NLenum type = NL_IP; /* default network type */ NLint count; if(nlInit() == NL_FALSE) printErrorExit(); printf("nlGetString(NL_VERSION) = %s\n\n", nlGetString(NL_VERSION)); printf("nlGetString(NL_NETWORK_TYPES) = %s\n\n", nlGetString(NL_NETWORK_TYPES)); if (argc == 2) { if(strcmp(argv[1], "NL_IPX") == 0) { type = NL_IPX; } else if(strcmp(argv[1], "NL_LOOP_BACK") == 0) { type = NL_LOOP_BACK; } } if(nlSelectNetwork(type) == NL_FALSE) printErrorExit(); /* list all the local addresses */ printf("local addresses are:\n"); alladdr = nlGetAllLocalAddr(&count); while(count-- > 0) { printf(" %s\n", nlAddrToString(alladdr++, string)); } printf("\n"); nlEnable(NL_SOCKET_STATS); /* enable reuse address to run two or more copies on one machine */ nlHint(NL_REUSE_ADDRESS, NL_TRUE); /* create a client socket */ sock = nlOpen(25000, NL_BROADCAST); if(sock == NL_INVALID) printErrorExit(); nlGetLocalAddr(sock, &addr); printf("socket address is %s\n", nlAddrToString(&addr, string)); mainTestLoop(sock); nlShutdown(); return 0; }
int main(int argc, char **argv) { NLsocket sock; NLaddress addr; NLbyte server[] = "status.everquest.com"; NLushort port = 24252; NLbyte command[] = {0xFF, 0xFF, 0x09, 0x00}; NLenum type = NL_UNRELIABLE; /* UDP */ NLbyte buffer[1024]; NLint count; if(!nlInit()) return 1; if(!nlSelectNetwork(NL_IP)) { nlShutdown(); return 1; } nlEnable(NL_BLOCKING_IO); /* create server the address */ nlGetAddrFromName(server, &addr); nlSetAddrPort(&addr, port); /* create the socket */ sock = nlOpen(0, type); if(sock == NL_INVALID) { nlShutdown(); return 1; } /* set the destination address */ nlSetRemoteAddr(sock, &addr); /* send the message */ nlWrite(sock, (NLvoid *)command, (NLint)sizeof(NLulong)); /* read the reply */ count = nlRead(sock, (NLvoid *)buffer, (NLint)sizeof(buffer)); if(count > 0) { printf("Banner is: %s\n", &buffer[4]); } nlShutdown(); return 0; }
int main(int argc, char **argv) { NLsocket sock; NLaddress addr; NLbyte buffer[4096]; int f; NLint count, total = 0; NLint crfound = 0; NLint lffound = 0; if (argc != 4) { printf("\nSyntax: getfile ServerName FullPath LocalFile\n"); return 1; } if(nlInit() == NL_FALSE) printErrorExit(); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); nlEnable(NL_SOCKET_STATS); nlEnable(NL_BLOCKING_IO); nlGetAddrFromName(argv[1], &addr); /* use the standard HTTP port */ nlSetAddrPort(&addr, 80); printf("Server address is %s\n\n", nlAddrToString(&addr, buffer)); /* open the socket and connect to the server */ sock = nlOpen(0, NL_TCP); if(sock == NL_INVALID) printErrorExit(); if(nlConnect(sock, &addr) == NL_FALSE) { printErrorExit(); } printf("Connected\n"); /* open the local file */ f = open(argv[3], O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IWRITE | S_IREAD); if(f < 0) { printf("Could not open local file\n"); printErrorExit(); } /* now let's ask for the file */ #ifdef TEST_GZIP /* this is for my own personal use to test compressed web pages */ sprintf(buffer, "GET %s HTTP/1.1\r\nHost:%s\r\nAccept: */*\r\nAccept-Encoding: gzip\r\nUser-Agent: HawkNL sample program Getfile\r\n\r\n" , argv[2], argv[1]); #else sprintf(buffer, "GET %s HTTP/1.0\r\nHost:%s\r\nAccept: */*\r\nUser-Agent: HawkNL sample program Getfile\r\n\r\n" , argv[2], argv[1]); #endif if(nlWrite(sock, (NLvoid *)buffer, (NLint)strlen(buffer)) == NL_INVALID) { close(f); printErrorExit(); } /* receive the file and write it locally */ while(1 == 1) { count = nlRead(sock, (NLvoid *)buffer, (NLint)sizeof(buffer) - 1); if(count < 0) { NLint err = nlGetError(); /* is the connection closed? */ if(err == NL_MESSAGE_END) { break; } else { close(f); printErrorExit(); } } total += count; if(count > 0) { /* parse out the HTTP header */ if(lffound < 2) { int i; for(i=0;i<count;i++) { if(buffer[i] == 0x0D) { crfound++; } else { if(buffer[i] == 0x0A) { lffound++; } else { /* reset the CR and LF counters back to 0 */ crfound = lffound = 0; } } if(lffound == 2) { /* i points to the second LF */ /* NUL terminate the header string and print it out */ buffer[i] = buffer[i-1] = 0x0; printf(buffer); /* write out the rest to the file */ write(f, &buffer[i+1], count - i - 1); break; } } if(lffound < 2) { /* we reached the end of buffer, so print it out */ buffer[count + 1] = 0x0; printf(buffer); } } else { write(f, buffer, count); printf("received %d bytes at %d bytes per second\r", total, nlGetSocketStat(sock, NL_AVE_BYTES_RECEIVED)); } } } close(f); nlShutdown(); return 0; }
int main(int argc, char **argv) { NLboolean isserver = NL_FALSE; NLsocket serversock; NLsocket clientsock; NLaddress addr; NLbyte server[] = "127.0.0.1:25000"; NLenum type = NL_UNRELIABLE; /* Change this to NL_RELIABLE for reliable connection */ NLbyte string[NL_MAX_STRING_LENGTH]; if(!nlInit()) printErrorExit(); printf("nlGetString(NL_VERSION) = %s\n\n", nlGetString(NL_VERSION)); printf("nlGetString(NL_NETWORK_TYPES) = %s\n\n", nlGetString(NL_NETWORK_TYPES)); if(!nlSelectNetwork(NL_IP)) printErrorExit(); if(argc > 1) { if(!strcmp(argv[1], "-s")) /* server mode */ isserver = NL_TRUE; } if(isserver) { /* create a server socket */ serversock = nlOpen(25000, type); /* just a random port number ;) */ if(serversock == NL_INVALID) printErrorExit(); if(!nlListen(serversock)) /* let's listen on this socket */ { nlClose(serversock); printErrorExit(); } nlGetLocalAddr(serversock, &addr); printf("Server address is %s\n", nlAddrToString(&addr, string)); mainServerLoop(serversock); } else { /* create a client socket */ clientsock = nlOpen(0, type); /* let the system assign the port number */ nlGetLocalAddr(clientsock, &addr); printf("our address is %s\n", nlAddrToString(&addr, string)); if(clientsock == NL_INVALID) printErrorExit(); /* create the NLaddress */ nlStringToAddr(server, &addr); printf("Address is %s\n", nlAddrToString(&addr, string)); /* now connect */ if(!nlConnect(clientsock, &addr)) { nlClose(clientsock); printErrorExit(); } mainClientLoop(clientsock); } nlShutdown(); return 0; }
int _tmain(int argc, NLchar **argv) #endif { NLbyte buffer[NL_MAX_STRING_LENGTH]; NLint count = 0; NLbyte b = (NLbyte)99; NLushort s = 0x1122; NLulong l = 0x11223344; NLfloat f = 12.3141592651f; NLdouble d = 123.12345678901234; NLchar string[NL_MAX_STRING_LENGTH] = TEXT("Hello"); NLbyte block[] = {9,8,7,6,5,4,3,2,1,0}; if(nlInit() == NL_FALSE) printErrorExit(); nlEnable(NL_BIG_ENDIAN_DATA); _tprintf(TEXT("nl_big_endian_data = %d\n"), nlGetBoolean(NL_BIG_ENDIAN_DATA)); _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION)); _tprintf(TEXT("nlGetString(NL_NETWORK_TYPES) = %s\n\n"), nlGetString(NL_NETWORK_TYPES)); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); _tprintf(TEXT("Short number: %#x, "), s); s = nlSwaps(s); _tprintf(TEXT("swapped: %#x, "), s); s = nlSwaps(s); _tprintf(TEXT("swapped back: %#x\n"), s); _tprintf(TEXT("Long number: %#lx, "), l); l = nlSwapl(l); _tprintf(TEXT("swapped: %#lx, "), l); l = nlSwapl(l); _tprintf(TEXT("swapped back: %#lx\n"), l); _tprintf(TEXT("Float number: %.10f, "), f); f = nlSwapf(f); _tprintf(TEXT("swapped: %.10f, "), f); f = nlSwapf(f); _tprintf(TEXT("swapped back: %.10f\n"), f); _tprintf(TEXT("Double number: %.14f, "), d); d = nlSwapd(d); _tprintf(TEXT("swapped: %.24f, "), d); d = nlSwapd(d); _tprintf(TEXT("swapped back: %.14f\n"), d); _tprintf(TEXT("\n")); _tprintf(TEXT("write byte %d to buffer\n"), b); _tprintf(TEXT("write short %#x to buffer\n"), s); _tprintf(TEXT("write long %#lx to buffer\n"), l); _tprintf(TEXT("write float %f to buffer\n"), f); _tprintf(TEXT("write double %.14f to buffer\n"), d); _tprintf(TEXT("write string %s to buffer\n"), string); _tprintf(TEXT("write block %d%d%d%d%d%d%d%d%d%d to buffer\n"), block[0] , block[1], block[2], block[3], block[4], block[5], block[6] , block[7], block[8], block[9]); _tprintf(TEXT("\n")); writeByte(buffer, count, b); writeShort(buffer, count, s); writeLong(buffer, count, l); writeFloat(buffer, count, f); writeDouble(buffer, count, d); writeString(buffer, count, string); writeBlock(buffer, count, block, 10); /* reset count to zero to read from start of buffer */ count = 0; readByte(buffer, count, b); readShort(buffer, count, s); readLong(buffer, count, l); readFloat(buffer, count, f); readDouble(buffer, count, d); readString(buffer, count, string); readBlock(buffer, count, block, 10); _tprintf(TEXT("read byte %d from buffer\n"), b); _tprintf(TEXT("read short %#x from buffer\n"), s); _tprintf(TEXT("read long %#lx from buffer\n"), l); _tprintf(TEXT("read float %f from buffer\n"), f); _tprintf(TEXT("read double %.14f from buffer\n"), d); _tprintf(TEXT("read string %s from buffer\n"), string); _tprintf(TEXT("read block %d%d%d%d%d%d%d%d%d%d from buffer\n"), block[0] , block[1], block[2], block[3], block[4], block[5], block[6] , block[7], block[8], block[9]); nlShutdown(); return 0; }
static int link_dump(bool nltarget_kernel, const char *ifname, int ifindex, struct nlattr **tb, char **recvbuf) { int rc = 0; char nlmsgbuf[NLMSGBUF_SIZE] = { 0, }; struct nlmsghdr *nlm = (struct nlmsghdr *)nlmsgbuf, *resp; struct nlmsgerr *err; char rtattbuf[RATTBUF_SIZE]; struct rtattr *rta; struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC, .ifi_index = ifindex }; unsigned int recvbuflen; uint32_t pid = 0; *recvbuf = NULL; nlInit(nlm, NLM_F_REQUEST, RTM_GETLINK); if (!nlAppend(nlm, sizeof(nlmsgbuf), &ifinfo, sizeof(ifinfo))) goto buffer_too_small; if (ifindex < 0 && ifname) { rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_IFNAME, ifname, strlen(ifname) + 1); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; } if (!nltarget_kernel) { pid = getLldpadPid(); if (pid == 0) return -1; } if (nlComm(nlm, recvbuf, &recvbuflen, pid) < 0) return -1; if (recvbuflen < NLMSG_LENGTH(0) || *recvbuf == NULL) goto malformed_resp; resp = (struct nlmsghdr *)*recvbuf; switch (resp->nlmsg_type) { case NLMSG_ERROR: err = (struct nlmsgerr *)NLMSG_DATA(resp); if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) goto malformed_resp; if (err->error) { virReportSystemError(-err->error, _("error dumping %s (%d) interface"), ifname, ifindex); rc = -1; } break; case GENL_ID_CTRL: case NLMSG_DONE: if (nlmsg_parse(resp, sizeof(struct ifinfomsg), tb, IFLA_MAX, ifla_policy)) { goto malformed_resp; } break; default: goto malformed_resp; } if (rc != 0) VIR_FREE(*recvbuf); return rc; malformed_resp: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); VIR_FREE(*recvbuf); return -1; buffer_too_small: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("internal buffer is too small")); return -1; }
static int link_del(const char *name) { int rc = 0; char nlmsgbuf[NLMSGBUF_SIZE]; struct nlmsghdr *nlm = (struct nlmsghdr *)nlmsgbuf, *resp; struct nlmsgerr *err; char rtattbuf[RATTBUF_SIZE]; struct rtattr *rta; struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC }; char *recvbuf = NULL; unsigned int recvbuflen; memset(&nlmsgbuf, 0, sizeof(nlmsgbuf)); nlInit(nlm, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, RTM_DELLINK); if (!nlAppend(nlm, sizeof(nlmsgbuf), &ifinfo, sizeof(ifinfo))) goto buffer_too_small; rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_IFNAME, name, strlen(name)+1); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; if (nlComm(nlm, &recvbuf, &recvbuflen, 0) < 0) return -1; if (recvbuflen < NLMSG_LENGTH(0) || recvbuf == NULL) goto malformed_resp; resp = (struct nlmsghdr *)recvbuf; switch (resp->nlmsg_type) { case NLMSG_ERROR: err = (struct nlmsgerr *)NLMSG_DATA(resp); if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) goto malformed_resp; if (err->error) { virReportSystemError(-err->error, _("error destroying %s interface"), name); rc = -1; } break; case NLMSG_DONE: break; default: goto malformed_resp; } VIR_FREE(recvbuf); return rc; malformed_resp: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); VIR_FREE(recvbuf); return -1; buffer_too_small: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("internal buffer is too small")); return -1; }
static int link_add(const char *type, const unsigned char *macaddress, int macaddrsize, const char *ifname, const char *srcdev, uint32_t macvlan_mode, int *retry) { int rc = 0; char nlmsgbuf[NLMSGBUF_SIZE]; struct nlmsghdr *nlm = (struct nlmsghdr *)nlmsgbuf, *resp; struct nlmsgerr *err; char rtattbuf[RATTBUF_SIZE]; struct rtattr *rta, *rta1, *li; struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC }; int ifindex; char *recvbuf = NULL; unsigned int recvbuflen; if (ifaceGetIndex(true, srcdev, &ifindex) != 0) return -1; *retry = 0; memset(&nlmsgbuf, 0, sizeof(nlmsgbuf)); nlInit(nlm, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, RTM_NEWLINK); if (!nlAppend(nlm, sizeof(nlmsgbuf), &ifinfo, sizeof(ifinfo))) goto buffer_too_small; rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_LINK, &ifindex, sizeof(ifindex)); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_ADDRESS, macaddress, macaddrsize); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; if (ifname) { rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_IFNAME, ifname, strlen(ifname) + 1); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; } rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_LINKINFO, NULL, 0); if (!rta || !(li = nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len))) goto buffer_too_small; rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_INFO_KIND, type, strlen(type)); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; if (macvlan_mode > 0) { rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_INFO_DATA, NULL, 0); if (!rta || !(rta1 = nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len))) goto buffer_too_small; rta = rtattrCreate(rtattbuf, sizeof(rtattbuf), IFLA_MACVLAN_MODE, &macvlan_mode, sizeof(macvlan_mode)); if (!rta || !nlAppend(nlm, sizeof(nlmsgbuf), rtattbuf, rta->rta_len)) goto buffer_too_small; rta1->rta_len = (char *)nlm + nlm->nlmsg_len - (char *)rta1; } li->rta_len = (char *)nlm + nlm->nlmsg_len - (char *)li; if (nlComm(nlm, &recvbuf, &recvbuflen, 0) < 0) return -1; if (recvbuflen < NLMSG_LENGTH(0) || recvbuf == NULL) goto malformed_resp; resp = (struct nlmsghdr *)recvbuf; switch (resp->nlmsg_type) { case NLMSG_ERROR: err = (struct nlmsgerr *)NLMSG_DATA(resp); if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) goto malformed_resp; switch (err->error) { case 0: break; case -EEXIST: *retry = 1; rc = -1; break; default: virReportSystemError(-err->error, _("error creating %s type of interface"), type); rc = -1; } break; case NLMSG_DONE: break; default: goto malformed_resp; } VIR_FREE(recvbuf); return rc; malformed_resp: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); VIR_FREE(recvbuf); return -1; buffer_too_small: macvtapError(VIR_ERR_INTERNAL_ERROR, "%s", _("internal buffer is too small")); return -1; }