libsmb/clidgram.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    client dgram calls
00004    Copyright (C) Andrew Tridgell 1994-1998
00005    Copyright (C) Richard Sharpe 2001
00006    Copyright (C) John Terpstra 2001
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 
00025 /*
00026  * cli_send_mailslot, send a mailslot for client code ...
00027  */
00028 
00029 BOOL cli_send_mailslot(BOOL unique, const char *mailslot,
00030                        uint16 priority,
00031                        char *buf, int len,
00032                        const char *srcname, int src_type, 
00033                        const char *dstname, int dest_type,
00034                        struct in_addr dest_ip)
00035 {
00036         struct packet_struct p;
00037         struct dgram_packet *dgram = &p.packet.dgram;
00038         char *ptr, *p2;
00039         char tmp[4];
00040         pid_t nmbd_pid;
00041 
00042         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
00043                 DEBUG(3, ("No nmbd found\n"));
00044                 return False;
00045         }
00046 
00047         if (!message_init())
00048                 return False;
00049 
00050         memset((char *)&p, '\0', sizeof(p));
00051 
00052         /*
00053          * Next, build the DGRAM ...
00054          */
00055 
00056         /* DIRECT GROUP or UNIQUE datagram. */
00057         dgram->header.msg_type = unique ? 0x10 : 0x11; 
00058         dgram->header.flags.node_type = M_NODE;
00059         dgram->header.flags.first = True;
00060         dgram->header.flags.more = False;
00061         dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
00062                 ((unsigned)sys_getpid()%(unsigned)100);
00063         /* source ip is filled by nmbd */
00064         dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
00065         dgram->header.packet_offset = 0;
00066   
00067         make_nmb_name(&dgram->source_name,srcname,src_type);
00068         make_nmb_name(&dgram->dest_name,dstname,dest_type);
00069 
00070         ptr = &dgram->data[0];
00071 
00072         /* Setup the smb part. */
00073         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
00074         memcpy(tmp,ptr,4);
00075 
00076         if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
00077                 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
00078                 return False;
00079         }
00080 
00081         set_message(ptr,17,strlen(mailslot) + 1 + len,True);
00082         memcpy(ptr,tmp,4);
00083 
00084         SCVAL(ptr,smb_com,SMBtrans);
00085         SSVAL(ptr,smb_vwv1,len);
00086         SSVAL(ptr,smb_vwv11,len);
00087         SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
00088         SSVAL(ptr,smb_vwv13,3);
00089         SSVAL(ptr,smb_vwv14,1);
00090         SSVAL(ptr,smb_vwv15,priority);
00091         SSVAL(ptr,smb_vwv16,2);
00092         p2 = smb_buf(ptr);
00093         fstrcpy(p2,mailslot);
00094         p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
00095         if (!p2) {
00096                 return False;
00097         }
00098 
00099         memcpy(p2,buf,len);
00100         p2 += len;
00101 
00102         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
00103 
00104         p.packet_type = DGRAM_PACKET;
00105         p.ip = dest_ip;
00106         p.timestamp = time(NULL);
00107 
00108         DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
00109                  mailslot, nmb_namestr(&dgram->source_name)));
00110         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name),
00111                     inet_ntoa(dest_ip)));
00112 
00113         return NT_STATUS_IS_OK(message_send_pid(pid_to_procid(nmbd_pid),
00114                                                 MSG_SEND_PACKET, &p, sizeof(p),
00115                                                 False));
00116 }
00117 
00118 /*
00119  * cli_get_response: Get a response ...
00120  */
00121 BOOL cli_get_response(const char *mailslot, char *buf, int bufsiz)
00122 {
00123         struct packet_struct *p;
00124 
00125         p = receive_unexpected(DGRAM_PACKET, 0, mailslot);
00126 
00127         if (p == NULL)
00128                 return False;
00129 
00130         memcpy(buf, &p->packet.dgram.data[92],
00131                MIN(bufsiz, p->packet.dgram.datasize-92));
00132 
00133         return True;
00134 }
00135 
00136 /*
00137  * cli_get_backup_list: Send a get backup list request ...
00138  */
00139 
00140 static char cli_backup_list[1024];
00141 
00142 int cli_get_backup_list(const char *myname, const char *send_to_name)
00143 {
00144         pstring outbuf;
00145         char *p;
00146         struct in_addr sendto_ip;
00147 
00148         if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
00149 
00150                 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
00151                 return False;
00152 
00153         }
00154 
00155         memset(cli_backup_list, '\0', sizeof(cli_backup_list));
00156         memset(outbuf, '\0', sizeof(outbuf));
00157 
00158         p = outbuf;
00159 
00160         SCVAL(p, 0, ANN_GetBackupListReq);
00161         p++;
00162 
00163         SCVAL(p, 0, 1); /* Count pointer ... */
00164         p++;
00165 
00166         SIVAL(p, 0, 1); /* The sender's token ... */
00167         p += 4;
00168 
00169         cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", 1, outbuf, 
00170                           PTR_DIFF(p, outbuf), myname, 0, send_to_name, 
00171                           0x1d, sendto_ip);
00172 
00173         /* We should check the error and return if we got one */
00174 
00175         /* Now, get the response ... */
00176 
00177         cli_get_response("\\MAILSLOT\\BROWSE",
00178                          cli_backup_list, sizeof(cli_backup_list));
00179 
00180         return True;
00181 
00182 }
00183 
00184 /*
00185  * cli_get_backup_server: Get the backup list and retrieve a server from it
00186  */
00187 
00188 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
00189 {
00190 
00191   /* Get the backup list first. We could pull this from the cache later */
00192 
00193   cli_get_backup_list(my_name, target);  /* FIXME: Check the response */
00194 
00195   if (!cli_backup_list[0]) { /* Empty list ... try again */
00196 
00197     cli_get_backup_list(my_name, target);
00198 
00199   }
00200 
00201   strncpy(servername, cli_backup_list, MIN(16, namesize));
00202 
00203   return True;
00204 
00205 }

Sambaに対してSat Aug 29 21:23:04 2009に生成されました。  doxygen 1.4.7