How to bind to a specific port in NKE?
How to bind to a specific port in NKE?
- Subject: How to bind to a specific port in NKE?
- From: "peiyuan" <email@hidden>
- Date: Fri, 17 Aug 2007 17:26:20 +0800
Hi all,
I am writing a simple server in NKE. No matter what port I specify, it would
be bind to 49156 and above. It seems to randomly bind ports for my
application. What's wrong with my code?
Currently I guess the problem is on " sin.sin_port = htons(24681);". But I
don't know why it won't "listen" to me.
I try to use "sin.sin_port = 24681;", but it won't work. It still binds to
port 49156 and above.
Environment:
OS: OS X 10.4.1
Type: Network Kernel Extension
In addition, what do the cookie do? How can I use it? I copy the example
from NKE pdf document.("#define ULTIMATE_ANSWER 0x00000042")
Could anyone give me some example? Thank you.
Part of the source code is listed below:
#define ULTIMATE_ANSWER 0x00000042
errno_t set_nonblocking(socket_t so, int value);
static void sock_accept_callback(socket_t so, void* cookie, int waitf);
static void sock_create_callback(socket_t so, void* cookie, int waitf);
int terminate=0;
static void mpfServer(void *myarg) {
socket_t so;
socket_t new_so;
errno_t err;
int protocol = 0;
uint32_t cookie = ULTIMATE_ANSWER;
struct sockaddr_in sin;
struct sockaddr_in from;
unsigned int alen;
int *memory;
const int on =1;
memory = _MALLOC(sizeof(sin), M_TEMP, M_WAITOK);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(24681);
if((err = sock_socket(PF_INET, SOCK_STREAM,protocol,
(sock_upcall)&sock_create_callback, (void*)cookie, &so)))
{
printf("Creating Socket error......\n");
};
sock_setsockopt(so,SOL_SOCKET,SO_REUSEADDR, &on,sizeof(on));
set_nonblocking(so, 1);
if(sock_bind(so, (struct sockaddr*)&sin)<0)
{
printf("Binding failed....\n");
}
printf("Binging port at %d\n", sin.sin_port);
sock_listen(so, 15);
while(terminate!=1)
{
alen = sizeof(from);
sock_accept(so, (struct sockaddr *)&from, alen, MSG_DONTWAIT,
(sock_upcall)&sock_accept_callback, (void*)cookie, &new_so);
IOSleep(10);
}
printf("Closing socket......\n");
sock_close(so);
_FREE(memory,M_TEMP);
printf("terminating child thread....\n");
IOExitThread();
}
kern_return_t MacServer_start (kmod_info_t * ki, void * d) {
IOThread mythread;
int noarg;
mythread = IOCreateThread(&mpfServer, (void *)noarg);
printf("MacServer Started........\n");
return KERN_SUCCESS;
}
kern_return_t MacServer_stop (kmod_info_t * ki, void * d) {
terminate=1;
IOSleep(50);
printf("MacServer will be unloaded\n");
return KERN_SUCCESS;
}
static void sock_accept_callback(socket_t so, void* cookie, int waitf)
{
printf("Socket Accept Callback been called.....\n");
}
static void sock_create_callback(socket_t so, void* cookie, int waitf)
{
printf("Socket Create Callback been called.....\n");
}
errno_t set_nonblocking(socket_t so, int value)
{
errno_t err;
int val = value;
if(value != 0 && value != 1) return EINVAL;
err = sock_ioctl(so, FIONBIO, &val);
return err;
}
Regards,
Chakotay
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden